用Keras跑tensorflow

在上一篇用tflearn來做深度學習辨識初音玩了一下tflearn
後來又去看了幾個當紅的深度學習套件,tensorflow做為低層運算的API,上層除了tflearn之外
Keras這個套件也能用tensorflow為基底去運作

參考:
https://keras.io/
http://tjo.hatenablog.com/entry/2016/06/09/190000 (日文 請注意)

而Keras在社群上活躍程度又比tflearn更高,姑且去了解一下他的用法

環境設定

1.把ANACONDA裝起來

https://www.continuum.io/downloads

2.安裝tensorflow
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.1-py2-none-any.whl
$ pip install  $TF_BINARY_URL
3.安裝keras
pip install keras
4.測試環境
$ python

>>> import keras
Using TensorFlow backend.

這邊可以看到keras用了TensorFlow當作他的運算基底
如果他不是用tensorflow而是用Theano作為基底的話,可以去改個人config

$ vim ~/.keras/keras.json

確定backend的設定是Theano或tensorflow,改成自己想要的

{
    "image_dim_ordering": "tf",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}
5.測試keras

首先上網找了keras CNN的範例來跑跑看
keras + tensorflow,用tensorboard看結果

參考:
https://keras.io/callbacks/#tensorboard
http://qiita.com/supersaiakujin/items/568605f999ef5cc741be (日文 請注意)

import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
import keras.callbacks
import keras.backend.tensorflow_backend as KTF
import tensorflow as tf

batch_size = 128
nb_classes = 10
nb_epoch   = 20
nb_data    = 28*28
log_filepath = '/tmp/keras_log'

# load data

(X_train, y_train), (X_test, y_test) = mnist.load_data()

# reshape

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])

# rescale

X_train = X_train.astype(np.float32)
X_train /= 255

X_test = X_test.astype(np.float32)
X_test /= 255

# convert class vectors to binary class matrices (one hot vectors)

Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

old_session = KTF.get_session()

with tf.Graph().as_default():
    session = tf.Session('')
    KTF.set_session(session)
    KTF.set_learning_phase(1)
    # build model

    model = Sequential()
    model.add(Dense(512, input_shape=(nb_data,), init='normal',name='dense1'))
    model.add(Activation('relu', name='relu1'))
    model.add(Dropout(0.2, name='dropout1'))
    model.add(Dense(512, init='normal', name='dense2'))
    model.add(Activation('relu', name='relu2'))
    model.add(Dropout(0.2, name='dropout2'))
    model.add(Dense(10, init='normal', name='dense3'))
    model.add(Activation('softmax', name='softmax1'))
    model.summary()

    model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.001), metrics=['accuracy'])

    tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, histogram_freq=1)
    cbks = [tb_cb]

    history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch = nb_epoch, verbose=1, callbacks=cbks)

    score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy;', score[1])


KTF.set_session(old_session)

Output:

...
('Test score:', 0.44597396918535231)
('Test accuracy;', 0.86890000000000001)
[Finished in 242.44s]

上面設定了輸出tensorflow log到/tmp/keras_log,可以用以下指令打開瀏覽器

$ tensorboard --logdir=/tmp/keras_log

跑出來了,看來效果還OK
之後再來玩玩其他範例

comments powered by Disqus