前言
具体内容就是:
输入一个图像,经过神经网络后,识别为一个数字。从而实现图像的分类。
资源:
https://download.youkuaiyun.com/download/fengzhongye51460/89578965
思路:
确定输入的图像:会单通道灰度的28*28的图像,
把图像平铺后,输送到784个神经元的输入层
输入层输送到隐藏层,提取特征
隐藏层输送到输出层,显示概率
初始化模型
import torch # Import PyTorch
from torch import nn # Import the neural network module from PyTorch
# Define the neural network class, inheriting from nn.Module
class Network(nn.Module):
def __init__(self):
super().__init__() # Call the initializer of the parent class nn.Module
self.layer1 = nn.Linear(784, 256) # Define the first linear layer (input size 784, output size 256)
self.layer2 = nn.Linear(256, 10) # Define the second linear layer (input size 256, output size 10)
def forward(self, x):
x = x.view(-1, 28*28) # Flatten the input tensor to a 1D tensor of size 28*28
x = self.layer1(x) # Pass the input through the first linear layer
x = torch.relu(x) # Apply the ReLU activation function
return self.layer2(x) # Pass the result through the second linear layer and return it
__init__中
在输入层和隐藏层之间,创建一个线性层1 ,784个神经元转为256个
在隐藏层和输出层之间,创建一个线性层2,把256个神经元转为1