Skip to content

Instantly share code, notes, and snippets.

@yampy
Created September 9, 2017 16:44
Show Gist options
  • Save yampy/8bf645b893b64e91aeb87372a1d7a5f3 to your computer and use it in GitHub Desktop.
Save yampy/8bf645b893b64e91aeb87372a1d7a5f3 to your computer and use it in GitHub Desktop.
Keras/Tensorflowで始めるディープラーニングの基礎 ref: http://qiita.com/yampy/items/975f28594f295702ef18
pip install --upgrade tensorflow
pip install keras
from keras.models import Sequential
from keras.layers import Dense, Activation
# モデルの作成
model = Sequential()
# モデルにレイヤーを積み上げていく
model.add(Dense(units=64, input_dim=100))
model.add(Activation('relu'))
model.add(Dense(units=10))
model.add(Activation('softmax'))
# 訓練プロセスの定義
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
# 訓練の実行
# (x_train, y_trainはNumpy行列の学習データ)
model.fit(x_train, y_train, epochs=5, batch_size=32)
# 予測の実行
classes = model.predict(x_test, batch_size=128)
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
59904/60000 [============================>.] - ETA: 0s - loss: 0.3267 - acc: 0.9009Epoch 00000: saving model to ./out/checkpoints/weights.00-0.08.hdf5
60000/60000 [==============================] - 135s - loss: 0.3263 - acc: 0.9011 - val_loss: 0.0785 - val_acc: 0.9749
Epoch 2/10
59904/60000 [============================>.] - ETA: 0s - loss: 0.1132 - acc: 0.9671Epoch 00001: saving model to ./out/checkpoints/weights.01-0.06.hdf5
60000/60000 [==============================] - 131s - loss: 0.1131 - acc: 0.9671 - val_loss: 0.0580 - val_acc: 0.9811
Epoch 3/10
59904/60000 [============================>.] - ETA: 0s - loss: 0.0871 - acc: 0.9735Epoch 00002: saving model to ./out/checkpoints/weights.02-0.05.hdf5
60000/60000 [==============================] - 135s - loss: 0.0870 - acc: 0.9735 - val_loss: 0.0469 - val_acc: 0.9850
(中略)
Epoch 10/10
59904/60000 [============================>.] - ETA: 0s - loss: 0.0410 - acc: 0.9876Epoch 00009: saving model to ./out/checkpoints/weights.09-0.03.hdf5
60000/60000 [==============================] - 140s - loss: 0.0410 - acc: 0.9876 - val_loss: 0.0295 - val_acc: 0.9902
tensorboard --logdir=./out/
from keras.layers import Input, Dense
from keras.models import Model
# インプットの定義
inputs = Input(shape=(784,))
# レイヤーの定義
nw = Dense(64, activation='relu')(inputs)
nw = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# モデルの定義(インプットとレイヤーを指定)
model = Model(inputs=inputs, outputs=predictions)
# 訓練プロセスの定義
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# 訓練の実行
model.fit(data, labels)
# 予測の実行
classes = model.predict(x_test, batch_size=128)
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
60000/60000 [==============================] - 7s - loss: 0.3319 - acc: 0.8974 - val_loss: 0.1267 - val_acc: 0.9607
Epoch 2/20
60000/60000 [==============================] - 6s - loss: 0.1621 - acc: 0.9524 - val_loss: 0.0921 - val_acc: 0.9733
Epoch 3/20
60000/60000 [==============================] - 6s - loss: 0.1318 - acc: 0.9610 - val_loss: 0.0874 - val_acc: 0.9750
(中略)
Epoch 20/20
60000/60000 [==============================] - 6s - loss: 0.0722 - acc: 0.9823 - val_loss: 0.1083 - val_acc: 0.9817
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment