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 Loss Function | |
| # Mean Squared Error is the most common choice of Loss Function for Linear Regression models. | |
| criterion = torch.nn.MSELoss() | |
| # Defining the Optimizer, which would update all the trainable parameters of the model, making the model learn the data distribution better and hence fit the distribution better. | |
| optimizer = torch.optim.Adam(model.parameters(), lr = 0.0005) |
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
| # We also need to convert all the data into tensors before we could use them for training our model. | |
| data_x = torch.tensor([[x] for x in x_list], dtype = torch.float) | |
| data_y = torch.tensor([[y] for y in y_list], dtype = torch.float) |
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
| losses = [] # to keep track of the epoch lossese | |
| slope_list = [] # to keep track of the slope learnt by the model | |
| intercept_list = [] # to keep track of the intercept learnt by the model | |
| EPOCHS = 2500 | |
| print('\nTRAINING...') | |
| for epoch in range(EPOCHS): | |
| # We need to clear the gradients of the optimizer before running the back-propagation in PyTorch | |
| optimizer.zero_grad() | |
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 see what are the learnt parameters after having trained the model for hundreds of epochs | |
| m_learnt = model.linear.weight.item() | |
| c_learnt = model.linear.bias.item() | |
| print('\nCompare the learnt parameters with the original ones') | |
| print('\nm_synthetic VS m_learnt') | |
| print(' {} {}'.format(m_synthetic, m_learnt)) | |
| print('\nc_synthetic VS c_learnt') | |
| print(' {} {}'.format(c_synthetic, c_learnt)) |
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
| # Plotting the epoch losses | |
| plt.plot(losses) | |
| plt.title('Loss VS Epoch') | |
| plt.xlabel('#Epoch') | |
| plt.ylabel('Loss') |
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
| plt.scatter(x_list, y_list , color = 'cyan') | |
| plt.plot((x_min, x_max), (m_learnt*x_min + c_learnt, m_learnt*x_max + c_learnt), color = 'r') | |
| plt.title('Synthetic Data Points, with m = {} and c = {}'.format(round(m_learnt, 2), round(c_learnt, 2))) | |
| 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
| plt.plot(slope_list) | |
| plt.plot(intercept_list) | |
| plt.title('Learnt Values of Slope and Intercept') | |
| plt.legend(['slope', 'intercept']) | |
| plt.xlabel('#Epochs') | |
| plt.ylabel('Learnt Parameteres') |
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
| import matplotlib.pyplot as plt | |
| %matplotlib inline | |
| import os, glob, sys | |
| def q(text = ''): # Just an exit function | |
| print(text) | |
| sys.exit() | |
| # Input data files are available in the "/kaggle/input/" directory. |
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 start by loading in the image data | |
| import torch | |
| from torchvision import datasets, transforms | |
| # Defining the transforms that we want to apply to the data. | |
| # Resizing the image to (224,224), | |
| # Randomly flipping the image horizontally(with the default probability of 0.5), | |
| # Converting the image to Tensore (converting the pixel values btween 0 and 1), | |
| # Normalizing the 3-channel data using the 'Imagenet' stats | |
| data_transforms = transforms.Compose([ |
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 dataloaders which would return data in batches | |
| batch_size = 64 | |
| train_loader = torch.utils.data.DataLoader(train_dataset, batch_size = batch_size, shuffle = True) | |
| val_loader = torch.utils.data.DataLoader(val_dataset, batch_size = batch_size, shuffle = False) | |
| print('number of batches in train_loader with a batch_size of {}: {}'.format(batch_size, len(train_loader))) | |
| print('number of batches in val_loader with a batch_size of {}: {}'.format(batch_size, len(val_loader))) |