-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarray.h
209 lines (195 loc) · 5.68 KB
/
Carray.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// @note: This Array class is created by professor Chiu. Using as it is after understanding.
//
#ifndef NEURAL_NETWORK_CARRAY_H
#define NEURAL_NETWORK_CARRAY_H
#include "common.h"
using std::size_t;
using Precision = double;
constexpr double DELTA_H = 1E-5;
constexpr int PADDING = 2;
constexpr int FILTER_H = 5;
constexpr int FILTER_W = 5;
inline double
derivative_error(double n, double d) {
return std::abs(n - d)/std::max(std::abs(n), std::abs(d));
}
// This holds a sequence of dimensions together in a single type.
template <size_t DP, size_t HP, size_t WP>
struct Dims {
constexpr static size_t D = DP;
constexpr static size_t H = HP;
constexpr static size_t W = WP;
constexpr static size_t N = D*H*W;
};
template <typename T, size_t D, size_t H, size_t W>
std::ostream &operator<<(std::ostream &os, const T (&a)[D][H][W]) {
for (size_t h = 0; h < D; h++) {
if (h > 0) {
os << "----------" << std::endl;
}
for (size_t i = 0; i < H; i++) {
for (size_t j = 0; j < W; j++) {
if (j > 0) {
os << " ";
}
os << std::fixed << std::setprecision(7) << a[h][i][j];
}
os << "\n";
}
}
return os;
}
/*
* Array class: This is a wrapper around native arrays to get range-checking.
* It is similar to std::array, but more convenient for multi-dimensional arrays.
*/
// Forward declaration for output operators.
template <typename T, size_t D, size_t... Ds> class Array;
// Output operators for up to 4-D.
template <typename T, size_t D0>
std::ostream &
operator<<(std::ostream &os, const Array<T, D0> &a) {
for (size_t i = 0; i < D0; i++) {
if (i > 0) {
os << " ";
}
os << std::fixed << std::setprecision(7) << a[i];
}
os << std::endl;
return os;
}
template <typename T, size_t D1, size_t D0>
std::ostream &
operator<<(std::ostream &os, const Array<T, D1, D0> &a) {
for (size_t i = 0; i < D1; i++) {
os << std::fixed << std::setprecision(7) << a[i];
}
return os;
}
template <typename T, size_t D2, size_t D1, size_t D0>
std::ostream &
operator<<(std::ostream &os, const Array<T, D2, D1, D0> &a) {
for (size_t h = 0; h < D2; h++) {
os << "Matrix " << h << ":" << std::endl;
os << a[h];
}
return os;
}
template <typename T, size_t D3, size_t D2, size_t D1, size_t D0>
std::ostream &
operator<<(std::ostream &os, const Array<T, D3, D2, D1, D0> &a) {
for (size_t g = 0; g < D3; g++) {
os << "Tensor " << g << ":" << std::endl;
os << a[g];
}
return os;
}
// General definition of template.
template <typename T, size_t D, size_t... Ds>
class Array {
friend std::ostream &operator<<<>(std::ostream &, const Array &);
public:
Array() = default;
template <typename U>
Array(const U &v) {
*this = v;
}
Array<T, Ds...> &operator[](const size_t i) {
assert(i < D);
return array[i];
}
const Array<T, Ds...> &operator[](const size_t i) const {
assert(i < D);
return array[i];
}
template <typename... Ts>
T &operator()(const size_t i, const Ts... rest) {
return (*this)[i](rest...);
}
template <typename... Ts>
const T &operator()(const size_t i, const Ts... rest) const {
return (*this)[i](rest...);
}
template <typename U>
Array &operator=(const U &v) {
std::fill(std::begin(array), std::end(array), v);
return *this;
}
template <typename U>
Array &operator=(const U (&a)[D]) {
std::copy(std::begin(a), std::end(a), std::begin(array));
return *this;
}
Array<T, Ds...> *begin() { return &array[0]; }
Array<T, Ds...> *end() { return &array[D]; }
const Array<T, Ds...> *begin() const { return &array[0]; }
const Array<T, Ds...> *end() const { return &array[D]; }
private:
Array<T, Ds...> array[D];
};
// Base case.
template <typename T, size_t D>
class Array<T, D> {
friend std::ostream &operator<<<>(std::ostream &, const Array &);
public:
Array() = default;
template <typename U>
Array(const U &v) {
*this = v;
}
T &operator[](const size_t i) {
#ifndef NDEBUG
if (i >= D) {
std::cerr << "Index " << i << " beyond end of array of size " << D << "." << std::endl;
assert(false);
abort();
}
#endif
return array[i];
}
const T&operator[](const size_t i) const {
#ifndef NDEBUG
if (i >= D) {
std::cerr << "Index " << i << " beyond end of array of size " << D << "." << std::endl;
assert(false);
abort();
}
#endif
return array[i];
}
T &operator()(const size_t i) {
return (*this)[i];
}
const T &operator()(const size_t i) const {
return (*this)[i];
}
template <typename U>
Array &operator=(const Array<U, D> &a) {
std::copy(std::begin(a), std::end(a), std::begin(array));
return *this;
}
template <typename U>
Array &operator=(const U (&a)[D]) {
std::copy(std::begin(a), std::end(a), std::begin(array));
return *this;
}
template <typename U>
Array &operator=(const U &v) {
std::fill(std::begin(array), std::end(array), v);
return *this;
}
T *begin() { return &array[0]; }
T *end() { return &array[D]; }
const T *begin() const { return &array[0]; }
const T *end() const { return &array[D]; }
private:
T array[D];
};
// Conversion.
template <typename T1, typename T2> struct ArrayDims;
template <typename T, size_t... Ds>
struct ArrayDims<T, Dims<Ds...>> {
using type = Array<T, Ds...>;
};
#endif //NEURAL_NETWORK_CARRAY_H