知识点回顾
- 图像数据的格式:灰度和彩色数据
- 模型的定义
- 显存占用的4种地方
- 模型参数+梯度参数
- 优化器参数
- 数据批量所占显存
- 神经元输出中间状态
- batchisize和训练的关系
课程代码:
# 先继续之前的代码
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader , Dataset # DataLoader 是 PyTorch 中用于加载数据的工具
from torchvision import datasets, transforms # torchvision 是一个用于计算机视觉的库,datasets 和 transforms 是其中的模块
import matplotlib.pyplot as plt
# 设置随机种子,确保结果可复现
torch.manual_seed(42)
# 1. 数据预处理,该写法非常类似于管道pipeline
# transforms 模块提供了一系列常用的图像预处理操作
# 先归一化,再标准化
transform = transforms.Compose([
transforms.ToTensor(), # 转换为张量并归一化到[0,1]
transforms.Normalize((0.1307,), (0.3081,)) # MNIST数据集的均值和标准差,这个值很出名,所以直接使用
])
import matplotlib.pyplot as plt
# 2. 加载MNIST数据集,如果没有会自动下载
train_dataset = datasets.MNIST(
root='./data',
train=True,
download=True,
transform=transform
)
test_dataset = datasets.MNIST(
root='./data',
train=False,
transform=transform
)
# 随机选择一张图片,可以重复运行,每次都会随机选择
sample_idx = torch.randint(0, len(train_dataset), size=(1,)).item() # 随机选择一张图片的索引
# len(train_dataset) 表示训练集的图片数量;size=(1,)表示返回一个索引;torch.randint() 函数用于生成一个指定范围内的随机数,item() 方法将张量转换为 Python 数字
image, label = train_dataset[sample_idx] # 获取图片和标签
# 可视化原始图像(需要反归一化)
def imshow(img):
img = img * 0.3081 + 0.1307 # 反标准化
npimg = img.numpy()
plt.imshow(npimg[0], cmap='gray') # 显示灰度图像
plt.show()
print(f"Label: {label}")
imshow(image)
Label: 7
# 打印下图片的形状
image.shape
torch.Size([1, 28, 28])
# 打印一张彩色图像,用cifar-10数据集
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
# 设置随机种子确保结果可复现
torch.manual_seed(42)
# 定义数据预处理步骤
transform = transforms.Compose([
transforms.ToTensor(), # 转换为张量并归一化到[0,1]
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # 标准化处理
])
# 加载CIFAR-10训练集
trainset = torchvision.datasets.CIFAR10(
root='./data',
train=True,
download=True,
transform=transform
)
# 创建数据加载器
trainloader = torch.utils.data.DataLoader(
trainset,
batch_size=4,
shuffle=True
)
# CIFAR-10的10个类别
classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
# 随机选择一张图片
sample_idx = torch.randint(0, len(trainset), size=(1,)).item()
image, label = trainset[sample_idx]
# 打印图片形状
print(f"图像形状: {image.shape}") # 输出: torch.Size([3, 32, 32])
print(f"图像类别: {classes[label]}")
# 定义图像显示函数(适用于CIFAR-10彩色图像)
def imshow(img):
img = img / 2 + 0.5 # 反标准化处理,将图像范围从[-1,1]转回[0,1]
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0))) # 调整维度顺序:(通道,高,宽) → (高,宽,通道)
plt.axis('off') # 关闭坐标轴显示
plt.show()
# 显示图像
imshow(image)
图像形状: torch.Size([3, 32, 32])
图像类别: frog
# 先归一化,再标准化
transform = transforms.Compose([
transforms.ToTensor(), # 转换为张量并归一化到[0,1]
transforms.Normalize((0.1307,), (0.3081,)) # MNIST数据集的均值和标准差,这个值很出名,所以直接使用
])
import matplotlib.pyplot as plt
# 2. 加载MNIST数据集,如果没有会自动下载
train_dataset = datasets.MNIST(
root='./data',
train=True,
download=True,
transform=transform
)
test_dataset = datasets.MNIST(
root='./data',
train=False,
transform=transform
)
# 定义两层MLP神经网络
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.flatten = nn.Flatten() # 将28x28的图像展平为784维向量
self.layer1 = nn.Linear(784, 128) # 第一层:784个输入,128个神经元
self.relu = nn.ReLU() # 激活函数
self.layer2 = nn.Linear(128, 10) # 第二层:128个输入,10个输出(对应10个数字类别)
def forward(self, x):
x = self.flatten(x) # 展平图像
x = self.layer1(x) # 第一层线性变换
x = self.relu(x) # 应用ReLU激活函数
x = self.layer2(x) # 第二层线性变换,输出logits
return x
# 初始化模型
model = MLP()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device) # 将模型移至GPU(如果可用)
from torchsummary import summary # 导入torchsummary库
print("\n模型结构信息:")
summary(model, input_size=(1, 28, 28)) # 输入尺寸为MNIST图像尺寸
模型结构信息:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Flatten-1 [-1, 784] 0
Linear-2 [-1, 128] 100,480
ReLU-3 [-1, 128] 0
Linear-4 [-1, 10] 1,290
================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.01
Params size (MB): 0.39
Estimated Total Size (MB): 0.40
----------------------------------------------------------------
class MLP(nn.Module):
def __init__(self, input_size=3072, hidden_size=128, num_classes=10):
super(MLP, self).__init__()
# 展平层:将3×32×32的彩色图像转为一维向量
# 输入尺寸计算:3通道 × 32高 × 32宽 = 3072
self.flatten = nn.Flatten()
# 全连接层
self.fc1 = nn.Linear(input_size, hidden_size) # 第一层
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes) # 输出层
def forward(self, x):
x = self.flatten(x) # 展平:[batch, 3, 32, 32] → [batch, 3072]
x = self.fc1(x) # 线性变换:[batch, 3072] → [batch, 128]
x = self.relu(x) # 激活函数
x = self.fc2(x) # 输出层:[batch, 128] → [batch, 10]
return x
# 初始化模型
model = MLP()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device) # 将模型移至GPU(如果可用)
from torchsummary import summary # 导入torchsummary库
print("\n模型结构信息:")
summary(model, input_size=(3, 32, 32)) # CIFAR-10 彩色图像(3×32×32)
模型结构信息:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Flatten-1 [-1, 3072] 0
Linear-2 [-1, 128] 393,344
ReLU-3 [-1, 128] 0
Linear-4 [-1, 10] 1,290
================================================================
Total params: 394,634
Trainable params: 394,634
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.01
Forward/backward pass size (MB): 0.03
Params size (MB): 1.51
Estimated Total Size (MB): 1.54
----------------------------------------------------------------
class MLP(nn.Module):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten() # nn.Flatten()会将每个样本的图像展平为 784 维向量,但保留 batch 维度。
self.layer1 = nn.Linear(784, 128)
self.relu = nn.ReLU()
self.layer2 = nn.Linear(128, 10)
def forward(self, x):
x = self.flatten(x) # 输入:[batch_size, 1, 28, 28] → [batch_size, 784]
x = self.layer1(x) # [batch_size, 784] → [batch_size, 128]
x = self.relu(x)
x = self.layer2(x) # [batch_size, 128] → [batch_size, 10]
return x
from torch.utils.data import DataLoader
# 定义训练集的数据加载器,并指定batch_size
train_loader = DataLoader(
dataset=train_dataset, # 加载的数据集
batch_size=64, # 每次加载64张图像
shuffle=True # 训练时打乱数据顺序
)
# 定义测试集的数据加载器(通常batch_size更大,减少测试时间)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=1000,
shuffle=False
)
@浙大疏锦行