-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.h
More file actions
156 lines (128 loc) · 4.53 KB
/
Copy pathcontext.h
File metadata and controls
156 lines (128 loc) · 4.53 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef __CONTEXT__
#define __CONTEXT__
#include "operations.h"
#include <random>
#include <stdexcept>
// TODO: Note that currently, in all of the operators which go between
// variable and constant expression, we're creating a copy of the
// constant variable. This is probably not ideal and may slow down
// the resulting program significantly.
template <typename T>
Variable<T> createVariable(T value) {
return Variable(new ConstExpression(std::move(value)));
}
template <std::size_t ...Dims>
Variable<Tensor<float, Dims...>> createTensorVariable() {
Tensor<float, Dims...> t = 0;
return Variable(new ConstExpression(std::move(t)));
}
template <std::size_t ...Dims>
Variable<Tensor<float, Dims...>> createTensorVariable(std::initializer_list<float> items) {
Tensor<float, Dims...> t = items;
return Variable(new ConstExpression(std::move(t)));
}
template <std::size_t ...Dims>
Variable<Tensor<float, Dims...>> createRandomTensorVariable() {
Tensor<float, Dims...> t;
static std::random_device rd;
static std::mt19937 gen;
std::uniform_real_distribution dis(-1.0, 1.0);
std::generate(t.begin(), t.end(), [&]() { return dis(gen); });
return Variable(new ConstExpression(std::move(t)));
}
template <std::size_t rows, std::size_t cols>
Variable<Matrix<float, rows, cols>> createMatrixVariable(std::initializer_list<float> items) {
Matrix<float, rows, cols> m = items;
return Variable(new ConstExpression(std::move(m)));
}
template <typename T>
void computeGradients(const Variable<T>& ex) {
ex.get()->backPropagate();
}
template <typename T>
Variable<T> square(const Variable<T>& v) {
return Variable(new Square(v.get()));
}
template <typename T>
Variable<T> exp(const Variable<T>& v) {
return Variable(new Exp(v.get()));
}
template <typename T, typename H>
Variable<T> operator+(const Variable<T>& a, const Variable<H>& b) {
return Variable(new Addition<T, H>(a.get(), b.get()));
}
template <typename T, typename H>
auto operator+(const Variable<T>& a, const H& b) {
Variable<H> v = createVariable(b);
return a + v;
}
template <typename T, typename H>
auto operator+(const T& a, const Variable<H>& b) {
Variable<T> v = createVariable(a);
return v + b;
}
template <typename T, typename H>
Variable<T> operator-(const Variable<T>& a, const Variable<H>& b) {
return Variable(new Subtraction<T, H>(a.get(), b.get()));
}
template <typename T, typename H>
auto operator-(const Variable<T>& a, const H& b) {
Variable<H> v = createVariable(b);
return a - v;
}
template <typename T, typename H>
auto operator-(const T& a, const Variable<H>& b) {
Variable<T> v = createVariable(a);
return v - b;
}
template <typename T, typename H>
Variable<H> operator*(const Variable<T>& a, const Variable<H>& b) requires (Floating<T> && TensorType<H>) {
return Variable(new Multiplication<T, H, H>(a.get(), b.get()));
}
template <typename T, typename H>
Variable<T> operator*(const Variable<T>& a, const Variable<H>& b) requires (!Floating<T> || !TensorType<H>) {
return Variable(new Multiplication<T, H, T>(a.get(), b.get()));
}
template <typename T, typename H>
auto operator*(const Variable<T>& a, const H& b) {
Variable<H> v = createVariable(b);
return a * v;
}
template <typename T, typename H>
auto operator*(const T& a, const Variable<H>& b) {
Variable<T> v = createVariable(a);
return v * b;
}
template <typename T, typename H>
Variable<T> operator/(const Variable<T>& a, const Variable<H>& b) {
return Variable(new Division<T, H>(a.get(), b.get()));
}
template <typename T, typename H>
auto operator/(const Variable<T>& a, const H& b) {
Variable<H> v = createVariable(b);
return a / v;
}
template <typename T, typename H>
auto operator/(const T& a, const Variable<H>& b) {
Variable<T> v = createVariable(a);
return v / b;
}
template <typename MatrixA, typename MatrixB>
Variable<typename MatMul<MatrixA, MatrixB>::ResultType> matmul(const Variable<MatrixA>& a, const Variable<MatrixB>& b) {
return Variable(new MatMul<MatrixA, MatrixB>(a.get(), b.get()));
}
template <typename MatrixA, typename MatrixB>
auto matmul(const Variable<MatrixA>& a, const MatrixB& b) {
Variable<MatrixB> v = createVariable(b);
return matmul(a, v);
}
template <typename MatrixA, typename MatrixB>
auto matmul(const MatrixA& a, const Variable<MatrixB>& b) {
Variable<MatrixA> v = createVariable(a);
return matmul(b, a);
}
template <TensorType T>
Variable<float> reduceAdd(const Variable<T>& t) {
return Variable(new ReduceAdd<T, float>(t.get()));
}
#endif