LoginSignup
8
9

More than 5 years have passed since last update.

TFv1.1でのKerasとの統合を試してみた

Last updated at Posted at 2017-04-04

XLAについて言及がないことで一部ではざわざわしたTensorFlowのv1.1のReleaseですが、ロードマップの通りKerasとのインテグレーションが始まっているようです。まだRCっぽいですが、個人的には目玉の1つであり、試してみたくてうずうずしたのでやってみました。

以前、TensorFlowのHighLevelAPIについての雑感つき解説と題した記事の中で書いたKerasのコードを流用しました。

サンプルコード

import tensorflow as tf
from tensorflow.contrib.keras.python import keras
from sklearn import cross_validation

# データの準備
iris = tf.contrib.learn.datasets.base.load_iris()
train_x, test_x, train_y, test_y = cross_validation.train_test_split(
    iris.data, iris.target, test_size=0.2
)

num_classes = 3
train_y = keras.utils.to_categorical(train_y, num_classes)
test_y = keras.utils.to_categorical(test_y, num_classes)

# モデルの定義
model = Sequential()

# ネットワークの定義
model.add(Dense(10, activation='relu', input_shape=(4,)))
model.add(Dense(20, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(3, activation='softmax'))

# モデルのサマリの確認
model.summary()

# モデルのコンパイル
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

# 学習
history = model.fit(train_x, train_y,
                    batch_size=100,
                    epochs=2000,
                    verbose=1,
                    validation_data=(test_x, test_y))

# 学習モデルの評価
score = model.evaluate(x_test, y_test, verbose=0)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

結果

  • 聞いていた通りtensorflow.contrib.keras.pythonimportさえすればKerasのモジュールにアクセスできるのでサイコーっぽい
  • Estimatorとの合わせ技で、データサイエンティストとエンジニアの分業が捗る未来がぼんやりみえました
  • CloudML Engineももっとつかいやすくなりそうな予感です

さっとしか試していませんので、詳しい考察はまた今度やりたいですね。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9