LoginSignup
14
20

More than 5 years have passed since last update.

Deep LearningライブラリKerasとKeras.jsの紹介

Last updated at Posted at 2016-10-25

Kerasとは?

公式ホームページから引用すると

Kerasは最小限で記述できる,モジュール構造に対応しているニューラルネットワークのライブラリです。Pythonによって記述されており、TensorflowやTheanoに対応しています。
革新的な研究、開発を行うためにはアイデアから結果まで最小限の時間で行うことが求められます。そこでKerasはより早い実装、改良を行うことを目的として開発されました。

「アイデアから結果まで最小限の時間で行う」というのがポイントでしょうか。直感的にわかりやすいコードでDeep Learningの構造を記述できる。

Kerasの基本

逐次モデル(Sequential)

Kerasでは2種類の方法でDeep Learningの構造を記述できます。SequentialとFunctional APIの2つです。Sequentialはシンプルにレイヤーを重ねていくような構造を書くのに適してると思う。
https://keras.io/ja/#30keras にあるコードですが

from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])

のようにレイヤーを書ける。addを使っても書ける。

model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))

model.add(Dropout(0.5))のようにDrop outも簡単に書ける。

Functional API(Model)

単純なレイヤー以外に複数の出力、有向非巡回グラフなど複雑なモデルを構築するときに使う。これまた公式リファレンスにありますがhttps://keras.io/ja/getting-started/functional-api-guide/

from keras.layers import Input, Dense
from keras.models import Model

inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(32, activation='relu')(inputs)
predictions = Dense(10, activation='softmax')(x)

# this creates a model that includes
# the Input layer and three Dense layers
model = Model(input=inputs, output=predictions)

compile, fit

どちらの方法でもモデル構築後にcompileで各種設定をして、fitで学習できる。
compleではロス関数や最適化手法を設定し、fitでエポック数、バッチサイズを指定する。

model.compile(loss='categorical_crossentropy’, optimizer=sgd, metrics=['accuracy'])
model.fit(X_train, y_train, nb_epoch=20, batch_size=16)

Keras.js

KerasをJavascriptから動かせるようにしたライブラリ
動作を試してみるだけなら下記URLから実行可能
https://transcranial.github.io/keras-js/

Keras.jsを動かしてみる

まずはGitからClone
git clone https://github.com/transcranial/keras-js
次にNode.jsなどで使うnpmでインストールとサーバー起動
npm install
npm run server

http://127.0.0.1:3000 にアクセスすると自分の環境でブラウザから使える。Chromeでは動きました。

14
20
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
14
20