1. 程式人生 > >【Kaggle-MNIST之路】CNN結構再改進+交叉熵損失函式(六)

【Kaggle-MNIST之路】CNN結構再改進+交叉熵損失函式(六)

簡述

程式碼

  • 注意,下一個版本會重新安排架構。
  • 這是本次架構的最後一個版本了。
import pandas as pd
import torch.utils.data as data
import torch
import torch.nn as nn

file = './all/train.csv'
LR =
0.01 class MNISTCSVDataset(data.Dataset): def __init__(self, csv_file, Train=True): self.dataframe = pd.read_csv(csv_file, iterator=True) self.Train = Train def __len__(self): if self.Train: return 42000 else: return 28000 def __getitem__
(self, idx): data = self.dataframe.get_chunk(100) ylabel = data['label'].as_matrix().astype('float') xdata = data.ix[:, 1:].as_matrix().astype('float') return ylabel, xdata class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.
layer1 = nn.Sequential( # (1, 28, 28) nn.Conv2d( in_channels=1, out_channels=32, kernel_size=3, # 卷積filter, 移動塊長 stride=1, # filter的每次移動步長 ), nn.ReLU(), nn.BatchNorm2d(32), nn.Conv2d( in_channels=32, out_channels=32, kernel_size=3, # 卷積filter, 移動塊長 stride=1, # filter的每次移動步長 ), nn.ReLU(), nn.BatchNorm2d(32), nn.Conv2d( in_channels=32, out_channels=32, kernel_size=5, # 卷積filter, 移動塊長 stride=2, # filter的每次移動步長 padding=2, ), nn.ReLU(), nn.BatchNorm2d(32), nn.Dropout(0.4), ) self.layer2 = nn.Sequential( nn.Conv2d( in_channels=32, out_channels=64, kernel_size=3, # 卷積filter, 移動塊長 stride=1, # filter的每次移動步長 ), nn.ReLU(), nn.BatchNorm2d(64), nn.Conv2d( in_channels=64, out_channels=64, kernel_size=3, # 卷積filter, 移動塊長 stride=1, # filter的每次移動步長 ), nn.ReLU(), nn.BatchNorm2d(64), nn.Conv2d( in_channels=64, out_channels=64, kernel_size=5, # 卷積filter, 移動塊長 stride=2, # filter的每次移動步長 padding=2, ), nn.ReLU(), nn.BatchNorm2d(64), nn.Dropout(0.4), ) self.layer3 = nn.Sequential( nn.Conv2d( in_channels=64, out_channels=128, kernel_size=4, # 卷積filter, 移動塊長 stride=1, # filter的每次移動步長 ), nn.ReLU(), nn.BatchNorm2d(128), ) self.layer4 = nn.Linear(128, 10) def forward(self, x): # print(x.shape) x = self.layer1(x) # print(x.shape) x = self.layer2(x) x = self.layer3(x) x = x.view(x.size(0), -1) x = self.layer4(x) return x net = CNN() loss_function = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(net.parameters(), lr=LR) EPOCH = 10 for epoch in range(EPOCH): mydataset = MNISTCSVDataset(file) train_loader = torch.utils.data.DataLoader(mydataset, batch_size=1, shuffle=True) print('epoch %d' % epoch) for step, (yl, xd) in enumerate(train_loader): xd = xd.reshape(100, 1, 28, 28).float() output = net(xd) yl = yl.long() loss = loss_function(output, yl.squeeze()) optimizer.zero_grad() loss.backward() optimizer.step() if step % 40 == 0: print('step %d' % step, loss) torch.save(net, 'divided-net.pkl')