-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDataHandler.h
231 lines (135 loc) · 4.39 KB
/
DataHandler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef DATAHANDLER_H
#define DATAHANDLER_H
#include "Tensor.h"
#include <fstream>
#include <string>
class DataHandler {
public:
//Constructor
DataHandler() {
//Empty
}
//Functions
TensorArray readCSV(std::string filename, std::string delimiter) {
std::ifstream file(filename);
if (file.is_open()) {
std::vector<std::vector<std::string > > data;
int num_rows = 0; int num_cols = 0;
std::string current_row;
while ( getline( file, current_row ) )
data.push_back(delimit(current_row, delimiter));
num_rows = data.size();
num_cols = data[0].size();
TensorArray out;
out.resize(num_rows);
for (int i = 0; i < num_rows; i++) {
out[i].resize(1, num_cols, 1);
for (int j = 0; j < num_cols; j++) {
out[i](0, j, 0) = std::stod(data[i][j]);
}
}
return out;
} else {
std::cout << "Unable to open file.\n";
TensorArray out;
return out;
}
}
TensorArray reshape(TensorArray data, int rows, int cols) {
if (rows * cols != data[0].getRows()) {
std::cout << "Rows (" << rows << ") * Cols (" << cols << ") must equal size of input tensors (" << data[0].getRows() << ")!";
return data;
}
TensorArray out;
out.resize(data.size());
//Fill tensors
for (int i = 0; i < data.size(); i++) {
//Reshape tensors
out[i].resize(1, rows, cols);
int count = 0;
for (int j = 0; j < rows; j++) {
for (int k = 0; k < cols; k++) {
out[i](0, j, k) = data[i](0, count, 0);
count++;
}
}
}
return out;
}
TensorArray onehot(TensorArray data, int max) {
if (data[0].getRows() > 1 || data[0].getCols() > 1) {
std::cout << "Tensors have to be dimensions of 1x1x1.\n";
return data;
}
TensorArray out;
out.resize(data.size());
for (int i = 0; i < data.size(); i++) {
out[i].resize(1, max, 1);
int hot = int(data[i](0, 0, 0));
out[i](0, hot, 0) = 1;
}
return out;
}
TensorArray normalise_minmax(TensorArray data, double min, double max) {
if (min >= max) {
std::cout << "MIN must be less than MAX.\n";
return data;
}
TensorArray out;
out.resize(data.size());
for (int n = 0; n < data.size(); n++) {
out[n].resize(data[0].getDim(), data[0].getRows(), data[0].getCols());
for (int i = 0; i < data[0].getDim(); i++)
for (int j = 0; j < data[0].getRows(); j++)
for (int k = 0; k < data[0].getCols(); k++)
out[n](i, j, k) = (data[n](i, j, k) - min) / (max - min);
}
return out;
}
std::vector<std::string > delimit(std::string s, std::string delimiter) {
std::vector<std::string > out;
int pos = 0;
while (pos >= 0) {
pos = s.find(delimiter);
std::string val = s.substr(0, pos);
s.erase(0, pos + delimiter.length());
out.push_back(val);
}
return out;
}
bool compare_onehot(Tensor a, Tensor b) {
if (a.getCols() > 1 || b.getCols() > 1 || a.getDim() > 1 || b.getDim() > 1 || a.getRows() != b.getRows()) {
std::cout << "Must be one-hot vectors (i.e. a 1xNx1 vectors) of equal length.\n";
return false;
}
double maxA = -9999.0;
int valA = 0;
double maxB = -9999.0;
int valB = 0;
for (int i = 0; i < a.getRows(); i++) {
if (a(0, i, 0) > maxA) {
maxA = a(0, i, 0);
valA = i;
}
if (b(0, i, 0) > maxB) {
maxB = b(0, i, 0);
valB = i;
}
}
if (valA == valB)
return true;
else
return false;
}
double getLearningRate(float val_acc, float lmin, float lmax, float amin, float amax) {
double m = (lmax - lmin) / (amin - amax);
double c = lmin - m * amax;
double lrate = (m * val_acc + c);
if (lrate > lmax)
lrate = lmax;
else if (lrate < lmin)
lrate = lmin;
return lrate;
}
};
#endif