'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
print('________test samples', x_test.shape)
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, (3, 3),
input_shape=(28, 28, 1)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(200))
model.add(Activation('relu'))
model.add(Dense(200))
model.add(Activation('relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
# model.save("models/mnist")
model.load_weights("models/mnist")
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
訓(xùn)練 keras 模型進(jìn)行mnist分類
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門歪架,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人霹陡,你說我怎么就攤上這事牡拇。” “怎么了穆律?”我有些...
- 文/不壞的土叔 我叫張陵惠呼,是天一觀的道長。 經(jīng)常有香客問我峦耘,道長剔蹋,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任辅髓,我火速辦了婚禮泣崩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘洛口。我一直安慰自己矫付,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布第焰。 她就那樣靜靜地躺著买优,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挺举。 梳的紋絲不亂的頭發(fā)上杀赢,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼汇歹!你這毒婦竟也來了屁擅?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布咪奖,位于F島的核電站,受9級特大地震影響酱床,放射性物質(zhì)發(fā)生泄漏羊赵。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一扇谣、第九天 我趴在偏房一處隱蔽的房頂上張望昧捷。 院中可真熱鬧,春花似錦罐寨、人聲如沸靡挥。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽跋破。三九已至,卻和暖如春楞慈,著一層夾襖步出監(jiān)牢的瞬間幔烛,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- Caffe自帶的代碼中包含了一個用LeNet訓(xùn)練mnist手寫字模型的例子弟劲,網(wǎng)上的說明很多,可以參考:https:...
- 跑著庸追,跑著,你就成功了台囱。 這個是簡書作者軍范律政系列日更文章的名稱淡溯。 我忽然也想寫寫我的早起和跑步。 現(xiàn)在我早上定...
- 好久沒有寫文章了,新年第一篇! 新的開始强品,重新開始膘侮,新的起點! 抓住必須要學(xué)的,專業(yè)知識的榛,相關(guān)軟件琼了,語言(普通話和...