# AyushAnuj/13bb0-linear-regression-basic-ipynb

2 years ago
In [1]:
``````import numpy as np
import torch``````
In [22]:
``````inputs = np.array([[73, 67, 43],
[91, 88, 64],
[87, 134, 58],
[102, 43, 37],
[69, 96, 70]], dtype='float32')``````
In [23]:
``````targets = np.array([[56, 70],
[81, 101],
[119, 133],
[22, 37],
[103, 119]], dtype='float32')``````
In [24]:
``````inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
print(inputs)
print(targets)
``````
```tensor([[ 73., 67., 43.], [ 91., 88., 64.], [ 87., 134., 58.], [102., 43., 37.], [ 69., 96., 70.]]) tensor([[ 56., 70.], [ 81., 101.], [119., 133.], [ 22., 37.], [103., 119.]]) ```
In [25]:
``````w = torch.randn(2, 3, requires_grad=True)
print(w)
print(b)
``````
```tensor([[ 2.0746, -1.9273, -0.3545], [ 0.4282, 0.3650, 1.0465]], requires_grad=True) tensor([-2.4295, 0.9029], requires_grad=True) ```
In [26]:
``````def model(x):
return x @ w.t() + b``````
In [27]:
``````preds = model(inputs)
print(preds)
``````
```tensor([[ 4.6401, 101.6169], [ -5.9359, 138.9662], [-100.7630, 147.7650], [ 113.1844, 98.9956], [ -69.1214, 138.7448]], grad_fn=<AddBackward0>) ```
In [28]:
``print(targets)``
```tensor([[ 56., 70.], [ 81., 101.], [119., 133.], [ 22., 37.], [103., 119.]]) ```
In [29]:
``````#minimum square loss (MSE)
def mse(t1, t2):
diff = t1-t2
In [30]:
``````loss = mse(preds, targets)
loss
``````
Out[30]:
``tensor(10332.4229, grad_fn=<DivBackward0>)``
In [31]:
``loss.backward()``
In [32]:
``````print(w)
``````
```tensor([[ 2.0746, -1.9273, -0.3545], [ 0.4282, 0.3650, 1.0465]], requires_grad=True) tensor([[ -6671.0776, -10628.4893, -5838.6606], [ 2946.6907, 2399.8352, 1664.3409]]) ```
In [38]:
``````for i in range(350):
preds = model(inputs)
loss = mse(preds,targets)
loss.backward()
``````
In [39]:
``````preds = model(inputs)
loss = mse(preds, targets)
print(loss)
``````
```tensor(3.5686, grad_fn=<DivBackward0>) ```
In [40]:
``````print(preds)
print(targets)
``````
```tensor([[ 57.1446, 70.4543], [ 80.9768, 101.0074], [121.5160, 131.9608], [ 23.2150, 37.0124], [ 98.0536, 119.9029]], grad_fn=<AddBackward0>) tensor([[ 56., 70.], [ 81., 101.], [119., 133.], [ 22., 37.], [103., 119.]]) ```
In [46]:
``import torch.nn as nn``
In [41]:
``````# 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)
``````
In [42]:
``````from torch.utils.data import TensorDataset
train_ds = TensorDataset(inputs, targets)
train_ds[0:3]``````
Out[42]:
``````(tensor([[ 73.,  67.,  43.],
[ 91.,  88.,  64.],
[ 87., 134.,  58.]]), tensor([[ 56.,  70.],
[ 81., 101.],
[119., 133.]]))``````
In [44]:
``````from torch.utils.data import DataLoader
batch_size = 5
for xb, yb in train_dl:
print(xb)
print(yb)
``````
```tensor([[102., 43., 37.], [ 87., 134., 58.], [ 91., 88., 64.], [102., 43., 37.], [ 69., 96., 70.]]) tensor([[ 22., 37.], [119., 133.], [ 81., 101.], [ 22., 37.], [103., 119.]]) tensor([[ 87., 134., 58.], [ 69., 96., 70.], [ 73., 67., 43.], [ 73., 67., 43.], [ 69., 96., 70.]]) tensor([[119., 133.], [103., 119.], [ 56., 70.], [ 56., 70.], [103., 119.]]) tensor([[102., 43., 37.], [ 73., 67., 43.], [ 87., 134., 58.], [ 91., 88., 64.], [ 91., 88., 64.]]) tensor([[ 22., 37.], [ 56., 70.], [119., 133.], [ 81., 101.], [ 81., 101.]]) ```
In [54]:
``nn.Linear??``
In [47]:
``````model = nn.Linear(3, 2)
print(model.weight)
print(model.bias)
``````
```Parameter containing: tensor([[-0.2452, -0.1592, -0.3139], [ 0.4542, -0.2202, 0.5189]], requires_grad=True) Parameter containing: tensor([-0.3980, 0.3863], requires_grad=True) ```
In [48]:
``````# Parameters
list(model.parameters())``````
Out[48]:
``````[Parameter containing:
tensor([[-0.2452, -0.1592, -0.3139],
Parameter containing:
In [55]:
``````preds = model(inputs)
preds
``````
Out[55]:
``````tensor([[-42.4615,  41.1061],
[-56.8102,  55.5559],
[-61.2668,  40.4973],
[-43.8698,  56.4487],
[-54.5721,  46.9156],
[-42.4615,  41.1061],
[-56.8102,  55.5559],
[-61.2668,  40.4973],
[-43.8698,  56.4487],
[-54.5721,  46.9156],
[-42.4615,  41.1061],
[-56.8102,  55.5559],
[-61.2668,  40.4973],
[-43.8698,  56.4487],
In [56]:
``import torch.nn.functional as F``
In [57]:
``loss_fn = F.mse_loss``
In [59]:
``````loss = loss_fn(model(inputs), targets)
print(loss)
``````
```tensor(10738.1406, grad_fn=<MseLossBackward>) ```
In [60]:
``````# Define optimizer
opt = torch.optim.SGD(model.parameters(), lr=1e-5)``````
In [61]:
``````def fit(num_epochs, model, loss_fn, opt):

# 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)

loss.backward()

# 4. Update parameters using gradients
opt.step()

# 5. Reset the gradients to zero

# Print the progress
if (epoch+1) % 10 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))

``````
In [65]:
``fit(100, model, loss_fn, opt)``
```Epoch [10/100], Loss: 0.7788 Epoch [20/100], Loss: 1.5927 Epoch [30/100], Loss: 1.4452 Epoch [40/100], Loss: 1.2139 Epoch [50/100], Loss: 1.5238 Epoch [60/100], Loss: 2.0133 Epoch [70/100], Loss: 1.2704 Epoch [80/100], Loss: 1.0680 Epoch [90/100], Loss: 1.4715 Epoch [100/100], Loss: 1.1136 ```
In [66]:
``````preds = model(inputs)
print(preds)
print(targets)
``````
```tensor([[ 57.1900, 70.3987], [ 81.5057, 100.4331], [120.2479, 133.3624], [ 21.5354, 37.1289], [100.3580, 118.7338], [ 57.1900, 70.3987], [ 81.5057, 100.4331], [120.2479, 133.3624], [ 21.5354, 37.1289], [100.3580, 118.7338], [ 57.1900, 70.3987], [ 81.5057, 100.4331], [120.2479, 133.3624], [ 21.5354, 37.1289], [100.3580, 118.7338]], grad_fn=<AddmmBackward>) tensor([[ 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.]]) ```
In [ ]:
``````import jovian
jovian.commit()
``````
```[jovian] Saving notebook.. ```
In [ ]:
`` ``