Load libraries¶

In [ ]:
# Import libraries
import pandas as pd
import torch 
from torch import nn
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
 

fahrenheit celsius convertion formula¶

In [ ]:
# Fahrenheit celsius convertion formula
celsius = np.linspace(-40, 100, 100)
fahrenheit = 1.8*celsius + 32
In [ ]:
# Plot fahrenheit celsius convertion 
plt.figure(figsize=(12,4),dpi=300)
plt.plot(celsius,fahrenheit, label='Celsius → Fahrenheit Formula' )
plt.xlabel("Celsius [°C]")  
plt.ylabel("Fahrenheit [°F]")     
plt.title("Celsius to Fahrenheit Conversion")
plt.grid(True)
plt.legend()
plt.show()
No description has been provided for this image

Training model¶

Data frame for training

In [ ]:
# Create training data
data = {
    "celsius": [-40, -28.9, -17.8, 0, 10, 20, 25, 30, 37.8, 100],
    "fahrenheit": [-40, -20, 0, 32, 50, 68, 77, 86, 100, 212]
    
}

# Store in dataframe
df = pd.DataFrame(data)
df
Out[ ]:
celsius fahrenheit
0 -40.0 -40
1 -28.9 -20
2 -17.8 0
3 0.0 32
4 10.0 50
5 20.0 68
6 25.0 77
7 30.0 86
8 37.8 100
9 100.0 212

Training data plot

In [ ]:
# Plot training data
plt.figure(figsize=(12,4),dpi=300)
plt.plot(celsius,fahrenheit, label='Celsius → Fahrenheit Formula' )
plt.scatter(df["celsius"],df["fahrenheit"] ,color='red', label='Training data')
plt.xlabel("Celsius [°C]")  
plt.ylabel("Fahrenheit [°F]")    
plt.title("Celsius to Fahrenheit Conversion with Training Data")
plt.grid(True)
plt.legend()
plt.show()
No description has been provided for this image

training data to tensor

In [ ]:
# Training data to tensor
# input: celcius
X = torch.tensor(df["celsius"].values, dtype=torch.float32).unsqueeze(1)
# output: fahrenheit
y = torch.tensor(df["fahrenheit"].values, dtype=torch.float32).unsqueeze(1)

print(X.shape)
print(y.shape)
torch.Size([10, 1])
torch.Size([10, 1])

model setup

In [ ]:
# Model setup
model = nn.Linear(1, 1)  #y = wx+b , w=weight=slope, b=bias=intercept
loss_fn = torch.nn.MSELoss() #Loss = Mean Squared Error (MSE) between predicted °F and true °F.
optimizer = torch.optim.SGD(model.parameters(), lr=0.0001) #Optimizer = Stochastic Gradient Descent with a very small learning rate.
In [ ]:
# Training loop
losses_list = []

for i in range(0, 50000): 
    # Training pass
    optimizer.zero_grad()
    outputs = model(X)
    loss = loss_fn(outputs, y)
    loss.backward()
    optimizer.step()

    # loss ucntion
    losses_list.append(loss.item())

print("Training done")

# define trained formula
w = model.weight.item()
b = model.bias.item()
print(f"Trained formula: y = {w:.4f} * x + {b:.4f}")
Training done
Final weight: 1.7998312711715698
Final bias:   31.999528884887695
Trained neuron formula: y = 1.7998 * x + 31.9995

loss function

In [ ]:
# Plot the loss function
plt.figure(figsize=(8,4),dpi=300)
plt.plot(losses_list)
plt.xlabel("Iteration")
plt.ylabel("Loss")
plt.title("Loss vs Iterations")
plt.grid(True)
plt.show()
No description has been provided for this image

Prediction¶

Prediction data

In [ ]:
# Prediction data
prediction_data = {
    "celsius": [-20, 5, 45, 72, 90] 
}

df_prediction = pd.DataFrame(prediction_data)
df_prediction
Out[ ]:
celsius
0 -20
1 5
2 45
3 72
4 90

prediction data to tensor

In [ ]:
# Prediction data to tensor
prediction_data = torch.tensor(df_prediction["celsius"], dtype=torch.float32).unsqueeze(-1)

print(prediction_data)
tensor([[-20.],
        [  5.],
        [ 45.],
        [ 72.],
        [ 90.]])

Making prediction

In [ ]:
# Making prediction
model.eval() #sets the model in evaluation mode
with torch.no_grad():  #disable the gradients
    prediction = model(prediction_data)
    print(prediction)
tensor([[ -3.9971],
        [ 40.9987],
        [112.9919],
        [161.5874],
        [193.9843]])
In [ ]:
# Plot prediction
y_prediction = prediction.numpy().flatten()  
plt.figure(figsize=(12,4),dpi=300)
plt.plot(celsius,fahrenheit, label='Celsius → Fahrenheit Formula' )
plt.scatter(df_prediction["celsius"], y_prediction, color='green', label='Prediction')
plt.xlabel("Celsius [°C]")  
plt.ylabel("Fahrenheit [°F]")     
plt.title("Celsius to Fahrenheit Conversion Prediction")
plt.grid(True)
plt.legend()
plt.show()
No description has been provided for this image

Validation¶

In [ ]:
# Prediction data
prediction_data = {
    "celsius": [-20, 5, 45, 72, 90] 
}

# Prediction and true value
y_prediction
y_true = 1.8*(np.array(prediction_data["celsius"])) + 32

# Computing errors
absolute_error = np.abs(y_prediction-y_true)
MAE_error = np.mean(absolute_error)
MSE_error = np.mean((y_prediction - y_true)**2)
rmse = np.sqrt(np.mean((y_prediction - y_true)**2))

print("Mean Absolute Error:", MAE_error)
print("Mean Squared Error:", MSE_error)
print("Root Mean Squared Error:", rmse)
Mean Absolute Error: 0.008113784790037926
Mean Squared Error: 9.5953778974916e-05
Root Mean Squared Error: 0.009795599980344032