# 辅导ECS 170、辅导Python/c++编程设计

- 首页 >> Algorithm 算法 ECS 170: Spring 2022

Homework Assignment 5

Due Date:

No later than Thursday, June 2, 9:15pm PDT. (You don't have to wait that long, and you

shouldn't. You have a final exam the next day. But I understand that some of you prefer to wait

until the last minute.)

You are expected to do this assignment on your own, not with another person.

This assignment looks really long. Don't panic. Most of this document is just examples.

The assignment:

Recall the perceptron learning model described in Lectures 16 and 17. Your task is to use Python

to write a function that implements the perceptron described in those lectures. Your function

should be named perceptron.

Your function should expect five arguments, in this order, left-to-right:

A number representing the threshold

A number representing the adjustment factor

A list of numbers representing the initial weights on the inputs

A list of examples for training the perceptron

A number representing the number of passes the perceptron should make through

the list of examples

Let’s map these descriptions onto the actual values used in the example:

In the example, the threshold was 0.5

The adjustment factor was 0.1

If contained in a list, the initial weights were [-0.5, 0, 0.5, 0, -0.5]

The list of examples was: [[True, [1,1,1,1,0]],

[False, [1,1,1,1,1]],

[False, [0,0,0,0,0]],

[False, [0,0,1,1,0]],

[False, [1,0,1,0,1]],

[False, [1,0,1,0,0]],

[False, [0,1,0,1,1]],

[False, [0,1,0,1,0]],

[False, [0,0,1,0,0]],

[False, [0,0,0,1,0]]]

The number of passes through the list of examples was 4

Each example in the list of examples has two parts. The first part (True or False) indicates

whether this example is a positive example (True) of the concept to be learned or a negative

example (False). The second part is a list of 1’s or 0’s. This list (the 1's and 0's) must be the same

length as the list of initial weights.

Now let’s say that the variable weights is bound to the list of weights shown above, and that the

variable examples is bound to that list of examples above. If you write your function exactly like

I did, then when your function is called like this:

>>> perceptron(0.5, 0.1, weights, examples, 4)

your function would print the following output (sorry, it’s really long, just like in Lecture 17):

Starting weights: [-0.5, 0, 0.5, 0, -0.5]

Threshold: 0.5 Adjustment: 0.1

Pass 1

inputs: [1, 1, 1, 1, 0]

prediction: False answer: True

adjusted weights: [-0.4, 0.1, 0.6, 0.1, -0.5]

inputs: [1, 1, 1, 1, 1]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.6, 0.1, -0.5]

inputs: [0, 0, 0, 0, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.6, 0.1, -0.5]

inputs: [0, 0, 1, 1, 0]

prediction: True answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [1, 0, 1, 0, 1]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [1, 0, 1, 0, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [0, 1, 0, 1, 1]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [0, 1, 0, 1, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [0, 0, 1, 0, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [0, 0, 0, 1, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

Homework Assignment 5

Due Date:

No later than Thursday, June 2, 9:15pm PDT. (You don't have to wait that long, and you

shouldn't. You have a final exam the next day. But I understand that some of you prefer to wait

until the last minute.)

You are expected to do this assignment on your own, not with another person.

This assignment looks really long. Don't panic. Most of this document is just examples.

The assignment:

Recall the perceptron learning model described in Lectures 16 and 17. Your task is to use Python

to write a function that implements the perceptron described in those lectures. Your function

should be named perceptron.

Your function should expect five arguments, in this order, left-to-right:

A number representing the threshold

A number representing the adjustment factor

A list of numbers representing the initial weights on the inputs

A list of examples for training the perceptron

A number representing the number of passes the perceptron should make through

the list of examples

Let’s map these descriptions onto the actual values used in the example:

In the example, the threshold was 0.5

The adjustment factor was 0.1

If contained in a list, the initial weights were [-0.5, 0, 0.5, 0, -0.5]

The list of examples was: [[True, [1,1,1,1,0]],

[False, [1,1,1,1,1]],

[False, [0,0,0,0,0]],

[False, [0,0,1,1,0]],

[False, [1,0,1,0,1]],

[False, [1,0,1,0,0]],

[False, [0,1,0,1,1]],

[False, [0,1,0,1,0]],

[False, [0,0,1,0,0]],

[False, [0,0,0,1,0]]]

The number of passes through the list of examples was 4

Each example in the list of examples has two parts. The first part (True or False) indicates

whether this example is a positive example (True) of the concept to be learned or a negative

example (False). The second part is a list of 1’s or 0’s. This list (the 1's and 0's) must be the same

length as the list of initial weights.

Now let’s say that the variable weights is bound to the list of weights shown above, and that the

variable examples is bound to that list of examples above. If you write your function exactly like

I did, then when your function is called like this:

>>> perceptron(0.5, 0.1, weights, examples, 4)

your function would print the following output (sorry, it’s really long, just like in Lecture 17):

Starting weights: [-0.5, 0, 0.5, 0, -0.5]

Threshold: 0.5 Adjustment: 0.1

Pass 1

inputs: [1, 1, 1, 1, 0]

prediction: False answer: True

adjusted weights: [-0.4, 0.1, 0.6, 0.1, -0.5]

inputs: [1, 1, 1, 1, 1]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.6, 0.1, -0.5]

inputs: [0, 0, 0, 0, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.6, 0.1, -0.5]

inputs: [0, 0, 1, 1, 0]

prediction: True answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [1, 0, 1, 0, 1]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [1, 0, 1, 0, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [0, 1, 0, 1, 1]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [0, 1, 0, 1, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [0, 0, 1, 0, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]

inputs: [0, 0, 0, 1, 0]

prediction: False answer: False

adjusted weights: [-0.4, 0.1, 0.5, 0.0, -0.5]