pytorch进阶学习(每日一更)day05--循环神经网络的实现

Recurrent Neural Network

一个简单的RNN的实现

架构:

主要有3层;详见代码

class ConvNet(nn.Module):
    def __init__(self,num_classes):
        super(ConvNet,self).__init__()
        #定义层结构(3大层)
        #layer1
        self.layer1=nn.Sequential(
            #kernel_size就是卷积核大小这里的5代表过滤器为5*5
            nn.Conv2d(1,16,kernel_size=5,stride=1,padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2,stride=2))
        #layer2
        self.layer2=nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        #layer3
        self.fc=nn.Linear(7*7*32,num_classes)

    def forward(self,x):
        out=self.layer1(x)
        out=self.layer2(out)
        #从layer2到FC层需要一次shape变化
        out=out.reshape(out.size(0),-1)
        #即输入7*7*32  --》  输出7*7*32
        out=self.fc(out)
        return out

完整代码:


import torch
import torchvision
import torch.nn as nn
import torchvision.transforms as transforms

#1.GPU
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")

#2.设置超参
num_epochs=5
num_classes=10
batch_size=100
learning_rate=0.001

# 3.加载数据
train_data=torchvision.datasets.MNIST(root="MNIST",
                           train=True,
                           download=True,
                           transform=transforms.ToTensor())

test_data=torchvision.datasets.MNIST(root="MNIST",
                           train=False,
                           download=True,
                           transform=transforms.ToTensor())

#4.数据加载
train_loader=torch.utils.data.DataLoader(dataset=train_data,
                            batch_size=batch_size,
                            shuffle=True)
test_loader=torch.utils.data.DataLoader(dataset=test_data,
                            batch_size=batch_size,
                            shuffle=False)

#5.设置RNN循环神经网络模型
class ConvNet(nn.Module):
    def __init__(self,num_classes):
        super(ConvNet,self).__init__()
        #定义层结构(3大层)
        #layer1
        self.layer1=nn.Sequential(
            #kernel_size就是卷积核大小这里的5代表过滤器为5*5
            nn.Conv2d(1,16,kernel_size=5,stride=1,padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2,stride=2))
        #layer2
        self.layer2=nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        #layer3
        self.fc=nn.Linear(7*7*32,num_classes)

    def forward(self,x):
        out=self.layer1(x)
        out=self.layer2(out)
        #从layer2到FC层需要一次shape变化
        out=out.reshape(out.size(0),-1)
        #即输入7*7*32  --》  输出7*7*32
        out=self.fc(out)
        return out

model=ConvNet(num_classes).to(device)

#6.定义模型
#loss
criterion=nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(model.parameters(),
                 lr=learning_rate)

#7.训练模型
total_step=len(train_loader)
for epoch in range(1,num_epochs+1):
    for i,(images,labels) in enumerate(train_loader):
        #数据处理
        images=images.to(device)
        labels=labels.to(device)

        #forward
        prev=model(images)
        #loss compute
        loss=criterion(prev,labels)

        #backfard
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        #print
        if (i+1)%100==0:
            print("Epoch[{}/{}],Step[{}/{}],Loss:{:.4f}"
                  .format(epoch,num_epochs,i,total_step,loss.item()))

model.eval()
                # 如果模型中用了dropout或bn,
                # 那么predict时必须使用eval
                # 否则结果是没有参考价值的,不存在选择的余地。

#8.模型测试
with torch.no_grad():
    correct=0
    total=0
    for images,labels in test_loader:
        images=images.to(device)
        labels=labels.to(device)

        output=model(images)
        _,predeicted=torch.max(output.detach(),1)

        total+=labels.size(0)
        correct+=(predeicted==labels).sum()

    print(' Test Accuracy of the model on the 10000 test  images: {}'.format(correct*100/total))

#9.保存模型
torch.save(model.state_dict(),"model.cktp")

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值