anaconda+theano+keras手寫字符識別新版


標題介紹運行環境了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:\Users\PC\.spyder2\mnist.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兆,我看這個大牛有這個數據,而且還附帶了安裝及原理等,自己看吧。。。。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM