• 安裝 Keras
    • python3 -m pip install git+git://github.com/fchollet/keras.git –upgrade
  • Keras 的 Hello World 就是處理有名的 MNIST 問題,下面是 sample code:
from keras.models import Sequential 
from keras.layers.core import Dense, Activation
from keras.utils import np_utils
from keras.datasets import mnist
from keras.optimizers import SGD
import numpy

(X_train, y_train), (X_test, y_test) = mnist.load_data()
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1] * X_train.shape[2])
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1] * X_test.shape[2])
Y_train = (numpy.arange(10) == y_train[:, None]).astype(int)
Y_test = (numpy.arange(10) == y_test[:, None]).astype(int)

model = Sequential()
model.add(Dense(output_dim=500, input_dim=28*28))
model.add(Activation('sigmoid'))
model.add(Dense(output_dim=500))
model.add(Activation('sigmoid'))
model.add(Dense(output_dim=10))
model.add(Activation('softmax'))
model.compile(loss='mse', optimizer=SGD(lr=0.1), metrics=['accuracy'])

model.fit(X_train, Y_train, nb_epoch=20, batch_size=100)
score = model.evaluate(X_test, Y_test, batch_size=32)
print('Total loss = ', score[0])
print('Accuracy of testing = ', score[1])
result = model.predict(X_test)
  • 執行狀況: 可看到 accuracy 上升狀況

keras

  • 將 backend 從 default 的 Theano 改為 TensorFlow
    • cd ~/.keras
    • vim keras.json
    • 將 Theano 改為 TensorFlow

theano

tensorflow

  • 重新執行變為 TensorFlow

keras_tensor

Reference

http://keras.io/backend/

http://keras.io/datasets/