Using a sequential method to build a neural network
objective of this section ; we will learn about a simplified way of defining the neural network architecture using the 'Sequential' class.
Consists of 8 steps
- Define the toy dataset
- Import the relevant packages and define the device we will work on
- Define the dataset class
- Define the dataset and dataloader object
- Define the model architecture using the 'Sequential' method avvailable in the 'nn' package
- Print a summary of the model we defined in step 5
- Define the loss function and optimizer and train the model, just like we did in the previous section. Note that, in this case , we need not define a model object ; a network is not defined with a class in this scenario
- results from trained the model then we can predict values on a validation dataset that we define now
Saving and loading a PyTorch model
Think of a scenario where you have to make inferences from an already-trained model. You would load the trained model instead of training it again.
Need the following things;
- A unique name(key) for each tensor(parameter)
- The logic to connect every tensor in the network with one or the other
- The values (weight/bias values) of each tensor
*** While the first point is taken care of during the __init__ phase of a definition, the second point is taken care of during the 'forward' method definition. By default, the values in a tensor are randomly initialized during the __init__ phase. But what we want is to load a specific set of weights that were learned when training a model and assoicate each value with a specific name. This is what you obtain by calling a special method, described in the following sections. ***
This sections have 3 steps ; state dict , Saving , Loading
Step 1. state dict
model.state_dict() command is at the root of understanding how saving and loading PyTorch models works. The dictionary in model.state_dict() corresponds to the parameter names (keys) and the values ( weight and bias values)
Step 2. Saving
Running torch.save(model.state_dict(), 'mymodel.pth') will save this model in a Python serialized format on the idsk with the name 'mymodel.pth'
Step 3. Loading
Loading a model would requires us to initialize the model with random weights first and then load the weights from 'state_dict':
1. Create an empty model with the same command that was used in the first place when training: 2. Load the model from disk and unserialize it to create an 'orderedDict'value; 3. Load 'state_dict' onto 'model', register to 'device', and make a prediction;
Questions
- Why should we convert integer inputs into float values during training?
- What are the various methods to reshape a tensor object?
- Why is computation fatser with tensor objects over NumPy arrays?
- What constitutes the init magic function in a neural network class?
- Why do we perform zero gradients before performing back-propagation?
- What magic functions constitute the dataset class?
- How do we make predictions on new data points?
- How do we fetch the intermediate layer values of a neural network?
- How does the sequential method help in simplifying defining the architecture of a neural network?