Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
import torch
import numpy as np
# Input (temp, rainfall, humidity)
inputs = np.array([[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]], dtype='float32')
# Convert inputs and targets to tensors
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
print(inputs)
print(targets)
# Weights and biases
w = torch.randn(2, 3, requires_grad=True)
b = torch.randn(2, requires_grad=True)
print(w)
print(b)
#define the model
def model(x):
return x @ w.t() + b
# Generate predictions
preds = model(inputs)
print(preds)
# Compare with targets
print(targets)
# MSE loss
def mse(t1, t2):
diff = t1 - t2
return torch.sum(diff * diff) / diff.numel()
# Compute loss
loss = mse(preds, targets)
print(loss)
# Compute gradients
loss.backward()
# Gradients for weights and bias
print(w.grad)
print(b.grad)
# Adjust weights & reset gradients
with torch.no_grad():
w -= w.grad * 1e-5
b -= b.grad * 1e-5
w.grad.zero_()
b.grad.zero_()
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.]])
tensor([[ 1.6669, -0.3427, 0.2052],
[-0.3638, 1.2536, 0.0636]], requires_grad=True)
tensor([-0.5041, -0.3123], requires_grad=True)
tensor([[107.0396, 59.8593],
[134.1554, 80.9733],
[110.4921, 139.7132],
[162.3734, 18.8405],
[ 95.9733, 99.3876]], grad_fn=<AddBackward0>)
tensor([[ 56., 70.],
[ 81., 101.],
[119., 133.],
[ 22., 37.],
[103., 119.]])
tensor(2652.0364, grad_fn=<DivBackward0>)
tensor([[ 4331.2178, 2463.7529, 1961.0278],
[-1036.8324, -841.1684, -674.6307]])
tensor([ 45.8068, -12.2452])
print(w)
print(b)
tensor([[ 1.6236, -0.3673, 0.1856],
[-0.3534, 1.2620, 0.0704]], requires_grad=True)
tensor([-0.5046, -0.3122], requires_grad=True)
#train for 100 epochs
for i in range(10000):
preds = model(inputs)
loss = mse(preds,targets)
loss.backward()
with torch.no_grad():
w -= w.grad * 1e-5
b -= b.grad * 1e-5
w.grad.zero_()
b.grad.zero_()
preds = model(inputs)
loss = mse(preds, targets)
print(loss)
tensor(0.5032, grad_fn=<DivBackward0>)