1. 程式人生 > >keras解決kaggle-手寫體數字識別

keras解決kaggle-手寫體數字識別

上一篇是用tensorflow完成的,發現tensorflow的程式碼量比較多。因為tensorflow是比較低層次的庫,而keras可以使用tensorflow為後端,實現模型起來程式碼會較少一點,而且也便於我們增加模型的深度。總的來說,keras更適合於上手。得到的成績比tensorflow稍高一點

首先資料處理與用tensorflow的差不多。

#引入所需要的庫
import numpy as np
import pandas as pd
from keras.datasets import mnist
from keras.models import Sequential
from
keras.layers import Dense from keras.layers import Dropout from keras.layers import Flatten from keras.layers.convolutional import Convolution2D from keras.layers.convolutional import MaxPooling2D from keras.utils import np_utils import matplotlib.pyplot as plt from keras.constraints import maxnorm from
keras.optimizers import SGD from keras.layers.core import Reshape import matplotlib.pyplot as plt %matplotlib inline

檢視資料

data = pd.read_csv('train.csv')

print('data({0[0]},{0[1]})'.format(data.shape))
print(data.head())

這裡寫圖片描述

#切片得到畫素,不包括標籤。
images = data.iloc[:, 1:].values
images = images.astype(np.float
) #歸一化 images = np.multiply(images, 1.0 / 255.0) print('images({0[0]},{0[1]})'.format(images.shape)) image_size = images.shape[1] # in this case all images are square image_width = image_height = np.ceil(np.sqrt(image_size)).astype(np.uint8) >>>images(42000,784)
#獲得標籤
labels = data.iloc[:,0].values
#轉化為one-hot vectors,這在分類中很有好處
labels = np_utils.to_categorical(labels)
labels_count = labels.shape[1]
#顯示出該向量。
plt.title(labels[9])
plt.plot(labels[9])
plt.xticks(range(10));

這裡寫圖片描述
劃分資料,把訓練集分為訓練和驗證集,驗證集可以來評估模型。

#split data into training & validation
validation_images = images[:2000]
validation_labels = labels[:2000]

train_images = images[2000:]
train_labels = labels[2000:]

print('train_images({0[0]},{0[1]})'.format(train_images.shape))
print('validation_images({0[0]},{0[1]})'.format(validation_images.shape))
>>>train_images(40000,784)
>>>validation_images(2000,784)

然後就是搭建模型了。

def larger_model():
    # create model
    model = Sequential()
    #把原來的784,變為28*28*1,變成影象後容易獲取特徵。
    model.add(Reshape((28, 28, 1), input_shape=(784, )))
    model.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(28, 28, 1), activation='relu'))  
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.4))
    model.add(Convolution2D(15, 3, 3, activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.4))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(50, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(labels_count, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

# build the model
model = larger_model()
# Fit the model
model.fit(train_images, train_labels, validation_data=(validation_images, validation_labels), nb_epoch=200, batch_size=200, verbose=2)

這裡只展示了前幾個epoch
這裡寫圖片描述

最後一步就是將測試資料放入模型中訓練。並存儲

#testa
test_images = pd.read_csv('test.csv').values
test_images = test_images.astype(np.float)
test_images = np.multiply(test_images, 1.0 / 255.0)
predictions = model.predict_classes(test_images, verbose=0)
submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)),
                         "Label": predictions})
submissions.to_csv("DR.csv", index=False, header=True)

與上一篇tensorflow相比,keras將更多的操作封裝了,大大地減少了程式碼量。還可以將更多的方法運用到這裡,比如BN層,或者是可以多加入卷積層或者是fc層。等比較好後再來更新。