Created
September 25, 2023 12:45
-
-
Save nemolize/cac08f682a354c3cee9b642a0653e17a to your computer and use it in GitHub Desktop.
MINIST.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyMpuhENmmbr1Kw+TtCP6KkE", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/nemolize/cac08f682a354c3cee9b642a0653e17a/minist.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"1. **パッケージのインストール:**\n", | |
"最初に必要なpython ライブラリをインストールします。Google Colabでは既にこれらのライブラリはインストールされていますが、他の環境では以下のコマンドによってインストールします。" | |
], | |
"metadata": { | |
"id": "fdeIM1KfRIG3" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!pip install numpy matplotlib tensorflow" | |
], | |
"metadata": { | |
"id": "X61MjgjgRLL8" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"2. **データのインポート:**\n", | |
"次に、MNISTデータセットのインポートと、画像データとラベルデータの分割します。" | |
], | |
"metadata": { | |
"id": "ZK16Gb1nQ3Zn" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "aFxh9wwnQrwl" | |
}, | |
"outputs": [], | |
"source": [ | |
"from tensorflow.keras.datasets import mnist\n", | |
"\n", | |
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"3. **データの前処理:**\n", | |
"MNISTデータセットの画像はグレースケールで、各ピクセルの値は0-255です。\n", | |
"一般的には、この値を0-1の範囲にスケーリング(正規化)します。" | |
], | |
"metadata": { | |
"id": "PbNcAEoMQ75B" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"train_images = train_images / 255.0\n", | |
"test_images = test_images / 255.0" | |
], | |
"metadata": { | |
"id": "goLQKTAFRV85" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"4. **モデルの作成:**\n", | |
"自分でニューラルネットワークの構造を定義します。以下のコードは単純な積層型のニューラルネットワークの例です。\n" | |
], | |
"metadata": { | |
"id": "_d5DSfYNRZy2" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from tensorflow.keras.models import Sequential\n", | |
"from tensorflow.keras.layers import Flatten, Dense\n", | |
"\n", | |
"model = Sequential([\n", | |
" Flatten(input_shape=(28, 28)),\n", | |
" Dense(128, activation='relu'),\n", | |
" Dense(10, activation='softmax')\n", | |
"])" | |
], | |
"metadata": { | |
"id": "k_YA-kJ2Resa" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"5. **コンパイル:**\n", | |
"損失関数、オプティマイザ、評価指標を決めてモデルをコンパイルします。" | |
], | |
"metadata": { | |
"id": "ztBiADpvRf04" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.compile(optimizer='adam',\n", | |
" loss='sparse_categorical_crossentropy',\n", | |
" metrics=['accuracy'])" | |
], | |
"metadata": { | |
"id": "dZJrUN7KRpu6" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"6. **トレーニング:**\n", | |
"モデルを学習させます。この際に、検証用データを指定しておくと、エポックの度に検証用データに対する評価も行われます。" | |
], | |
"metadata": { | |
"id": "MNloV1R6Rt7z" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.fit(train_images, train_labels, epochs=5)" | |
], | |
"metadata": { | |
"id": "C8X6wM0LRvtA" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"7. **評価:**\n", | |
"学習させたモデルの性能を評価します。" | |
], | |
"metadata": { | |
"id": "sxvJWPIZR0MC" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)\n", | |
"\n", | |
"print('\\nTest accuracy:', test_acc)\n", | |
"\n" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "8i5GJhieR4XN", | |
"outputId": "403980f5-a7f0-41f3-e695-002d9376387e" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"313/313 - 0s - loss: 0.0702 - accuracy: 0.9792 - 477ms/epoch - 2ms/step\n", | |
"\n", | |
"Test accuracy: 0.979200005531311\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment