-
Notifications
You must be signed in to change notification settings - Fork 1
/
ntuple.h
93 lines (78 loc) · 2.65 KB
/
ntuple.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
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
#ifndef NTUPLE_H
#define NTUPLE_H
#include <iostream>
#include <cstdlib>
#include <cstdint>
#include <vector>
#include <cmath>
using namespace std;
class NTupleNetwork {
public:
float learningRate = 0.01;
explicit NTupleNetwork() {
}
void learn(const vector<int> &tuples, float target) {
float score = 0;
for (size_t i=0; i < tuples.size(); i++) {
score += weights[i][tuples[i]];
}
score = tanh(score+bias);
float error = target - score;
float delta = error * tanh_prime(score);
for (size_t i=0; i < tuples.size(); i++) {
weights[i][tuples[i]] += learningRate * delta;
}
bias += learningRate * delta;
}
float predict(const vector<int> &tuples) {
float output = 0;
for (size_t i=0; i < tuples.size(); i++) {
output += weights[i][tuples[i]];
}
return tanh(output+bias);
}
void learnSym(const vector<int> &tuples, float target) {
float score = 0;
score += weightsSym[0][tuples[0]];
score += weightsSym[1][tuples[1]];
score += weightsSym[0][tuples[2]];
score += weightsSym[0][tuples[3]];
score += weightsSym[1][tuples[4]];
score += weightsSym[0][tuples[5]];
score += weightsSym[2][tuples[6]];
score += weightsSym[2][tuples[7]];
score = tanh(score+biasSym);
float error = target - score;
float delta = error * tanh_prime(score);
weightsSym[0][tuples[0]] += learningRate * delta;
weightsSym[1][tuples[1]] += learningRate * delta;
weightsSym[0][tuples[2]] += learningRate * delta;
weightsSym[0][tuples[3]] += learningRate * delta;
weightsSym[1][tuples[4]] += learningRate * delta;
weightsSym[0][tuples[5]] += learningRate * delta;
weightsSym[2][tuples[6]] += learningRate * delta;
weightsSym[2][tuples[7]] += learningRate * delta;
biasSym += learningRate * delta;
}
float predictSym(const vector<int> &tuples) {
float output = 0;
output += weightsSym[0][tuples[0]];
output += weightsSym[1][tuples[1]];
output += weightsSym[0][tuples[2]];
output += weightsSym[0][tuples[3]];
output += weightsSym[1][tuples[4]];
output += weightsSym[0][tuples[5]];
output += weightsSym[2][tuples[6]];
output += weightsSym[2][tuples[7]];
return tanh(output+biasSym);
}
private:
float weights[8][54] = {};
float bias = 0;
float weightsSym[3][54] = {};
float biasSym = 0;
float tanh_prime(float x) { // x already tanhed
return 1 - x*x;
}
};
#endif // NTUPLE_H