top of page

Machine Learning in Unity - Creating Custom Perceptron

Hey welcome back. I am gonna start a blog chain on machine learning. This is the first blog in that chain where i am gonna discuss the basics of machine learning. Then later i am gonna create a custom Perceptron function where i am gonna train my cat in game.

Sounds cool right? Lets start!!!!!

What is Machine Learning?

Machine learning is an application of artificial intelligence (AI) that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. Machine learning focuses on the development of computer programs that can access data and use it learn for themselves.

The process of learning begins with observations or data, such as examples, direct experience, or instruction, in order to look for patterns in data and make better decisions in the future based on the examples that we provide. The primary aim is to allow the computers learn automatically without human intervention or assistance and adjust actions accordingly.


This learning can go in 2 ways:

  1. Supervised Learning

  2. Unsupervised Learning

Supervised Learning:

Supervised learning as the name indicates the presence of a supervisor as a teacher. Basically supervised learning is a learning in which we teach or train the machine using data which is well labeled that means some data is already tagged with the correct answer. After that, the machine is provided with a new set of examples(data) so that supervised learning algorithm analyses the training data(set of training examples) and produces a correct outcome from labeled data.

For instance, suppose you are given a basket filled with different kinds of fruits. Now the first step is to train the machine with all different fruits one by one.

Next time i give that one of those fruits from the basket and ask the machine to identify which fruit, the machine will be able to tell me which fruit i gave based on the data it has already learned.


Unsupervised Learning:

Unsupervised learning is the training of machine using information that is neither classified nor labeled and allowing the algorithm to act on that information without guidance. Here the task of machine is to group unsorted information according to similarities, patterns and differences without any prior training of data.


Unlike supervised learning, no teacher is provided that means no training will be given to the machine. Therefore machine is restricted to find the hidden structure in unlabeled data by our-self.

For instance, suppose it is given an image having both dogs and cats which have not seen ever.

Thus the machine has no idea about the features of dogs and cat so we can’t categorize it in dogs and cats. But it can categorize them according to their similarities, patterns, and differences i.e., we can easily categorize the above picture into two parts. First first may contain all pics having dogs in it and second part may contain all pics having cats in it. Here you didn’t learn anything before, means no training data or examples.


Now its the time to talk about Perceptrons in Machine Learning!!!


A Perceptron is a neural network unit (an artificial neuron) that does certain computations to detect features or business intelligence in the input data.

A Perceptron is an algorithm for supervised learning of binary classifiers. This algorithm enables neurons to learn and processes elements in the training set one at a time.

The Perceptron algorithm learns the weights for the input signals in order to draw a linear decision boundary.

This enables you to distinguish between the two linearly separable classes +1 and -1.

Perceptron Learning Rule states that the algorithm would automatically learn the optimal weight coefficients. The input features are then multiplied with these weights to determine if a neuron fires or not.

Perceptron function looks like:


It has only two values: Yes and No or True and False

The Perceptron learning rule converges if the two classes can be separated by the linear hyperplane(This can include logic gates like AND, OR, NOR, NAND.). However, if the classes cannot be separated perfectly by a linear classifier, it could give rise to errors(as in an XOR gate).


Here i am gonna give you the logic to program perception in unity.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class InputData
{
    public int[] inputArray;
    public int expectedOutput;
}
public class PerceptiveLearningBaseScript : MonoBehaviour
{
    public InputData[] _inputData;
    public int _trailRounds;
    public float _weight1;
    public float _weight2;
    public float _bias;

    // Start is called before the first frame update
    void Start()
    {
        _weight1 = Random.Range(-1.0f, 1.0f);
        _weight2 = Random.Range(-1.0f, 1.0f);
        _bias = Random.Range(-1.0f, 1.0f);


        for (int i = 0; i < _trailRounds; i++)
        {
            OutPutCalculation();
            Debug.Log("------------------------------------------------------------------------------------------------");
        }
    }

    public void OutPutCalculation()
    {
        for(int i = 0; i < _inputData.Length; i++)
        {
            int _output;
            float _tempOutput = (_inputData[i].inputArray[0] * _weight1) + (_inputData[i].inputArray[1] * _weight2) + (_bias);
            
            if (_tempOutput > 0)
                _output = 1;
            else
                _output = 0;

            int error = 0;
            if(_output != _inputData[i].expectedOutput)
            {
                 error = (_inputData[i].expectedOutput - _output);
                _weight1 = (_inputData[i].inputArray[0] * error) + (_weight1);
                _weight2 = (_inputData[i].inputArray[1] * error) + (_weight2);
                _bias = error + _bias;

            }

            Debug.Log("Input1: " + _inputData[i].inputArray[0] + "  " + "Input2: " + _inputData[i].inputArray[1] + "  " +"Output: "+ _output + "  " + "Error: " + error);
        }

    }

}


