Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE HELP IN PYTHON. A perceptron is a binary linear classifier used in supervised learning which helps to classify the given input data. According to

PLEASE HELP IN PYTHON.
A perceptron is a binary linear classifier used in supervised learning which helps to classify the given input data. According to perceptron learning, the algorithm automatically learns the optimal weight coefficients. The input features are then multiplied with these weights to determine if a neuron fires or not. The perceptron receives multiple input signals, and if the sum of the input signals exceeds a certain threshold, it either outputs a signal or does not return an output. In the context of supervised learning and classification, the perceptron can then be used to predict the class of a sample.
In python, mody the given code which has sections missing below for the self-driving section.
import numpy as np
import matplotlib.pyplot as plt
import PIL
from PIL import Image
#Let's first create a function that reads an image and returns its 23x23 binary matrix representation:
def get_matrix(path):
img = Image.open(path).convert('LA')
mat=np.zeros((23,23))
for x in range(23):
for y in range (23):
mat[y][x]=img.getpixel((x, y))[0]
if mat[y][x]!=0:
mat[y][x]=1
return mat
#Now we can read and print our 3 self-driving images:
left=get_matrix('Imagen1.png')
plt.imshow(left, cmap="gray")
plt.show()
straight=get_matrix('Imagen2.png')
plt.imshow(straight, cmap="gray")
plt.show()
right=get_matrix('Imagen3.png')
plt.imshow(right, cmap="gray")
plt.show()
#This is how one of our images looks like when binarized:
for i in range(23):
for j in range (23):
print(int(left[i][j]),end ="")
print('
')
#Output: 00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
01100000000000000000000
00110000000000000000000
00011100000000000000000
00011110000000000000000
00001111000000000000000
00000111110000000000000
00000011111000000000000
00000011111100000000000
00000001111110000000000
00000001111111000000000
00000001111111100000000
00000001111111110000000
00000001111111110000000
00000011111111110000000
00000011111111111000000
00000011111111111000000
00000011111111111000000
#Now using the code of the first point (PERCEPTRON) train a neural network that can "drive a car" based on our 3 images:
#If presented with the image left, it has to respond [1,0]. If presented with the image straight, it has to respond [0,0]. And, finally, if presented with the image right, it has to respond [0,1]
#INSTRUCTIONS: This perceptron network has two neurons and 529(23x23) inputs. The matrix of patterns P is of size 530x3(530 and not 529 because you have to add, to each of the 3 patterns, one more component=1 for the bias). The W matrix of weights is of size 2x530(2 neurons of 530 parameters) and has to be set randomly. The target matrix T is the following:
T=np.array([[1,0,0],
[0,0,1]])
#You have to reshape the matrixes left, straight, and right to form the matrix P (do not forget to add 1 in the end to each reshaped matrix):
# Modify Your code here
P.shape
# Output: (530,3)
# Set W randomly:
# Modify Your code here
W.shape
# Output: (2,530)
# Train in a 'while' loop:
#Modify Your code here
# Print the error:
print(sum(sum(E)))
# Output: 0
# Print the actual output of the network after training:
# Modify Your code here
# Output: array([[1,0,0],
[0,0,1]])

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

More Books

Students also viewed these Databases questions

Question

5. Is your hero motivated, at least in part, by guilt?

Answered: 1 week ago

Question

Writing a Strong Introduction

Answered: 1 week ago