Contenido del Curso
Neural Networks with PyTorch
Neural Networks with PyTorch
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:
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())
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:
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}")
3. Defining the Linear Model
We use PyTorch's nn.Linear
to define a simple linear regression model.
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()}")
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.
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
5. Training the Model
Training involves:
- Forward pass: Compute predictions.
- Compute loss.
- Backward pass: Calculate gradients.
- Update weights and biases.
# 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())
6. Visualizing the Results
After training, visualize the regression line over the dataset.
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()
¡Gracias por tus comentarios!