Lab 4: perceptron


Download the code for the lab here 

1. Consider the perceptron below, which takes in 4 inputs.

Let’s say the inputs represent the following (with 1=True and 0=False):

  • I1: whether an object is a fruit
  • I2: whether an object is red
  • I3: whether an object is rotten
  • I4: whether an object is a sphere

Suppose we want the output of this perceptron to be 1 if the object is a red fruit that is not rotten, and 0 otherwise

a) What is the logic for the output in terms of I1 to I4? (e.g. I1 OR I2, I3 AND I4)

b) How is the output determined in terms of I1 to I4, w1 to w4, and the threshold θ?

c) Provide a set of values for w1 to w4 that will lead to the desired output

2. What is the perceptron learning rule? Give the formula and a brief description of what it does.



Part 1: Getting an output from a list of inputs

  • Open the script Perceptron.cs
  • Change learningRate to your desired value. This value can be adjusted later on once you see what the learning looks like
  • Implement GetPerceptronOutput() and show your TA once you are done. Your perceptron should now be able to calculate an output based on a list of inputs.
  • Open the scene Part1ManuallyEntered and press Play. The robot here is supposed to eat red, non-rotten fruits and avoid all other objects. It uses a perceptron to determine whether an object it collides with is the type it should eat.
  • Enter the weights you provided for the prelab and click Move. See if the robot is able to identify the objects properly using the weight entered (the robot will light up as green if the action is correct, and red if the action is incorrect). Adjust the weights if you need to and show your TA once you have successfully finds a combination that results in no error.


Part 2: Training setup

Note: part 2.1 and 2.2 can be done independently of one another.

2.1: Implementing Train()

  • Return to the Perceptron.cs script
  • Implement the Train() function and show your TA once you’re done

2.2: Setting up the training track

  • Open the scene Part2Training
  • Open the Prefabs folder in the Project window
  • Drag objects in the Prefabs folder onto the track, lining them in a straight line so the robot can hit them as it moves forward (note: as the track is pretty long, adjusting the z value of objects on the track might be helpful to put them into view).
  • This will be the training set for the robot. When creating the training set, think about what you need to include to ensure the perceptron weights will be adjusted properly after training

2.3: Training and testing

  • Part2Training should now have a full track of objects in a straight line. To start training your robot, simply hit play and let the robot moves through the track. You can see how the weights change as the perceptron is trained.
  • To rerun the training track, simply hit Stop and Play again. The perceptron will start with the weights it ended with in the last run.
  • Once the weights look stable, switch to the scene Part2Testing and hit Play to see if your robot can accurately identifies the red, non-rotten fruits. Show your TA your work once your robot passes the test.
    • If all your implementation is correct and the weights are stable but your robot is still making mistakes during testing, what else can you change to adjust its learning?
  • BONUS: play with the perceptron threshold and learning rate. How does changing either or both of these values affect the perceptron?



1. What is the concept of the perceptron based on? What are some similarities and differences between the perceptron and its inspiration?

2. For the following conditions for the perceptron output (same as the prelab), either give an example set of weights to satisfy the condition or explain why this perceptron cannot satisfy the condition:

  • The output is 1 for either red fruits or spheres of any color; 0 otherwise.
  • The output is 1 for either red fruits or red spheres, but not red fruits that are also spheres; 0 otherwise.
  • The output is 1 for blue cubes; 0 otherwise

3. (Optional) How do you think this lab could be improved for students in the future?