Skip to content

Instantly share code, notes, and snippets.

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('=====')
# 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'))
# saving the entire model
torch.save(model, 'weights_only.pth')
# loading the entire model
model_new = torch.load('entire_model.pth')
# 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
# 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)
# 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])
# 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'")
# Defining the model architecture.
class LinearRegressionModel(torch.nn.Module):
def __init__(self):
super(LinearRegressionModel, self).__init__()
self.linear = torch.nn.Linear(1, 1) # this layer of the model has a single neuron, that takes in one scalar input and gives out one scalar output.
def forward(self, x):
y_pred = self.linear(x)
return y_pred
for name, parameter in model.named_parameters():
print('name : {}'.format(name))
print('parameter : {}'.format(parameter.item()))
print('learnable : {}'.format(parameter.requires_grad))
print('parameter.shape: {}'.format(parameter.shape))
print('---------------------------------')