The story here is we’ll learn about Parameter Estimation by pretending we have two thermometers on our desk. One we know measures in Celsius and the other is in a unit we don’t know, hint its fahrenheit. Our goal is to create a simple model to take a measurement from the unknown thermometer and predict a measurement in Celcius.
Importing required libraries:
%matplotlib inline
import numpy as np
import torch
torch.set_printoptions(edgeitems = 2, linewidth=75)
Creating our input data
t_c = [0.5, 14.0, 15.0, 28.0, 11.0, 8.0,
3.0, -4.0, 6.0, 13.0, 21.0]
t_u = [35.7, 55.9, 58.2, 81.9, 56.3, 48.9,
33.9, 21.8, 48.4, 60.4, 68.4]
t_c = torch.tensor(t_c)
t_u = torch.tensor(t_u)
Defining our simple linear regression model
def model(t_u, w, b):
return w * t_u + b
Defining our mean squared error loss function
def loss_fn(t_p, t_c):
squared_diffs = (t_p - t_c) ** 2
return squared_diffs.mean()
Initilializing our betas and make our first prediction. This is understandably going to be garbage as our betas are just ones and zeros to be initialized and our startig points for learning.
w = torch.ones(())
b = torch.zeros(())
t_p = model(t_u, w, b)
t_p
tensor([35.7000, 55.9000, 58.2000, 81.9000, 56.3000, 48.9000, 33.9000,
21.8000, 48.4000, 60.4000, 68.4000])
Compute the loss which we expect to be high, due to the terrible betas.
loss = loss_fn(t_p, t_c)
loss
tensor(1763.8848)
Showing how torch shapes workw ith multiplication
x = torch.ones(())
y = torch.ones(3,1)
z = torch.ones(1,3)
a = torch.ones(2, 1, 1)
print(f"shapes: x: {x.shape}, y: {y.shape}")
print(f" z: {z.shape}, a: {a.shape}")
print("x * y:", (x * y).shape)
print("y * z:", (y * z).shape)
print("y * z * a:", (y * z * a).shape)
shapes: x: torch.Size([]), y: torch.Size([3, 1])
z: torch.Size([1, 3]), a: torch.Size([2, 1, 1])
x * y: torch.Size([3, 1])
y * z: torch.Size([3, 3])
y * z * a: torch.Size([2, 3, 3])