Question: Perceptron and Simple Neural Network 1. Create a python file named myperceptron.py. Import the following packages. import numpy as np import matplotlib.pyplot as plt import

Perceptron and Simple Neural Network

1. Create a python file named myperceptron.py. Import the following packages.

import numpy as np

import matplotlib.pyplot as plt

import neurolab as nl

2. Load the data file and show the printout.

text = np.loadtxt('perceptron_data.txt')

# Separate datapoints and labels

data = text[:, :2]

labels = text[:, 2].reshape((text.shape[0], 1))

print(data)

print(labels)

3. Plot the data points and show the figure (Paste it below).

plt.figure()

plt.scatter(data[:,0], data[:,1])

plt.xlabel('Dimension 1')

plt.ylabel('Dimension 2')

plt.title('Input data')

plt.show()

4. Add the following lines to define the minimum and maximum values for each dimension. Show the printout.

# Define minimum and maximum values for each dimension

dim1_min, dim1_max, dim2_min, dim2_max = 0, 1, 0, 1

n_output = labels.shape[1]

print(n_output)

5. Add the following lines. Explain what the method nl.net.newp do?

D1 = [dim1_min, dim1_max]

D2 = [dim2_min, dim2_max]

perceptron = nl.net.newp([D1, D2], n_output)

6. Add the following lines. (1) Explain what the method perceptron.train do? (2) Explain what the meaning of each parameter is?

error_progress = perceptron.train(data, labels, epochs=4, lr=0.01)

7. Add the following lines and show the figure (paste it below).

plt.figure()

plt.plot(error_progress)

plt.xlabel('epochs')

plt.ylabel('Training error')

plt.title('Training progress')

plt.grid()

plt.show()

8. Create a new python file named myfirstnn.py. Import the following libraries.

import numpy as np

import matplotlib.pyplot as plt

import neurolab as nl

9. Load the data file by adding the following lines. Show the printout.

text = np.loadtxt('first_nn_data.txt')

data = text[:, 0:2]

labels = text[:, 2:]

print(data)

print(labels)

10. Add the following lines. Show the printout.

D1 = [data[:,0].min(), data[:,0].max()]

D2 = [data[:,1].min(), data[:,1].max()]

n_output = labels.shape[1]

print(n_output)

11. Add the following lines. Show the figure.

nn = nl.net.newp([D1, D2], n_output)

error_progress = nn.train(data, labels, epochs=50, lr=0.02)

plt.figure()

plt.plot(error_progress)

plt.xlabel('Epochs')

plt.ylabel('Training error')

plt.title('Training progress')

plt.grid()

plt.show()

12. Add the following lines. (1) Show the printout. (2) what is the meaning of the printout?

test = [[0.5, 4.5], [4.0, 0.5], [4.5, 8.2]]

for t in test:

print(t, '-->', nn.sim([t])[0])

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!