Permalink
Browse files

Initial Commit

  • Loading branch information...
Robert Wells Robert Wells
Robert Wells authored and Robert Wells committed Oct 21, 2015
0 parents commit 19d5960ed05b23c55cf5fe1ba47939eadd29b562
@@ -0,0 +1,10 @@
Feed Forward Artificial Neural Network (ANN)
V1.0
I hope to extend this asset in future and provide more significant documentation
but until then if you require any assistance or have any suggestions for improvements,
please email me at robert.wells@gandhigames.co.uk
and I will get back to you as soon as possible.
I have included a simple test scene that creates an ANN, provides it with random input each time-step and prints the output (see ANNTest script).
Not a very useful application but it shows you an ANN could be setup.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,20 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Simulation;
[RequireComponent (typeof(NeuralNetController), typeof(Utilities))]
public class ANNTest : MonoBehaviour
{
private NeuralNetController _netController;
void Start ()
{
_netController = GetComponent<NeuralNetController> ();
}
void Update ()
{
Utilities.instance.PrettyPrint (_netController.UpdateNeuralNet (Utilities.instance.RandomInputBuilder (_netController)));
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,201 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simulation
{
//Contains a list of layers: represents the whole neural net
public class NeuralNet
{
#region Variables
private int _numOfInput; //Number of inputs for each neuron
public int numOfInput { get { return _numOfInput; } }
private int _numOfOutput; //Number of outputs of each neuron
public int numOfOutput { get { return _numOfOutput; } }
private int _numHiddenLayers; //Number of hidden layers
public int numOfHiddenLayers { get { return _numHiddenLayers; } }
private int _numOfNeuronsPerHiddenLayer; //Number of neurons per hidden layer
public int numOfNeuronsPerHiddenLayer { get { return _numOfNeuronsPerHiddenLayer; } }
List<NeuronLayer> layers; //List containing layers
#endregion
#region Constructor
public NeuralNet (int numOfInput, int numOfOutput, int numOfHiddenLayers, int numOfNeuronsPerHiddenLayer)
{
_numOfInput = numOfInput;
_numOfOutput = numOfOutput;
_numHiddenLayers = numOfHiddenLayers;
_numOfNeuronsPerHiddenLayer = numOfNeuronsPerHiddenLayer;
layers = new List<NeuronLayer> ();
CreateNeuralNet ();
}
#endregion
#region ANN Construction Method
public void CreateNeuralNet ()
{
//If there are hidden layers
if (_numHiddenLayers > 0) {
//Create first layer
layers.Add (new NeuronLayer (_numOfNeuronsPerHiddenLayer, _numOfInput));
//Create any other subsequent hidden layers
for (int i = 0; i < _numHiddenLayers - 1; i++) {
//Input from first hidden layer
layers.Add (new NeuronLayer (_numOfNeuronsPerHiddenLayer,
_numOfNeuronsPerHiddenLayer));
}
//Output layer
//Input from subsequent or first hidden layer
layers.Add (new NeuronLayer (_numOfOutput, _numOfNeuronsPerHiddenLayer));
} else { //If no hidden layers
//Input layer
layers.Add (new NeuronLayer (_numOfOutput, _numOfInput));
}
}
#endregion
#region Update Method
//Receives input and returns output: performs caluclations for neural net
public List<float> Update (params float[] inputs)
{
List<float> inputList = new List<float> ();
inputList.AddRange (inputs);
//Output from each layer
List<float> outputs = new List<float> ();
int weightCount = 0;
//Return empty if not corrent number of inputs
if (inputList.Count != _numOfInput) {
Console.WriteLine ("NeuralNet|Update|Size of inputs list not equal number of inputs");
return outputs;
}
//Each layer
for (int i = 0; i < _numHiddenLayers + 1; i++) {
if (i > 0) {
//Clear input and add output from previous layer
inputList.Clear ();
inputList.AddRange (outputs);
}
outputs.Clear ();
weightCount = 0;
for (int j = 0; j < layers[i].NumNeurons; ++j) {
float netInput = 0.0f;
int NumInputs = layers [i].Neurons [j].NumInputs;
//Each weight
for (int k = 0; k < NumInputs - 1; ++k) {
//Sum the weights x inputs
netInput += layers [i].Neurons [j].Weight [k] *
inputList [weightCount++];
}
//Add in the bias
netInput += layers [i].Neurons [j].Weight [NumInputs - 1] *
Utilities.instance.bias;
//Store result in output
outputs.Add (Sigmoid (netInput));
weightCount = 0;
}
}
return outputs;
}
#endregion
#region Get and Set Methods
//Gets weights from network
public List<float> GetWeights ()
{
//Temporarily store wights
List<float> weights = new List<float> ();
//Each layer
for (int i = 0; i < _numHiddenLayers + 1; ++i) {
//Each neuron
for (int j = 0; j < layers[i].NumNeurons; ++j) {
//Each weight
for (int k = 0; k < layers[i].Neurons[j].NumInputs; ++k) {
weights.Add (layers [i].Neurons [j].Weight [k]);
}
}
}
return weights;
}
//Gets number of weights
public int GetNumberOfWeights ()
{
int weights = 0;
//Each layer
for (int i = 0; i < _numHiddenLayers + 1; ++i) {
//Eeach neuron
for (int j = 0; j < layers[i].NumNeurons; ++j) {
//Each weight
for (int k = 0; k < layers[i].Neurons[j].NumInputs; ++k)
weights++;
}
}
return weights;
}
//Sets weights for network (initially set to random values)
public void SetWeights (ref List<float> weights)
{
//Used to cycle through received weights
int weightCount = 0;
//Each layer
for (int i = 0; i < _numHiddenLayers + 1; ++i) {
//Each neuron
for (int j = 0; j < layers[i].NumNeurons; ++j) {
//Each weight
for (int k = 0; k < layers[i].Neurons[j].NumInputs; ++k) {
layers [i].Neurons [j].Weight [k] = weights [weightCount++];
}
}
}
}
#endregion
#region Helper Methods
//S shaped output
public float Sigmoid (float netInput)
{
return (float)(1 / (1 + Math.Exp (-netInput / Utilities.instance.activationResponse)));
}
public bool IsSameSetup (NeuralNet other)
{
return (other._numOfInput == this._numOfInput && other._numOfOutput == this._numOfOutput
&& other._numHiddenLayers == this._numHiddenLayers
&& other._numOfNeuronsPerHiddenLayer == this._numOfNeuronsPerHiddenLayer);
}
#endregion
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,30 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Simulation
{
public class NeuralNetController : MonoBehaviour
{
public int numOfInput;
public int numOfOutput;
public int numOfHiddenLayers;
public int numOfNeuronsPerHiddenLayer;
private NeuralNet _neuralNet;
void Awake ()
{
_neuralNet = new NeuralNet (numOfInput, numOfOutput, numOfHiddenLayers, numOfNeuronsPerHiddenLayer);
}
// Update is called once per frame
public List<float> UpdateNeuralNet (params float[] input)
{
if (_neuralNet == null)
return null;
return _neuralNet.Update (input);
}
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 19d5960

Please sign in to comment.