-
-
Save kiransair/fa65152a76e8def89a503bdd43e9e458 to your computer and use it in GitHub Desktop.
TF_Forum_23296.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": "ABX9TyMQ/hvHz+Lcf+mGHqyZX+PH", | |
"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/kiransair/fa65152a76e8def89a503bdd43e9e458/tf_forum_23296.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"id": "Od8DyldmvHSu" | |
}, | |
"outputs": [], | |
"source": [ | |
"import tempfile\n", | |
"import os\n", | |
"import keras\n", | |
"import tensorflow as tf" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"print(\"TensorFlow Version:\",tf.__version__)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "otJCE3cSvO5p", | |
"outputId": "0e3d62ce-f048-4077-fe4b-ac83293d9312" | |
}, | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"TensorFlow Version: 2.15.0\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Load MNIST dataset\n", | |
"mnist = tf.keras.datasets.mnist\n", | |
"(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n", | |
"\n", | |
"# Normalize the input image so that each pixel value is between 0 to 1.\n", | |
"train_images = train_images / 255.0\n", | |
"test_images = test_images / 255.0\n", | |
"\n", | |
"# Define the model architecture.\n", | |
"model = keras.Sequential([\n", | |
" keras.layers.InputLayer(input_shape=(28, 28)),\n", | |
" keras.layers.Reshape(target_shape=(28, 28, 1)),\n", | |
" keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation='relu'),\n", | |
" keras.layers.MaxPooling2D(pool_size=(2, 2)),\n", | |
" keras.layers.Flatten(),\n", | |
" keras.layers.Dense(10)\n", | |
"])\n", | |
"\n", | |
"# Train the digit classification model\n", | |
"model.compile(optimizer='adam',\n", | |
" loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", | |
" metrics=['accuracy'])\n", | |
"\n", | |
"model.fit(\n", | |
" train_images,\n", | |
" train_labels,\n", | |
" epochs=1,\n", | |
" validation_split=0.1,\n", | |
")" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "lG2nro7yvWps", | |
"outputId": "77685d33-9e50-41ab-d63a-f511a31dc3dc" | |
}, | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", | |
"11490434/11490434 [==============================] - 0s 0us/step\n", | |
"1688/1688 [==============================] - 23s 12ms/step - loss: 0.3057 - accuracy: 0.9142 - val_loss: 0.1173 - val_accuracy: 0.9700\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<keras.src.callbacks.History at 0x78523a593fa0>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 4 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.save_weights('test_model_weights.h5')" | |
], | |
"metadata": { | |
"id": "o3GaeaEEvbwR" | |
}, | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import keras\n", | |
"import tensorflow as tf\n", | |
"print(\"TensorFlow Version:\",tf.__version__)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "twDZa-qYvil0", | |
"outputId": "7f15dac3-94e1-4d7b-ed8a-4ab59a3e8432" | |
}, | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"TensorFlow Version: 2.16.1\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model = keras.Sequential([\n", | |
" keras.layers.InputLayer(shape=(28, 28)),\n", | |
" keras.layers.Reshape(target_shape=(28, 28, 1)),\n", | |
" keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation='relu'),\n", | |
" keras.layers.MaxPooling2D(pool_size=(2, 2)),\n", | |
" keras.layers.Flatten(),\n", | |
" keras.layers.Dense(10)\n", | |
"])" | |
], | |
"metadata": { | |
"id": "4wLOq234wGJn" | |
}, | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.load_weights('/content/test_model_weights.h5')" | |
], | |
"metadata": { | |
"id": "Qu-RruLcvijC" | |
}, | |
"execution_count": 6, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "5nL5pEqhvify" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "bWzWeEl2vic6" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"pip install tensorflow==2.16.1" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 1000 | |
}, | |
"id": "ZwufHgOEve7F", | |
"outputId": "725a87c7-3453-48a3-9fb4-18daaa3a2db0" | |
}, | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Collecting tensorflow==2.16.1\n", | |
" Downloading tensorflow-2.16.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (589.8 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m589.8/589.8 MB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hRequirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (1.4.0)\n", | |
"Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (1.6.3)\n", | |
"Requirement already satisfied: flatbuffers>=23.5.26 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (24.3.7)\n", | |
"Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (0.5.4)\n", | |
"Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (0.2.0)\n", | |
"Collecting h5py>=3.10.0 (from tensorflow==2.16.1)\n", | |
" Downloading h5py-3.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.8/4.8 MB\u001b[0m \u001b[31m79.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hRequirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (16.0.6)\n", | |
"Collecting ml-dtypes~=0.3.1 (from tensorflow==2.16.1)\n", | |
" Downloading ml_dtypes-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m68.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hRequirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (3.3.0)\n", | |
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (24.0)\n", | |
"Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (3.20.3)\n", | |
"Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (2.31.0)\n", | |
"Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (67.7.2)\n", | |
"Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (1.16.0)\n", | |
"Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (2.4.0)\n", | |
"Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (4.10.0)\n", | |
"Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (1.14.1)\n", | |
"Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (1.62.1)\n", | |
"Collecting tensorboard<2.17,>=2.16 (from tensorflow==2.16.1)\n", | |
" Downloading tensorboard-2.16.2-py3-none-any.whl (5.5 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.5/5.5 MB\u001b[0m \u001b[31m52.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hCollecting keras>=3.0.0 (from tensorflow==2.16.1)\n", | |
" Downloading keras-3.1.0-py3-none-any.whl (1.1 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m45.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hRequirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (0.36.0)\n", | |
"Requirement already satisfied: numpy<2.0.0,>=1.23.5 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.16.1) (1.25.2)\n", | |
"Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0->tensorflow==2.16.1) (0.43.0)\n", | |
"Requirement already satisfied: rich in /usr/local/lib/python3.10/dist-packages (from keras>=3.0.0->tensorflow==2.16.1) (13.7.1)\n", | |
"Collecting namex (from keras>=3.0.0->tensorflow==2.16.1)\n", | |
" Downloading namex-0.0.7-py3-none-any.whl (5.8 kB)\n", | |
"Collecting optree (from keras>=3.0.0->tensorflow==2.16.1)\n", | |
" Downloading optree-0.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (286 kB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m286.8/286.8 kB\u001b[0m \u001b[31m26.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hRequirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow==2.16.1) (3.3.2)\n", | |
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow==2.16.1) (3.6)\n", | |
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow==2.16.1) (2.0.7)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow==2.16.1) (2024.2.2)\n", | |
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.17,>=2.16->tensorflow==2.16.1) (3.6)\n", | |
"Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.17,>=2.16->tensorflow==2.16.1) (0.7.2)\n", | |
"Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.17,>=2.16->tensorflow==2.16.1) (3.0.1)\n", | |
"Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard<2.17,>=2.16->tensorflow==2.16.1) (2.1.5)\n", | |
"Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich->keras>=3.0.0->tensorflow==2.16.1) (3.0.0)\n", | |
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich->keras>=3.0.0->tensorflow==2.16.1) (2.16.1)\n", | |
"Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich->keras>=3.0.0->tensorflow==2.16.1) (0.1.2)\n", | |
"Installing collected packages: namex, optree, ml-dtypes, h5py, tensorboard, keras, tensorflow\n", | |
" Attempting uninstall: ml-dtypes\n", | |
" Found existing installation: ml-dtypes 0.2.0\n", | |
" Uninstalling ml-dtypes-0.2.0:\n", | |
" Successfully uninstalled ml-dtypes-0.2.0\n", | |
" Attempting uninstall: h5py\n", | |
" Found existing installation: h5py 3.9.0\n", | |
" Uninstalling h5py-3.9.0:\n", | |
" Successfully uninstalled h5py-3.9.0\n", | |
" Attempting uninstall: tensorboard\n", | |
" Found existing installation: tensorboard 2.15.2\n", | |
" Uninstalling tensorboard-2.15.2:\n", | |
" Successfully uninstalled tensorboard-2.15.2\n", | |
" Attempting uninstall: keras\n", | |
" Found existing installation: keras 2.15.0\n", | |
" Uninstalling keras-2.15.0:\n", | |
" Successfully uninstalled keras-2.15.0\n", | |
" Attempting uninstall: tensorflow\n", | |
" Found existing installation: tensorflow 2.15.0\n", | |
" Uninstalling tensorflow-2.15.0:\n", | |
" Successfully uninstalled tensorflow-2.15.0\n", | |
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", | |
"tf-keras 2.15.1 requires tensorflow<2.16,>=2.15, but you have tensorflow 2.16.1 which is incompatible.\u001b[0m\u001b[31m\n", | |
"\u001b[0mSuccessfully installed h5py-3.10.0 keras-3.1.0 ml-dtypes-0.3.2 namex-0.0.7 optree-0.10.0 tensorboard-2.16.2 tensorflow-2.16.1\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"application/vnd.colab-display-data+json": { | |
"pip_warning": { | |
"packages": [ | |
"h5py", | |
"keras", | |
"ml_dtypes", | |
"tensorboard", | |
"tensorflow" | |
] | |
}, | |
"id": "000e7d1fcf7b4b479a138388dcf1b1e5" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "VpTytwB7vjmX" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment