{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"96a167c2-050b-230b-8c70-745ee9acbbc3"},"source":""},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"a908c4d5-3cd4-75c9-4998-ceae887b4be1"},"outputs":[],"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"18b556d6-9f78-42c0-5b89-1cd284a55499"},"outputs":[],"source":"from keras.layers import Input, Dense"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"bcb60cbb-7b9d-31c5-d2fd-5a0630932a04"},"outputs":[],"source":"from keras.models import Model"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ec37362f-6685-fdb7-955a-93f32ba82d6d"},"outputs":[],"source":"inputs = Input(shape=(784, ))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6e57abf4-bbd6-aa29-d9f0-8f0e5395fc96"},"outputs":[],"source":"x = Dense(512, activation='relu')(inputs)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"50518472-3f9d-ca3d-2655-5d3e821020ab"},"outputs":[],"source":"outputs = Dense(10, activation='softmax')(x)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"1d15a32a-6044-a0df-91cf-926152c13ec5"},"outputs":[],"source":"model = Model(input=inputs, output=outputs)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"4c22a5d4-ebc5-09bd-15be-45443d9a17ad"},"outputs":[],"source":"model.summary()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"60f5b6ec-36d1-79d2-34ee-02900f778197"},"outputs":[],"source":"# ==== デフォルト設定で使う場合 ====\n# optimizerに全て小文字のストリングを渡すだけ（超カンタン！）\n# 例えば以下のような設定が使える\n# optimizer='sgd'\n# optimizer='adadelta'\n# optimizer='adam'\n# optimizer='rmsprop\n\n# ==== 好みに合わせて設定を変えたい場合 ====\n# まず、最適化手法のクラスを読み込む\nfrom keras.optimizers import SGD, Adadelta, Adam, RMSprop\n# それらをインスタンス化してからoptimizer引数に渡す\n# 例）　SGD with Nesterov momentum を用いる場合\nsgd_nesterov=SGD(lr=0.01, momentum=0.9, nesterov=True)\n\n# モデルのコンパイル\nmodel.compile(loss='categorical_crossentropy', # 損失関数（この量のパラメータ勾配で学習する）\n              optimizer='sgd', # 最適化手法（デフォルト設定）\n              #optimizer='rmsprop', # 最適化手法（デフォルト設定）\n              #optimizer=sgd_nesterov, # 最適化手法（お好み設定）\n              metrics=['accuracy'] # 評価指標\n             )"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6b242615-29e9-5e91-0b1b-e560a416d724"},"outputs":[],"source":"from keras.datasets import mnist"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"8b9f53ae-23a0-f2d5-34fe-d70201d8bdc9"},"outputs":[],"source":"# 手書き文字データセット（MNIST）の読み込み\n(X_train, y_train), (X_test, y_test) = mnist.load_data()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"5f81c17c-8ee7-b72b-f460-46b534c6d54c"},"outputs":[],"source":"# X_trainを見てみる\nX_train"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d6ff8df8-1d65-be1f-71dc-25f408d72cc1"},"outputs":[],"source":"# 可視化用ライブラリの読み込み\n%matplotlib inline\nimport matplotlib.pyplot as plt"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6cacab81-19c5-05e8-7aba-c22c1d8bcd02"},"outputs":[],"source":"# 入力データを可視化（最初の５文字）\nfig, ax = plt.subplots(1, 5)\n\nfor ii in range(5):\n    ax[ii].imshow(X_train[ii], cmap='gray')\n    ax[ii].axis('off')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d71cb9e5-138f-4618-ba10-bee63cdb6ce6"},"outputs":[],"source":"X_train = X_train.reshape(60000, 784)\nX_test = X_test.reshape(10000, 784)\nX_train = X_train.astype('float32') / 255.\nX_test = X_test.astype('float32') / 255.\nprint(X_train.shape[0], 'train samples')\nprint(X_test.shape[0], 'test samples')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"21b6384c-92ed-c566-54b4-4c4e1c7669b6"},"outputs":[],"source":"from keras.utils import np_utils"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"93f10d25-9a66-2dd1-446e-29c4e9151e2c"},"outputs":[],"source":"nb_classes = 10"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"774e7e3c-9b86-1372-ced3-61423e772e60"},"outputs":[],"source":"# convert class vectors to binary class matrices\nY_train = np_utils.to_categorical(y_train, nb_classes)\nY_test = np_utils.to_categorical(y_test, nb_classes)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"f5d1b75e-1213-6cb2-726e-ee8b2d54024a"},"outputs":[],"source":"Y_train[:5]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6a6d8645-aebe-473e-468e-fe106bcdd8b2"},"outputs":[],"source":"y_train[:5]"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"16e774fb-3789-5bd8-9472-c880604664e7"},"outputs":[],"source":"import time"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"301d167e-9cca-989f-1287-e263e38d10fe"},"outputs":[],"source":"tic = time.time()\n\n# 学習のメインパート。\n# 学習途中の損失関数の値などはhistory.historyに保存されている。\nhistory = model.fit(X_train, Y_train,\n                    batch_size=128, \n                    nb_epoch=20,\n                    verbose=0, \n                    validation_data=(X_test, Y_test))\n\ntoc = time.time()\n\n# 学習にかかった時間を表示\nprint(\"Execution time: {0:.2f} [sec]\".format(toc - tic))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"33ece450-657b-3629-6872-c661556ba982"},"outputs":[],"source":"# テストデータに対する評価値\nscore = model.evaluate(X_test, Y_test, verbose=0)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c6ef812f-97a9-ca37-4291-5153a4ee7f05"},"outputs":[],"source":"print('Test score:', score[0]) # 損失関数の値\nprint('Test accuracy:', score[1]) # 精度"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"7f99a8e5-31c0-4e24-34fa-1ff59a439333"},"outputs":[],"source":"# 学習曲線\nfig, ax = plt.subplots(1, 2, figsize=(12, 6))\nax[0].set_title('Training performance (Loss)')\nax[0].plot(history.epoch, history.history['loss'], label='loss')\nax[0].plot(history.epoch, history.history['val_loss'], label='val_loss')\nax[0].set(xlabel='Epoch', ylabel='Loss')\nax[0].legend()\n\nax[1].set_title('Training performance (Accuracy)')\nax[1].plot(history.epoch, history.history['acc'], label='acc')\nax[1].plot(history.epoch, history.history['val_acc'], label='val_acc')\nax[1].set(xlabel='Epoch', ylabel='Accuracy')\nax[1].legend(loc='best')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9490b60f-feca-09f5-8ea5-d0e0d94cd528"},"outputs":[],"source":"# 予測値\nY_test_pred = model.predict(X_test)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"92393b42-26b7-1fbd-81b2-2cd1734e9c39"},"outputs":[],"source":"# 予測の形\nY_test_pred.shape"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"77d219cd-db34-94ab-3e13-1be24b186f2f"},"outputs":[],"source":"# 予測の可視化\nplt.imshow(Y_test_pred[:10], cmap='gray', interpolation='nearest', vmin=0, vmax=1)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2a525198-545e-f15f-bce7-7ef25e1c3f3a"},"outputs":[],"source":"# 入力データを可視化（最初の10文字）\nfig, ax = plt.subplots(1, 10, figsize=(10, 2))\n\nfor ii in range(10):\n    ax[ii].imshow(X_test[ii].reshape(28, 28), cmap='gray')\n    ax[ii].axis('off')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6150baae-7b82-35dd-12f7-1aac60c5265b"},"outputs":[],"source":"plt.imshow(Y_test_pred[:40], cmap='gray', interpolation='nearest', vmin=0, vmax=1)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"62292481-0704-dc1f-6b0c-2968ae7d5b60"},"outputs":[],"source":"# 予測の可視化\nplt.imshow(Y_test_pred[30:40], cmap='gray', interpolation='nearest', vmin=0, vmax=1)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"83ab1e66-31c4-774a-dcc0-4be43f6b5b47"},"outputs":[],"source":"# 入力データを可視化（30~39文字目までの10文字）\nfig, ax = plt.subplots(1, 10, figsize=(10, 2))\n\nfor ii in range(10):\n    ax[ii].imshow(X_test[ii+30].reshape(28, 28), cmap='gray')\n    ax[ii].axis('off')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b6b128d4-3ebd-2575-6668-d166638715d8"},"outputs":[],"source":""}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.5.2"}},"nbformat":4,"nbformat_minor":0}