Showing posts with label neurone. Show all posts
Showing posts with label neurone. Show all posts

Thursday 24 August 2017

Robot control by a neuron.

This year the Computing team has been fortunate enough to host three Nuffield Research Placement students (https://www.nuffieldresearchplacements.org/) all working with Dr Scott Turner.


Michael Welsh
Michael has been working on using a micro:bit based bitbot from 4tronix to produce a potential teaching tool; an example of artificial neurons used control a robot. The aim is for this tool to be used with 3rd-year Undergraduates, as part of a module on Artificial Intelligence.

Michael's solution was to use the computer to run and train a single neuron; then for the robot to send values from the line sensors back to the program running on a Computer and receive control signals. 

Sounds easy? No really, in the end, the software running on the computer had to also send and receive the data through a microbit (via USB) and then use radio to communicate with the bit:bot robot. All the various developed parts of the solution were implemented in Python by Michael.




Example of the code.

import serial
from math import fabs
import random
import sys
import glob


#TO DO: GUI, implement manual mode. Make manual mode a button?? When in automatic, weight boxes are greyed out. Otherwise, they are able to be typed into.

def forward(n):
    microbit.write(('f'+str(n)).encode('utf-8'))
def backward(n):
    microbit.write(('b'+str(n)).encode('utf-8'))
def turnR(n):
    microbit.write(('r'+str(n)).encode('utf-8'))
def turnL(n):
    microbit.write(('l'+str(n)).encode('utf-8'))
def end():
    microbit.write('e'.encode('utf-8'))


def serial_ports():
    """ Lists serial port names

        :raises EnvironmentError:
            On unsupported or unknown platforms
        :returns:
            A list of the serial ports available on the system
    """
    if sys.platform.startswith('win'):
        ports = ['COM%s' % (i + 1) for i in range(256)]
    elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
        # this excludes your current terminal "/dev/tty"
        ports = glob.glob('/dev/tty[A-Za-z]*')
    elif sys.platform.startswith('darwin'):
        ports = glob.glob('/dev/cu.*')
    else:
        raise EnvironmentError('Unsupported platform')

    result = []
    for port in ports:
        try:
            s = serial.Serial(port)
            s.close()
            if '/dev/cu' in port:
                if '/dev/cu.usbmodem' in port:
                    result.append(port)
            else:
                result.append(port)
        except (OSError, serial.SerialException):
            pass
    return result


class neuron:

    def __init__(self,ins):

        self.inputs = [1]

        self.weights = []

        for i in range(ins+1):
            self.weights.append(random.uniform(-1,1))
        #print(self.weights)

        self.LC = 0.005
        
    def get_output(self,inputs):
        self.inputs = [1]+inputs

        WSum = 0

        for i in range(len(self.inputs)):
            WSum += int(self.inputs[i]) * self.weights[i]

        sums = sorted([fabs(WSum), fabs(WSum - (1/3)), fabs(WSum-(2/3)), fabs(WSum-1)])
        #print(sums)
        if sums[0] == fabs(WSum): #return 0 - move backwards
            backward(5)
            return 'B',WSum
        elif sums[0] == fabs(WSum - (1/3)):
            turnR(5)
            return 'R', WSum
        elif sums[0] == fabs(WSum - (2/3)):
            turnL(5)
            return 'L', WSum
        else:
            forward(20)
            return 'F', WSum

    def train(self,inputs,desired):

        result = self.get_output(inputs)[1]
        print(result)
        error = desired - result
        #print(error)

        for w in range(len(self.weights)):

            change = self.LC * int(self.inputs[w]) * error

            print('change in weight ' + str(w) + ': ' + str(change))

            self.weights[w] += change


for i in serial_ports():
    try:
        microbit = serial.Serial(port=i, baudrate=115200)
        break
    except:
        pass

microbit.setDTR(1)
microbit.close()
microbit.open()

control = neuron(2)

mode = input('Automatic or Manual? (A/M)\n')
while mode.lower() not in ['a','m']:
    mode = input('Automatic or Manual? (A/M)\n')
if mode == 'm':
    control.weights[0],control.weights[1],control.weights[2] = int(input('Enter weight 0/bias: ')),int(input('Enter weight 1: ')), int(input('Enter weight 2: '))
while True:
    #microbit.write(input('input: ').encode('utf-8'))
    out = microbit.read(6)
    microbit.flush()
    try:
        out = out.decode()
    except:
        pass
    for i in range(len(out)):
        if str(out[i]) in ['0','1']:
            if i == 0:
                if out[1] == ']':
                    num2 = out[i]
                elif out[1] == ',':
                    num1 = out[i]
            elif i == 5:
                if out[4] == '[':
                    num1 = out[i]
                elif out[4] == ' ':
                    num2 = out[i]
            else:
                if out[i-1] == '[':
                    num1 = out[i]
                elif out[i-1] == ' ':
                    num2 = out[i]
                elif out[i+1] == ']':
                    num2 = out[i]
                elif out[i+1] == ',':
                    num1 = out[i]
        
        #print(out)
    try:
        sleft=int(num1)
        sright=int(num2)
        print([sleft, sright])
        if mode == 'a':
            if [sleft, sright] == [0, 0]:
                control.train([sleft,sright],0)
            elif [sleft, sright] == [0, 1]:
                control.train([sleft,sright], 1/3)
            elif [sleft,sright] == [1, 0]:
                control.train([sleft,sright], 2/3)
            else:
                control.train([sleft,sright], 1)
        else:
            if [sleft, sright] == [0, 0]:
                control.get_output([sleft,sright])
            elif [sleft, sright] == [0, 1]:
                control.get_output([sleft,sright])
            elif [sleft,sright] == [1, 0]:
                control.get_output([sleft,sright])
            else:
                control.get_output([sleft,sright])
    except:
        pass


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

Monday 28 November 2016

Scratch for Neurones



1. Single Neurone


Instructions:


  • Set the inputs by pressing the buttons marked input 1 and input 2 (Red is off(False or 0) and Green is on(True or 1))
  • Change the weights by changing weights 1 to 3, wx goes with input x and weight 3 is the bias.
  • To activate the neuron you need to click on the the yellow ball ('the neuron').

The video below show it in action and explains the code.



To see the code go to https://scratch.mit.edu/projects/131892234/ .

A slight modification click on the bell to change the weights

The code is available at https://scratch.mit.edu/projects/171190294/

2. Training a Neurone
In this part, the training of a neuron all written in Scratch is tackled. The video shows it action and you can have a go at using the software yourself at the end of the post. The Scratch code can be found at https://scratch.mit.edu/projects/132915502/






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

Monday 20 July 2015

Lego Robot and Neural Networks

An overview of using Lego RCX  robots for teaching neural networks present at workshop in 2011.



The video below shows the robot trying out sets of weights for two neurones, until a set of weights are found that enable the robot to go around the circle.





As a part of a set of tools I have found the following useful for teaching the principles of simple neurones.

 Example code:

import josx.platform.rcx.*;

public class annlf{
 public static void main(String[] args)
 {
  int w[][] ={//put weights here};
  int o[]={1,1};
  int s1,s2,res1,res2;
  int sensor1=0,sensor2=0;
  robot_1 tom=new robot_1();
  Sensor.S1.activate();
  Sensor.S3.activate();
  for(;;){
   sensor1=Sensor.S1.readValue();
   sensor2=Sensor.S3.readValue();
   LCD.showNumber(sensor1);
   if (sensor1<42)
    s1=1;
   else
    s1=0;
   if (sensor2<42)
    s2=1;
   else
    s2=0;
   res1=w[0][1]*s1+w[0][2]*s2+w[0][0];
   if (res1>=0)
    o[0]=1;
   else
    o[0]=0;
   res2=w[1][1]*s1+w[1][2]*s2+w[1][0];
   if (res2>=0)
    o[1]=1;
   else
    o[1]=0;
   if ((o[0]==1)&&(o[1]==1))
    tom.forward1(10);
   if ((o[0]==0)&&(o[1]==0))
    tom.backward1(20);
   if ((o[0]==1)&&(o[1]==0))
    tom.tlturn(20);
   if ((o[0]==0)&&(o[1]==1))
    tom.trturn(20);
   LCD.refresh();
  }
 }
}

The example code uses two neurones to produce a line follower. The nice thing about this though is it easy to adapted this for a single neuron or multiple neuron tasks. For more on this some examples can be found here.
The above approaches used the Mindstorms RCX robots but it can equally be done with the newer NXT robots


 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.

ChatGPT, Data Scientist - fitting it a bit

This is a second post about using ChatGPT to do some data analysis. In the first looked at using it to some basic statistics  https://robots...