Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
224 lines (206 loc) · 5.19 KB
/
Copy pathmain.cpp
File metadata and controls
224 lines (206 loc) · 5.19 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <vector>
#include <cmath>
#include <cassert>
using namespace std;
class Tensor {
private:
int rows,cols,shape;
vector<float> data;
inline float f(float x) {
return 1/(1+exp(-x));
}
inline void print1d() {
assert(data.empty()!=true);
cout<<"Tensor : ({ ";
for(int i=0;i<size()-1;i++) {
cout<<data[i]<<" ,";
}
cout<<data[size()-1];
cout<<" })"<<endl;
}
inline void print2d() {
assert(data.empty()!=true);
cout << "Tensor : ({\n";
for (int r=0;r<rows;r++){
cout << " { ";
for(int c=0;c<cols;c++){
cout << data[r*cols + c];
if(c != cols-1) cout << ", ";
}
cout << " }\n";
}
cout << "})" << endl;
}
public:
// Tensor Constructors
Tensor(vector<float> arr) {
data = arr;
shape=1;rows=1;cols=arr.size();
}
Tensor(int size, float fill=0.0) {
rows=1;shape=1;cols=size;
data.resize(size,fill);
}
Tensor(const vector<vector<float>>& arr2D) {
rows = arr2D.size();
cols = arr2D.empty() ? 0 : arr2D[0].size();
shape = 2;
data.clear();
for (const auto& row : arr2D) {
if (row.size() != cols)
throw runtime_error("All rows must have the same length");
data.insert(data.end(), row.begin(), row.end());
}
}
Tensor(int row,int col, float fill=0.0) {
rows=row; cols=col;shape=2;
data.resize(row*col,fill);
}
Tensor() {data.resize(0,0.0f);shape=1;rows=0;cols=0;}
// Helper Functions
int size() { return data.size(); }
void print() {
if (shape==1) {print1d();}
else if (shape==2) {print2d();}
}
// Math functions
void sigmoid_() {
for (int i=0;i<size();i++) {
data[i] = f(data[i]);
}
}
Tensor sigmoid() {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = f(data[i]);
}
return out;
}
void power_(float x) {
for (int i=0;i<size();i++) {
data[i] = pow(data[i],x);
}
}
Tensor power(float x) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = pow(data[i],x);
}
return out;
}
float dot(Tensor& t1) {
float sum = 0.0f;
for (int i=0;i<size();i++) {
sum += (data[i] * t1.data[i]);
}
return sum;
}
// Easy indexing
float &operator()(int r, int c) { return data[r*cols+c]; }
const float &operator()(int r,int c) const { return data[r*cols+c]; }
float &operator()(int c) { return data[c]; }
const float &operator()(int c) const { return data[c]; }
// Operators
Tensor operator+(const Tensor& t) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = data[i] + t.data[i];
}
return out;
}
Tensor operator+(float x) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = data[i] + x;
}
return out;
}
Tensor operator-(const Tensor& t) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = data[i] - t.data[i];
}
return out;
}
Tensor operator-(float x) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = data[i] - x;
}
return out;
}
Tensor operator*(const Tensor& t) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = data[i] * t.data[i];
}
return out;
}
Tensor operator*(float x) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = data[i] * x;
}
return out;
}
Tensor operator/(const Tensor& t) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = data[i] / t.data[i];
}
return out;
}
Tensor operator/(float x) {
Tensor out(*this);
for (int i=0;i<size();i++) {
out.data[i] = data[i] / x;
}
return out;
}
// Inplace operators
void operator+=(const Tensor& t) {
for (int i=0;i<size();i++) {
data[i] += t.data[i];
}
}
void operator+=(float x) {
for (int i=0;i<size();i++) {
data[i] += x;
}
}
void operator-=(const Tensor& t) {
for (int i=0;i<size();i++) {
data[i] -= t.data[i];
}
}
void operator-=(float x) {
for (int i=0;i<size();i++) {
data[i] -= x;
}
}
void operator*=(const Tensor& t) {
for (int i=0;i<size();i++) {
data[i] *= t.data[i];
}
}
void operator*=(float x) {
for (int i=0;i<size();i++) {
data[i] *= x;
}
}
void operator/=(const Tensor& t) {
for (int i=0;i<size();i++) {
data[i] /= t.data[i];
}
}
void operator/=(float x) {
for (int i=0;i<size();i++) {
data[i] /= x;
}
}
};
int main(void) {
Tensor t(5,1.0f),t2(5,2.0f), t3;
return 0;
}