<jupyter_start><jupyter_text>Linear regression using PyTorch built-ins<jupyter_code>import torch.nn as nn
import numpy as np
import torch
# Input (temp, rainfall, humidity)
inputs = np.array([[73, 67, 43], [91, 88, 64], [87, 134, 58], 
                   [102, 43, 37], [69, 96, 70], [73, 67, 43], 
                   [91, 88, 64], [87, 134, 58], [102, 43, 37], 
                   [69, 96, 70], [73, 67, 43], [91, 88, 64], 
                   [87, 134, 58], [102, 43, 37], [69, 96, 70]], 
                  dtype='float32')

# Targets (apples, oranges)
targets = np.array([[56, 70], [81, 101], [119, 133], 
                    [22, 37], [103, 119], [56, 70], 
                    [81, 101], [119, 133], [22, 37], 
                    [103, 119], [56, 70], [81, 101], 
                    [119, 133], [22, 37], [103, 119]], 
                   dtype='float32')

inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
inputs<jupyter_output><empty_output><jupyter_text>Dataset and DataLoader<jupyter_code>from torch.utils.data import TensorDataset
# Define dataset
train_ds = TensorDataset(inputs, targets)
train_ds[0:3]
from torch.utils.data import DataLoader
# Define data loader
batch_size = 5
train_dl = DataLoader(train_ds, batch_size, shuffle=True)
for xb, yb in train_dl:
    print(xb)
    print(yb)
    break<jupyter_output>tensor([[102.,  43.,  37.],
        [102.,  43.,  37.],
        [ 87., 134.,  58.],
        [ 69.,  96.,  70.],
        [ 87., 134.,  58.]])
tensor([[ 22.,  37.],
        [ 22.,  37.],
        [119., 133.],
        [103., 119.],
        [119., 133.]])<jupyter_text>nn.LinearInstead of initializing the weights & biases manually, we can define the model using the `nn.Linear` class from PyTorch, which does it automatically.<jupyter_code># Define model
model = nn.Linear(3, 2)
print(model.weight)
print(model.bias)
# Parameters
list(model.parameters())
# Generate predictions
preds = model(inputs)
preds<jupyter_output><empty_output><jupyter_text>Loss Function<jupyter_code># Import nn.functional
import torch.nn.functional as F
# Define loss function
loss_fn = F.mse_loss
loss = loss_fn(model(inputs), targets)
print(loss)<jupyter_output>tensor(2316.1611, grad_fn=<MseLossBackward>)<jupyter_text>Optimizer<jupyter_code># Define optimizer
opt = torch.optim.SGD(model.parameters(), lr=1e-5)<jupyter_output><empty_output><jupyter_text>Train the model1. Generate predictions2. Calculate the loss3. Compute gradients w.r.t the weights and biases4. Adjust the weights by subtracting a small quantity proportional to the gradient5. Reset the gradients to zero<jupyter_code># Utility function to train the model
def fit(num_epochs, model, loss_fn, opt, train_dl):
    
    # Repeat for given number of epochs
    for epoch in range(num_epochs):
        
        # Train with batches of data
        for xb,yb in train_dl:
            
            # 1. Generate predictions
            pred = model(xb)
            
            # 2. Calculate loss
            loss = loss_fn(pred, yb)
            
            # 3. Compute gradients
            loss.backward()
            
            # 4. Update parameters using gradients
            opt.step()
            
            # 5. Reset the gradients to zero
            opt.zero_grad()
        
        # Print the progress
        if (epoch+1) % 10 == 0:
            print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
fit(500, model, loss_fn, opt,train_dl)
# Generate predictions
preds = model(inputs)
preds
# Compare with targets
targets<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13324022346368716, 'fraction_numerical': 0.07513966480446928, 'mean_word_length': 2.5989949748743717, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '33425', 'license': 'MIT', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1167, 'n_words': 334, 'path': 'Deep-Learning-using-Pytorch/Linear-regression-using-built-ins.ipynb', 'repo_name': 'Storiesbyharshit/Deep-Learning', 'size': '3580'}