PyTorch First Neural Network
In this chapter we will introduce how to implement a simple feedforward neural network with PyTorch to accomplish a binary classification task
The following example shows how to use PyTorch to implement a simple neural network for training on a binary classification task
The network structure includes an input layer, a hidden layer, and an output layer, using the ReLU activation function and the Sigmoid activation function
It uses the mean squared error loss function and the stochastic gradient descent optimizer
The training process gradually adjusts model parameters through forward propagation, loss computation, backpropagation, and parameter updates
Example
Import the PyTorch library
import
torch
import
torch.
nn
as
nn
Define the input layer size, hidden layer size, output layer size, and batch size
n_in
,
n_h
,
n_out
,
batch_size
=
10
,
5
,
1
,
10
Create dummy input data and target data
x
=
torch.
randn
(
batch_size
,
n_in
)
Randomly generate input data
y
=
torch.
tensor
(
[
[
1.0
]
,
[
0.0
]
,
[
0.0
]
,
[
1.0
]
,
[
1.0
]
,
[
1.0
]
,
[
0.0
]
,
[
0.0
]
,
[
1.0
]
,
[
1.0
]
]
)
Target output data
Create a sequential model containing linear layers, ReLU activation, and Sigmoid activation
model
=
nn.
Sequential
(
nn.
Linear
(
n_in
,
n_h
)
,
Linear transformation from the input layer to the hidden layer
nn.
ReLU
(
)
,
ReLU activation function of the hidden layer
nn.
Linear
(
n_h
,
n_out
)
,
Linear transformation from the hidden layer to the output layer
nn.
Sigmoid
(
)
Sigmoid activation function of the output layer
)
Define the mean squared error loss function and the stochastic gradient descent optimizer
criterion
=
torch.
nn
.
MSELoss
(
)
optimizer
=
torch.
optim
.
SGD
(
model.
parameters
(
)
,
lr
=
0.01
)
Learning rate of 0.01
Execute the gradient descent algorithm for model training
for
epoch
in
range
(
50
)
:
Iterate 50 times
y_pred
=
model
(
x
)
Forward propagation, compute the predicted values
loss
=
criterion
(
y_pred
,
y
)
Compute the loss
(
'epoch: '
,
epoch
,
'loss: '
,
loss.
item
(
)
)
Print the loss value
optimizer.
zero_grad
(
)
Clear the gradients
loss.
backward
(
)
Backpropagate and compute gradients
optimizer.
step
(
)
Update the model parameters
The output is similar to the following:
epoch: 0 loss: 0.2591968774795532
epoch: 1 loss: 0.25902628898620605
epoch: 2 loss: 0.25885599851608276
epoch: 3 loss: 0.25868603587150574
epoch: 4 loss: 0.25851646065711975
...
Define the network parameters:
n_in, n_h, n_out, batch_size = 10, 5, 1, 10
n_in: The input layer size is 10, meaning each data point has 10 featuresn_h: The hidden layer size is 5, meaning the hidden layer contains 5 neuronsn_out: The output layer size is 1, meaning it outputs a scalar representing the binary classification result (0 or 1)batch_size: Each batch contains 10 samples
Generate input data and target data:
x = torch.randn(batch_size, n_in) # 随机生成输入数据
y = torch.tensor([[1.0], [0.0], [0.0],
[1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]]) # 目标输出数据
x: Randomly generate an input data matrix of shape(10, 10), representing 10 samples, each with 10 featuresy: Target output data (labels), representing the class label (0 or 1) of each input sample, is a 10x1 tensor
Define the neural network model:
model = nn.Sequential(
nn.Linear(n_in, n_h), # 输入层到隐藏层的线性变换
nn.ReLU(), # 隐藏层的ReLU激活函数
nn.Linear(n_h, n_out), # 隐藏层到输出层的线性变换
nn.Sigmoid() # 输出层的Sigmoid激活函数
)
nn.Sequential Used to define network layers sequentially
nn.Linear(n_in, n_h): Define the linear transformation from the input layer to the hidden layer, with 10 input features and 5 neurons in the hidden layernn.ReLU(): Add a ReLU activation function after the hidden layer to introduce nonlinearitynn.Linear(n_h, n_out): Define the linear transformation from the hidden layer to the output layer, with 1 output neuronnn.Sigmoid(): The output layer uses a Sigmoid activation function to map the result to the range 0 to 1 for the binary classification task
Define the loss function and optimizer:
criterion = torch.nn.MSELoss() # 使用均方误差损失函数
optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # 使用随机梯度下降优化器,学习率为 0.01
Training loop:
for epoch in range(50): # 训练50轮
y_pred = model(x) # 前向传播,计算预测值
loss = criterion(y_pred, y) # 计算损失
print('epoch: ', epoch, 'loss: ', loss.item()) # 打印损失值
optimizer.zero_grad() # 清零梯度
loss.backward() # 反向传播,计算梯度
optimizer.step() # 更新模型参数
for epoch in range(50): Perform 50 training iterationsy_pred = model(x): Perform forward propagation, using the current model parameters to compute the input datax's predicted valuesloss = criterion(y_pred, y): Compute the loss between the predicted values and the targetyvaluesoptimizer.zero_grad(): Clear the gradient values from the previous training iterationloss.backward(): Backpropagate to compute the gradients of the loss function with respect to the model parametersoptimizer.step(): Update the model parameters according to the computed gradients
Visualization code:
Example
import
torch
import
torch.
nn
as
nn
import
matplotlib.
pyplot
as
plt
Define the input layer size, hidden layer size, output layer size, and batch size
n_in
,
n_h
,
n_out
,
batch_size
=
10
,
5
,
1
,
10
Create dummy input data and target data
x
=
torch.
randn
(
batch_size
,
n_in
)
Randomly generate input data
y
=
torch.
tensor
(
[
[
1.0
]
,
[
0.0
]
,
[
0.0
]
,
[
1.0
]
,
[
1.0
]
,
[
1.0
]
,
[
0.0
]
,
[
0.0
]
,
[
1.0
]
,
[
1.0
]
]
)
Target output data
Create a sequential model containing linear layers, ReLU activation, and Sigmoid activation
model
=
nn.
Sequential
(
nn.
Linear
(
n_in
,
n_h
)
,
Linear transformation from the input layer to the hidden layer
nn.
ReLU
(
)
,
ReLU activation function of the hidden layer
nn.
Linear
(
n_h
,
n_out
)
,
Linear transformation from the hidden layer to the output layer
nn.
Sigmoid
(
)
Sigmoid activation function of the output layer
)
Define the mean squared error loss function and the stochastic gradient descent optimizer
criterion
=
torch.
nn
.
MSELoss
(
)
optimizer
=
torch.
optim
.
SGD
(
model.
parameters
(
)
,
lr
=
0.01
)
Learning rate of 0.01
Used to store the loss value of each epoch
losses
=
[
]
Execute the gradient descent algorithm for model training
for
epoch
in
range
(
50
)
:
Iterate 50 times
y_pred
=
model
(
x
)
Forward propagation, compute the predicted values
loss
=
criterion
(
y_pred
,
y
)
Compute the loss
losses.
append
(
loss.
item
(
)
)
Record the loss value
(
f
'Epoch [{epoch+1}/50], Loss: {loss.item():.4f}'
)
Print the loss value
optimizer.
zero_grad
(
)
Clear the gradients
loss.
backward
(
)
Backpropagate and compute gradients
optimizer.
step
(
)
Update the model parameters
Visualize the loss curve
plt.
figure
(
figsize
=
(
8
,
5
)
)
plt.
plot
(
range
(
1
,
51
)
,
losses
,
label
=
'Loss'
)
plt.
xlabel
(
'Epoch'
)
plt.
ylabel
(
'Loss'
)
plt.
title
(
'Training Loss Over Epochs'
)
plt.
legend
(
)
plt.
grid
(
)
plt.
show
(
)
Visualize the comparison between predicted results and actual target values
y_pred_final
=
model
(
x
)
.
detach
(
)
.
numpy
(
)
Final predicted values
y_actual
=
y.
numpy
(
)
Actual value
plt.
figure
(
figsize
=
(
8
,
5
)
)
plt.
plot
(
range
(
1
,
batch_size +
1
)
,
y_actual
,
'o-'
,
label
=
'Actual'
,
color
=
'blue'
)
plt.
plot
(
range
(
1
,
batch_size +
1
)
,
y_pred_final
,
'x--'
,
label
=
'Predicted'
,
color
=
'red'
)
plt.
xlabel
(
'Sample Index'
)
plt.
ylabel
(
'Value'
)
plt.
title
(
'Actual vs Predicted Values'
)
plt.
legend
(
)
plt.
grid
(
)
plt.
show
(
)
The display is shown as follows:

Another example
Suppose we have a two-dimensional dataset, with the goal of classifying points into two categories based on their positions (for example, red and blue points)
The following example shows how to use a neural network to accomplish a simple binary classification task, laying the foundation for more complex tasks; through PyTorch's modular interface, building, training, and visualizing neural networks are all very intuitive
1. Data Preparation
First, we generate some simple two-dimensional data
Example
import
torch
import
torch.
nn
as
nn
import
torch.
optim
as
optim
import
matplotlib.
pyplot
as
plt
Generate some random data
n_samples
=
100
data
=
torch.
randn
(
n_samples
,
2
)
Generate 100 two-dimensional data points
labels
=
(
data
[
:
,
0
]
**
2
- data
[
:
,
1
]
**
2
<
1
).
float
(
)
.
unsqueeze
(
1
)
Points inside the circle are 1, outside are 0
Visualize the data
plt.
scatter
(
data
[
:
,
0
]
,
data
[
:
,
1
]
,
c
=
labels.
squeeze
(
)
,
cmap
=
'coolwarm'
)
plt.
title
(
"Generated Data"
)
plt.
xlabel
(
"Feature 1"
)
plt.
ylabel
(
"Feature 2"
)
plt.
show
(
)
Data description:
dataare the input two-dimensional points, each with two featureslabelsis the target classification; a point inside the circular region is 1, otherwise 0
The display is as follows:

2. Define the Neural Network
Create a simple feedforward neural network with PyTorch
The feedforward neural network uses one hidden layer, capturing nonlinear patterns in the data through simple linear transformations and activation functions
Example
class
SimpleNN
(
nn.
Module
)
:
def
init
(
self
)
:
super
(
SimpleNN
,
self
)
.
init
(
)
Define the layers of the neural network
self
.
fc1
=
nn.
Linear
(
2
,
4
)
The input layer has 2 features, the hidden layer has 4 neurons
self
.
fc2
=
nn.
Linear
(
4
,
1
)
The hidden layer outputs to 1 neuron (for binary classification)
self
.
sigmoid
=
nn.
Sigmoid
(
)
Binary classification activation function
def
forward
(
self
,
x
)
:
x
=
torch.
relu
(
self
.
fc1
(
x
)
)
Use the ReLU activation function
x
=
self
.
sigmoid
(
self
.
fc2
(
x
)
)
The output layer uses the Sigmoid activation function
return
x
Instantiate the model
model
=
SimpleNN
(
)
3. Define the Loss Function and Optimizer
Example
Define the loss function and optimizer for binary classification
criterion
=
nn.
BCELoss
(
)
Binary cross-entropy loss
optimizer
=
optim.
SGD
(
model.
parameters
(
)
,
lr
=
0.1
)
Use the stochastic gradient descent optimizer
4. Train the Model
Train the model with the data so that it learns to classify
Example
Training
epochs
=
100
for
epoch
in
range
(
epochs
)
:
Forward propagation
outputs
=
model
(
data
)
loss
=
criterion
(
outputs
,
labels
)
Backpropagation
optimizer.
zero_grad
(
)
loss.
backward
(
)
optimizer.
step
(
)
Print the loss every 10 epochs
if
(
epoch +
1
)
%
10
==
0
:
(
f
'Epoch [{epoch + 1}/{epochs}], Loss: {loss.item():.4f}'
)
5. Test the Model and Visualize the Results
We test the model and draw the decision boundary on the plot
Example
Visualize the decision boundary
def
plot_decision_boundary
(
model
,
data
)
:
x_min
,
x_max
=
data
[
:
,
0
]
.
min
(
)
1
,
data
[
:
,
0
]
.
max
(
)
1
y_min
,
y_max
=
data
[
:
,
1
]
.
min
(
)
1
,
data
[
:
,
1
]
.
max
(
)
1
xx
,
yy
=
torch.
meshgrid
(
torch.
arange
(
x_min
,
x_max
,
0.1
)
,
torch.
arange
(
y_min
,
y_max
,
0.1
)
,
indexing
=
'ij'
)
grid
=
torch.
cat
(
[
xx.
reshape
(
1
,
1
)
,
yy.
reshape
(
1
,
1
)
]
,
dim
=
1
)
predictions
=
model
(
grid
)
.
detach
(
)
.
numpy
(
)
.
reshape
(
xx.
shape
)
plt.
contourf
(
xx
,
yy
,
predictions
,
levels
=
[
0
,
0.5
,
1
]
,
cmap
=
'coolwarm'
,
alpha
=
0.7
)
plt.
scatter
(
data
[
:
,
0
]
,
data
[
:
,
1
]
,
c
=
labels.
squeeze
(
)
,
cmap
=
'coolwarm'
,
edgecolors
=
'k'
)
plt.
title
(
"Decision Boundary"
)
plt.
show
(
)
plot_decision_boundary
(
model
,
data
)
6. Complete Code
The complete code is as follows:
Example
import
torch
import
torch.
nn
as
nn
import
torch.
optim
as
optim
import
matplotlib.
pyplot
as
plt
Generate some random data
n_samples
=
100
data
=
torch.
randn
(
n_samples
,
2
)
Generate 100 two-dimensional data points
labels
=
(
data
[
:
,
0
]
**
2
- data
[
:
,
1
]
**
2
<
1
).
float
(
)
.
unsqueeze
(
1
)
Points inside the circle are 1, outside are 0
Visualize the data
plt.
scatter
(
data
[
:
,
0
]
,
data
[
:
,
1
]
,
c
=
labels.
squeeze
(
)
,
cmap
=
'coolwarm'
)
plt.
title
(
"Generated Data"
)
plt.
xlabel
(
"Feature 1"
)
plt.
ylabel
(
"Feature 2"
)
plt.
show
(
)
Define the feedforward neural network
class
SimpleNN
(
nn.
Module
)
:
def
init
(
self
)
:
super
(
SimpleNN
,
self
)
.
init
(
)
Define the layers of the neural network
self
.
fc1
=
nn.
Linear
(
2
,
4
)
The input layer has 2 features, the hidden layer has 4 neurons
self
.
fc2
=
nn.
Linear
(
4
,
1
)
The hidden layer outputs to 1 neuron (for binary classification)
self
.
sigmoid
=
nn.
Sigmoid
(
)
Binary classification activation function
def
forward
(
self
,
x
)
:
x
=
torch.
relu
(
self
.
fc1
(
x
)
)
Use the ReLU activation function
x
=
self
.
sigmoid
(
self
.
fc2
(
x
)
)
The output layer uses the Sigmoid activation function
return
x
Instantiate the model
model
=
SimpleNN
(
)
Define the loss function and optimizer
criterion
=
nn.
BCELoss
(
)
Binary cross-entropy loss
optimizer
=
optim.
SGD
(
model.
parameters
(
)
,
lr
=
0.1
)
Use the stochastic gradient descent optimizer
Training
epochs
=
100
for
epoch
in
range
(
epochs
)
:
Forward propagation
outputs
=
model
(
data
)
loss
=
criterion
(
outputs
,
labels
)
Backpropagation
optimizer.
zero_grad
(
)
loss.
backward
(
)
optimizer.
step
(
)
Print the loss every 10 epochs
if
(
epoch +
1
)
%
10
==
0
:
(
f
'Epoch [{epoch + 1}/{epochs}], Loss: {loss.item():.4f}'
)
Visualize the decision boundary
def
plot_decision_boundary
(
model
,
data
)
:
x_min
,
x_max
=
data
[
:
,
0
]
.
min
(
)
1
,
data
[
:
,
0
]
.
max
(
)
1
y_min
,
y_max
=
data
[
:
,
1
]
.
min
(
)
1
,
data
[
:
,
1
]
.
max
(
)
1
xx
,
yy
=
torch.
meshgrid
(
torch.
arange
(
x_min
,
x_max
,
0.1
)
,
torch.
arange
(
y_min
,
y_max
,
0.1
)
,
indexing
=
'ij'
)
grid
=
torch.
cat
(
[
xx.
reshape
(
1
,
1
)
,
yy.
reshape
(
1
,
1
)
]
,
dim
=
1
)
predictions
=
model
(
grid
)
.
detach
(
)
.
numpy
(
)
.
reshape
(
xx.
shape
)
plt.
contourf
(
xx
,
yy
,
predictions
,
levels
=
[
0
,
0.5
,
1
]
,
cmap
=
'coolwarm'
,
alpha
=
0.7
)
plt.
scatter
(
data
[
:
,
0
]
,
data
[
:
,
1
]
,
c
=
labels.
squeeze
(
)
,
cmap
=
'coolwarm'
,
edgecolors
=
'k'
)
plt.
title
(
"Decision Boundary"
)
plt.
show
(
)
plot_decision_boundary
(
model
,
data
)
Loss output during training:
Epoch [10/100], Loss: 0.5247
Epoch [20/100], Loss: 0.3142
...
Epoch [100/100], Loss: 0.0957
The figure shows the original data points (red and blue), as well as the classification boundary learned by the model
