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

Conteúdo do Curso

PyTorch Essentials

PyTorch Essentials

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

book
Creating a Simple Neural Network

In this chapter, we will build a basic PyTorch neural network using the wine quality dataset, a classic dataset in machine learning. The goal of this task is to predict wine quality (target) based on its physicochemical properties (features). We will focus on defining a Model class for the neural network using PyTorch's nn.Module.

Dataset Overview

The wine quality dataset contains information about red or white wine samples. Each sample has physicochemical properties such as acidity, sugar content, and pH as features, along with a target variable ('quality' column) indicating the quality score (classified as an integer from 0 to 10).

Here are the first five rows of the dataset (you can scroll it to view all the columns):

Defining the Model Class

We'll begin by importing the required PyTorch modules (nn, F) along with torch itself. 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 and does not involve images, text, or audio data, 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: 16 and 12 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 (11 for this dataset);
  • Hidden layers: arbitrary neuron counts (16 and 12);
  • Output layer: matches the number of classes (6 wine quality classes).

Complete Implementation

12345678910111213141516171819202122232425262728293031323334353637
import torch import torch.nn as nn import torch.nn.functional as F import pandas as pd wine_df = pd.read_csv('https://staging-content-media-cdn.codefinity.com/courses/1dd2b0f6-6ec0-40e6-a570-ed0ac2209666/section_2/wine.csv') # Extract features and target features = wine_df.drop(columns=["quality"]).values target = wine_df["quality"].values # Define the model class class WineQualityModel(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 = features.shape[1] # Number of features in the dataset hidden1 = 16 # Number of neurons in the first hidden layer hidden2 = 12 # Number of neurons in the second hidden layer output_classes = len(wine_df["quality"].unique()) # Number of unique classes in the target # Set manual seed for reproducibility torch.manual_seed(42) # Create an instance of the model model = WineQualityModel(input_features, hidden1, hidden2, output_classes) # Display the model structure print(model)
copy

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
We're sorry to hear that something went wrong. What happened?
some-alt