-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp.cc
57 lines (49 loc) · 1.29 KB
/
mlp.cc
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
#ifndef MLP_CC
#define MLP_CC
#include <vector>
#include "value.cc"
#include "parameter.cc"
#include "layer.cc"
class MLP : BaseParameterClass
{
private:
public:
std::vector<Layer> layers;
MLP(std::vector<size_t> layer_sizes)
{
layers = std::vector<Layer>();
layers.reserve(layer_sizes.size());
for (auto index = 0; index < layer_sizes.size(); index++)
{
// inputs are previous layers number of nodes
layers.push_back(Layer(layer_sizes[index], layer_sizes[std::max(index - 1, 0)]));
}
}
std::vector<ValuePtr> run(std::vector<double> inputs)
{
auto values = std::vector<ValuePtr>();
values.reserve(inputs.size());
std::transform(inputs.begin(), inputs.end(), std::back_inserter(values), [](auto double_value)
{ return ValuePtr(new Value(double_value)); });
for (auto layer : layers)
{
values = layer.run(values);
}
return values;
}
void zero_grad() override
{
for (auto layer : layers)
{
layer.zero_grad();
}
}
void update_params(double alpha) override
{
for (auto layer : this->layers)
{
layer.update_params(alpha);
}
}
};
#endif