-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIF.cpp
224 lines (178 loc) · 5.62 KB
/
LIF.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <random>
#include <vector>
class LIF{
public:
float beta;
const int threshold = 1;
float state;
// constructors
LIF(): beta(0.5), state(0){};
LIF(float beta): beta(beta), state(0){};
void reset(){
this->state = 0;
}
bool update(float& input){
if (state <= 0){
state = input; // avoid multiply
}
else{
state = state*beta + input;
}
if(state > threshold){
state = 0;
return true;
}
return false;
}
};
class LIFNeuronLayer{
public:
int size;
float beta =0.9;
std::vector<bool> out; // Replace bool* with std::vector<bool>
std::vector<float> states;
const int threshold = 1;
// constructors
LIFNeuronLayer(int size) : size(size), states(size), out(size) {}
void reset_states(){
for (int i = 0; i < size; ++i) {
states[i] = 0;
}
};
void state_update(float& state, const float& inp){
if (state <= 0){
state = inp; // avoid multiply
}
else{
state = state*beta + inp;
}
}
std::vector<bool> 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;
}
};
// class AccumLinear{
// // todo: add bias
// // improve memory management
// public:
// int nr_ins;
// int nr_outs;
// float** weights;
// float* out;
// // Constructor with initialization
// AccumLinear(int nr_ins, int nr_outs) : nr_ins(nr_ins), nr_outs(nr_outs) {
// float weights[nr_ins][nr_outs];
// float out[nr_outs];
// // Initialize weights and output to zero
// for (int i = 0; i < nr_ins; ++i) {
// for (int j = 0; j < nr_outs; ++j) {
// weights[i][j] = 0.1;
// }
// }
// for (int i = 0; i < nr_outs; ++i) {
// out[i] = 0.0;
// }
// }
// // Constructor with provided weights
// AccumLinear(int nr_ins, int nr_outs, const float** input_weights) : nr_ins(nr_ins), nr_outs(nr_outs) {
// float weights[nr_ins][nr_outs];
// float out[nr_outs];
// // Copy provided weights
// for (int i = 0; i < nr_ins; ++i) {
// for (int j = 0; j < nr_outs; ++j) {
// weights[i][j] = input_weights[i][j];
// }
// }
// // Initialize output to zero
// for (int i = 0; i < nr_outs; ++i) {
// out[i] = 0.0;
// }
// }
// float* forward(float* input){
// // Initialize output array to zero
// for (int i = 0; i < nr_outs; ++i) {
// out[i] = 0.0;
// }
class AccumLinear {
public:
int nr_ins;
int nr_outs;
std::vector<std::vector<float> > weights;
std::vector<float> out;
// Constructor with initialization
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) {}
// Constructor with provided weights
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) {}
// Function to perform the forward pass
std::vector<float> 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] == true)
{
for (int j = 0; j < nr_outs; ++j) {
out[j] += weights[i][j];
}
}
}
return out;
}
};
int main(){
LIF lif(0.5);
AccumLinear layer1(3,5);
// LIFNeuronLayer lif1(3);
// LIFNeuronLayer lif2(5);
// lif1.reset_states();
// lif2.reset_states();
// float in[2] = {.3, .4};
// std::vector<float> in = {.3, 1};
// Example usage
const int nr_ins = 3;
const int nr_outs = 2;
// Initialize weights
const std::vector<std::vector<float> > input_weights = {{.30, .40}, {.70, -.30}, {-.20, .0}};
// Create AccumLinear instance
AccumLinear accumLinear(nr_ins, nr_outs, input_weights);
// Example input
const std::vector<float> input = {1.0, 5.0, 2.0};
// Perform forward pass
LIFNeuronLayer lif1(3);
LIFNeuronLayer lif2(2);
lif1.reset_states();
lif2.reset_states();
for (int i = 0; i < 10; ++i) {
std::vector<bool> spk1 = lif1.update(input);
std::vector<float> result = accumLinear.forward(spk1);
std::vector<bool> spk2 = lif2.update(result);
// Display result
std::cout << "first spikes: ";
for (int i = 0; i < nr_ins; ++i) {
std::cout << spk1[i] << " ";
}
std::cout << std::endl;
std::cout << "first layer outputs: ";
for (int i = 0; i < nr_outs; ++i) {
std::cout << result[i] << " ";
}
std::cout << std::endl;
std::cout << "second spikes: ";
for (int i = 0; i < nr_outs; ++i) {
std::cout << spk2[i] << " ";
}
std::cout << std::endl;
}
return 0;
}