Skip to content

Instantly share code, notes, and snippets.

@SupreethRao99
Created July 28, 2021 13:22
Show Gist options
  • Save SupreethRao99/e53e122883149326e82553a0cf7e6811 to your computer and use it in GitHub Desktop.
Save SupreethRao99/e53e122883149326e82553a0cf7e6811 to your computer and use it in GitHub Desktop.
colab-issue.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "colab-issue.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyMBv/gP4bWURduq6Nh4Pp+B",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"b4e349e1c89a441b9081fe1a054ef2c6": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HBoxModel",
"state": {
"_view_name": "HBoxView",
"_dom_classes": [],
"_model_name": "HBoxModel",
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.5.0",
"box_style": "",
"layout": "IPY_MODEL_96c4f4c731b843a9bde5d26c33dfab5a",
"_model_module": "@jupyter-widgets/controls",
"children": [
"IPY_MODEL_1f5f8355296043d7bb3323c386952c68",
"IPY_MODEL_a6459fa57be149e6bcef10e1fc2cd48c"
]
}
},
"96c4f4c731b843a9bde5d26c33dfab5a": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"1f5f8355296043d7bb3323c386952c68": {
"model_module": "@jupyter-widgets/controls",
"model_name": "FloatProgressModel",
"state": {
"_view_name": "ProgressView",
"style": "IPY_MODEL_e08521c0669040ee8a6464cddecbf1e4",
"_dom_classes": [],
"description": "Dl Completed...: 100%",
"_model_name": "FloatProgressModel",
"bar_style": "success",
"max": 4,
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"value": 4,
"_view_count": null,
"_view_module_version": "1.5.0",
"orientation": "horizontal",
"min": 0,
"description_tooltip": null,
"_model_module": "@jupyter-widgets/controls",
"layout": "IPY_MODEL_8eaf665661c44e8c9f24f17c67267242"
}
},
"a6459fa57be149e6bcef10e1fc2cd48c": {
"model_module": "@jupyter-widgets/controls",
"model_name": "HTMLModel",
"state": {
"_view_name": "HTMLView",
"style": "IPY_MODEL_5b8a226f6fd04b6caf733a0e36f6f72c",
"_dom_classes": [],
"description": "",
"_model_name": "HTMLModel",
"placeholder": "​",
"_view_module": "@jupyter-widgets/controls",
"_model_module_version": "1.5.0",
"value": " 4/4 [00:01<00:00, 3.44 file/s]",
"_view_count": null,
"_view_module_version": "1.5.0",
"description_tooltip": null,
"_model_module": "@jupyter-widgets/controls",
"layout": "IPY_MODEL_25d6f488baf44d6aab4f15ab3c091fd0"
}
},
"e08521c0669040ee8a6464cddecbf1e4": {
"model_module": "@jupyter-widgets/controls",
"model_name": "ProgressStyleModel",
"state": {
"_view_name": "StyleView",
"_model_name": "ProgressStyleModel",
"description_width": "initial",
"_view_module": "@jupyter-widgets/base",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.2.0",
"bar_color": null,
"_model_module": "@jupyter-widgets/controls"
}
},
"8eaf665661c44e8c9f24f17c67267242": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
},
"5b8a226f6fd04b6caf733a0e36f6f72c": {
"model_module": "@jupyter-widgets/controls",
"model_name": "DescriptionStyleModel",
"state": {
"_view_name": "StyleView",
"_model_name": "DescriptionStyleModel",
"description_width": "",
"_view_module": "@jupyter-widgets/base",
"_model_module_version": "1.5.0",
"_view_count": null,
"_view_module_version": "1.2.0",
"_model_module": "@jupyter-widgets/controls"
}
},
"25d6f488baf44d6aab4f15ab3c091fd0": {
"model_module": "@jupyter-widgets/base",
"model_name": "LayoutModel",
"state": {
"_view_name": "LayoutView",
"grid_template_rows": null,
"right": null,
"justify_content": null,
"_view_module": "@jupyter-widgets/base",
"overflow": null,
"_model_module_version": "1.2.0",
"_view_count": null,
"flex_flow": null,
"width": null,
"min_width": null,
"border": null,
"align_items": null,
"bottom": null,
"_model_module": "@jupyter-widgets/base",
"top": null,
"grid_column": null,
"overflow_y": null,
"overflow_x": null,
"grid_auto_flow": null,
"grid_area": null,
"grid_template_columns": null,
"flex": null,
"_model_name": "LayoutModel",
"justify_items": null,
"grid_row": null,
"max_height": null,
"align_content": null,
"visibility": null,
"align_self": null,
"height": null,
"min_height": null,
"padding": null,
"grid_auto_rows": null,
"grid_gap": null,
"max_width": null,
"order": null,
"_view_module_version": "1.2.0",
"grid_template_areas": null,
"object_position": null,
"object_fit": null,
"grid_auto_columns": null,
"margin": null,
"display": null,
"left": null
}
}
}
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/SupreethRao99/e53e122883149326e82553a0cf7e6811/colab-issue.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "HfQEgmDcPqEI"
},
"source": [
"import tensorflow as tf\n",
"import tensorflow_datasets as tfds"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 222,
"referenced_widgets": [
"b4e349e1c89a441b9081fe1a054ef2c6",
"96c4f4c731b843a9bde5d26c33dfab5a",
"1f5f8355296043d7bb3323c386952c68",
"a6459fa57be149e6bcef10e1fc2cd48c",
"e08521c0669040ee8a6464cddecbf1e4",
"8eaf665661c44e8c9f24f17c67267242",
"5b8a226f6fd04b6caf733a0e36f6f72c",
"25d6f488baf44d6aab4f15ab3c091fd0"
]
},
"id": "sYxRYd2lQPIi",
"outputId": "2037d156-66aa-429b-da91-745cb4814f6e"
},
"source": [
"(ds_train, ds_test), ds_info = tfds.load(\n",
" 'mnist',\n",
" split=['train', 'test'],\n",
" shuffle_files=True,\n",
" as_supervised=True,\n",
" with_info=True,\n",
")"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[1mDownloading and preparing dataset mnist/3.0.1 (download: 11.06 MiB, generated: 21.00 MiB, total: 32.06 MiB) to /root/tensorflow_datasets/mnist/3.0.1...\u001b[0m\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"WARNING:absl:Dataset mnist is hosted on GCS. It will automatically be downloaded to your\n",
"local data directory. If you'd instead prefer to read directly from our public\n",
"GCS bucket (recommended if you're running on GCP), you can instead pass\n",
"`try_gcs=True` to `tfds.load` or set `data_dir=gs://tfds-data/datasets`.\n",
"\n"
],
"name": "stderr"
},
{
"output_type": "display_data",
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b4e349e1c89a441b9081fe1a054ef2c6",
"version_minor": 0,
"version_major": 2
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, description='Dl Completed...', max=4.0, style=ProgressStyle(descriptio…"
]
},
"metadata": {
"tags": []
}
},
{
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1mDataset mnist downloaded and prepared to /root/tensorflow_datasets/mnist/3.0.1. Subsequent calls will reuse this data.\u001b[0m\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "cNW4irM-QS2-"
},
"source": [
"def normalize_img(image, label):\n",
" \"\"\"Normalizes images: `uint8` -> `float32`.\"\"\"\n",
" return tf.cast(image, tf.float32) / 255., label\n",
"\n",
"ds_train = ds_train.map(normalize_img)\n",
"ds_train = ds_train.batch(128)"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "REFE87jhQTZ5"
},
"source": [
"ds_test = ds_test.map(normalize_img)\n",
"ds_test = ds_test.batch(128)"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ZfQu-SABQW5W"
},
"source": [
"model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Flatten(input_shape=(28, 28, 1)),\n",
" tf.keras.layers.Dense(128,activation='relu'),\n",
" tf.keras.layers.Dense(10, activation='softmax')\n",
"])\n",
"model.compile(\n",
" loss='sparse_categorical_crossentropy',\n",
" optimizer=tf.keras.optimizers.Adam(0.001),\n",
" metrics=['accuracy']\n",
")"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZRhT_AU2QbWN",
"outputId": "0aa49946-0316-4a32-dc74-3cb55d1b8e50"
},
"source": [
"model.fit(ds_train,epochs=10,validation_data=ds_test)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"469/469 [==============================] - 10s 21ms/step - loss: 0.3597 - accuracy: 0.8998 - val_loss: 0.1909 - val_accuracy: 0.9456\n",
"Epoch 2/10\n",
"469/469 [==============================] - 4s 8ms/step - loss: 0.1650 - accuracy: 0.9535 - val_loss: 0.1381 - val_accuracy: 0.9596\n",
"Epoch 3/10\n",
"469/469 [==============================] - 4s 8ms/step - loss: 0.1184 - accuracy: 0.9664 - val_loss: 0.1133 - val_accuracy: 0.9667\n",
"Epoch 4/10\n",
"469/469 [==============================] - 3s 7ms/step - loss: 0.0912 - accuracy: 0.9742 - val_loss: 0.1003 - val_accuracy: 0.9714\n",
"Epoch 5/10\n",
"469/469 [==============================] - 4s 8ms/step - loss: 0.0734 - accuracy: 0.9792 - val_loss: 0.0923 - val_accuracy: 0.9733\n",
"Epoch 6/10\n",
"469/469 [==============================] - 4s 8ms/step - loss: 0.0603 - accuracy: 0.9831 - val_loss: 0.0877 - val_accuracy: 0.9745\n",
"Epoch 7/10\n",
"469/469 [==============================] - 4s 8ms/step - loss: 0.0503 - accuracy: 0.9865 - val_loss: 0.0842 - val_accuracy: 0.9757\n",
"Epoch 8/10\n",
"469/469 [==============================] - 4s 8ms/step - loss: 0.0422 - accuracy: 0.9891 - val_loss: 0.0816 - val_accuracy: 0.9756\n",
"Epoch 9/10\n",
"469/469 [==============================] - 4s 9ms/step - loss: 0.0353 - accuracy: 0.9913 - val_loss: 0.0803 - val_accuracy: 0.9758\n",
"Epoch 10/10\n",
"469/469 [==============================] - 4s 9ms/step - loss: 0.0295 - accuracy: 0.9930 - val_loss: 0.0786 - val_accuracy: 0.9763\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x7f1769bb0c50>"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Td_as7ZCQif8",
"outputId": "c65ce3fe-9745-429b-a45e-323af1475d35"
},
"source": [
"model.save('MNIST-Model')"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"INFO:tensorflow:Assets written to: MNIST-Model/assets\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"INFO:tensorflow:Assets written to: MNIST-Model/assets\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iAMyWSpsQn2G",
"outputId": "c594ef3f-b904-4c2c-aa12-38fd10e34ac3"
},
"source": [
"!zip -r /content/MNIST.zip /content/MNIST-Model"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
" adding: content/MNIST-Model/ (stored 0%)\n",
" adding: content/MNIST-Model/assets/ (stored 0%)\n",
" adding: content/MNIST-Model/variables/ (stored 0%)\n",
" adding: content/MNIST-Model/variables/variables.data-00000-of-00001 (deflated 12%)\n",
" adding: content/MNIST-Model/variables/variables.index (deflated 59%)\n",
" adding: content/MNIST-Model/saved_model.pb (deflated 87%)\n",
" adding: content/MNIST-Model/keras_metadata.pb (deflated 85%)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"id": "ka7I7rE9Qv-o",
"outputId": "a4e7a9f4-2489-4f85-ed8a-1237c4036094"
},
"source": [
"from google.colab import files\n",
"files.download('/content/MNIST.zip')"
],
"execution_count": 9,
"outputs": [
{
"output_type": "display_data",
"data": {
"application/javascript": [
"\n",
" async function download(id, filename, size) {\n",
" if (!google.colab.kernel.accessAllowed) {\n",
" return;\n",
" }\n",
" const div = document.createElement('div');\n",
" const label = document.createElement('label');\n",
" label.textContent = `Downloading \"${filename}\": `;\n",
" div.appendChild(label);\n",
" const progress = document.createElement('progress');\n",
" progress.max = size;\n",
" div.appendChild(progress);\n",
" document.body.appendChild(div);\n",
"\n",
" const buffers = [];\n",
" let downloaded = 0;\n",
"\n",
" const channel = await google.colab.kernel.comms.open(id);\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
"\n",
" for await (const message of channel.messages) {\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
" if (message.buffers) {\n",
" for (const buffer of message.buffers) {\n",
" buffers.push(buffer);\n",
" downloaded += buffer.byteLength;\n",
" progress.value = downloaded;\n",
" }\n",
" }\n",
" }\n",
" const blob = new Blob(buffers, {type: 'application/binary'});\n",
" const a = document.createElement('a');\n",
" a.href = window.URL.createObjectURL(blob);\n",
" a.download = filename;\n",
" div.appendChild(a);\n",
" a.click();\n",
" div.remove();\n",
" }\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {
"tags": []
}
},
{
"output_type": "display_data",
"data": {
"application/javascript": [
"download(\"download_eb82d8cd-2f8a-46ca-86ec-a208047048f6\", \"MNIST.zip\", 1092977)"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {
"tags": []
}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment