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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
#include <vector>
#include <cmath>
#include <cassert>
#include <iostream>
#include <tuple>
#include <random>
#include <functional>
template<typename Type>
class Matrix {
size_t cols {};
size_t rows {};
public:
std::vector<std::vector<Type>> data;
std::tuple<size_t, size_t> shape;
size_t elementCount {};
/* constructors */
Matrix(size_t rowsArg, size_t colsArg) : cols(colsArg), rows(rowsArg),
elementCount(rows* cols), shape(std::tuple<size_t, size_t>(rows, cols)) {
data = std::vector<std::vector<Type>>(rows, std::vector<Type>(cols));
}
Matrix() {};
//methods
void print();
Matrix<Type> matmul(Matrix<Type>& m);
Matrix<Type> multiply_elementwise(Matrix<Type>& m);
Matrix<Type> multiply_scalar(Type scalar);
Matrix<Type> square();
Matrix<Type> add(Matrix<Type>& m);
Matrix<Type> sub(Matrix& target);
Matrix<Type> T();
Matrix<Type> apply_function(Type(*func)(Type));
Type& operator()(size_t row, size_t col) {
assert(row < data.size() && col < data[0].size());
return data[row][col];
}
Matrix operator+(Matrix& target) {
return add(target);
}
Matrix operator-() {
Matrix output(rows, cols);
for (size_t r = 0; r < rows; ++r) {
for (size_t c = 0; c < cols; ++c) {
output(r, c) = -(*this)(r, c);
}
}
return output;
}
Matrix operator-(Matrix& target) { // for cleaner usage
return sub(target);
}
};
// methods
template<typename Type>
void Matrix<Type>::print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}
template <typename Type>
Matrix<Type> Matrix<Type>::matmul(Matrix<Type>& target) {
assert(cols == target.rows);
Matrix output(rows, target.cols);
for (size_t r = 0; r < output.rows; ++r) {
for (size_t c = 0; c < output.cols; ++c) {
for (size_t k = 0; k < target.rows; ++k)
output(r, c) += (*this)(r, k) * target(k, c);
}
}
return output;
};
template <typename T>
struct mtx {
static Matrix<T> randn(size_t rows, size_t cols) {
Matrix<T> M(rows, cols);
std::random_device rd {};
std::mt19937 gen { rd() };
// init Gaussian distr. w/ N(mean=0, stdev=1/sqrt(numel))
T n = static_cast<T>(M.elementCount);
T stdev { 1 / sqrt(n) };
std::normal_distribution<T> d { 0, stdev };
// fill each element w/ draw from distribution
for (size_t r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
M(r, c) = d(gen);
}
}
return M;
}
};
template <typename Type>
Matrix<Type> Matrix<Type>::multiply_elementwise(Matrix<Type>& target) {
assert(shape == target.shape);
Matrix output((*this));
for (size_t r = 0; r < output.rows; ++r) {
for (size_t c = 0; c < output.cols; ++c) {
output(r, c) = target(r, c) * (*this)(r, c);
}
}
return output;
}
template<typename Type>
Matrix<Type> Matrix<Type>::square() {
Matrix output((*this));
output = multiply_elementwise(output);
return output;
}
template<typename Type>
Matrix<Type> Matrix<Type>::multiply_scalar(Type scalar) {
Matrix output((*this));
for (size_t r = 0; r < output.rows; ++r) {
for (size_t c = 0; c < output.cols; ++c) {
output(r, c) = scalar * (*this)(r, c);
}
}
return output;
}
template<typename Type>
Matrix<Type> Matrix<Type>::add(Matrix& target) {
assert(shape == target.shape);
Matrix output(rows, std::get<1>(target.shape));
for (size_t r = 0; r < output.rows; ++r) {
for (size_t c = 0; c < output.cols; ++c) {
output(r, c) = (*this)(r, c) + target(r, c);
}
}
return output;
}
template<typename Type>
Matrix<Type> Matrix<Type>::sub(Matrix& target) {
Matrix neg_target = -target;
return add(neg_target);
}
template<typename Type>
Matrix<Type> Matrix<Type>::T() {
size_t new_rows { cols }, new_cols { rows };
Matrix transposed(new_rows, new_cols);
for (size_t r = 0; r < new_rows; ++r) {
for (size_t c = 0; c < new_cols; ++c) {
transposed(r, c) = (*this)(c, r); // swap row and col
}
}
return transposed;
}
template<typename Type>
Matrix<Type> Matrix<Type>::apply_function(Type(*func)(Type)) {
Matrix output((*this));
for (size_t r = 0; r < rows; ++r) {
for (size_t c = 0; c < cols; ++c) {
output(r, c) = func((*this)(r, c));
}
}
return output;
}
template<typename T>
class MLP {
public:
std::vector<size_t> units_per_layer;
std::vector<Matrix<T>> bias_vectors;
std::vector<Matrix<T>> weight_matrices;
std::vector<Matrix<T>> activations;
double lr = .001;
MLP(const std::vector<size_t>& units_per_layer) :
units_per_layer(units_per_layer) {
//weight_matrices(),
//bias_vectors(),
//activations() {
for (size_t i = 0; i < units_per_layer.size() - 1; ++i) {
size_t in_channels { units_per_layer[i] };
size_t out_channels { units_per_layer[i + 1] };
// initialize to random Gaussian
auto W = mtx<T>::randn(out_channels, in_channels);
weight_matrices.push_back(W);
auto b = mtx<T>::randn(out_channels, 1);
bias_vectors.push_back(b);
activations.resize(units_per_layer.size());
}
}
inline auto sigmoid(double x) {
return 1.0 / (1 + exp(-x));
}
inline auto d_sigmoid(double x) {
return (x * (1 - x));
}
auto forward(const Matrix<T>& x) {
assert(std::get<0>(x.shape) == units_per_layer[0] && std:::get<1>(x.shape));
activations[0] = x;
auto prev(x);
for (int i = 0; i < units_per_layer.size() - 1; ++i) {
auto y = weight_matrices[i].matmul(prev);
y = y + bias_vectors[i];
y = y.apply_function(sigmoid);
activations[i + 1] = y;
prev = y;
}
return prev;
}
void backprop(const Matrix<T>& target) {
assert(get<0>(target.shape) == units_per_layer.back());
// determine the simple error
// error = target - output
auto y = target;
auto y_hat = activations.back();
auto error = (target - y_hat);
// backprop the error from output to input and step the weights
for (int i = weight_matrices.size() - 1; i >= 0; --i) {
//calculating errors for previous layer
auto Wt = weight_matrices[i].T();
//// delta NOT DEFINED
//auto prev_errors = Wt.matmul(delta);
// apply derivative of function evaluated at activations
//backprop for biases
auto d_outputs = activations[i + 1].apply_function(d_sigmoid);
auto gradients = error.multiply_elementwise(d_outputs);
gradients = gradients.multiply_scalar(lr);
// backprop for weights
auto a_trans = activations[i].T();
auto weight_gradients = gradients.matmul(a_trans);
//adjust weights
bias_vectors[i] = bias_vectors[i].add(gradients);
weight_matrices[i] = weight_matrices[i].add(weight_gradients);
////error = prev_errors;
}
}
};
int main() {
const std::vector<size_t> layers { 3,3 };
MLP<double> test (layers);
}
|