-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.cpp
77 lines (60 loc) · 1.88 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
#include "neuron_utils.h"
#include "neuron.h"
using namespace std;
/************************************************************************
* neuron
************************************************************************/
/************************************************************************
* neuron layer
************************************************************************/
neuronLayer::neuronLayer(int numNeurons, int numInputsPerNeuron)
:mNumNeurons(numNeurons),
mNumInputsPerNeuron(numInputsPerNeuron)
{
mWeights = new double*[mNumNeurons];
mOutActivations = new double[mNumNeurons];
mOutErrors = new double[mNumNeurons];
reset();
}
neuronLayer::neuronLayer(neuronLayer& nl)
:neuronLayer(nl.mNumNeurons, nl.mNumInputsPerNeuron)
{
int copySize = mNumNeurons * sizeof(double);
memcpy(mOutActivations, nl.mOutActivations, copySize);
memcpy(mOutErrors, nl.mOutErrors, copySize);
for (int i = 0; i < mNumNeurons; i++)
{
memcpy(mWeights[i], nl.mWeights[i], copySize);
}
}
neuronLayer::~neuronLayer()
{
/** release weights */
for (int i = 0; i < mNumNeurons; i++)
{
delete []mWeights[i];
}
delete []mWeights;
/** release activations */
delete []mOutActivations;
/** release errors */
delete []mOutErrors;
}
void neuronLayer::reset()
{
memset(mOutActivations, 0, mNumNeurons * sizeof(double));
memset(mOutErrors, 0, mNumNeurons * sizeof(double));
for (int i = 0; i < mNumNeurons; ++i)
{
//we need an additional weight for the bias hence the +1
int numWeights = mNumInputsPerNeuron + 1;
double* curWeights = new double[numWeights];
mWeights[i] = curWeights;
for (int w = 0; w < numWeights; w++)
{
//set up the weights with an initial random value
double temp = RandomClamped();
curWeights[w] = temp;
}
}
}