PyTorch NN Basics
The objective of this homework is to apply some of the tools we have learned so far (widgets, GitHub, PyTorch, and NN) to build a test bed to evaluate multiple NN parameters easily.
Test bed for Simple Neural Networks
We will make a test bed where we can create dense NNs with different parameters. The parameters we will be able to test in real time are:
- Number of hidden layers.
- Number of neurons for the hidden layers.
- Activation function used in hidden layers (Sigmoid, Tanh, or ReLu)
- Activation function used in the output layer (Sigmoid, Tanh, ReLu, Linear)
- Number of epochs to train the model.
Make a dynamic NN module (10)
Inside MyModels.py create a class called DenseModel that imports from nn.Module and receives the following parameters in the constructor:
- hidden_layers. Number of hidden layers (default value 1)
- neurons_per_layer. Number of neurons for hidden layers (default value 1)
- activation_hidden. Activation function to be used in the hidden layers. Options: 'relu', 'sigmoid', 'tanh','linear'. Default value of 'relu'.
- activation_output. Activation function to be used in the output layer. Options: 'relu', 'sigmoid', 'tanh', 'linear'.
Depending on the input values, the constructor should create the appropriate number of layers, neurons, and activation functions.
Finally, create a forward method that receives the input data and returns the output of the previously created NN model.
Make a dynamic Training module (10)
Inside Training.py create a function called training that receives the following parameters:
- x. The input data to be used in the training.
- y. The output data to be used in the training.
- optimizer. The optimizer to be used in the training.
- loss. The loss function to be used in the training. (default value:
nn.MSELoss()
) - model. The model to be trained. (default value:
DenseModel()
) - epochs. Number of epochs to train the model. (default value:
500
)
The function should train the model for the specified number of epochs and return:
- The loss function for each epoch as a list.
- The trained model.
Make your test bed (10)
Following the provided jupyter notebook called TestBed.ipynb fill the missing code of the widget to make a test bed where you can:
- A slider to select the number of hidden layers. Options from 1 to 5.
- A slider to select the number of neurons for each hidden layer. Options from 1 to 100.
- A slider to select the number of epochs to train the model. Options from 1 to 1000.
- A dropdown menu to select the activation function for the hidden layers. Options are: 'relu', 'sigmoid', 'tanh', 'linear'. (default value: 'relu')
- A dropdown menu to select the activation function for the output layer. Options are: 'relu', 'sigmoid', 'tanh', 'linear'. (default value: 'linear')
- A button to start the training.
The widget should show the following plots:
- Figure 1. Observations to be approximated and the function approximated by the trained model.
- Figure 2. The loss function for each epoch.