anaconda+theano+keras手写字符识别新版

时间:2023-03-09 22:59:19
anaconda+theano+keras手写字符识别新版

标题介绍运行环境了win7

看网上好多keras识别minist 但是一般由于版本问题,无法直接用,,,这里还要特别感谢keras中文文档作者(三当家SCP)。教程整的非常好。还有就是最好你在安装anaconda 之前把原来安装过的PY卸载掉,要不然安装mingw的时候会出问题,,,安装就不详细介绍了网上有很多种----大致流程——anaconda-mingw-theano(注意环境变量,系统变量啥的)-keras。

下边附上一个可用程序哈,亲测可用。。。并附上数据,数据来源于网络,见文章底部,你就不用运行的时候在下载数据,这样很容易出错的。非GPU环境 win7 64bit

好的:其实重点是新的语法的问题。。。。。。。。。。。。。。。。。。。更新地方已标红。。。。。。。

 from __future__ import absolute_import
from __future__ import print_function
import numpy as np
np.random.seed(1337) # for reproducibility
import cPickle as pickle from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD, Adam, RMSprop
from keras.utils import np_utils '''
Train a simple deep NN on the MNIST dataset.
Get to 98.30% test accuracy after 20 epochs (there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a GRID K520 GPU.
''' batch_size = 128
nb_classes = 10
nb_epoch = 10 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 # the data, shuffled and split between tran and test sets
#(X_train, y_train), (X_test, y_test) = mnist.load_data()
train_x, train_y, test_x, test_y = read_data("C:\Users\PC\.spyder2\mnist.pkl.gz")
X_train = train_x
X_test = test_x
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples') # convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(train_y, nb_classes)
Y_test = np_utils.to_categorical(test_y, nb_classes) model = Sequential()
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')) rms = RMSprop()
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)
print('Test score:', score[0])
print('Test accuracy:', score[1])

数据在这里:http://www.cnblogs.com/xueliangliu/archive/2013/04/03/2997437.html。。。由于没法上传有15兆,我看这个大牛有这个数据,而且还附带了安装及原理等,自己看吧。。。。。。。