-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuron.cpp
164 lines (131 loc) · 2.92 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
#include "Neuron.h"
#include "Utils.h"
#include <cmath>
#include <iostream>
using namespace std;
Neuron::Neuron(int id, Neuron::Layer layer)
: m_id(id)
, m_layer(layer)
, m_sigError(0.0f)
{}
Neuron::Neuron(int id, Neuron* other)
: m_id(id)
, m_layer( other->layer() )
, m_sigError(0.0f)
{}
Neuron::~Neuron()
{}
int Neuron::id() const
{
return m_id;
}
void Neuron::setId(int id)
{
m_id = id;
}
Neuron::Layer Neuron::layer() const
{
return m_layer;
}
double Neuron::output() const
{
return m_lastOutput;
}
double Neuron::signalError() const
{
return m_sigError;
}
QList< Link* > Neuron::inConnections() const
{
return m_inConnections;
}
QList< Link* > Neuron::outConnections() const
{
return m_outConnections;
}
void Neuron::addInConnection(Link* link)
{
m_inConnections.push_front(link);
}
void Neuron::removeInConnection(Link* link)
{
m_inConnections.removeOne(link);
}
void Neuron::addOutConnection(Link* link)
{
m_outConnections.push_front(link);
}
void Neuron::removeOutConnection(Link* link)
{
m_outConnections.removeOne(link);
}
void Neuron::removeOutTowards(Neuron* next)
{
for (QList< Link* >::iterator it = m_outConnections.begin(); it != m_outConnections.end(); it++) {
Link* link = (*it);
if (*(link->successor()) == (*next)) {
m_outConnections.erase(it);
delete link;
}
}
}
void Neuron::setSignalError(double err)
{
m_sigError = err;
}
bool Neuron::operator==(const Neuron& other) const
{
return m_id == other.id();
}
SigmoidNeuron::SigmoidNeuron(int id, Neuron::Layer layer)
: Neuron(id, layer)
{}
SigmoidNeuron::SigmoidNeuron(int id, Neuron* other)
: Neuron(id, other->layer())
{}
SigmoidNeuron::~SigmoidNeuron()
{}
void SigmoidNeuron::computeOutput()
{
double z = 0.0;
/*
* Takes the output of the predecessor neuron and the weight of the link,
* for each incoming connection.
*/
Q_FOREACH (Link* in, m_inConnections) {
z += (in->output() * in->weight());
}
m_lastOutput = 1.0f / (1.0f + exp(-z));
/*
* Sets the output on the outgoing link, so it can be retrieved by the
* successor neuron.
*/
Q_FOREACH (Link* out, m_outConnections) {
out->setOutput(m_lastOutput);
}
}
TangentNeuron::TangentNeuron(int id, Neuron::Layer layer)
: Neuron(id, layer)
{}
TangentNeuron::TangentNeuron(int id, Neuron* other)
: Neuron(id, other->layer())
{}
TangentNeuron::~TangentNeuron()
{}
void TangentNeuron::computeOutput()
{
double z = 0.0;
Q_FOREACH (Link* in, m_inConnections) {
z += (in->output() * in->weight());
}
if (z < -10.0) {
m_lastOutput = -1.0;
} else if (z > 10.0) {
m_lastOutput = 1.0;
} else {
m_lastOutput = tanh(z);
}
Q_FOREACH (Link* out, m_outConnections) {
out->setOutput(m_lastOutput);
}
}