-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensor.hpp
More file actions
108 lines (70 loc) · 2.18 KB
/
Tensor.hpp
File metadata and controls
108 lines (70 loc) · 2.18 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
#ifndef TENSOR_H
#define TENSOR_H
#include <functional>
#include <vector>
#include <tuple>
#include <map>
#include <string>
class Tensor {
using BackpropFunction = std::function<double(double)>;
public:
enum CreationOp {
ADD,
SUB,
NEG,
MUL,
SUM,
EXPAND,
TRANSPOSE,
MM,
NONE
};
static bool noGrad;
Tensor();
Tensor( std::vector<std::vector<double>> values, bool autograd=false, const Tensor* const creators[]=nullptr, CreationOp creationOp=NONE );
Tensor( std::vector<double> values, bool autograd=false, const Tensor* const creators[]=nullptr, CreationOp creationOp=NONE );
Tensor( std::tuple<int, int> size, double *data, bool autograd=false, const Tensor* const creators[]=nullptr, CreationOp creationOp=NONE );
Tensor( const Tensor& original );
~Tensor();
Tensor getGrad();
void backward( Tensor grad, const Tensor* gradOrigin=nullptr ) const;
Tensor operator +( const Tensor &right );
Tensor operator -();
Tensor operator -( const Tensor &right );
Tensor operator *( const Tensor &right );
Tensor operator *( const double &right );
double& operator[]( int index );
Tensor mm( const Tensor &right );
Tensor transpose() const;
Tensor sum( int dim=0 );
Tensor expand( int dim, int copies );
Tensor& operator =( const Tensor &right );
std::string to_string() const;
bool getAutograd() const;
static Tensor random( int rows, int cols, bool autograd=false );
static Tensor fill( int rows, int cols, double value, bool autograd=false );
static void setNoGrad( bool b );
void update( const Tensor &change );
void clearGrad();
std::tuple<int, int> size();
double* getData();
void setBackpropFunction( BackpropFunction func );
void applyActivationFunction( BackpropFunction func );
private:
std::tuple<int, int> sz;
mutable Tensor *grad;
double *data;
CreationOp creationOp;
const Tensor* const *creators;
bool autograd;
int id;
mutable std::map<int, int> children;
BackpropFunction backpropFunc;
static int nextID;
int dim;
void addChild( int id ) const;
void createChildren() const;
static int createTensorId();
bool gradFromAllChildren() const;
};
#endif