Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Creation of a Simple Neural Network | Neural Networks in PyTorch
PyTorch Essentials
course content

Contenido del Curso

PyTorch Essentials

PyTorch Essentials

1. PyTorch Introduction
2. More Advanced Concepts
3. Neural Networks in PyTorch

book
Creation of a Simple Neural Network

Our goal it to build a basic PyTorch neural network using the digits dataset, a classic dataset in machine learning. The task is to predict the digit (target) based on the image of its handwriting, represented as a set of pixel values (features).

Dataset Overview

The digits dataset contains handwritten digit images represented as numerical pixel values. Each sample consists of 64 features, corresponding to pixel intensities from a 8×8 grayscale image. The target variable ('target' column) represents the digit class (0-9), indicating which number the image corresponds to.

The first step is reading the CSV file and extracting the features (X) and the target variable (y), which represents the output we aim to predict:

Defining the Model Class

First, all the required PyTorch modules (nn, F) should be imported. The nn module is used for defining model layers and architectures, while the F module contains activation functions, loss functions, and other utilities often used in a functional style.

We can now proceed with defining the model class:

Model Architecure

Since the task is a simple multiclass classification task, a multilayer perceptron (MLP) with 2 hidden layers is sufficient.

As you already know, an MLP consists of fully connected layers (also called dense layers), where hidden layers process the input features, and the output layer provides the final class predictions. These fully connected layers are represented as nn.Linear layers in PyTorch.

Forward Propagation

This .forward() method defines the forward propagation of data through the model.

The input tensor x is first passed through the first fully connected layer (fc1), followed by the ReLU activation function to introduce non-linearity. It then goes through the second fully connected layer (fc2), again followed by ReLU.

Finally, the transformed data passes through the output layer (out), which produces the raw scores (logits) for the output classes.

Creating the Model

Since the model class is now defined, we can now define model parameters and instantiate the model.

Similarly to the number of hidden layers, the number of neurons in each hidden layer is chosen somewhat arbitrarily in our example: 32 and 16 for the first and second hidden layers, respectively.

Consequently, the resulting model is structured as follows:

  • Input layer: matches the number of features in the dataset (64 for this dataset);
  • Hidden layers: arbitrary neuron counts (32 and 16);
  • Output layer: matches the number of classes (10 digits).

Complete Implementation

12345678910111213141516171819202122232425262728293031323334
import torch.nn as nn import torch.nn.functional as F import pandas as pd digits_df = pd.read_csv('https://content-media-cdn.codefinity.com/courses/1dd2b0f6-6ec0-40e6-a570-ed0ac2209666/section_3/digits.csv') # Extract features and target X = digits_df.drop(columns=["target"]).values y = digits_df["target"].values # Define the model class class DigitsClassifier(nn.Module): def __init__(self, input_features, hidden1, hidden2, output_classes): super().__init__() # Define the layers self.fc1 = nn.Linear(input_features, hidden1) # Input to first hidden layer self.fc2 = nn.Linear(hidden1, hidden2) # First hidden to second hidden layer self.out = nn.Linear(hidden2, output_classes) # Second hidden to output layer def forward(self, x): # Pass data through layers with activation functions a1 = F.relu(self.fc1(x)) # First hidden layer with ReLU a2 = F.relu(self.fc2(a1)) # Second hidden layer with ReLU output = self.out(a2) # Output layer (no activation for raw scores) return output # Define model parameters input_features = X.shape[1] # Number of features (pixels) hidden1 = 32 # Number of neurons in first hidden layer hidden2 = 16 # Number of neurons in second hidden layer output_classes = len(digits_df["target"].unique()) # Number of unique digits (0-9) # Create an instance of the model model = DigitsClassifier(input_features, hidden1, hidden2, output_classes) # Display the model structure print(model)
copy
Which of the following statements about implementing a neural network in PyTorch is correct?

Which of the following statements about implementing a neural network in PyTorch is correct?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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