import torch
from torchvision import transforms # 是一个常用的图片变换类
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
batch_size = 64
transform = transforms.Compose(
[
transforms.ToTensor(), # 把数据转换成张量
transforms.Normalize((0.1307,), (0.3081,)) # 0.1307是均值,0.3081是标准差
]
)
train_dataset = datasets.MNIST(root='.\mnist',
train=True,
download=False,
transform=transform)
train_loader = DataLoader(train_dataset,
shuffle=True,
batch_size=batch_size)
test_dataset = datasets.MNIST(root='.\mnist',
train=False,
download=False,
transform=transform)
test_loader = DataLoader(test_dataset,
shuffle=True,
batch_size=batch_size)
class CNN(torch.nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.layer1 = torch.nn.Sequential(
torch.nn.Conv2d(1, 25, kernel_size=3),
torch.nn.BatchNorm2d(25),
torch.nn.ReLU(inplace=True)
)
self.layer2 = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer3 = torch.nn.Sequential(
torch.nn.Conv2d(25, 50, kernel_size=3),
torch.nn.BatchNorm2d(50),
torch.nn.ReLU(inplace=True)
)
self.layer4 = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2)
)
self.fc = torch.nn.Sequential(
torch.nn.Linear(50 * 5 * 5, 1024),
torch.nn.ReLU(inplace=True),
torch.nn.Linear(1024, 128),
torch.nn.ReLU(inplace=True),
torch.nn.Linear(128, 10)
)
def forward(self, x):
# print("forward")
x = self.layer1(x)
# print(x.shape)
x = self.layer2(x)
# print(x.shape)
x = self.layer3(x)
# print(x.shape)
x = self.layer4(x)
# print(x.shape)
x = x.view(x.size(0), -1) # 在进入全连接层之前需要把数据拉直Flatten
x = self.fc(x)
return x
model = CNN()
# 下面两行代码主要是如果有GPU那么就使用GPU跑代码,否则就使用cpu。cuda:0表示第1块显卡
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # 将数据放在GPU上跑所需要的代码
model.to(device) # 将数据放在GPU上跑所需要的代码
criterion = torch.nn.CrossEntropyLoss() # 使用交叉熵损失
optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.5) # momentum表示冲量,冲出局部最小
def train(epochs):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
# print(inputs.shape,target.shape)
inputs, target = inputs.to(device), target.to(device) # 将数据放在GPU上跑所需要的代码
# print(inputs.shape)
optimizer.zero_grad()
# 前馈+反馈+更新
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
# if batch_idx % 10 == 0: # 不让他每一次小的迭代就输出,而是300次小迭代再输出一次
# print('[%d,%5d] loss:%.3f' % (epoch + 1, batch_idx + 1, running_loss / 10))
# running_loss = 0.0
torch.save(model, 'model_{}.pth'.format(epochs))
def test():
correct = 0
total = 0
with torch.no_grad(): # 下面的代码就不会再计算梯度
for data in test_loader:
inputs, target = data
inputs, target = inputs.to(device), target.to(device) # 将数据放在GPU上跑所需要的代码
outputs = model(inputs)
_, predicted = torch.max(outputs.data, dim=1) # _为每一行的最大值,predicted表示每一行最大值的下标
total += target.size(0)
correct += (predicted == target).sum().item()
print('Accuracy on test set:%d %%' % (100 * correct / total))
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1.使用离线mnist手写数字数据集 MNIST数据集是从NIST的两个手写数字数据集中提取的,包含60000张训练图像和10000张测试图像,每张图像都是28×28的灰度图像,且包含一个手写数字。这些图像可以被用于训练和测试机器学习模型,以实现对手写数字的识别。MNIST数据集的论文旨在证明基于CNN的方法可以取代之前的基于手工特征的方法,成为模式识别问题上的主流方法。因此,该数据集对于研究和开发手写数字识别的机器学习模型具有重要价值。 2.内含mnist数据集,解压后运行py文件即可直接使用,不需重新下载 3.适合入门学习pytorch的朋友 4.PyTorch是一个开源的Python机器学习库,专门针对GPU加速的深度神经网络(DNN)编程。它既可以看作加入了GPU支持的numpy,同时也可以看成一个拥有自动求导功能的强大的深度神经网络。它是由Facebook开源的神经网络框架,已经被广泛应用于自然语言处理等应用程序。PyTorch的计算图是动态的,可以根据计算需要实时改变计算图。与Tensorflow相比,PyTorch更加灵活,更加强大。 5.本代码支持自动适配gpu训练
资源推荐
资源详情
资源评论
收起资源包目录
cnn_mnist.zip (1个子文件)
cnn_mnist.py 4KB
共 1 条
- 1
资源评论
码农呆呆
- 粉丝: 145
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功