-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTensor.h
More file actions
209 lines (130 loc) · 3.56 KB
/
Tensor.h
File metadata and controls
209 lines (130 loc) · 3.56 KB
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
/**
* @file Tensor.h
* @author Philip Salmony (pms67@cam.ac.uk)
* @date 12/12/2017
* @version 1.0
*
* @brief PiCNN, Tensor, 3D double container class
*
* @section DESCRIPTION
*
* The Tensor class is used to store double values in a 3D array.
* If the dim argument is 1, a tensor reduces to a 2D matrix.
*
*/
#ifndef TENSOR_H
#define TENSOR_H
#include "Matrix.h"
class Tensor {
private:
int dim;
int rows;
int cols;
std::vector<Matrix > m;
void redim() {
m.resize(dim);
for (int d = 0; d < dim; d++)
m[d].resize(rows, cols);
return;
}
public:
//Constructors
Tensor() {
dim = 0; rows = 0; cols = 0;
}
Tensor(int dim, int rows, int cols) {
this->dim = dim; this->rows = rows; this->cols = cols;
redim();
}
Tensor(int dim, int n) {
this->dim = dim; this->rows = n; this->cols = n;
redim();
}
Tensor(int n) {
this->dim = n; this->rows = n; this->cols = n;
redim();
}
//Operators
Matrix operator()(int d) { return m[d]; }
const Matrix operator()(int d) const { return m[d]; }
double& operator()(int d, int r, int c) { return m[d](r, c); }
const double& operator()(int d, int r, int c) const { return m[d](r, c); }
//Properties
int getDim() { return dim; }
int getRows() { return rows; }
int getCols() { return cols; }
//Functions
void resize(int dim, int rows, int cols) {
this->dim = dim; this->rows = rows; this->cols = cols;
redim();
}
void resize(int dim, int n) {
this->dim = dim; this->rows = n; this->cols = n;
redim();
}
void resize(int n) {
this->dim = n; this->rows = n; this->cols = n;
redim();
}
Tensor copy() {
Tensor tnew(dim, rows, cols);
for (int d = 0; d < dim; d++)
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
tnew(d, i, j) = m[d](i, j);
return tnew;
}
void clear() {
for (int d = 0; d < dim; d++)
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
m[d](i, j) = 0;
return;
}
void set(int d, Matrix mset) {
m[d] = mset.copy();
return;
}
void set(int d, double val) {
m[d].set(val);
return;
}
void set(double val) {
for (int d = 0; d < dim; d++)
m[d].set(val);
return;
}
void rand(float min, float max) {
std::default_random_engine generator;
std::uniform_real_distribution<double> distribution(min, max);
generator.seed(time(0));
for (int d = 0; d < dim; d++)
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
m[d](i, j) = distribution(generator);
return;
}
void randn(float mean, float stddev) {
std::default_random_engine generator;
std::normal_distribution<double> distribution(mean, stddev);
generator.seed(time(0));
for (int d = 0; d < dim; d++)
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
m[d](i, j) = distribution(generator);
return;
}
void print() {
for (int d = 0; d < dim; d++) {
std::cout << d << ":\n";
m[d].print();
}
return;
}
void print(int n) {
m[n].print();
return;
}
};
typedef std::vector<Tensor > TensorArray;
#endif