-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.cpp
More file actions
executable file
·192 lines (174 loc) · 4.78 KB
/
tensor.cpp
File metadata and controls
executable file
·192 lines (174 loc) · 4.78 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
#include "tensor.hpp"
#include <algorithm>
Tensor::Tensor(const std::vector<int>& shape){
//jsp si faut pas bloquer le cas {}
this->shape = shape;
for(int i = 0;i<shape.size();i++){
if(i==0){
stride.push_back(1);
}
else{
stride.push_back(stride[i-1]*shape[i-1]);
}
}
size = shape.empty() ? 1 : stride.back()*shape.back();
data = std::make_shared<std::vector<double>>(size, 0.0);
}
Tensor::Tensor(){
this->shape = {};
this->stride = {};
this->size = 1;
data = std::make_shared<std::vector<double>>(1, 0.0);
}
double& Tensor::get(const std::vector<int>& pos){
int dim = pos.size();
assert(dim == this->shape.size());
for(int i = 0; i<dim;i++){
assert(pos[i]<this->shape[i]);
}
int sum = 0;
for(int i = 0;i<dim;i++){
sum += pos[i]*this->stride[i];
}
return (*data)[sum];
}
const double& Tensor::get(const std::vector<int>& pos) const{
int dim = pos.size();
assert(dim == this->shape.size());
for(int i = 0; i<dim;i++){
assert(pos[i]<this->shape[i]);
}
int sum = 0;
for(int i = 0;i<dim;i++){
sum += pos[i]*this->stride[i];
}
return (*data)[sum];
}
//for scalars
double& Tensor::get(){
assert(this->size == 1);
return (*data)[0];
}
const double& Tensor::get() const {
assert(this->size == 1);
return (*data)[0];
}
template <typename Func>
void iterate(const std::vector<int>& shape, const std::vector<int>& stride, Func&& fn){
int ndim = shape.size();
std::vector<int> x(ndim,0);
int offset = 0;
while(true){
fn(offset,x);
int dim = ndim - 1;
while(dim>=0){
x[dim]++;
offset += stride[dim];
if (x[dim] < shape[dim]){
break;
}
offset -= shape[dim] * stride[dim];
x[dim] = 0;
dim --;
}
if(dim < 0){
break;
}
}
}
//this is a specific case but idk if broader case is needed.
//i should use exceptions but idk how to use them yet
Tensor Tensor::broadcast(const std::vector<int>& shape) const{
int dim = this->shape.size();
int newDim = shape.size();
bool oneFlag = false;
for(int i = 0; i < newDim;i++){
if (i < dim){
if (this->shape[dim-1] == 1)
oneFlag = true;
else
assert(this->shape[dim-i] == shape[newDim - i ] && !oneFlag);
}
}
Tensor out(shape);
out.data = this->data;
return out;
}
//TODO: do try catch block for broadcasting,
//rn we only broadcast other so first arg will always be the final shape
Tensor Tensor::operator+(const Tensor& other){
int other_dim = other.shape.size();
Tensor broadcasted = other;
if(other_dim == this->shape.size()){
for(int i = 0; i < other_dim;i++){
if(other.shape[i] != this->shape[i]){
broadcasted = other.broadcast(this->shape);
break;
}
}
}
else
broadcasted = other.broadcast(this->shape);
Tensor out(this->shape);
iterate(this->shape,this->stride, [&](int offset, const std::vector<int>& x){
out.get(x) = this->get(x) + other.get(x);
});
return out;
}
Tensor Tensor::operator*(const Tensor& other){
int other_dim = other.shape.size();
assert(other_dim == this->shape.size());
for(int i = 0; i < other_dim;i++){
assert(other.shape[i] == this->shape[i]);
}
Tensor out(this->shape);
iterate(this->shape,this->stride, [&](int offset, const std::vector<int>& x){
out.get(x) = this->get(x) * other.get(x);
});
return out;
}
Tensor Tensor::mm(const Tensor& other){
assert(this->shape.size() == 2 && other.shape.size() == 2);
int n = this->shape[0];
assert(this->shape[1] == other.shape[0]);
Tensor out({this->shape[1],other.shape[1]});
iterate(out.shape,out.stride,[&](int offset,const std::vector<int>& x){
for(int i = 0;i<n;i++){
out.get(x) += this->get({x[0],i})*other.get({i,x[1]});
}
});
return out;
}
Tensor Tensor::sigmoid(){
Tensor out(this->shape);
iterate(out.shape,out.stride,[&](int offset, const std::vector<int>& x){
out.get(x) = 1/(1 + exp(-this->get(x)));
});
return out;
}
Tensor Tensor::sigmoidDeriv(){
Tensor out(this->shape);
iterate(out.shape,out.stride,[&](int offset, const std::vector<int>& x){
out.get(x) = exp(-this->get(x))/pow(1 + exp(-this->get(x)),2);
});
return out;
}
//not sure this makes a reference to data.
Tensor Tensor::transpose(){
assert(this->shape.size() == 2 );
Tensor out({this->shape[1],this->shape[0]});
out.stride = {this->stride[1],this->stride[0]};
out.data = this->data;
return out;
}
void Tensor::printShape(){
std::cout << "[";
for(const int& i: this->shape){
std::cout << i;
}
std::cout << "]";
}
int main(){
Tensor aaaa({2,2});
std::cout << "nice \n";
}