카테고리 없음

# Chapter 2

Hardy. 2021. 5. 25. 10:13

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

  1. Define the toy dataset
  2. Import the relevant packages and define the device we will work on
  3. Define the dataset class
  4. Define the dataset and dataloader object
  5. Define the model architecture using the 'Sequential' method avvailable in the 'nn' package
  6. Print a summary of the model we defined in step 5
  7. 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
  8. 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

  1. Why should we convert integer inputs into float values during training?
  2. What are the various methods to reshape a tensor object?
  3. Why is computation fatser with tensor objects over NumPy arrays?
  4. What constitutes the init magic function in a neural network class?
  5. Why do we perform zero gradients before performing back-propagation?
  6. What magic functions constitute the dataset class?
  7. How do we make predictions on new data points?
  8. How do we fetch the intermediate layer values of a neural network?
  9. How does the sequential method help in simplifying defining the architecture of a neural network?