Repository navigation
Expand file tree
/
Copy pathModel.cpp
More file actions
138 lines (119 loc) · 3.39 KB
/
Copy pathModel.cpp
File metadata and controls
138 lines (119 loc) · 3.39 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
#include "Model.h"
#include <utility>
#include <fstream>
#include <iostream>
#include <iterator>
#define DELIMITER ' '
template <typename T>
std::vector<std::vector<T>> operator-=(std::vector<std::vector<double>> &x, SquareArray<T> y) {
for (int i = 0; i < x.size(); ++i) {
for (int j = 0; j < x[i].size(); ++j) {
x[i][j] -= y[i][j];
}
}
return x;
}
template <typename T>
double Model<T>::f(int i, SquareArray<T> const &x) {
return std::exp(this->w.calc(x, i)/this->sigma);
}
template <typename T>
void Model<T>::update(SquareArray<T> const &x) {
std::fill(diff.cube.begin(), diff.cube.end(), 0);
for (size_t i1 = 0; i1 < filters; ++i1) {
diff.plus_index(i1, (x - w[i1]) * (f(i1, x)));
for (size_t i2 = 0; i2 < filters; ++i2) {
if (i1 != i2) {
diff.minus_index(i1, (w[i2] - w[i1]) * (2.0 * lambda * f(i1, w[i2])));
}
}
}
w += ((diff * learning_rate) / sigma);
}
/*
* Saves an array to file with following format
*
* 1 2
* 3 4
*
* 5 6
* 7 8
*
* 3 2
* 4 1
*
* represents (3x2x2) array
*
* [[[1 2],
* [3 4] ],
*
* [[5 6],
* [7 8] ],
*
* [[3 2],
* [4 1] ]]
*/
template <typename T>
void Model<T>::save(const char &subfigure) {
std::string path = "../saved/figure2";
path.push_back(subfigure);
path.append(".fig");
std::ofstream output_file(path);
std::cout << "Saving figure" << std::endl;
for(size_t layer = 0; layer < filters; layer++) {
for (size_t row = 0; row < resolution; row++) {
auto temp2 = w[layer];
auto temp = temp2[row];
std::ostream_iterator<double> output_iterator(output_file, " ");
std::copy(temp.begin(), temp.end(), output_iterator);
output_file << "\n";
}
output_file << "\n";
}
}
/*
* Trims the right side of given string to ensure there isn't any excess whitespace
* @param s string to trim
*/
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
/*
* Reads a file and loads numbers into a model's mu
* @param subfigure char representing the subfigure to load
* @return true if mu was properly loaded
*/
template <typename T>
bool Model<T>::load(const char &subfigure) {
std::string path = "../saved/figure2";
path.push_back(subfigure);
path.append(".fig");
if(!std::filesystem::exists(path)){
std::cout << "Figure " << subfigure << " not found." << std::endl;
return false;
}
std::ifstream file(path);
std::string line;
this->w.cube.clear();
std::vector<T> inner = {};
while (std::getline(file, line)) {
rtrim(line);
// TODO The following line may or may not need to be active, depending on system locale \
If filter plots are empty, try (un)commenting it.
//std::replace(line.begin(), line.end(), '.', ',');
size_t last = 0, next;
while ((next = line.find(DELIMITER, last)) != std::string::npos) {
inner.emplace_back(std::stod(line.substr(last, next-last)));
last = next + 1;
}
if(!line.substr(last).empty()){
inner.emplace_back(std::stod(line.substr(last)));
}
}
this->w.cube.swap(inner);
return true;
}
template class Model<int>;
template class Model<double>;