-
Notifications
You must be signed in to change notification settings - Fork 4
/
variable.h
76 lines (61 loc) · 2.12 KB
/
variable.h
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
#ifndef VARIABLE_H
#define VARIABLE_H
#include<initializer_list>
#include<ostream>
#include<random>
namespace dlframework{
const unsigned TENSOR_MAX_DIM=10;
const unsigned OPERATOR_MAX_NUM=10;
class Tensor{
public:
int dim;
int length;
int shape[TENSOR_MAX_DIM];
float * p;
Tensor();
Tensor(float);
Tensor(const std::initializer_list<unsigned> & init_shape);
Tensor(const Tensor & rhs);
Tensor(const Tensor & rhs, bool shape_only);
Tensor(Tensor && rhs);
virtual ~Tensor();
Tensor & operator=(const std::initializer_list<float> & array);
Tensor & operator=(const Tensor & rhs);
Tensor & operator=(Tensor && rhs);
float & operator()(const std::initializer_list<unsigned> & indices);
Tensor & operator+=(const Tensor & b);
Tensor operator+(const Tensor& b) const;
Tensor & operator-=(const Tensor & b);
Tensor operator-(const Tensor & b) const;
Tensor & operator*=(const float & x);
void reshape(const std::initializer_list<unsigned> & init_shape);
friend std::ostream& operator<<(std::ostream & o, const Tensor & rhs);
};
class Variable{
public:
Tensor * data;
Tensor * grad;
bool requires_grad; //existing data: false; trainable parameters & intermediate results: true
int op;
Variable(bool does_require_grad=false);
Variable(float x, bool does_require_grad=false);
Variable(const std::initializer_list<unsigned> & init_shape, bool does_require_grad=false);
Variable(const Variable & rhs); //shadow copy
Variable(Variable && rhs);
Variable(Tensor & tensor, bool does_require_grad=false); //identity copy of a new tensor.
Variable(Tensor && tensor, bool does_require_grad=false);
Variable & operator=(const std::initializer_list<float> & array);
void clear_data();
void zero_grad();
friend std::ostream& operator<<(std::ostream & o, const Variable & rhs);
virtual ~Variable();
};
class Init{
public:
static std::default_random_engine dlframework_random_generator;
static void set_seed(unsigned val);
static void normal(Tensor & x, float mean=0.0, float std=1.0);
static void uniform(Tensor & x, float a=0.0, float b=1.0);
};
}// end dlframework
#endif