This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Let's now visualize how the synthetic data looks like | |
| plt.scatter(x_list, y_list , color = 'cyan') | |
| plt.plot((x_min, x_max), (m_synthetic*x_min + c_synthetic, m_synthetic*x_max + c_synthetic), color = 'r') | |
| plt.title('Synthetic Data with m = {} and c = {}'.format(m_synthetic, c_synthetic)) | |
| plt.xlabel("Independent Variable 'x'") | |
| plt.ylabel("Dependent Variable 'y'") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Defining the number of data points to be generated | |
| num_points = 100 | |
| # Let's generate the synthetic data points | |
| x_list = [] | |
| y_list = [] | |
| for _ in range(num_points): | |
| # Selecting a random integer from the predefined range | |
| x = random.randint(x_range[0] , x_range[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Defining the range of 'x', the independent variable | |
| x_range = [-2000, 2000] | |
| # Defining the extent of noise, which would be added to both the dependent as well as the independent variable | |
| deviation = 100 | |
| # For the data points to roughly fall on a staright line, we need to define the slope and the intercept of that line | |
| # Let's intoduce some randomness in the slope and intercept selection process | |
| m_synthetic = random.randint(-100, 100)/100. # m_synthetic is real number from the set(-1.0, -0.99, -0.98 ...., 0.98, 0.99, 1.0) | |
| c_synthetic = random.randint(-10, 10) # c_synthetic is an integer from the set(-10, -9, -8 ..., 8, 9, 10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Importing the bare necessities... | |
| seed_for_reproducibility = 3333 | |
| import numpy as np | |
| np.random.seed(seed_for_reproducibility) | |
| import random | |
| random.seed(seed_for_reproducibility) | |
| from matplotlib import pyplot as plt | |
| %matplotlib inline | |
| import torch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # saving the entire model | |
| torch.save(model, 'weights_only.pth') | |
| # loading the entire model | |
| model_new = torch.load('entire_model.pth') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # saving state_dict() of 'model' | |
| torch.save(model.state_dict(), 'weights_only.pth') | |
| # loading the state_dict | |
| model_new = NeuralNet() | |
| model_new.load_state_dict(torch.load('weights_only.pth')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for key in model.layer2.state_dict(): | |
| print('name: ', name) | |
| param = model.layer2.state_dict()[key] | |
| print('type(param): ', type(param)) | |
| print('param.shape : ', param.shape) | |
| print('param.requires_grad: ', param.requires_grad) | |
| print('isinstance(param, .nn.Module): ', isinstance(param, nn.Module)) | |
| print('isinstance(param, nn.Parameter): ', isinstance(param, nn.Parameter)) | |
| print('isinstance(param, torch.Tensor): ', isinstance(param, torch.Tensor)) | |
| print('=====') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for name, child in model.named_children(): | |
| print('name: ', name) | |
| print('isinstance(child, nn.Module): ', isinstance(child, nn.Module)) | |
| print('isinstance(child, nn.Parameter): ', isinstance(child, nn.Parameter)) | |
| print('isinstance(child, torch.Tensor) ', isinstance(child, torch.Tensor)) | |
| print('=====') | |
| # Also try model.children(). It doesn't return the name of the children, but the children (nn.Module objects) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for param in model.layer1.parameters(): | |
| param.requires_grad = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for name , param in model.named_parameters(): | |
| print('type(param): ', type(param)) | |
| print('isinstance(param, nn.Module): ', isinstance(param, nn.Module)) | |
| print('isinstance(param, nn.Parameter): ', isinstance(param, nn.Parameter)) | |
| print('isinstance(param, torch.Tensor) ', isinstance(param, torch.Tensor)) | |
| print('=====') | |
| # Also try model.parameters(). It doesn't return the name of the parameters but just the parameters. |