Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Neural Network Implementation for Unity

Discussion in 'Scripting' started by flyte, Feb 1, 2017.

  1. flyte

    flyte

    Joined:
    Jun 24, 2013
    Posts:
    4
    So I've written a Neural Network class for unity and I thought I'd post here to see if anyone had any ideas about what to use it for?

    Preliminary test with basic AI's have shown really great success.
    If anyone's interested in the code that drives it, I can post it here.
    Details:
    The Network uses a feed forward system with optional choice for Sigmoid output or non-Sigmoid.
    I have also included a genetic algorithm and am considering adding in a backpropagation option.

    **For anyone unfamiliar with a Neural Network it is an adaptive learning algorithm modeled after the brain that can perform any number of tasks**

    Any ideas of what it could be used for would be appreciated,
     
    nelsonlarocca, TheWarper and mgear like this.
  2. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Look at what the game NERO does. They let players train their AI to fight each other. There is a document about how the AI works floating around the net somewhere.

    Well done on a successful neural net implemenation. Mine got an increasing error after backpropagation.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,564
    Nueral nets are cool. But their practical use in games is fairly limited.

    They tend to be too slow to learn the players strategy at runtime. Unless you have the player base to pull thousands of games worth of data into the cloud.

    They also tend to perform worse then an AI explicitly designed for a task.

    Still, I find them fascinating and would be interested in seeing code.
     
    ericbegue likes this.
  4. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Neural nets are generally not good for game AI I think, unless you use recurrent neural networks or LSTM's which offer an form of feedback/memory. I'm not sure what the performance impact of more powerful neural networks are though. Also they are harder to implement. I think for some things it's best to have a routine handle it, like pathfinding and shooting. The neural network can take care of who to shoot and what kind of path to take (fleeing, flanking).

    If you have a fast learning neural network like NERO has, and your players have a role in training the AI, they can learn quickly and do what the player wants.
     
  5. flyte

    flyte

    Joined:
    Jun 24, 2013
    Posts:
    4
    What I was thinking about was doing a decent amount of pre-training on the developer end and then using the player's game play to train the specifics of the networks.

    For anyone interested in the code:
    Code (CSharp):
    1. public class singleNeuron
    2. {
    3.  
    4.     public int numInputs;
    5.     public List<double> vecWeights = new List<double>();
    6.  
    7.     public singleNeuron(int f_NumInputs)
    8.     {
    9.         for(int i = 0; i <f_NumInputs + 1; i++)
    10.         {
    11.             vecWeights.Add(Random.Range(-1f,1f));
    12.         }
    13.         numInputs = f_NumInputs;
    14.     }
    15.  
    16. }
    17. public class NeuronLayer
    18. {
    19.  
    20.     public int NumNeurons;
    21.     public List<singleNeuron> vecNeurons = new List<singleNeuron>();
    22.  
    23.     public NeuronLayer(int f_NumNeurons, int numInputsPerNeuron)
    24.     {
    25.  
    26.         for(int i=0; i<f_NumNeurons; i++)
    27.         {
    28.             vecNeurons.Add(new singleNeuron(numInputsPerNeuron));
    29.         }
    30.         NumNeurons = f_NumNeurons;
    31.     }
    32.  
    33. }
    34. public  class NeuralNet
    35. {
    36.     int numInputs;
    37.     int numOutputs ;
    38.     int numHiddenLayers;
    39.     int neuronsPerHiddenLayer;
    40.     List<NeuronLayer> vecLayers = new List<NeuronLayer>();
    41.     bool sigmoidtr;
    42.     public NeuralNet(bool sig, int numInputSet, int numOutputsSet, int numHiddenLayersSet, int neuronsPerHiddenLayerSet)
    43.     {
    44.          sigmoidtr = sig;
    45.          numInputs = numInputSet;
    46.          numOutputs = numOutputsSet;
    47.          numHiddenLayers = numHiddenLayersSet;
    48.          neuronsPerHiddenLayer = neuronsPerHiddenLayerSet;
    49.          CreateNet();
    50.     }
    51.  
    52.     public  void CreateNet()
    53.     {
    54.  
    55.         if(numHiddenLayers > 0)
    56.         {
    57.            
    58.             vecLayers.Add(new NeuronLayer(neuronsPerHiddenLayer,numInputs));
    59.  
    60.             for(int i =0; i<numHiddenLayers-1; i++)
    61.             {
    62.                 vecLayers.Add(new NeuronLayer(neuronsPerHiddenLayer,neuronsPerHiddenLayer));
    63.             }
    64.  
    65.             vecLayers.Add(new NeuronLayer(numOutputs,neuronsPerHiddenLayer));
    66.         }
    67.         else
    68.         {
    69.             vecLayers.Add(new NeuronLayer(numOutputs,numInputs));
    70.         }
    71.  
    72.     }
    73.     public  List<double> GetWeights()
    74.     {
    75.  
    76.         List<double> weights = new List<double>();
    77.         for(int i = 0; i<numHiddenLayers+1; i++)
    78.         {
    79.  
    80.             for(int j=0; j<vecLayers[i].NumNeurons; j++)
    81.             {
    82.  
    83.                 for(int k=0; k<vecLayers[i].vecNeurons[j].numInputs; ++k)
    84.                 {
    85.  
    86.                     weights.Add(vecLayers[i].vecNeurons[j].vecWeights[k]);
    87.  
    88.                 }
    89.             }
    90.  
    91.         }
    92.         return weights;
    93.  
    94.     }
    95.     public void PutWeights(List<double> weights)
    96.     {
    97.  
    98.         int cWeight = 0;
    99.         for(int i = 0; i<numHiddenLayers+1; i++)
    100.         {
    101.  
    102.             for(int j=0; j<vecLayers[i].NumNeurons; j++)
    103.             {
    104.  
    105.                 for(int k=0; k<vecLayers[i].vecNeurons[j].numInputs; ++k)
    106.                 {
    107.                    
    108.                     vecLayers[i].vecNeurons[j].vecWeights[k] = weights[cWeight++];
    109.  
    110.                 }
    111.             }
    112.  
    113.         }
    114.         return;
    115.  
    116.     }
    117.     public int GetNumberofWeights()
    118.     {
    119.  
    120.         int weights = 0;
    121.         for(int i = 0; i<numHiddenLayers+1; i++)
    122.         {
    123.  
    124.             for(int j=0; j<vecLayers[i].NumNeurons; j++)
    125.             {
    126.  
    127.                 for(int k=0; k<vecLayers[i].vecNeurons[j].numInputs; k++)
    128.                 {
    129.  
    130.                     weights++;
    131.  
    132.                 }
    133.             }
    134.  
    135.         }
    136.         return weights;
    137.  
    138.     }
    139.     public double Sigmoid(double netinput, double response)
    140.     {
    141.  
    142.         return (1/(1+System.Math.Exp((double)(-netinput/(double)response))));
    143.  
    144.     }
    145.     public List<double> cycle(List<double> inputs)
    146.     {
    147.          List<double> outputs = new List<double>();
    148.          int cWeight = 0;
    149.  
    150.         for(int i =0; i<numHiddenLayers+1; i++)
    151.         {
    152.  
    153.             if(i>0)
    154.             {
    155.                 inputs.Clear();
    156.                 for(int y = 0; y<outputs.Count; y++)
    157.                 {
    158.  
    159.                     inputs.Add(outputs[y]);
    160.  
    161.                 }
    162.  
    163.             }
    164.             outputs.Clear();
    165.             cWeight = 0;
    166.  
    167.         for(int j=0; j<vecLayers[i].NumNeurons; j++)
    168.         {
    169.                
    170.             double netinputs = 0;
    171.             int f_NumInputs = 1 + vecLayers[i].vecNeurons[j].numInputs;
    172.             cWeight = 0;
    173.                
    174.             for(int k=0; k<f_NumInputs - 1; k++)
    175.             {          
    176.  
    177.                         netinputs += vecLayers[i].vecNeurons[j].vecWeights[k] * inputs[cWeight++];
    178.             }
    179.             netinputs += vecLayers[i].vecNeurons[j].vecWeights[f_NumInputs-1] * (-1);
    180.                 if (sigmoidtr == true) {
    181.                     outputs.Add (Sigmoid (netinputs, -1));
    182.                 } else {
    183.                     if (netinputs > 0) {
    184.                         outputs.Add (1);
    185.                     }
    186.                     else outputs.Add(0);
    187.                 }
    188.             }
    189.         }
    190.  
    191.         return outputs;
    192.     }
    193. }
    **Sorry about the crappy formatting**
    If you van think of anyway to streamline it feel free to suggest.
     
  6. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Did you connect every neuron to every neuron? It seems a bit hard to tell there. Connecting every neuron to every neuron tends to be a bad idea, as it can lead to overfitting the training data (the neural net learns precisely how to get the right output for the training input, but it can't get the proper outputs for previously unseen inputs). And having too few connections makes the neural network unable to learn the target function (so it can't generate the correct outputs). There is no exact science to how to connect neural networks. It would be a good idea to start with a very loosely connected neural network and then use your genetic algorithm to gradually add connections and keep the neural nets that perform better with more connections. The game NERO would breed the neural nets every few minutes, removing the worst performing neural net only.

    With my code I had a "Neural Network Editor" for my neural network but it was really clunky because it used regular EditorGUI stuff instead of nice interfaces.
     
  7. flyte

    flyte

    Joined:
    Jun 24, 2013
    Posts:
    4
    What architecture did you use for your network?
    With the feed forward networks form you usually connect each layer of neurons to every neuron in the next layer, but if you have a better setup I'd be interested to know?
    Also on the breeding note I also have a genetic algorithm that allows the top 20 neural nets to breed using a roulette wheel style selection.
     
  8. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Whoops, turns out I got things mixed up. It's the number of nodes, not the the number of connections, that seems to affect under/overfitting. In my notes every node is connected to every node in the next layer. It makes sense come to think of it, since if a extra connection was increasing the error, backpropagation will make that connection's weight zero.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,564
    It's also worth noting that connecting every node in each layer to every node in the next tends to make the network simpler to calculate.
     
  10. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    I suppose, well if you have a genetic algorithm allowing neuron links to be removed it may help you evolve a better performing neural net but I suppose you could just make random weights zero instead of deleting the connection
     
  11. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,294
    Neural Network is overrated here. It's just a mathematical tool to fit multivariable functions.
     
    Kiwasi likes this.
  12. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Neural nets seem to be getting us closer to true intelligence than other AI is. There are not just neural nets that can remember, but neural nets that can learn what details to pay attention to.
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,564
    Going to have to agree here. Nueral networks work especially well for a small domain of problems. It's good at pattern recognition, expecially with non trivial patterns. Handwriting or image recognition.

    But they really are a long, long way off a general learning solution. And they don't have a huge practical use in games.
     
  14. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,294
    @VengeanceDesign
    That's a science fiction view of AI. Really, when we talk about AI, we talk more about artificial things than intelligent things.
    Don't get hyped by AI. For instance, you can evolves some neural-network based bots to avoid some obstacles, and it might look like the bots are "learning" something. But that's just superficial. What is really going on, is just an "intelligent" programmer solving an optimization problem using a genetic algorithm.
     
    Last edited: Feb 3, 2017
  15. flyte

    flyte

    Joined:
    Jun 24, 2013
    Posts:
    4
    I agree, it's more accurate to describe it as a mathematical function that helps with certain approximations. However it does present at times the illusion of Intelligence.
     
  16. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Until we get biological computing, neural nets seem like some of the best AI to use for things where we don't know how we get the solution i.e. face recognition. And when we have biological computing I reckon we will have neuron-like organisms/chips to let us compose neural networks of things as sophisticated as human neurons. Perhaps neural networks will stop being software and start being hardware.

    It is true that neural networks are basically function approximators, but that doesn't mean complex ones might not become arguably capable of learning concepts.
     
  17. nelsonlarocca

    nelsonlarocca

    Joined:
    Aug 11, 2014
    Posts:
    15
    Hi Flyte, would you pls PM me to get further infomation ? I would like to know about your background on neural networks and know about your programming. Thanks. Reach me on Unity Connect pls