Skip to main content

Produce a Microbit python neural network 1: Without the microbit initially

These two posts are really extensions of a set of post in response to a question from Carl Simmons (@Activ8Thinking) concerning building a micro:bit simple neuron. In those, the Microsoft MakeCode was used.

In this two-post series is going to go through building neurones and neural networks in Python and by the end of the second post a python-based microbit neural network


  • Post 1 (this one) A single neuron and a simple neuron network will be produced in Python are produced.
  • Post 2 looks at producing a network of neurons, ie. neural network using the idea from post 1 but using a three microbits.; looking to solve the problem that a single neuron can't solve, making an Exclusive OR gate (XOR)



1. Overview and non-microbit neuron
1.1 Overview: 
The characteristics of the system will be:
  • Inputs are going to be binary
  • Weighted sum is bias+W1*input1+w2*input2
  • If weighted sum>=0 then the output is True (T on the LEDs) or '1'
  • If weighted sum<0 then the output is False (F on the LEDs) or '0

1.2. Single Neuron (without the Microbit)
Lets start without the microbit and build a single neuron in Python and useful exercise in it's own right just to see the mechanism. A class Neuron is produced and all possible combination of a two-input binary system are feed in. 

class Neuron:
    def __init__(self, input1, bias, w1, w2):
        self.input1 = input1
        self.bias = bias
        self.w1=w1
        self.w2=w2
    
    def CalculateOutput (self):
        output1 = 0
        net = self.bias+self.input1[0]*self.w1+self.input1[1]*self.w2
        if net >= 0:
            output1 = 1
        else:
            output1 = 0
        return output1


for x1 in range (2):
    for x2 in range (2):
        neuron1= Neuron([x1,x2],-1,1,1)

        print("x1="+str(x1)+"x2= "+str(x2)+" Output= " +str(neuron1.CalculateOutput()))

The code above implements a simple single neuron and the weights -1,1,1 produce an OR gate and -2,1,1 produces an AND gate.

1.3 A Neural Network
We can extend the code above to produce a neural network, by feeding the outputs of one or more neurons as inputs to other neurones. The code below produces an Exclusive XOR - essentially for the two input case if the two inputs are different then the output is True. The same inputs go to two neurones but they have different weight (bias, W1 and W2) but the outputs from these neurones are the inputs to a third neurone. The code is shown below (the Neuron class hasn't changed):
class Neuron:
    def __init__(self, input1, bias, w1, w2):
        self.input1 = input1
        self.bias = bias
        self.w1=w1
        self.w2=w2
    
    def CalculateOutput (self):
        output1 = 0
        net = self.bias+self.input1[0]*self.w1+self.input1[1]*self.w2
        if net >= 0:
            output1 = 1
        else:
            output1 = 0
        return output1


for x1 in range (2):
    for x2 in range (2):
        neuron1= Neuron([x1,x2],-1,-1,1)
        neuron2= Neuron([x1,x2],-1,1,-1)
        neuron3= Neuron([neuron1.CalculateOutput(),neuron2.CalculateOutput()],-1,1,1)
        print("x1="+str(x1)+"x2= "+str(x2)+" Output 1= "+str(neuron1.CalculateOutput())+" Output 2= "+str(neuron2.CalculateOutput())+" Output overall= "+str(neuron3.CalculateOutput()))

        



2. Where next - building a Physical Microbit Neural Network
The figure below shows the arrangement of the connections to be built; pin 2 is the output of each neuron. The two micro:bits/neurons on the left of the picture taking in the two same inputs; the output from these neurons are the two inputs to the output neuron on the right.



figure 1

The micro:bit objects used in Figure 1 were produced using the micro:bit Frtzing diagram available at https://github.com/microbit-foundation/dev-docs/issues/36 thanks to David Whale (@whalleygeek ) for this.








All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Comments

Popular posts from this blog

Robot Software

In the previous blog posts for this 'series' "It is a good time...."  Post 1  looked at the hardware unpinning some of this positive rise in robots; Post 2  looked at social robots; Post 3  looked at a collection of small robots; Post 4 looked at further examples of small robots Robots, such as the forthcoming Buddy and JIBO, will be based some established open sourceand other technologies. Jibo will be based around various technologies including Electron and JavaScript (for more details see:  http://blog.jibo.com/2015/07/29/jibo-making-development-readily-accessible-to-all-developers/ ). Buddy is expected to be developed around tools for Unity3d, Arduino and OpenCV, and support Python, C++, C#, Java and JavaScript (for more details see http://www.roboticstrends.com/article/customize_your_buddy_companion_robot_with_this_software_development_kit ).  This post contin ues with some of the software being used with the smaller robots.  A number ...

Speech Recognition in Scratch 3 - turning Hello into Bonjour!

The Raspberry Pi Foundation recently released a programming activity Alien Language , with support Dale from Machine Learning for Kids , that is a brilliant use of Scratch 3 - Speech Recognition to control a sprite in an alien language. Do the activity, and it is very much worth doing, and it will make sense! I  would also recommend going to the  machinelearningforkids.co.uk   site anyway it is full of exciting things to do (for example loads of activities  https://machinelearningforkids.co.uk/#!/worksheets  ) . Scratch 3 has lots of extensions that are accessible through the Extension button in the Scratch 3 editor (see below) which add new fun new blocks to play with. The critical thing for this post is  Machine Learning for Kids  have created a Scratch 3 template with their own extensions for Scratch 3 within it  https://machinelearningforkids.co.uk/scratch3/ . One of which is a Speech to Text extension (see below). You must use this one ...

Escape the Maze with a VR robot - Vex VR

You don't need to buy a robot to get programming a robot, now there are a range of free and relatively simple to start with robot simulators to play with. Three examples are listed below: - Make code for Lego EV3  https://robotsandphysicalcomputing.blogspot.com/2020/05/programming-robots-virtually-3-lego-ev3.html   - i Robot simulator  https://robotsandphysicalcomputing.blogspot.com/2020/04/programming-robots-virtually-2-irobot.html - Vex robotics Vexcode VR   https://robotsandphysicalcomputing.blogspot.com/2020/04/programming-robots-virtually-1-vexcode.html   It is the last one of these ( https://www.vexrobotics.com/vexcode-vr ) that is the focus of this post and return to hit, after an earlier discussion in  https://robotsandphysicalcomputing.blogspot.com/2020/04/programming-robots-virtually-1-vexcode.html   .  Two of the nice things about the package, apart from being free, are it uses a Scratch-like programming language and it provides a ...