ai代写 | 代写机器学习 | 人工智能代做 | Python | assignment – Assignment 4 – MLP for digit classification

Assignment 4 – MLP for digit classification

ai代写 | 代写机器学习 | 人工智能代做 | Python | assignment – 这道题目是利用进行的人工智能代写任务, 涵盖了机器学习/Python等方面, 这个项目是assignment代写的代写题目

network代做 代写network 计算机网络

1. The problem

In this assignment, you will train a multi-layer perceptron to understand handwritten digits. In particular, the network will take an image of a digit (0-9) and output a prediction of whether the number is odd or even.

Click here to download the files _ (

2. Datasets

The data is drawn from a data set called MNIST. Each example is a 28 by 28 gray-scale image, where each pixel is a value between 0 and 1. Given .pkl files contain a list of data points. Each datapoint itself is a list of 785 elements. First 784 elements are the normalized values of pixels (features). Also, the last column of each row is the label of the image (0 or 1). Our data set has 10,000 training, 2,000 validation and 2,000 test examples. We have not provided the test data to you; you will submit your model and we will evaluate its accuracy on the test data.

3. The network

The network structure is defined by layers parameter in the init_network function. The default model structure is [784,10,1] which means our training data has 784 features (28 28 pixel images), then we have one hidden layer with 10 neurons and one output neuron as the output layer. You may change the number of layers or number of neurons if you are interested and apply the network on other problems, but you should submit your assignment with default network structure. To obtain a simple perceptron network , should pass layers = [784, 1] to init_network function. Clarification 1: You can get the desired results for full mark with the provided parameters and network structure. With a correct implementation you can achieve a full-mark-model in less thatn 100 epochs. Clarification 2: You don’t need to check for overfitting anymore. Just make sure you reach 94% on validation set.

The parameters of a network are represented by its weights W and biases B. For example, W[l][i][j] is the weight which connects neuron i in layer l + 1 to neuron j in layer l. Likewise, B[l][i] is the bias for neuron i in layer l +

In the default settings, the dimensions of W and B are:

W[0].shape = (10,784) W[1].shape = (1,10) B[0].shape = (10,1) B[1].shape = (1,1)

4. Your part

You need to implement 2 functions and compare a simple perceptron with the MLP. Your code goes between.




You are allowed to write extra functions, but do not change the names or arguments of the given functions.

4.1 Prerequisites

You need to use Python 3.5+ and install numpy package in your environment to run the code. To install numpy you can use the command line tool pip:

pip install numpy

You should keep the same structure of folders when running the code ( train.pkl and valid.pkl should go in the dataset folder)

4.2 Implementing functions

Important : You are strongly encouraged to use vectorized operations in numpy; that is, operations that input or output a whole vector or matrix. For example, you should use a dot product in the computation of a neuron’s activation. Vector operations in numpy are much faster than implementing them directly in python. Using vectorized calculations, each epoch should take less than 3 seconds; without vectorized calculations, each epoch may take up to 60 seconds.

This web page _ ( has more information about vectorized calculations.

compute_activations In the compute_activations function, you need to implement the feed-forward phase of a neural network that calculates the value of each node’s input sum (z) and the result after applying activation function on it (a). The output should be two lists (a_s and z_s), with one vector per layer that holds the activations for that layer. That is, your results should have the resulting dimensions:

z_s[0] = None z_s[1].shape = (10,1) z_s[2].shape = (1,1) a_s[0].shape = (784,1) a_s[1].shape = (10,1) a_s[2].shape = (1,1)

backpropagation In the backpropagation function, you should implement the backpropagation algorithm, which calculates the derivative of the training error with respect to each of the network’s weights. Specifically, you should return d_w and d_b, representing the derivative with respect to the weights and biases respectively. The derivatives should have the same shape as W and B.

4.3 Compare with single-layer perceptron

Once you have finished your backpropagation implementation, use it to compare a multi-layer perceptron to a single-layer perceptron on this task.

Default MLP:

Evaluate using randomly initialized weights: Train Loss: 0.5105 Valid Loss: 0.5128 Train Acc: 0.4957 Valid Acc: 0.

Sample values for debugging:

z_s[1][0:4]= [10.08685914 0.40671609 8.98672967 6.87494907]

a_s[1][0:4]= [0.3495267]

d_w[1][0][0:4]= [0.14788401 0.0887785 0.14787168 0.00015266]


Single layer perceptron:

5. Useful functions

5.1 View input images

The plot_data_row function will display the image of a given digit, which can be useful for getting an intuition for the problem. The input to this function is a list of 785 elements (from train/valid files). A sample usage of this function is provided in the training loop.

5.2 Save and Load the model

[ 3.19604823e06 3.26817897e04 1.98406730e05 1.78973802e04]

W[0][0][0:4] [ 0.47143516 1.19097569 1.43270697 0.3126519 ]

B[0][0:4] [ 0.13423762 0.475063 0.11270962 0.03957573] Epoch # 1/40 Train Loss: 0.2345 Valid Loss: 0.2397 Train Acc: 0.8433 Valid A Epoch # 2/40 Train Loss: 0.1981 Valid Loss: 0.2059 Train Acc: 0.8729 Valid A Epoch # 3/40 Train Loss: 0.1760 Valid Loss: 0.1826 Train Acc: 0.8922 Valid A

Evaluate using randomly initialized weights: Train Loss: 0.4276 Valid Loss: 0.4532 Train Acc: 0.5755 Valid Acc: 0.

Sample values for debugging:

z_s[1][0:4]= [7.88349796]

a_s[1][0:4]= [0.99962323]

d_w[1][0][0:4]= [0. 0. 0. 0.]

d_b[0][0:4]= [1.41902384e07]

W[0][0][0:4] [ 0.47143516 1.19097569 1.43270697 0.3126519 ]


Epoch # 1/40 Train Loss: 0.1570 Valid Loss: 0.1678 Train Acc: 0.8517 Valid A Epoch # 2/40 Train Loss: 0.1394 Valid Loss: 0.1515 Train Acc: 0.8710 Valid A Epoch # 3/40 Train Loss: 0.1302 Valid Loss: 0.1433 Train Acc: 0.8813 Valid A

After training the model for max_epoch epochs, the model is saved with a timestamp at the end of the filename. The timestamp is added to make sure you dont overwrite some previously well-trained model. We will run your model to test it — make sure it works by running, for example:

python mode=test model=model 20190328164647.pkl

6. Evaluation

Your work will be evaluated based on the accuracy of your model on the test data (which you dont have access to), your code and your report. In order to get full mark for accuracy you need to have at least 92% accuracy on the test dataset. Remember that having a accuracy on validation set doesn’t guarantee to have the same accuracy on unseen data. So do your best!

6.1 Self evaluation

To validate your implementations, the files include example values of z, a, d_w, d_b, W and B that you should get from running compute_activations and backpropagation. Because the code uses a seed for the random number generator, your results should be identical to ours (up to the limit of numerical precision).

7. What to submit

1. Your code in a single python script provided for you
2. A report.pdf file containing:
Comparison between a simple perceptron with MLP.
Give one short piece of feedback about the course so far. What have you found most interesting? Is there a topic that you had
trouble understanding? Are there any changes that could improve the value of the course to you?
How many hours did you spend on this assignment?
3. Your model file. Rename it to model.pkl.
Updated Wed April 03 2019, 18:42 by mmazraeh.