Training and Evaluating a Simple Neural Network in PyTorch

Introduction: The Importance of Evaluation in Neural Networks

Welcome to the first lesson of the "Improving Neural Networks with PyTorch" course. In this course, you will learn practical ways to make your neural networks perform better and avoid common pitfalls. We start with one of the most important steps in any machine learning project: evaluating your model. Evaluation helps you understand how well your model is learning and whether it is likely to perform well on new, unseen data. This is especially important in deep learning, where models can easily become too complex and start to "memorize" the training data — a problem known as overfitting.

Overfitting happens when a model learns the training data too well, including its noise and outliers, and as a result, performs poorly on new, unseen data. The model essentially "memorizes" the training set instead of learning general patterns. On the other hand, underfitting occurs when a model is too simple to capture the underlying structure of the data, resulting in poor performance on both the training and validation sets.

In this lesson, you will learn how to set up a simple neural network using PyTorch, train it on a dataset, and evaluate its performance using a validation set. This foundation will prepare you for more advanced techniques in later lessons, such as dropout, early stopping, and batch normalization.

Preparing Data for PyTorch

Before you can train a neural network, you need to prepare your data. In this example, we will use scikit-learn to generate a synthetic classification dataset. This is a common approach for learning and testing, as it allows you to focus on the model itself without worrying about data collection.

First, we use make_classification from scikit-learn to create a dataset with 1,000 samples and 20 features. The features are then scaled using StandardScaler, which is important because neural networks often train better when input features are on a similar scale. After scaling, we split the data into training and validation sets using train_test_split. The training set is used to fit the model, while the validation set helps us check how well the model is doing on data it hasn't seen before.

In real-world scenarios, it's helpful to ensure reproducibility when splitting the data. You can do this by setting the random_state parameter in train_test_split. This way, every time you run the code, you get the same split between training and validation sets.

Since PyTorch models work with tensors, we convert the NumPy arrays from scikit-learn into PyTorch tensors. For binary classification, the target labels are reshaped to have a single column, which matches the output of our neural network.

Here is the code for preparing the data:

Python
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import torch

X, y = make_classification(n_samples=1000, n_features=20)
X = StandardScaler().fit_transform(X)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

X_train = torch.tensor(X_train, dtype=torch.float32)
y_train = torch.tensor(y_train, dtype=torch.float32).unsqueeze(1)
X_val = torch.tensor(X_val, dtype=torch.float32)
y_val = torch.tensor(y_val, dtype=torch.float32).unsqueeze(1)

After running this code, you will have your data ready for training and evaluation in PyTorch.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal