-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.cpp
More file actions
44 lines (40 loc) · 1.17 KB
/
tensor.cpp
File metadata and controls
44 lines (40 loc) · 1.17 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
#include "tensor.h"
Tensor::Tensor(std::vector<double> data_, std::vector<int> shape_) {
data = data_;
shape = shape_;
}
double& Tensor::operator()(std::vector<int> indices) {
int index = 0;
int stride = 1;
for (int i = 0; i < indices.size(); i++) {
index += stride * indices[i];
stride *= shape[i];
}
return data[index];
}
void Tensor::print() {
std::cout << "Tensor shape: (";
for (int i = 0; i < shape.size(); i++) {
std::cout << shape[i];
if (i != shape.size() - 1) {
std::cout << ", ";
}
}
std::cout << ")" << std::endl;
std::cout << "Tensor data:" << std::endl;
for (int i = 0; i < shape[0]; i++) {
if (shape.size() == 1) {
std::cout << data[i] << " ";
} else {
std::vector<int> indices(shape.size() - 1);
for (int j = 0; j < shape.size() - 1; j++) {
indices[j] = 0;
}
indices.insert(indices.begin(), i);
Tensor sub_tensor(data, shape);
sub_tensor.shape.erase(sub_tensor.shape.begin());
sub_tensor.print();
}
}
std::cout << std::endl;
}