-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuron.cpp
168 lines (137 loc) · 2.64 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
/* Neural Network Code for PDNE
* Written by Graham Holker on 6-14-2010
*/
#ifndef NEURON
#define NEURON
#include <iostream>
#include <cmath>
#include "Neuron.hpp"
#include "DataCollector.hpp"
CfgFile *Neuron::config = NULL;
void Neuron::set_config( CfgFile& cfg )
{
config = &cfg;
}
Neuron::Neuron( int num, double *in_weights, int num_weights )
{
if( config == NULL )
DataCollector::debug << "Neuron::config is NULL." << endline;
init( num );
setWeights( in_weights, num_weights );
}
Neuron::Neuron( int num )
{
init( num );
}
Neuron::Neuron()
{
// default to INPUT
init( 0 );
}
void Neuron::init( int num )
{
isOrNeuron = false;
threshold = 1.0;
this->num_inputs = (short)num;
setValue( 0. );
inputs = NULL;
initInputs();
initWeights();
}
void Neuron::initInputs()
{
if( !isInput() )
{
inputs = new Neuron*[num_inputs];
for( int i = 0; i < num_inputs; i++ )
inputs[i] = NULL;
}
}
Neuron::~Neuron()
{
// DELETE WEIGHTS
if( !isInput() )
{
delete [] weights;
weights = NULL;
}
// DELETE BRANCHES
if( !isInput() )
{
for( int i = 0; i < num_inputs; i++ )
if( inputs[i] && !inputs[i]->isInput() )
delete inputs[i];
delete [] inputs;
inputs = NULL;
}
}
bool Neuron::isInput()
{
return num_inputs <= 0;
}
void Neuron::initWeights()
{
if( !isInput() )
{
weights = new double[num_inputs];
for( int i = 0; i < num_inputs; i++ )
weights[i] = 0.;
}
}
void Neuron::setWeights( double *w, int nw )
{
for( int i = 0; ( i < num_inputs ) && ( i < nw ); i++ )
weights[i] = w[i];
}
void Neuron::setWeight( short i, double weight )
{
if( i < num_inputs )
weights[i] = weight;
}
double Neuron::getWeight( short i )
{
return weights[i];
}
void Neuron::setValue( double val )
{
value = val;
}
double Neuron::getValue()
{
return value;
}
// EVALUATE -- RECURSIVE
double Neuron::evaluate()
{
// IF INPUT, RETURN VALUE
if( isInput() )
return value;
// ELSE EVALUATE
// FIND WEIGHTED SUM
double sum = 0.;
for( int i = 0; i < num_inputs; i++ )
sum += weights[i] * inputs[i]->evaluate();
// ACTIVATION FUNCTION
switch( config->activation_function )
{
case SIGMOID:
return fsigmoid( sum, config->activation_slope );
case BINARY:
return binary_activation( sum, threshold );
case HYPERBOLIC_TANGENT:
return hyperbolic_tangent( sum, config->activation_slope );
}
return 0.;
}
void Neuron::setLink( short i, Neuron *n )
{
if( i < num_inputs )
{
inputs[i] = n;
}
}
short Neuron::getNumInputs()
{
return num_inputs;
}
#endif