-
Notifications
You must be signed in to change notification settings - Fork 0
/
HELPERS.cpp
73 lines (60 loc) · 1.83 KB
/
HELPERS.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
67
68
69
70
71
72
73
// NeuralNetwork.cpp
#include "HELPERS.h"
#include <algorithm> // for std::fill
void LIF::reset() {
this->state = 0;
}
bool LIF::update(float& input) {
if (state <= 0) {
state = input; // avoid multiply
} else {
state = state * beta + input;
}
if (state > threshold) {
state = 0;
return true;
}
return false;
}
LIFNeuronLayer::LIFNeuronLayer(int size) : size(size), states(size), out(size) {}
// LIFNeuronLayer::LIFNeuronLayer():{}
void LIFNeuronLayer::reset_states() {
for (int i = 0; i < size; ++i) {
states[i] = 0;
}
}
void LIFNeuronLayer::state_update(float& state, const float& inp) {
if (state <= 0) {
state = inp; // avoid multiply
} else {
state = state * beta + inp;
}
}
std::vector<bool> LIFNeuronLayer::update(const std::vector<float>& input) {
// now zero reset
for (int i = 0; i < input.size(); ++i) {
state_update(states[i], input[i]);
if (states[i] > threshold) {
states[i] = 0;
out[i] = true;
} else {
out[i] = false;
}
}
return out;
}
AccumLinear::AccumLinear(int nr_ins, int nr_outs) : nr_ins(nr_ins), nr_outs(nr_outs), weights(nr_ins, std::vector<float>(nr_outs, 0.0)), out(nr_outs, 0.0) {}
AccumLinear::AccumLinear(int nr_ins, int nr_outs, const std::vector<std::vector<float>>& weights) : nr_ins(nr_ins), nr_outs(nr_outs), weights(weights), out(nr_outs, 0.0) {}
// AccumLinear::AccumLinear() :{}
std::vector<float> AccumLinear::forward(const std::vector<bool>& input) {
// Initialize output array to zero
std::fill(out.begin(), out.end(), 0.0);
for (int i = 0; i < nr_ins; i++) {
if (input[i]) {
for (int j = 0; j < nr_outs; ++j) {
out[j] += weights[i][j];
}
}
}
return out;
}