-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron.h
56 lines (48 loc) · 1.37 KB
/
perceptron.h
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
#ifndef PERCEPTRON_H
#define PERCEPTRON_H
#include "data.h"
#include "model.h"
#include "autograd.h"
struct perception_parameter: public model_parameter
{
/* data */
};
class Perceptron: public Model<perception_parameter>
{
public:
Perceptron(shared_ptr<DataLoader> dataloader): Model(dataloader) {}
void train()
{
weights = EinsorD(dataloader->X.cols(), 1, true, 0);
param.current_iteration = 0;
while(true)
{
EinsorD x, y;
dataloader->loadData(x, y, param.batch_size);
EinsorD result = hadamardprod(y, x * weights);
EinsorD mask((result.data->array() <= 0).matrix().cast<double>());
result = hadamardprod(result, mask);
result.sum().backward();
*weights.data += param.learning_rate * *weights.grad;
weights.grad_zero();
if (param.max_iterations > 0 && param.current_iteration > param.max_iterations)
break;
++param.current_iteration;
}
cout << weights;
}
EinsorD predict(EinsorD &input, bool post_process = true)
{
EinsorD result = input * weights;
activate(result);
return result;
}
void activate(EinsorD& result)
{
}
private:
EinsorD weights;
EinsorD bias;
};
void testPerceptron(shared_ptr<DataLoader> dataloader);
#endif