10 卷积神经网络

复制代码
#----导包
import torch
from torch import nn
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim

#----准备数据集
batch_size = 64
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.1307,),(0.3081,))])


#----下载和加载train和test
trainset = datasets.MNIST(root='./data', train=True, transform=transform, download=False)
train_loader = DataLoader(trainset, batch_size=batch_size, shuffle=True)

testset = datasets.MNIST(root='./data', train=False, transform=transform, download=False)
test_loader = DataLoader(trainset, batch_size=batch_size, shuffle=False)

#-----搭建卷积神经网络
class Net(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 10, 5)
        self.conv2 = nn.Conv2d(10, 20, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(320, 10)

    def forward(self, x):
        x = F.relu(self.pool(self.conv1(x)))
        x = F.relu(self.pool(self.conv2(x)))
        x = x.view(x.size(0), -1) #可以改成x = x.view(-1, 320)
        x = self.fc1(x)
        return x

model = Net()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") #将模型迁移到GPU
model.to(device)#将模型迁移到GPU

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

def train(epoch):
    running_loss = 0.0
    for batch_idx, (inputs, targets) in enumerate(train_loader):
        inputs, targets = inputs.to(device), targets.to(device)
        optimizer.zero_grad()

        outputs = model(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
        if batch_idx % 300 == 299:
            print('[%d, %5d] loss: %.3f' %(epoch + 1, batch_idx + 1, running_loss / 300))
            running_loss = 0.0

def test(epoch):
    correct = 0
    total = 0
    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(test_loader):
            inputs, targets = inputs.to(device), targets.to(device)
            outputs = model(inputs)
            _, predicted = torch.max(outputs.data, 1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()
    accuracy = 100 * correct / total
    print('Accuracy of the network on the 10000 test images: %f ' % accuracy)

训练和测试结果:

相关推荐
2401_841495641 天前
【机器学习】k近邻法
人工智能·python·机器学习·分类··knn·k近邻算法
开心-开心急了1 天前
Kivy 乒乓游戏教程 基于Minconda或Anconda 运行
python·conda·1024程序员节·kivy
望获linux1 天前
【Linux基础知识系列:第一百五十九篇】磁盘健康监测:smartctl
linux·前端·数据库·chrome·python·操作系统·软件
爬虫程序猿1 天前
用 Python 给京东商品详情做“全身 CT”——可量产、可扩展的爬虫实战
开发语言·爬虫·python
诗句藏于尽头1 天前
自动签到之实现掘金模拟签到
python·1024程序员节
蓝纹绿茶1 天前
bash:**:pip:***python: 错误的解释器: 没有那个文件或目录
开发语言·python·pip
START_GAME1 天前
深度学习Diffusers:用 DiffusionPipeline 实现图像生成
开发语言·python·深度学习
Deamon Tree1 天前
后端开发常用Linux命令
linux·运维·python
卡卡恩1 天前
使用uv创建系统全局python执行环境
python
查士丁尼·绵1 天前
笔试-座位调整
python