What if we record all the information in a game, and we record the player actions, then we train a neural network with that data? Thats what this is! This is a implementation of Vectorized neural network in unity, an open source project that uses neural networks and backpropagation in C#, and train it via stochastic gradient descend using as examples human meditions
Artificial neural networks are a group of algorithm that can imitate almost any existing function, just like an universal stimator, it's a hierarchical matrix multiplication function that can imitate even the human behaviour.
With the human monitor script, the keys that human presses and the sensors of the bot are keeped to be used as a dataset for the Neural net script and it's signals are interpreted by the brain to make a human - like behaviour
- More examples
This tutorial was made for Hector Pulido for his youtube channel
https://www.youtube.com/c/HectorAndresPulidoPalmar
And his Twitch Channel
https://www.twitch.tv/hector_pulido_
Open it on unity 2018 or greater (sorry about that >-< ), and play around with the project.
The asset contais 2 important part, the first one is the neural network part, this piece is inside the brain component, what usually is used in a BotHandler, that component manage the training process and the prediction process, the other important part is the human monitor, this script must be inherited to save the correct meditions, both brain and human monitor contains tools to save and deploy the data. The basic workflow is to use an bot handler that manage a brain, and a human monitor that train the brain
The neural network is the base of this project, it's imposible to the bot to learn without this, the brain contais the neural network and the hyperparameters
using NeuralReplicantBot.PerceptronHandler;
using LinearAlgebra;
public class LogicDoor : BotHandler //<- inherit from BotHandler
{
protected override void Awake() //<- you can override the Awake
{
base.Awake(); // <- but remember to call the base Awake
Foo();
}
void Foo()
{
if(isTraining) // Training variable is pretty useful to deploy
{
brain.Learn(x, y); //<-- You can train, but also you can do it in the HumanMonitor.
}
}
void GetOutput()
{
print(brain.GetOutput(x));
}
}
The bot is imitating the human, for this is important to register all important actions from the player before the training the class that do that is the HumanMonitor.
using NeuralReplicantBot.PerceptronHandler;
public class ExampleMonitor : HumanMonitor //<- its super important to inherit from HumanMonitor
{
protected override void Medition(ref Medition m)
{
//It's important to add information to the medition object m
//This part of the code is runned many times
m.outputs.AddRange(y); // Adding y array to the output count
m.inputs.AddRange(x); // Adding x array to the output count
}
protected override void MeditionEnd(Medition m)
{
// This part of the code is runned once, when the medition count is over
// so is important to decide what to do with the information
// eg. train the neural network
brain.Learn(input, output); //<- this is a good place to train the brain
Foo(); //<- you can set BotHandler's property isTraining as false
}
}
Now you can load a neural network whenever you want via drag and drop a .nn file, that file is also compatible with Evolutionary Neural Networks on Unity For bots, the same feature is available for the HumanMonitor (and it's childs) as an .med file, to save a .nn or .med, just press the button create, that will generate a EMPTY file, that file can ve overwritten after the training
This is a super simple script to understand how the brain works, the neural network learns how to make some logic doors, this example does not contains a human monitor
At this moment there is only one complex example in the project, a self driving car with a lot of raycast as inputs and Horizontal and Vertical axis as output
This is a asset that train a neural networks using genetic algorithm in unity to make a bot that can play a game or just interact with the envoriment
https://github.com/HectorPulido/Evolutionary-Neural-Networks-on-unity-for-bots
Those are three Genetics Algorithm using unity, The First one is a simple algorithm that Looks for the minimun of a function, The Second one is a solution for the Travelling Salesman Problem, The Third one is a Automata machine
https://github.com/HectorPulido/Three-Genetics-Algorithm-Using-Unity
This is a simple MultiLayer perceptron made with Simple Linear Algebra for C# , is a neural network based on This Algorithm but generalized. This neural network can calcule logic doors like Xor Xnor And Or via Stochastic gradient descent backpropagation with Sigmoid as Activation function, but can be used to more complex problems.
https://github.com/HectorPulido/Vectorized-multilayer-neural-network
This project contains a copy of:
- Unity Standar assets
- Simple linear algebra https://github.com/HectorPulido/Simple_Linear_Algebra
Everything else is MIT licensed
Please consider Support on Patreon
https://www.patreon.com/HectorPulido