# Max pool
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(128 * 7 * 7, 128),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(128, 128),
nn.ReLU(inplace=True),
nn.Linear(128, 10),
)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return F.log_softmax(x, dim=1)
def train(model, device, train_loader, optimizer, epoch):
# 模型设置为训练模式, 等价于 model.train(True)
# 主要涉及 Dropout和 BatchNorm层
# 查到的一些博客中的解释(不一定准确)
# train时, BN用每一批数据的均值方差; eval时, BN用所有训练数据的均值方差
# train时, Dropout随机启用神经元; eval时, 用所有神经元
model.train()
for idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target
深度学习-图像分类(mnist)
最新推荐文章于 2024-08-19 21:58:17 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)