-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec3d.cpp
66 lines (59 loc) · 2.11 KB
/
vec3d.cpp
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
#include "vec3d.hpp"
#include "mlp.hpp"
#include "configurations.h"
#include <iostream>
using namespace std;
vec3d::vec3d(const vector<int> &layer_sizes, bool use_random, bool use_bias_neurons){
for(int k = 0; k < layer_sizes.size() - 1; k++){
vector<vector<double> > vec1;
for(int i = 0; i < (use_bias_neurons ? layer_sizes[k] + 1 : layer_sizes[k]); i++){ // if we add bias, add one artificial node to each layer
vector<double> vec2;
for(int j = 0; j < layer_sizes[k + 1]; j++){
if (use_random)
vec2.push_back(MLP::get_rand()); // when we create real W, we initialize it with random variables
else
vec2.push_back(0); // when we create W for keeping last deltaW (used for momentum), we have to initialize it with zeroes
}
vec1.push_back(vec2);
}
data.push_back(vec1);
}
}
vec3d::vec3d(const vec3d& vec){
for(int i = 0; i < vec.data.size(); i++){
data.push_back(vector<vector<double> >());
for(int j = 0; j < vec.data[i].size(); j++){
data[i].push_back(vector<double>(vec.data[i][j]));
}
}
}
double& vec3d::operator()(int i, int j, int k){ // returns weigth of edge that connects ith neuron at kth layer to jth neuron at k+1th layer
return data[k][i][j];
}
ostream& operator<<(ostream &out, const vec3d& v){ // prints weight matrix
for (int k = 0; k < v.data.size(); k++){
for (int i = 0; i < v.data[k].size(); i++){
for (int j = 0; j < v.data[k][i].size(); j++){
out << "(" << i << ", " << j << ", " << k << "): " << v.data[k][i][j] << endl;
}
}
}
return out;
}
inline int get_w_idx(int i, int j, int k){
//return layer_starts_w[k] + i * layer_sizes[k + 1] + j;
#ifdef STRANGE_VERSION
return layer_starts_w[k] + i + layer_sizes[k]* j;
#else
return layer_starts_w[k] + i + (layer_sizes[k] + 1) * j;
#endif
}
void vec3d::fill_w_array(float *w){
for (int k = 0; k < data.size(); k++){
for (int i = 0; i < data[k].size(); i++){
for (int j = 0; j < data[k][i].size(); j++){
w[get_w_idx(i, j, k)] = data[k][i][j];
}
}
}
}