Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Assignment 4

Machine Learning

(20 point) Question 1 – Backpropagation

1. Build a three-layer neural network to classify three classes of Iris flowers using Numpy and basic Scikit learn functions. The layers are as follows: input, hidden layer, and output layer.

Number of neurons of the input layer is 4.

Number of hidden layer neurons is 3. Activation function of the hidden layer is ReLu.

Number of neurons of the output layer is 3, since we have three classes. The output neurons are using softmax activation function.

There is a link connecting all the input neurons to the hidden layer neurons. All the hidden layer neurons also connected to all the output neurons. (when all the neurons of previous layer are connected to the next layer is called dense layer).

2. Load Iris data as follows

from  sklearn.datasets import  load_iris

iris = load_iris()

list(iris(keys())

3. Use last two digits of your student id and the following code to choose 5 random samples from the entire 150 Iris flower samples.

import random

random.seed()

random.sample( range(0,149),   5)

4. Use following line in a for loop to generate initial value of the weights to the hidden layer and also from the hidden layer to the output layer

random.random()

Make sure you use following line before the for loop to set the seed of the random generator.

random.seed()

5. Use cross entropy as the cost function.

Use the 5 selected random Iris flower samples as the training set and show the details of the first two rounds of changing the weights by implementing backpropagation yourself.

Show details of your work

i. Calculate entropy based cost function after setting the initial weights, and after one round of training (6 points).

ii. Use gradient descent with the step size of 0.5 and update weights from the hidden layer to the output layer (7 points).

iii. Use gradient descent with step size of 0.5 and update wights from the input neurons to the hidden layer (7 points).

iv. Bonus: Run your code to classify three types of flowers using all four parameters as an input.

Use Sigmoid activation function.

Use cross validation with CV = 5.

Choose five neurons in the hidden layer. Train your neural network model.

Show the result of your trained network and also calculate accuracy of your model. (5 points)