当前位置:   article > 正文

神经网络之模型搭建和参数优化---基于CNN_神经网络优化尺寸参数

神经网络优化尺寸参数

模型搭建与参数优化

本文主要是复习pytorch实战计算机视觉的内容,模型采用CNN,数据集是手写体

1.模型搭建

class Model(torch.nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.conv1 = torch.nn.Sequential(
            torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),
            torch.nn.ReLU(),
            torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(stride=2,kernel_size=2)
        )
        self.fc = torch.nn.Sequential(
            torch.nn.Linear(14*14*128,1024),
            torch.nn.ReLU(),
            torch.nn.Dropout(p=0.5),
            torch.nn.Linear(1024,10)
        )
        
    def forward(self,x):
        x = self.conv1(x) #卷积处理
        x = x.view(-1,14*14*128) # 传入全连接层是必须扁平化处理否则会报错
        x = self.fc(x)
        return x
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2.模型训练和参数优化

cost = torch.nn.CrossEntropyLoss()#损失函数采用交叉熵
optimizer = torch.optim.Adam(model.parameters())#参数优化采用Adam优化的是model中所有参数
if Use_gpu:
    model = model.cuda()
n_epochs = 5
for epoch in range(n_epochs):
    running_loss = 0.0
    runing_correct = 0.0
    print("Epoch{}/{}".format(epoch,n_epochs))
    for data in data_loader_train:
        X_train,y_train = data
        if Use_gpu:            
            X_train,y_train = Variable(X_train.cuda()),Variable(y_train.cuda())
        else:
            X_train,y_train = Variable(X_train),Variable(y_train)
        outputs = model(X_train)
        _,pred = torch.max(outputs.data,1) 
        optimizer.zero_grad() #梯度清零
        loss = cost(outputs,y_train) #计算损失值
        loss.backward() #反向传播
        optimizer.step()
        running_loss+=loss.data
        runing_correct += torch.sum(pred==y_train.data)
    testing_correct=0
    for data in data_loader_test:
        X_test,y_test = data
        if Use_gpu:
            X_test,y_test = Variable(X_test.cuda()),Variable(y_test.cuda())
        else:
            X_test,y_test = Variable(X_test),Variable(y_test)
            
        outputs = model(X_test)
        _,pred = torch.max(outputs.data,1)
        testing_correct += torch.sum(pred==y_test.data)
    print("Loss is:{:.4f},Train acc:{:.4f},Test acc:{:.4f}"
          .format(running_loss/len(data_train),
                  100*runing_correct/len(data_train),
                  100*testing_correct/len(data_test)))
   
 输出:
Epoch0/5
Loss is:0.0007,Train acc:98.0000,Test acc:98.0000
Epoch1/5
Loss is:0.0005,Train acc:99.0000,Test acc:98.0000
Epoch2/5
Loss is:0.0003,Train acc:99.0000,Test acc:98.0000
Epoch3/5
Loss is:0.0003,Train acc:99.0000,Test acc:98.0000
Epoch4/5
Loss is:0.0002,Train acc:99.0000,Test acc:98.0000 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

3.对测试数据可视化

data_loader_test = torch.utils.data.DatalLoader(dataset=data_test,batch_size=4,shuffle=True)

X_test,y_test = next(iter(data_loader_test))
if Use_gpu:
    X_test = Variable(X_test.cuda())
else:
    X_test = Variable(X_test)
pred = model(X_test)
_,pred = torch.max(pred,1)
print("predict Label is:",[i for i in pred.data])
print("Real Label is:",[i for i in y_test])
img = torchvision.utils.make_grid(X_test)
img = img.cpu.numpy().transpose(1,2,0) #cuda数据类型不能直接转numpy需要先变成cpu
std = [0.5,0.5,0.5]
mean = [0.5,0.5,0.5]
img = img*std + mean
plt.imshow(img)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述

致谢

深度学习之pytorch实战计算机视觉

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/647591
推荐阅读
相关标签
  

闽ICP备14008679号