简单的卷积神经网络实现

本文介绍了如何参照莫烦Python19的教程,动手实现一个简单的卷积神经网络,涵盖了网络的流程图及关键代码。
摘要由CSDN通过智能技术生成

卷积神经网络

参考教程:CNN(莫烦Python19)

卷积神经网络流程图

在这里插入图片描述
卷积神经网络的流程图

实现代码

import torch
import torch.nn as nn  #神经网络的层
import torch.utils.data as Data #批处理数据
import torchvision #包括数据库
import matplotlib.pyplot as plt
from torch.autograd import Variable

#hyper parameters
EPOCH = 1
BATCH_SIZE = 50
LR = 0.001
DOWNLOAD_MNIST = False

#训练集
train_data = torchvision.datasets.MNIST(
    root = './mnist', #数据保存的位置
    train = True, #用的训练集的数据,False表示测试集
    transform=torchvision.transforms.ToTensor(), #把下载的数据改为tensor的格式,数据值为(0,1)
    download=DOWNLOAD_MNIST
)


#plot one example
# print(train_data.train_data.size()) #(60000, 28, 28)
# print(train_data.train_labels.size()) #(60000)
# plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
# plt.title('%i'%train_data.train_labels[0])
# plt.show()

#数据批处理
train_loader = Data.DataLoader(
    dataset = train_data,
    batch_size=BATCH_SIZE,
    shuffle=True
)

#测试集
test_data = torchvision.datasets.MNIST(
    root = './mnist', #数据保存的位置
    train = False, #用的训练集的数据,False表示测试集
)
test_x = torch.unsqueeze(test_data.data, dim=1).type(torch.FloatTensor)[:2000]/255.#unsqueeze增加1维,跟reshape不同
test_y = test_data.targets[:2000]

#建立神经网络
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(  #卷积层
            nn.Conv2d(      #(1, 28*28)
                in_channels=1, #输入的通道数
                out_channels=16, #卷积核的高度
                kernel_size=5, #卷积核的大小
                stride=1, #步长
                padding=2,#边缘填充,if stride=1,padding=(kernel_size-1)/2,卷积后图片大小不变
            ), #二维卷积 #(16, 28*28)
            nn.ReLU(), #激励函数(16, 28*28)
            nn.MaxPool2d(kernel_size=2), #池化,长宽变为1/2(16, 14*14)
        )

        self.conv2 = nn.Sequential( #第二个卷积
            nn.Conv2d(16, 32, 5, 1, 2), #输入16层,输出32层(32, 14*14)
            nn.ReLU(),
            nn.MaxPool2d(2), #(32, 7*7)
        )

        self.out = nn.Linear(32*7*7, 10) #输入32*7*7, 输出10个种类,

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x) #(batch, 32, 7, 7) 考虑batch
        x = x.view(x.size(0), -1) #(batch, 32*7*7), x.size(0)把batch维度保留,-1把图片展开,全连接层
        output = self.out(x)
        return output

cnn = CNN()
#print(cnn) #输出卷积神经网络的结构

optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)  #优化器
loss_func = nn.CrossEntropyLoss()#损失函数,交叉熵


# training and testing
for epoch in range(EPOCH):
    for step, (x, y) in enumerate(train_loader):
        output = cnn(x)
        loss = loss_func(output, y)
        optimizer.zero_grad() #每次把梯度更新为0
        loss.backward()
        optimizer.step()

        if step % 50 == 0:
            test_output = cnn(test_x)
            pred_y = torch.max(test_output, 1)[1].data.squeeze()
            accuracy = (pred_y == test_y).sum().item() / test_y.size(0)
            print('Epoch:', epoch, '|train loss: %.4f' % loss.data, '|test accuracy: %.2f' % accuracy)

# print 10 predictions from test data
test_output= cnn(test_x[:10])
pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()
print(pred_y, 'prediction number')
print(test_y[:10].numpy(), 'real number')

输出:

Epoch: 0 |train loss: 2.3054 |test accuracy: 0.21
Epoch: 0 |train loss: 0.5556 |test accuracy: 0.83
Epoch: 0 |train loss: 0.1806 |test accuracy: 0.89
Epoch: 0 |train loss: 0.4179 |test accuracy: 0.91
Epoch: 0 |train loss: 0.1588 |test accuracy: 0.92
Epoch: 0 |train loss: 0.2451 |test accuracy: 0.94
Epoch: 0 |train loss: 0.1875 |test accuracy: 0.95
Epoch: 0 |train loss: 0.1007 |test accuracy: 0.96
Epoch: 0 |train loss: 0.0869 |test accuracy: 0.95
Epoch: 0 |train loss: 0.0752 |test accuracy: 0.96
Epoch: 0 |train loss: 0.1131 |test accuracy: 0.96
Epoch: 0 |train loss: 0.1229 |test accuracy: 0.96
Epoch: 0 |train loss: 0.0537 |test accuracy: 0.96
Epoch: 0 |train loss: 0.1545 |test accuracy: 0.97
Epoch: 0 |train loss: 0.0289 |test accuracy: 0.97
Epoch: 0 |train loss: 0.1248 |test accuracy: 0.97
Epoch: 0 |train loss: 0.6727 |test accuracy: 0.97
Epoch: 0 |train loss: 0.1419 |test accuracy: 0.98
Epoch: 0 |train loss: 0.1016 |test accuracy: 0.97
Epoch: 0 |train loss: 0.1430 |test accuracy: 0.98
Epoch: 0 |train loss: 0.1900 |test accuracy: 0.97
Epoch: 0 |train loss: 0.0696 |test accuracy: 0.97
Epoch: 0 |train loss: 0.0637 |test accuracy: 0.98
Epoch: 0 |train loss: 0.0247 |test accuracy: 0.98
[7 2 1 0 4 1 4 9 5 9] prediction number
[7 2 1 0 4 1 4 9 5 9] real number
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值