-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuron.cpp
263 lines (199 loc) · 4.24 KB
/
neuron.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "neuron.h"
#include <math.h>
#include <inttypes.h>
#include <iomanip>
TYPE func(TYPE x)
{
//return x;
//return std::tanh(x);
return 1/(1+exp(-x));
}
TYPE func_deriv(TYPE x)
{
//return 1;
//x = tanh(x);
//return 1.0 - x*x;
return x*(1-x);
}
TYPE func_inv(TYPE x)
{
//return x;
//return std::atanh(x);
return x;
}
Neuron::Neuron()
{
value = 0.0;
connections.resize(0);
}
Neuron::Neuron(TYPE default_val, int size)
{
value = 0.0;
connections.resize(size);
set_connections(default_val);
}
Neuron::Neuron(TYPE default_val, TYPE biasv, int size)
{
value = 0.0;
connections.resize(size);
set_connections(default_val);
bias = biasv;
}
//FORWARD FEEDING
void Neuron::activate()
{
value = func(value);
}
void Neuron::feed(const values_t& input)
{
#ifndef OPTIMIZED
if(input.size() != connections.size()){
throw "Invalid fire sizes!";
}
#endif
value = bias;
for(int i=0; i<input.size(); i++){
value += input[i] * connections[i];
}
}
void Neuron::feed(const layer_t& layer)
{
#ifndef OPTIMIZED
if(layer.size() != connections.size()){
throw "Invalid fire sizes!";
}
#endif
value = bias;
for(int i=0; i<layer.size(); i++){
value += layer[i].value * connections[i];
}
}
void Neuron::feed_forward(const values_t& input)
{
feed(input);
activate();
}
void Neuron::feed_forward(const layer_t& layer)
{
feed(layer);
activate();
}
//BACKWARD FEEDING
void Neuron::reverse_activate()
{
value = func_inv(value);
}
//OTHER
void Neuron::set_connections(TYPE default_val)
{
for(int i=0; i<connections.size(); i++){
connections[i] = default_val;
}
bias = default_val;
}
size_t Neuron::size()
{
return connections.size();
}
TYPE& Neuron::operator[](size_t index)
{
return connections[index];
}
/*
void Neuron::set_value(TYPE val)
{
value=val;
}
TYPE Neuron::get_value() const
{
return value;
}
size_t Neuron::get_size() const
{
return connections.size();
}
void Neuron::set_connection_at(int connection, TYPE value)
{
connections[connection] = value;
}
TYPE Neuron::get_connection_at(int connection) const
{
return connections[connection];
}
*/
void Neuron::randomize_connections(TYPE min, TYPE max)
{
for(int i=0; i<connections.size(); i++){
connections[i] = RANDY(min, max);
}
bias = RANDY(min, max);
}
void Neuron::print() const
{
for(int i=0; i<connections.size(); i++){
printf("\t%.5f", connections[i]);
}
printf(", bias: %.5f, value at %.5f\n", bias, value);
}
//serialization
void Neuron::write_to(std::FILE* stream) const
{
fprintf(stream, "size: %lu,", connections.size());
for(int i=0; i<connections.size(); i++){
fprintf(stream, " %.17lf", connections[i]);
}
fprintf(stream, ", bias: %.17lf", bias);
fprintf(stream, "\n");
}
void Neuron::read_from(std::FILE* stream)
{
size_t size = 0;
fscanf(stream, "size: %lu,", &size);
connections.resize(size);
for(int i=0; i<size; i++){
fscanf(stream, " %lf", &connections[i]);
}
fscanf(stream, ", bias: %lf", &bias);
fscanf(stream, "\n");
}
bool operator==(const Neuron& n1, const Neuron& n2)
{
if(n1.connections.size() != n2.connections.size())
return false;
for(int i=0; i<n1.connections.size(); i++){
if(abs(n1.connections[i] - n2.connections[i]) > EPSILON)
return false;
}
if(abs(n1.bias - n2.bias) > EPSILON)
return false;
return true;
}
std::istream& operator>>(std::istream& is, Neuron& n)
{
//value doesnt need to be saved
//is >> n.value;
n.value = 0.0;
size_t size;
is >> size;
n.connections.resize(size);
for(int i=0; i<size; i++){
is >> n.connections[i];
}
is >> n.bias;
//if( /* T could not be constructed */ )
// is.setstate(std::ios::failbit);
return is;
}
std::ostream& operator<<(std::ostream& os, const Neuron& n)
{
//value doesnt need to be saved
//os << n.value;
os << n.connections.size();
os << " " << std::setprecision(17);
for(int i=0; i<n.connections.size(); i++){
os << n.connections[i] << " ";
}
os << n.bias;
os << "\n";
return os;
}