From 97bb3ed11ebee4bf0edafbcc27e7c7be34dfdfcd Mon Sep 17 00:00:00 2001 From: Hiroki Ito Date: Fri, 16 Aug 2019 07:50:33 +0900 Subject: [PATCH] =?UTF-8?q?Colaboratory=20=E3=82=92=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E3=81=97=E3=81=A6=E4=BD=9C=E6=88=90=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- keras_tf_sample.ipynb | 301 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 301 insertions(+) create mode 100644 keras_tf_sample.ipynb diff --git a/keras_tf_sample.ipynb b/keras_tf_sample.ipynb new file mode 100644 index 0000000..ed9d440 --- /dev/null +++ b/keras_tf_sample.ipynb @@ -0,0 +1,301 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "keras-tf-sample.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zy5ichg1VA97", + "colab_type": "text" + }, + "source": [ + "以下のサイトを参考にしました。\n", + "- 「Keras / Tensorflowで始めるディープラーニング入門」 https://qiita.com/yampy/items/706d44417c433e68db0d" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7jRl9looUSZ9", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "b0c3cd80-32ac-413d-d585-b103ddadcda7" + }, + "source": [ + "import keras\n", + "from keras.datasets import mnist\n", + "from keras.models import Sequential\n", + "from keras.layers import Dense, Dropout, Flatten\n", + "from keras.layers import Conv2D, MaxPooling2D\n", + "from keras import backend as K" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Using TensorFlow backend.\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7tTXTQn0UyGi", + "colab_type": "code", + "colab": {} + }, + "source": [ + "batch_size = 128\n", + "num_classes = 10\n", + "epochs = 3\n", + "\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "trCXc0XGU1xt", + "colab_type": "code", + "colab": {} + }, + "source": [ + "img_rows, img_cols = 28, 28\n", + "\n", + "(x_train, y_train), (x_test, y_test) = mnist.load_data()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "9z32qSQqU9Px", + "colab_type": "code", + "colab": {} + }, + "source": [ + "#Kerasのバックエンドで動くTensorFlowとTheanoでは入力チャンネルの順番が違うので場合分けして書いています\n", + "if K.image_data_format() == 'channels_first':\n", + " x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)\n", + " x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)\n", + " input_shape = (1, img_rows, img_cols)\n", + "else:\n", + " x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n", + " x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n", + " input_shape = (img_rows, img_cols, 1)\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "du1XCGbBVECT", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "aadeacb2-91e6-4e05-d707-b9ece60eaef0" + }, + "source": [ + "x_train = x_train.astype('float32')\n", + "x_test = x_test.astype('float32')\n", + "x_train /= 255\n", + "x_test /= 255\n", + "print('x_train shape:', x_train.shape)\n", + "print(x_train.shape[0], 'train samples')\n", + "print(x_test.shape[0], 'test samples')" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "x_train shape: (60000, 28, 28, 1)\n", + "60000 train samples\n", + "10000 test samples\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "AqnRhJyoVrjO", + "colab_type": "code", + "colab": {} + }, + "source": [ + "y_train = y_train.astype('int32')\n", + "y_test = y_test.astype('int32')\n", + "y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)\n", + "y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "CSklWGNjVvOR", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 315 + }, + "outputId": "ce8a8390-6d52-4e43-b0aa-6ab33ce0470c" + }, + "source": [ + "model = Sequential()\n", + "model.add(Conv2D(32, kernel_size=(3, 3),\n", + " activation='relu',\n", + " input_shape=input_shape))\n", + "model.add(Conv2D(64, (3, 3), activation='relu'))\n", + "model.add(MaxPooling2D(pool_size=(2, 2)))\n", + "model.add(Dropout(0.25))\n", + "model.add(Flatten())\n", + "model.add(Dense(128, activation='relu'))\n", + "model.add(Dropout(0.5))\n", + "model.add(Dense(num_classes, activation='softmax'))" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "WARNING: Logging before flag parsing goes to stderr.\n", + "W0815 22:39:48.780859 139626640222080 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n", + "\n", + "W0815 22:39:48.803259 139626640222080 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n", + "\n", + "W0815 22:39:48.809604 139626640222080 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n", + "\n", + "W0815 22:39:48.843924 139626640222080 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n", + "\n", + "W0815 22:39:48.847448 139626640222080 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.\n", + "\n", + "W0815 22:39:48.860003 139626640222080 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "j_d10YewWISw", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 105 + }, + "outputId": "a3d42703-5820-47ca-e5c1-737362237107" + }, + "source": [ + "model.compile(loss=keras.losses.categorical_crossentropy,\n", + " optimizer=keras.optimizers.Adadelta(),\n", + " metrics=['accuracy'])" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "W0815 22:39:48.964365 139626640222080 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.\n", + "\n", + "W0815 22:39:48.975083 139626640222080 deprecation_wrapper.py:119] From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3295: The name tf.log is deprecated. Please use tf.math.log instead.\n", + "\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "D_Yuht6EWMpx", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + }, + "outputId": "b422c382-6e64-4e24-d720-5528a99dd734" + }, + "source": [ + "model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,\n", + " verbose=1, validation_data=(x_test, y_test))" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "W0815 22:39:49.110717 139626640222080 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_grad.py:1250: add_dispatch_support..wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n", + "Instructions for updating:\n", + "Use tf.where in 2.0, which has the same broadcast rule as np.where\n" + ], + "name": "stderr" + }, + { + "output_type": "stream", + "text": [ + "Train on 60000 samples, validate on 10000 samples\n", + "Epoch 1/3\n", + "60000/60000 [==============================] - 158s 3ms/step - loss: 0.2604 - acc: 0.9192 - val_loss: 0.0632 - val_acc: 0.9808\n", + "Epoch 2/3\n", + "60000/60000 [==============================] - 157s 3ms/step - loss: 0.0910 - acc: 0.9732 - val_loss: 0.0436 - val_acc: 0.9855\n", + "Epoch 3/3\n", + "60000/60000 [==============================] - 157s 3ms/step - loss: 0.0650 - acc: 0.9805 - val_loss: 0.0370 - val_acc: 0.9870\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 9 + } + ] + } + ] +} \ No newline at end of file