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

Machine Learning and Computational Physics

Fall 2021

Assignment 1

Expressive power of deep networks

In this assignment, you will design simple feedforward networks using PyTorch. The primary objective is to understand how the expressive power of the network varies with depth and width.

1. Write a class called MLP that inherits from torch .nn .Module.  The    init    method should take the following arguments:

•  input dim: dimension of the input vector

• output dim: dimension of the output vector/prediction

• width: width of each hidden layer (number of neurons per layer)

• depth: depth of the neural network (number of hidden layers + output layer)

• activation: type of activation function used in each neuron

Such class should model a neural network based on these input arguments.  An instance of this class should be a constructed and initialized network.   The weights and biases of each layer should be initialized using uniform distribution between  (-1,  1).   We will consider the use of tanh  and  sin activation functions in all layers. The following PyTorch tools/hints might be useful

• torch .nn .Linear()

• torch .nn .ModuleList()

• There are multiple ways to use the sin activation function.  For example:  (i) Define a self . attribute in the    init    method to represent the activation function, and use it in the forward method; (ii) Define a class Sine that inherits from torch .nn .Module

• To initialize the weights with an uniform distribution you can use torch .nn .init .uniform  () and the class function torch .nn .Module .apply().

• To convert the network prediction to a numpy array use  .detach() .numpy()

2.  Using the above class, construct different instances of the network by fixing input dim  =  1, output dim =  1, depth  =  15 and varying width  =  5,10,20,40.  Repeat this with both tanh and sin activation functions.

(a)  For each configuration, calculate and print the total number of network parameters (weights and

biases).

(b)  Create an array of 10,000 uniformly chosen points between -1 and 1.  Using this as the input

to the network, evaluate the network prediction for each of the above configurations.  Note that there is no training involved here. Plot the outputs from each network in a grid of 1 × 4 subplots (corresponding to different configurations of width), where the width varies along each column. Do this for both tanh   and sin activation functions.

(c)  Plot the FFT of each of the network output  (produced in previous step) in another figure in similar 1 × 4 grid format (numpy .fft .fft might be useful for this).

(d) What trends do you observe as a function of width?

(e) What effect does the choice of activation function appear to have.

(f)  How much do these trends vary if you re-run the script?  What is the cause of this variation, if

any?

(g) What is your conclusion about the expressivity of neural networks as a function of width and/or

activation function?

Instructions:

• You need to submit your work as a single notebook saved as A1 FirstName LastName .ipnyb  (for example A1 Tommy Trojan .ipnyb).   You can create this notebook locally  (on your computer using Jupyter notebook) or on cloud using Google Colab (which we recommend).  If you are using Google Colab, then please make sure that you are signed in to your USC Google account before starting. This will make sharing your saved work little easier.

• At the very beginning of your notebook insert a text cell and write your name and USC id.

• For questions requiring descriptive output (such as question 2d,  2e,  2f in this assignment) use indi- vidual text cell.

• Make sure that your entire notebook runs successfully on Google Colab before submitting it. It is your responsibility to ensure this.

• When you write any piece of code, remember to make it as readable as possible.   The readability of a code is almost as important as its proper functioning.   This means commenting exhaustively but concisely every step, and giving explanatory names to each variable, function, or class that are consistent throughout the code.   Each variable’s name should be intuitive and based on what the variable represents – do not be scared of using long names, and make wise use plurals and singulars. Each function should perform the (possibly one) task its name suggests.  Remember:  ” Code  is  read much more often than it is written, so plan accordingly” .

• Once you finish the assignmentm save it and share it with pinti@usc .edu.  (If you are using Google Colab, then the notebook will automatically be saved to your Google Drive. Once you locate it in your Google Drive, right click on it and share it with pinti@usc .edu). While sharing make sure that you enable “editor” option, so that we can run your notebook on our end while grading it.