Imitation Learning In Unity by HectorPulido - 38

Games & ProjectsNetwork

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!

Unknown VersionMIT LicenseUpdated 2 years agoCreated on July 7th, 2018
Go to source


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

This project is still under development and is highly experimental



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
And his Twitch Channel


Open it on unity 2018 or greater (sorry about that >-< ), and play around with the project.

How make it works

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
	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()


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

When you finish to train…

Load and save
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


Evolutionary Neural Networks on Unity For bots

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

More Genetic algorithms on Unity

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

Vectorized Multilayer Neural Network from scratch

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.


This project contains a copy of:

Everything else is MIT licensed


Please consider Support on Patreon
Please consider support on patreon

Show all projects by HectorPulido