Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Linear Regression | Preparing for Neural Networks
Neural Networks with PyTorch
course content

Contenido del Curso

Neural Networks with PyTorch

Neural Networks with PyTorch

1. PyTorch Basics
2. Preparing for Neural Networks
3. Neural Networks

book
Linear Regression

In this chapter, we will use a real dataset to implement linear regression in PyTorch. The dataset contains two columns:

  • 'Number of Appliances': the number of appliances in a household (input feature, X);
  • 'Electricity Bill': the corresponding electricity bill amount (target output, Y).

By the end of this chapter, you will learn how to preprocess a dataset, define a linear regression model, compute loss, and train the model to predict the electricity bill based on the number of appliances.

1. Loading and Inspecting the Dataset

The dataset is stored in a CSV file. We'll load it using pandas and inspect the first few rows:

12345
import pandas as pd # Load the dataset bills_df = pd.read_csv('https://staging-content-media-cdn.codefinity.com/courses/1dd2b0f6-6ec0-40e6-a570-ed0ac2209666/section_2/electricity_bills.csv') # Display the first five rows print(bills_df.head())
copy

2. Preparing the Data for PyTorch

Next, we should extract the input X and target Y columns, convert them into PyTorch tensors, and reshape them into 2D tensors to ensure compatibility with PyTorch's operations:

1234567
import torch # Extract input (Number of Appliances) and target (Electricity Bill) X = torch.tensor(data['Number of Appliances'].values).reshape(-1, 1) Y = torch.tensor(data['Electricity Bill'].values).reshape(-1, 1) # Print the shapes of X and Y print(f"Shape of X: {X.shape}") print(f"Shape of Y: {Y.shape}")
copy

3. Defining the Linear Model

We use PyTorch's nn.Linear to define a simple linear regression model.

12345
import torch.nn as nn # Define the linear regression model model = nn.Linear(in_features=1, out_features=1) # Single input and output feature print(f"Initial weight: {model.weight.item()}") print(f"Initial bias: {model.bias.item()}")
copy

4. Defining the Loss Function and Optimizer

We use Mean Squared Error (MSE) as the loss function and Stochastic Gradient Descent (SGD) as the optimizer.

12345
import torch.optim as optim # Define the loss function (MSE) loss_fn = nn.MSELoss() # Define the optimizer (SGD) optimizer = optim.SGD(model.parameters(), lr=0.01) # Learning rate = 0.01
copy

5. Training the Model

Training involves:

  1. Forward pass: Compute predictions.
  2. Compute loss.
  3. Backward pass: Calculate gradients.
  4. Update weights and biases.
1234567891011121314151617181920
# Training loop epochs = 100 for epoch in range(epochs): # Forward pass Y_pred = model(X) loss = loss_fn(Y_pred, Y) # Backward pass optimizer.zero_grad() # Reset gradients loss.backward() # Compute gradients # Update parameters optimizer.step() if (epoch + 1) % 10 == 0: print(f"Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}") # Final parameters print("Trained Weight:", model.weight.item()) print("Trained Bias:", model.bias.item())
copy

6. Visualizing the Results

After training, visualize the regression line over the dataset.

1234567891011
import matplotlib.pyplot as plt # Visualize the predictions with torch.no_grad(): # No gradient computation Y_pred = model(X) plt.scatter(X.numpy(), Y.numpy(), label="Original Data") plt.plot(X.numpy(), Y_pred.numpy(), color="red", label="Fitted Line") plt.title("Linear Regression with PyTorch") plt.xlabel("Number of Appliances") plt.ylabel("Electricity Bill") plt.legend() plt.show()
copy

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt