-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNN.cs
More file actions
215 lines (198 loc) · 6.96 KB
/
NN.cs
File metadata and controls
215 lines (198 loc) · 6.96 KB
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
using UnityEngine;
using System.Collections.Generic;
using System.IO;
public class NN
{
//user defineable variables
int numEpochs;
//number of inputs - this includes the input bias
int numInputs = 0;
//number of hidden units
int numHiddens;
//number of output units
int numOutputs;
//Size of experience replay memory!
int sizeOfExperienceReplayMemory;
//learning rate
float LR;
int batchSize;
//training data
List<RL.Transition> transitions, batchTransitions;
//the outputs of the neurons
float[] hiddenVal;
float[] outputVal;
//the errors
float[] hiddenDelta;
float[] outputDelta;
//the weights
float[,] weightsIH;
float[,] weightsHO;
public NN(int inputs, int hiddens, int outputs)
{
transitions = new List<RL.Transition>();
batchTransitions = new List<RL.Transition>();
numInputs = inputs;
numHiddens = hiddens;
numOutputs = outputs;
numEpochs = 50;
sizeOfExperienceReplayMemory = 500;
LR = 0.01f;
batchSize = 25;
InitWeights();
}
void InitWeights()
{
hiddenVal = new float[numHiddens];
outputVal = new float[numOutputs];
hiddenDelta = new float[numHiddens];
outputDelta = new float[numOutputs];
weightsIH = new float[numInputs, numHiddens];
weightsHO = new float[numHiddens + 1, numOutputs];
for (int j = 0; j < numHiddens; j++)
{
for (int i = 0; i < numInputs; i++)
weightsIH[i, j] = (Random.Range(0.0f, 1.0f) - 0.5f) / 10;
for (int i = 0; i < numOutputs; i++)
weightsHO[j, i] = (Random.Range(0.0f, 1.0f) - 0.5f) / 10;
}
for (int i = 0; i < numOutputs; i++)
weightsHO[numHiddens, i] = (Random.Range(0.0f, 1.0f) - 0.5f) / 10;
}
float sigmoid(float x)
{
return 1.0f / (1 + (float)Mathf.Exp((float)-x));
}
public List<float> calcNet(float[] input)
{
//calculate the outputs of the hidden neurons
for (int i = 0; i < numHiddens; i++)
{
hiddenVal[i] = 0;
for (int j = 0; j < numInputs; j++)
hiddenVal[i] += input[j] * weightsIH[j, i];
hiddenVal[i] = sigmoid(hiddenVal[i]);
}
//calculate the output of the network
List<float> outPred = new List<float>();
for (int j = 0; j < numOutputs; j++)
{
outputVal[j] = 0;
for (int i = 0; i < numHiddens; i++)
outputVal[j] += hiddenVal[i] * weightsHO[i, j];
outputVal[j] += 1 * weightsHO[numHiddens, j];
outputVal[j] = sigmoid(outputVal[j]);
outPred.Add(outputVal[j]);
}
return outPred;
}
public void Train(RL.Transition newTransition)
{
while (transitions.Count >= sizeOfExperienceReplayMemory)
transitions.RemoveAt(0);
transitions.Add(newTransition);
TrainNetwork();
}
bool SampleBatch()
{
batchTransitions.Clear();
if (transitions.Count < 2 * batchSize)
return false;
for (int i = 0; i < Mathf.Min(batchSize, transitions.Count); i++)
batchTransitions.Add(transitions[Random.Range(0, transitions.Count)]);
return true;
}
void TrainNetwork()
{
if (!SampleBatch())
return;
for (int e = 0; e < numEpochs; e++)
{
for (int t = 0; t < batchTransitions.Count; t++)
{
float target = batchTransitions[t].reward;
float output = calcNet(batchTransitions[t].state)[batchTransitions[t].action];
//calculate the error
outputDelta[batchTransitions[t].action] = (target - output) * output * (1 - output);
//change network weights
WeightChangesHO(batchTransitions[t].action, output);
WeightChangesIH(batchTransitions[t], output);
}
}
}
//adjust the weights hidden-output
void WeightChangesHO(int action, float outPred)
{
for (int k = 0; k <= numHiddens; k++)
{
float gradient = outputDelta[action] * 1; //This is for bias weight
if (k < numHiddens)
gradient = outputDelta[action] * hiddenVal[k];
weightsHO[k, action] += LR * gradient;
}
}
//adjust the weights input-hidden
void WeightChangesIH(RL.Transition curTransition, float outPred)
{
for (int h = 0; h < numHiddens; h++)
{
hiddenDelta[h] = weightsHO[h, curTransition.action] * outputDelta[curTransition.action];
hiddenDelta[h] *= hiddenVal[h] * (1 - hiddenVal[h]); //Derivative for sigmoid function
for (int i = 0; i < numInputs; i++)
{
float gradient = hiddenDelta[h] * curTransition.state[i];
weightsIH[i, h] += LR * gradient;
}
}
}
public void Save(StreamWriter writer)
{
writer.WriteLine(numEpochs);
writer.WriteLine(numInputs);
writer.WriteLine(numHiddens);
writer.WriteLine(numOutputs);
writer.WriteLine(sizeOfExperienceReplayMemory);
writer.WriteLine(LR);
writer.WriteLine(LR);
for (int i = 0; i < numInputs; i++)
for (int h = 0; h < numHiddens; h++)
{
writer.WriteLine(weightsIH[i, h]);
}
for (int h = 0; h <= numHiddens; h++)
for (int o = 0; o < numOutputs; o++)
{
writer.WriteLine(weightsHO[h, o]);
}
}
public void Load(StreamReader reader)
{
try
{
numEpochs = int.Parse(reader.ReadLine());
numInputs = int.Parse(reader.ReadLine());
numHiddens = int.Parse(reader.ReadLine());
numOutputs = int.Parse(reader.ReadLine());
InitWeights();
sizeOfExperienceReplayMemory = int.Parse(reader.ReadLine());
LR = float.Parse(reader.ReadLine());
LR = float.Parse(reader.ReadLine());
hiddenVal = new float[numHiddens];
weightsIH = new float[numInputs, numHiddens];
for (int i = 0; i < numInputs; i++)
for (int h = 0; h < numHiddens; h++)
{
weightsIH[i, h] = float.Parse(reader.ReadLine());
}
weightsHO = new float[numHiddens + 1, numOutputs];
for (int h = 0; h <= numHiddens; h++)
for (int o = 0; o < numOutputs; o++)
{
weightsHO[h, o] = float.Parse(reader.ReadLine());
}
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
}
}