import numpy as npimport gzip import struct import keras as ks import logging from keras.layers import Dense, Activation, Flatten, Convolution2D from keras.utils import np_utils def read_data(label_url,image_url): with gzip.open(label_url) as flbl: magic, num = struct.unpack(">II",flbl.read(8)) label = np.fromstring(flbl.read(),dtype=np.int8) with gzip.open(image_url,'rb') as fimg: magic, num, rows, cols = struct.unpack(">IIII",fimg.read(16)) image = np.fromstring(fimg.read(),dtype=np.uint8).reshape(len(label),rows,cols) return (label, image) (train_lbl, train_img) = read_data('mnist/train-labels-idx1-ubyte.gz','mnist/train-images-idx3-ubyte.gz') (val_lbl, val_img) = read_data('mnist/t10k-labels-idx1-ubyte.gz','mnist/t10k-images-idx3-ubyte.gz') def to4d(img): return img.reshape(img.shape[0],784).astype(np.float32)/255 train_img = to4d(train_img) val_img = to4d(val_img) #important train_LBL = np_utils.to_categorical(train_lbl,nb_classes=10) val_LBL = np_utils.to_categorical(val_lbl,nb_classes=10) model = ks.models.Sequential() model.add(Dense(128,input_dim=784)) model.add(Activation('relu')) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dense(10)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy',optimizer='adadelta',metrics=['accuracy']) model.fit(x=train_img,y=train_LBL,batch_size=100,nb_epoch=10,verbose=1,validation_data=(val_img,val_LBL))
源码如上↑
mnist的数据集下载好后保存在mnist的文件夹里,main.py里复制粘贴上面的源码应该就能跑了,main.py要放在mnist文件夹的外边,最重要的就是#important下面的两行,keras似乎必须要对标签分类之后才能使用标签。