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
|
//Matrix.h
//NOTE: the part of interest is in the apply_function method all the way down!!
#pragma once
#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(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) = function((*this)(r, c));
}
}
return output;
}
//MLP.cpp
#include "Matrix.h"
#include "MLP.h"
#include <iostream>
void MLP::printParameters(){
for(int i = 0; i < weights.size(); i++){
std::cout << "weights in layer " << i << ":" << std::endl;
weights[i].print();
std::cout << "biases in layer " << i << ":" << std::endl;
biases[i].print();
}
}
inline double MLP::sigmoid(double x) {
return 1.0 / (1 + exp(-x));
}
/* here, passing sigmoid gives an error: argument of type
"double (MLP::*)(double x)" is incompatible with parameter of type
"double (*)(double)" */
void MLP::feedforward(Matrix<double> x){
for (size_t i = 0; i < layers.size(); i++){
auto z = weights[i].matmul(x) + biases[i];
activations[i] = z.apply_function(sigmoid);
}
}
| |