Using the same code logic(OR logic) i have trained my cat in unity. I applied supervised Perceptron learning to train my cat.

if shape is sphere - color is red - Tell cat to eat

shape is sphere - color is yellow - Tell cat to jump

shape is cube- color is red- Tell cat to jump

shape is cube - color is yellow - Tell cat to jump


this looks similar to OR gate which i have already constructed in my previous example.

0 OR 0 - 0

0 OR 1 - 1

1 OR 1 - 1

1 OR 0 - 1


Output:


Full Code:

Attach this code to cat:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Catbrain : MonoBehaviour
{
    public InputData[] _inputData;
    public int _trailRounds;
    public float _weight1;
    public float _weight2;
    public float _bias;

    private Animator _animator;
    public GameObject _rightText;
    public GameObject _wrongText;

    public Text _trainingRound;
    int _traningRoundNumber = 0;

    // Start is called before the first frame update
    void Start()
    {
        _weight1 = Random.Range(-1.0f, 1.0f);
        _weight2 = Random.Range(-1.0f, 1.0f);
        _bias = Random.Range(-1.0f, 1.0f);


        _animator = GetComponent<Animator>();     
    }

    public void OutPutCalculation()
    {
        for (int i = 0; i < _inputData.Length; i++)
        {
            int _output;
            float _tempOutput = (_inputData[i].inputArray[0] * _weight1) + (_inputData[i].inputArray[1] * _weight2) + (_bias);

            if (_tempOutput > 0)
                _output = 1;
            else
                _output = 0;

            int error = 0;
            if (_output != _inputData[i].expectedOutput)
            {
                error = (_inputData[i].expectedOutput - _output);
                _weight1 = (_inputData[i].inputArray[0] * error) + (_weight1);
                _weight2 = (_inputData[i].inputArray[1] * error) + (_weight2);
                _bias = error + _bias;

            }

            if (_output == 1)
                _animator.SetTrigger("_jump");
            else if (_output == 0)
                _animator.SetTrigger("_eat");    
            

            if(error == 0)
            {
                _rightText.SetActive(true);
                _wrongText.SetActive(false);
            }
            else
            {
                _rightText.SetActive(false);
                _wrongText.SetActive(true);
            }

        }

        _traningRoundNumber += 1;
        _trainingRound.text = "Training Round: " + _traningRoundNumber.ToString();

    }


    // Update is called once per frame
    void Update()
    {

    }
}

Attach this code to transform where you want to generate food/shapes with colors


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FoodSpawning : MonoBehaviour
{

    public GameObject RedCircle;
    public GameObject YellowCircle;
    public GameObject RedSquare;
    public GameObject YellowSquare;


    public Catbrain instance;

    private GameObject _currentSpawn;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            Destroy(_currentSpawn);
            _currentSpawn = Instantiate(RedCircle, transform.position,Quaternion.identity);
            //Trigger Training
            instance._inputData[0].inputArray[0] = 0;
            instance._inputData[0].inputArray[1] = 0;
            instance._inputData[0].expectedOutput = 0;

            instance.OutPutCalculation();
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            Destroy(_currentSpawn);
            _currentSpawn = Instantiate(YellowCircle, transform.position, Quaternion.identity);

            //Trigger Training
            instance._inputData[0].inputArray[0] = 0;
            instance._inputData[0].inputArray[1] = 1;
            instance._inputData[0].expectedOutput = 1;

            instance.OutPutCalculation();

        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            Destroy(_currentSpawn);
            _currentSpawn = Instantiate(RedSquare, transform.position, Quaternion.identity);

            //Trigger Training
            instance._inputData[0].inputArray[0] = 1;
            instance._inputData[0].inputArray[1] = 1;
            instance._inputData[0].expectedOutput = 1;

            instance.OutPutCalculation();

        }
        if (Input.GetKeyDown(KeyCode.F))
        {
            Destroy(_currentSpawn);
            _currentSpawn = Instantiate(YellowSquare, transform.position, Quaternion.identity);

            //Trigger Training
            instance._inputData[0].inputArray[0] = 1;
            instance._inputData[0].inputArray[1] = 0;
            instance._inputData[0].expectedOutput = 1;

            instance.OutPutCalculation();

        }
    }



}







 
 
 

Recent Posts

See All

Comments


bottom of page