赞
踩
任务网址:kaggle_Digit Recognizer
任务回顾:识别数字。
上次采用KNN实现,最终准确率为0.97,受限于算法已经比较难以提升。
本次采用CNN实现,开始使用pytorch。
PyTorch主要优势:
1、调用GPU多线程张量运算。
2、深度神经网络自动求导。
本次思路采用CNN实现,使用pytorch。
参考LE-NET5模型:
代码版本如下:
#LENET-5
#nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=1),
#nn.ReLU(),
#nn.MaxPool2d(2),
#nn.Conv2d(6, 16, 5, 1, 1),
#nn.ReLU(),
#nn.MaxPool2d(2),
#nn.Linear(400,120),
#nn.ReLU(),
#nn.Linear(120, 84),
#nn.ReLU(),
#nn.Linear(84, 10)
head()头文件:
总共有784组feature,label为y值,表示的该手写数字是几;
其余都是乘客feature,包括:
28*28=784个灰度像素,值代表亮度。
info()信息:
观察可以发现,train数据共42000个,维度为42000784;
test数据共28000个,维度为28000784。
其他数据处理:归一化。
import pandas as pd import numpy as np import torch import torch.nn as nn from torch.autograd import Variable #超参数 batch_size = 64 # 2^5=64 aerfa=0.1 num_epoc=30 #导入数据 train_data = pd.read_csv(r'D:\python\kaggle\识别数字\digit-recognizer\train.csv',dtype=np.float32) test_data = pd.read_csv(r'D:\python\kaggle\识别数字\digit-recognizer\test.csv',dtype=np.float32) #处理数据 train_np = train_data.values x_test = test_data.values y_train_np = train_np[:,0] x_train_np = train_np[:,1:] #数据分割 from sklearn.model_selection import train_test_split x_train_np, x_valid_np, y_train_np, y_valid_np = train_test_split(x_train_np,y_train_np,test_size = 0.2,random_state = 42) y_train_ts = torch.from_numpy(y_train_np).type(torch.LongTensor) x_train_ts = torch.from_numpy(x_train_np) #BATCH train_dataset=torch.utils.data.TensorDataset(x_train_ts,y_train_ts) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size = batch_size, shuffle = True , drop_last = True) #MODEL class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv = nn.Sequential( #LENET-5 #nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=1), #nn.ReLU(), #nn.MaxPool2d(2), #nn.Conv2d(6, 16, 5, 1, 1), #nn.ReLU(), #nn.MaxPool2d(2), #不知道是啥 nn.Conv2d(in_channels=1, out_channels=8, kernel_size=3, stride=1, padding=1), # nn.ReLU(), nn.Conv2d(8, 16, 3, 1, 1), nn.ReLU(), nn.MaxPool2d(kernel_size=2), nn.Conv2d(16, 16, 3, 1, 1), nn.ReLU(), nn.Conv2d(16, 8, 3, 1, 1), nn.ReLU(), nn.MaxPool2d(kernel_size=2) ) self.fc = nn.Sequential( #nn.Linear(400,120), #nn.ReLU(), #nn.Linear(120, 84), #nn.ReLU(), #nn.Linear(84, 10) nn.Linear(8 * 7 * 7, 256), nn.ReLU(), #nn.Dropout(0.5), nn.Linear(256, 256), nn.ReLU(), #nn.Dropout(0.5), nn.Linear(256, 10) ) def forward(self,img): x = self.conv(img) x = torch.flatten(x, 1) x = self.fc(x.view(x.shape[0], -1)) return x #模型实例化 model = CNN() model = model.cuda() error = nn.CrossEntropyLoss() error = error.cuda() optim = torch.optim.SGD(model.parameters(), lr=aerfa) #训练 for epoch in range(1,num_epoc+1): print('Epoch:{}/{}'.format(epoch, num_epoc)) for data in train_loader: images, labels = data images = Variable(images.view(batch_size, 1, 28, 28)) labels = Variable(labels) images = images.cuda() labels = labels.cuda() outputs = model(images) loss = error(outputs, labels) optim.zero_grad() loss.backward() optim.step() model=model.cpu() #训练集准确率 train_right=0 output_train = model(torch.from_numpy(x_train_np).view(x_train_np.shape[0],1,28,28)) train_predict = torch.max(output_train,1)[1].numpy() for i in range(x_train_np.shape[0]): if train_predict[i] == y_train_np[i]: train_right+=1 print(train_right,x_train_np.shape[0],train_right/x_train_np.shape[0]) #测试机准确率 valid_right=0 output_valid = model(torch.from_numpy(x_valid_np).view(x_valid_np.shape[0],1,28,28)) valid_predict = torch.max(output_valid,1)[1].numpy() for i in range(x_valid_np.shape[0]): if valid_predict[i] == y_valid_np[i]: valid_right+=1 print(valid_right,x_valid_np.shape[0],valid_right/x_valid_np.shape[0]) #输出到文件 test_results = np.zeros((x_test.shape[0],2),dtype='int32') ## test_results.size=(测试样本个数,2) for i in range(x_test.shape[0]): ##对于每一个测试样本 one_image = torch.from_numpy(x_test[i]).view(1,1,28,28) ##one_image=第i个测试样本,shape=1*1*28*28 one_output = model(one_image) ##将one_image输入到模型中 test_results[i,0] = i+1 ##test_results第0列表示第i个样本(从1到m) test_results[i,1] = torch.max(one_output.data,1)[1].numpy()##第一列表示预测的类别 Data = {'ImageId': test_results[:, 0], 'Label': test_results[:, 1]} DataFrame = pd.DataFrame(Data) DataFrame.to_csv('CNN.csv', index=False, sep=',')
训练集准确率:0.996
cv集准确率:0.986
测试集准确率:
使用LE-NET5的准确率为0.91(程序中注释掉的部分),采用改良版的LENET5的准确率为0.985。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。