生活随笔
收集整理的這篇文章主要介紹了
【Kaggle-MNIST之路】自定义程序结构(七)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡述
這一篇跟這個系列的其他文章不一樣,這個是重新安排下程序結構
其中model這個模型專門放模型就好了
- model/init.py中不用寫就好了。
- model/CNN.py中的內容
- 模型是基于之前的【Kaggle-MNIST之路】CNN結構再改進+交叉熵損失函數(六)
import torch
.nn
as nn
class CNN(nn
.Module
):def __init__(self
):super(CNN
, self
).__init__
()self
.layer1
= nn
.Sequential
(nn
.Conv2d
(in_channels
=1,out_channels
=32,kernel_size
=3, stride
=1, ),nn
.ReLU
(),nn
.BatchNorm2d
(32),nn
.Conv2d
(in_channels
=32,out_channels
=32,kernel_size
=3, stride
=1, ),nn
.ReLU
(),nn
.BatchNorm2d
(32),nn
.Conv2d
(in_channels
=32,out_channels
=32,kernel_size
=5, stride
=2, 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, stride
=1, ),nn
.ReLU
(),nn
.BatchNorm2d
(64),nn
.Conv2d
(in_channels
=64,out_channels
=64,kernel_size
=3, stride
=1, ),nn
.ReLU
(),nn
.BatchNorm2d
(64),nn
.Conv2d
(in_channels
=64,out_channels
=64,kernel_size
=5, stride
=2, 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, stride
=1, ),nn
.ReLU
(),nn
.BatchNorm2d
(128),)self
.layer4
= nn
.Linear
(128, 10)def forward(self
, x
):x
= self
.layer1
(x
)x
= self
.layer2
(x
)x
= self
.layer3
(x
)x
= x
.view
(x
.size
(0), -1)x
= self
.layer4
(x
)return x
import pandas
as pd
import torch
.utils
.data
as data
import torch
import torch
.nn
as nn
from model
.CNN
import CNN
file = './all/train.csv'
LR
= 0.01class 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 42000else:return 28000def __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
, xdatanet
= 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')
import torch
import torch
.utils
.data
as data
import pandas
as pd
import csv
from model
.CNN
import CNN
file = './all/test.csv'class MNISTCSVDataset(data
.Dataset
):def __init__(self
, csv_file
, Train
=False):self
.dataframe
= pd
.read_csv
(csv_file
, iterator
=True)self
.Train
= Train
def __len__(self
):if self
.Train
:return 42000else:return 28000def __getitem__(self
, idx
):data
= self
.dataframe
.get_chunk
(100)xdata
= data
.as_matrix
().astype
('float')return xdatanet
= torch
.load
('divided-net.pkl')myMnist
= MNISTCSVDataset
(file)
test_loader
= torch
.utils
.data
.DataLoader
(myMnist
, batch_size
=1, shuffle
=False)values
= []
for _
, xd
in enumerate(test_loader
):xd
= xd
.reshape
(100, 1, 28, 28).float()output
= net
(xd
)values
= values
+ output
.argmax
(dim
=1).numpy
().tolist
()with open('./all/sample_submission.csv', 'r') as fp_in
, open('newfile.csv', 'w', newline
='') as fp_out
:reader
= csv
.reader
(fp_in
)writer
= csv
.writer
(fp_out
)for i
, row
in enumerate(reader
):if i
== 0:writer
.writerow
(row
)else:row
[-1] = str(values
[i
- 1])writer
.writerow
(row
)
這個就是這個文件的架構啦~
總結
以上是生活随笔為你收集整理的【Kaggle-MNIST之路】自定义程序结构(七)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。