-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAD.h
133 lines (94 loc) · 2.87 KB
/
AD.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
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
#pragma once
#include <boost/numeric/ublas/matrix.hpp>
#include <map>
#include <vector>
#include <memory>
#include <iostream>
#include <atomic>
#include <boost/container/flat_map.hpp>
namespace AD {
class ResultType
{
public:
typedef boost::numeric::ublas::matrix<float> matrix;
private:
public:
float m_curValueFloat;
matrix m_curValueMatrix;
bool m_movedAway = false;
enum class CurValueType { scalar, matrix };
CurValueType m_curValueType = CurValueType::scalar;
ResultType();
ResultType(matrix&& m);
ResultType(const matrix& m);
ResultType(float v);
ResultType(int v);
ResultType(ResultType&& other);
ResultType(const ResultType& other);
void operator=(const ResultType& other);
void operator=(ResultType&& other);
~ResultType();
ResultType Transpose() const;
ResultType Product(const ResultType& other) const;
ResultType operator*(const ResultType& other) const;
// 1 to all
template<typename T>
ResultType ApplyUnaryFunc(const T& t) const
{
if (m_curValueType == CurValueType::scalar)
return t(m_curValueFloat);
//matrix m = m_curValueMatrix;
matrix m = MatrixCache::Get().GetMatrix(m_curValueMatrix.size1(), m_curValueMatrix.size2());
const size_t target = m.data().size();
for (size_t i = 0; i < target; i++)
{
m.data()[i] = t(m_curValueMatrix.data()[i]);
}
return m;
}
ResultType operator+(const ResultType& other) const;
ResultType operator-(const ResultType& other) const;
friend std::ostream & operator<<(std::ostream &os, const ResultType& p);
};
class Differentiable;
class Utils
{
public:
typedef boost::numeric::ublas::matrix<float> matrix;
// will create a matrix in 1 row
static matrix CreateRowMatrix(std::vector<float>&& v);
/**
{{0 1 2 3},
{2,3,4,1},
{4,4,4,4}} will create a 3x4 matrix
*/
static matrix CreateMatrix(std::vector<std::vector<float> >&&v);
};
class ExprWrapper
{
std::shared_ptr<Differentiable> m_holdedValue;
template<typename T, typename ...Args>
static std::shared_ptr<T> Make(Args&&... args)
{
return std::make_shared<T>(std::forward<Args>(args)...);
}
public:
ExprWrapper() {}
ExprWrapper(ResultType v);
ExprWrapper(std::shared_ptr<Differentiable> diff);
ExprWrapper operator+(const ExprWrapper& other) const;
ExprWrapper operator+(const ResultType& other) const;
ExprWrapper operator-(const ExprWrapper& other) const;
ExprWrapper operator-(const ResultType& other) const;
ExprWrapper operator*(const ExprWrapper& other) const;
ExprWrapper operator*(const ResultType& other) const;
ExprWrapper MatrixMult(const ResultType& other) const;
ExprWrapper MatrixMult(const ExprWrapper& other) const;
ExprWrapper Sigmoid() const;
ExprWrapper Pow(int powValue) const;
ExprWrapper Softmax() const;
ResultType Calc();
ResultType GetGradBy(ExprWrapper& other);
void Update(ResultType&& newValue);
};
}