标题介绍运行环境了win7
看网上好多keras识别minist 但是一般由于版本问题,无法直接用,,,这里还要特别感谢keras中文文档作者(三当家SCP)。教程整的非常好。还有就是最好你在安装anaconda 之前把原来安装过的PY卸载掉,要不然安装mingw的时候会出问题,,,安装就不详细介绍了网上有很多种----大致流程——anaconda-mingw-theano(注意环境变量,系统变量啥的)-keras。
下边附上一个可用程序哈,亲测可用。。。并附上数据,数据来源于网络,见文章底部,你就不用运行的时候在下载数据,这样很容易出错的。非GPU环境 win7 64bit
好的:其实重点是新的语法的问题。。。。。。。。。。。。。。。。。。。更新地方已标红。。。。。。。
1 from __future__ import absolute_import 2 from __future__ import print_function 3 import numpy as np 4 np.random.seed(1337) # for reproducibility 5 import cPickle as pickle 6 7 from keras.models import Sequential 8 from keras.layers.core import Dense, Dropout, Activation 9 from keras.optimizers import SGD, Adam, RMSprop 10 from keras.utils import np_utils 11 12 ''' 13 Train a simple deep NN on the MNIST dataset. 14 Get to 98.30% test accuracy after 20 epochs (there is *a lot* of margin for parameter tuning). 15 2 seconds per epoch on a GRID K520 GPU. 16 ''' 17 18 batch_size = 128 19 nb_classes = 10 20 nb_epoch = 10 21 22 23 def read_data(data_file): 24 import gzip 25 f = gzip.open(data_file, "rb") 26 train, val, test = pickle.load(f) 27 f.close() 28 train_x = train[0] 29 train_y = train[1] 30 test_x = test[0] 31 test_y = test[1] 32 return train_x, train_y, test_x, test_y 33 34 35 # the data, shuffled and split between tran and test sets 36 #(X_train, y_train), (X_test, y_test) = mnist.load_data() 37 train_x, train_y, test_x, test_y = read_data("C:UsersPC.spyder2mnist.pkl.gz") 38 X_train = train_x 39 X_test = test_x 40 X_train = X_train.astype("float32") 41 X_test = X_test.astype("float32") 42 X_train /= 255 43 X_test /= 255 44 print(X_train.shape[0], 'train samples') 45 print(X_test.shape[0], 'test samples') 46 47 # convert class vectors to binary class matrices 48 Y_train = np_utils.to_categorical(train_y, nb_classes) 49 Y_test = np_utils.to_categorical(test_y, nb_classes) 50 51 model = Sequential() 52 model.add(Dense(input_dim=784, output_dim=128)) 53 model.add(Activation('relu')) 54 model.add(Dropout(0.2)) 55 model.add(Dense(output_dim=128)) 56 model.add(Activation('relu')) 57 model.add(Dropout(0.2)) 58 model.add(Dense(output_dim=10)) 59 model.add(Activation('softmax')) 60 61 rms = RMSprop() 62 model.compile(loss='categorical_crossentropy', optimizer=rms,metrics=['accuracy']) 63 64 model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch) 65 score = model.evaluate(X_test, Y_test, batch_size=batch_size) 66 print('Test score:', score[0]) 67 print('Test accuracy:', score[1])
数据在这里:http://www.cnblogs.com/xueliangliu/archive/2013/04/03/2997437.html。。。由于没法上传有15兆,我看这个大牛有这个数据,而且还附带了安装及原理等,自己看吧。。。。。。。