Skip to content

Instantly share code, notes, and snippets.

@antimon2
Last active July 12, 2019 12:10
Show Gist options
  • Save antimon2/2939e75f2620112ac0525c677beab41c to your computer and use it in GitHub Desktop.
Save antimon2/2939e75f2620112ac0525c677beab41c to your computer and use it in GitHub Desktop.
FashionMNIST_Sample
Display the source blob
Display the rendered blob
Raw
Loading
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
Loading
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
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:39:36.684090Z",
"end_time": "2019-07-12T09:39:36.695312Z"
},
"trusted": true
},
"cell_type": "code",
"source": "import sys\nsys.version_info",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "sys.version_info(major=3, minor=6, micro=3, releaselevel='final', serial=0)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:40:38.267560Z",
"end_time": "2019-07-12T09:40:40.983625Z"
},
"trusted": true
},
"cell_type": "code",
"source": "import tensorflow as tf\ntf.__version__",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "'1.14.0'"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:40:46.437767Z",
"end_time": "2019-07-12T09:40:46.441584Z"
},
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\nimport pandas as pd\n# from sklearn.datasets import load_iris\nnp.random.seed(123)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:40:49.469839Z",
"end_time": "2019-07-12T09:40:49.886259Z"
},
"trusted": true
},
"cell_type": "code",
"source": "fashion_mnist = tf.keras.datasets.fashion_mnist\n(xtrn, ytrn), (xtst, ytst) = fashion_mnist.load_data()",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:40:52.843852Z",
"end_time": "2019-07-12T09:40:52.848248Z"
},
"trusted": true
},
"cell_type": "code",
"source": "xtrn = xtrn.reshape(-1, 28, 28, 1)\nxtst = xtst.reshape(-1, 28, 28, 1)",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:40:54.910136Z",
"end_time": "2019-07-12T09:40:54.915778Z"
},
"trusted": true
},
"cell_type": "code",
"source": "[np.shape(data) for data in (xtrn, ytrn, xtst, ytst)]",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "[(60000, 28, 28, 1), (60000,), (10000, 28, 28, 1), (10000,)]"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:40:57.518643Z",
"end_time": "2019-07-12T09:40:57.522935Z"
},
"trusted": true
},
"cell_type": "code",
"source": "batch_size = 1000",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:41:10.657509Z",
"end_time": "2019-07-12T09:41:10.887428Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Specify the model\nmodel = tf.keras.models.Sequential([\n tf.keras.layers.Conv2D(20, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)),\n tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n tf.keras.layers.Conv2D(50, kernel_size=(5, 5), activation='relu'),\n tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n tf.keras.layers.Flatten(),\n tf.keras.layers.Dense(500, activation = \"relu\"),\n tf.keras.layers.Dense(10, activation = \"softmax\")\n])",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "WARNING: Logging before flag parsing goes to stderr.\nW0712 18:41:10.661669 140735824790400 deprecation.py:506] From /Users/antimon2/.pyenv/versions/3.6.3/envs/OSCNagoyaDemoPY/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\nInstructions for updating:\nCall initializer instance with the dtype argument instead of passing it to the constructor\n",
"name": "stderr"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:41:11.685645Z",
"end_time": "2019-07-12T09:41:11.691697Z"
},
"trusted": true
},
"cell_type": "code",
"source": "model.summary()",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "Model: \"sequential\"\n_________________________________________________________________\nLayer (type) Output Shape Param # \n=================================================================\nconv2d (Conv2D) (None, 24, 24, 20) 520 \n_________________________________________________________________\nmax_pooling2d (MaxPooling2D) (None, 12, 12, 20) 0 \n_________________________________________________________________\nconv2d_1 (Conv2D) (None, 8, 8, 50) 25050 \n_________________________________________________________________\nmax_pooling2d_1 (MaxPooling2 (None, 4, 4, 50) 0 \n_________________________________________________________________\nflatten (Flatten) (None, 800) 0 \n_________________________________________________________________\ndense (Dense) (None, 500) 400500 \n_________________________________________________________________\ndense_1 (Dense) (None, 10) 5010 \n=================================================================\nTotal params: 431,080\nTrainable params: 431,080\nNon-trainable params: 0\n_________________________________________________________________\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:41:14.914450Z",
"end_time": "2019-07-12T09:41:15.023231Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Specify the loss, optimizers and metrics for model training\nmodel.compile(\n optimizer = \"adam\",\n loss = \"sparse_categorical_crossentropy\",\n metrics = [\"accuracy\"]\n)",
"execution_count": 10,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:41:38.695407Z",
"end_time": "2019-07-12T09:43:21.232211Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Train the model for 100 epochs\ntrained = model.fit(xtrn, ytrn, validation_data = (xtst, ytst), batch_size = batch_size, shuffle = True, epochs = 5)",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "Train on 60000 samples, validate on 10000 samples\nEpoch 1/5\n60000/60000 [==============================] - 20s 337us/sample - loss: 4.0824 - acc: 0.6297 - val_loss: 0.5741 - val_acc: 0.7883\nEpoch 2/5\n60000/60000 [==============================] - 20s 339us/sample - loss: 0.4985 - acc: 0.8167 - val_loss: 0.4760 - val_acc: 0.8302\nEpoch 3/5\n60000/60000 [==============================] - 21s 342us/sample - loss: 0.4187 - acc: 0.8467 - val_loss: 0.4326 - val_acc: 0.8460\nEpoch 4/5\n60000/60000 [==============================] - 20s 340us/sample - loss: 0.3729 - acc: 0.8636 - val_loss: 0.4143 - val_acc: 0.8516\nEpoch 5/5\n60000/60000 [==============================] - 21s 343us/sample - loss: 0.3433 - acc: 0.8730 - val_loss: 0.4033 - val_acc: 0.8570\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:43:33.075147Z",
"end_time": "2019-07-12T09:43:33.086230Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Save loss and accuracy to csv for visualization\npd.DataFrame(trained.history).to_csv(\"fmnist-error-tf.csv\")",
"execution_count": 12,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:43:36.907973Z",
"end_time": "2019-07-12T09:43:45.269614Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Predict Species\ntrn_yidx = np.argmax(model.predict(xtrn), axis = 1)\ntst_yidx = np.argmax(model.predict(xtst), axis = 1)",
"execution_count": 13,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:43:46.982132Z",
"end_time": "2019-07-12T09:43:47.207119Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Check the accuracy of the model\nsum(trn_yidx == ytrn) / ytrn.shape[0]",
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 14,
"data": {
"text/plain": "0.8840833333333333"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:43:48.570132Z",
"end_time": "2019-07-12T09:43:48.631268Z"
},
"trusted": true
},
"cell_type": "code",
"source": "sum(tst_yidx == ytst) / ytst.shape[0]",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "0.857"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:43:51.737565Z",
"end_time": "2019-07-12T09:44:00.565563Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# or \nmodel.evaluate(xtrn, ytrn)",
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"text": "60000/60000 [==============================] - 9s 147us/sample - loss: 0.3157 - acc: 0.8841\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "[0.3156977436264356, 0.88408333]"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-07-12T09:44:01.968103Z",
"end_time": "2019-07-12T09:44:03.543113Z"
},
"trusted": true
},
"cell_type": "code",
"source": "model.evaluate(xtst, ytst)",
"execution_count": 17,
"outputs": [
{
"output_type": "stream",
"text": "10000/10000 [==============================] - 2s 157us/sample - loss: 0.4033 - acc: 0.8570\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "[0.40326199498176574, 0.857]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "tensorflow1_14",
"display_name": "TF1.14 for OSC2019Nagoya (Python 3)",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "FashionMNIST_Sample",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment