-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.cpp
More file actions
130 lines (112 loc) · 3.3 KB
/
Copy pathtensor.cpp
File metadata and controls
130 lines (112 loc) · 3.3 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
#include "tensor.h"
using namespace std;
namespace ts
{
Tensor::Tensor() : shape{}, dtype_{}, data_{nullptr} {}
Tensor::Tensor(const vector<vector<double>> &data)
{
if (!data.empty() && !data[0].empty())
{
dimension = 2; // assuming 2D data
shape = {data.size(), data[0].size()};
stride = {shape[1], 1};
dtype_ = "double"; // set the data type
data_ = new double[shape[0] * shape[1]];
for (size_t i = 0; i < shape[0]; ++i)
{
for (size_t j = 0; j < shape[1]; ++j)
{
data_[i * stride[0] + j] = data[i][j];
}
}
}
else
{
throw std::invalid_argument("Tensor data cannot be empty.");
}
}
Tensor::Tensor(const vector<size_t> &shape, const string &dtype, double init_value) : shape(shape), dtype_(dtype)
{
if (!shape.empty())
{
dimension = shape.size();
size_t total_size = 1;
stride.resize(dimension);
for (int i = dimension - 1; i >= 0; --i)
{
stride[i] = (i == dimension - 1) ? 1 : stride[i + 1] * shape[i + 1];
total_size *= shape[i];
}
data_ = new double[total_size];
std::fill_n(data_, total_size, init_value);
}
else
{
throw std::invalid_argument("Tensor shape cannot be empty.");
}
}
Tensor::Tensor(const vector<size_t> &shape, const string &dtype, vector<double> data_vector)
: shape(shape), dtype_(dtype)
{
if (shape.empty())
{
throw std::invalid_argument("Tensor shape cannot be empty.");
}
dimension = shape.size();
size_t total_size = 1;
stride.resize(dimension);
for (int i = dimension - 1; i >= 0; --i)
{
stride[i] = (i == dimension - 1) ? 1 : stride[i + 1] * shape[i + 1];
total_size *= shape[i];
}
if (data_vector.size() != total_size)
{
throw std::invalid_argument("Data vector size does not match tensor's total size.");
}
data_ = new double[total_size];
std::copy(data_vector.begin(), data_vector.end(), data_);
}
vector<size_t> Tensor::size() const
{
return shape;
}
vector<size_t> Tensor::get_shape() const
{
return shape;
}
string Tensor::type() const
{
return dtype_;
}
double *Tensor::data_ptr() const
{
return data_;
}
vector<size_t> Tensor::get_stride() const
{
return stride;
}
double Tensor::get_element(size_t index) const {
if (index >= total_size()) {
throw std::out_of_range("Index out of range");
}
return data_[index];
}
void Tensor::set_element(size_t index, double value) {
if (index >= total_size()) {
throw std::out_of_range("Index out of range");
}
data_[index] = value;
}
size_t Tensor::total_size() const {
size_t total = 1;
for (size_t dim_size : shape) {
total *= dim_size;
}
return total;
}
int Tensor::dimens() const{
return dimension;
}
}