-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.cs
154 lines (113 loc) · 4.38 KB
/
NeuralNetwork.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MathNet.Numerics.LinearAlgebra;
using System;
using Random = UnityEngine.Random;
public class NeuralNetwork : MonoBehaviour
{
public float fitness;
//input layer with 1 row, 3 columns
//one column per sensor
public Matrix<float> inputLayer = Matrix<float>.Build.Dense(1, 3);
public List<Matrix<float>> hiddenLayers = new List<Matrix<float>>();
public Matrix<float> outputLayer = Matrix<float>.Build.Dense(1, 2);
public List<Matrix<float>> weights = new List<Matrix<float>>();
public List<float> biases = new List<float>();
//hiddenLayer is the number of layers between the input and the output layer
//loop through hidden layer hiddenLayer
//add number of hiddenNeurons in each iteration
//matrix multiplication - matrix A has to have equal number of columns to matrix B's rows
public void Initialise(int hiddenLayerCount, int hiddenNeuronCount)
{
inputLayer.Clear();
hiddenLayers.Clear();
outputLayer.Clear();
weights.Clear();
biases.Clear();
for (int i = 0; i < hiddenLayerCount + 1; i++)
{
Matrix<float> f = Matrix<float>.Build.Dense(1, hiddenNeuronCount);
hiddenLayers.Add(f);
biases.Add(Random.Range(-1f, 1f));
//WEIGHTS
if (i == 0)
{
Matrix<float> inputToH1 = Matrix<float>.Build.Dense(3, hiddenNeuronCount);
weights.Add(inputToH1);
}
Matrix<float> HiddenToHidden = Matrix<float>.Build.Dense(hiddenNeuronCount, hiddenNeuronCount);
weights.Add(HiddenToHidden);
}
Matrix<float> OutputWeight = Matrix<float>.Build.Dense(hiddenNeuronCount, 2);
weights.Add(OutputWeight);
biases.Add(Random.Range(-1f, 1f));
RandomiseWeights();
}
public NeuralNetwork InitialiseCopy(int hiddenLayerCount, int hiddenNeuronCount)
{
NeuralNetwork n = new NeuralNetwork();
List<Matrix<float>> newWeights = new List<Matrix<float>>();
for (int i = 0; i < this.weights.Count; i++)
{
Matrix<float> currentWeight = Matrix<float>.Build.Dense(weights[i].RowCount, weights[i].ColumnCount);
for (int x = 0; x < currentWeight.RowCount; x++)
{
for (int y = 0; y < currentWeight.ColumnCount; y++)
{
currentWeight[x, y] = weights[i][x, y];
}
}
newWeights.Add(currentWeight);
}
List<float> newBiases = new List<float>();
newBiases.AddRange(biases);
n.weights = newWeights;
n.biases = newBiases;
n.InitialiseHidden(hiddenLayerCount, hiddenNeuronCount);
return n;
}
public void InitialiseHidden(int hiddenLayerCount, int hiddenNeuronCount)
{
inputLayer.Clear();
hiddenLayers.Clear();
outputLayer.Clear();
for (int i = 0; i < hiddenLayerCount + 1; i++)
{
Matrix<float> newHiddenLayer = Matrix<float>.Build.Dense(1, hiddenNeuronCount);
hiddenLayers.Add(newHiddenLayer);
}
}
public void RandomiseWeights()
{
for (int i = 0; i < weights.Count; i++)
{
for (int x = 0; x < weights[i].RowCount; x++)
{
for (int y = 0; y < weights[i].ColumnCount; y++)
{
weights[i][x, y] = Random.Range(-1f, 1f);
}
}
}
}
public (float, float) StartNetwork(float a, float b, float c)
{
inputLayer[0, 0] = a;
inputLayer[0, 1] = b;
inputLayer[0, 2] = c;
inputLayer = inputLayer.PointwiseTanh();
hiddenLayers[0] = ((inputLayer * weights[0]) + biases[0]).PointwiseTanh();
for (int i = 1; i < hiddenLayers.Count; i++)
{
hiddenLayers[i] = ((hiddenLayers[i - 1] * weights[i]) + biases[i]).PointwiseTanh();
}
outputLayer = ((hiddenLayers[hiddenLayers.Count - 1] * weights[weights.Count - 1]) + biases[biases.Count - 1]).PointwiseTanh();
//First output is acceleration and second output is steering
return (Sigmoid(outputLayer[0, 0]), (float)Math.Tanh(outputLayer[0, 1]));
}
private float Sigmoid(float s)
{
return (1 / (1 + Mathf.Exp(-s)));
}
}