diff --git a/site/en/tutorials/keras/save_and_load.ipynb b/site/en/tutorials/keras/save_and_load.ipynb index e830276cd3..bcd2e6bcd3 100644 --- a/site/en/tutorials/keras/save_and_load.ipynb +++ b/site/en/tutorials/keras/save_and_load.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "metadata": { "cellView": "form", "id": "2pHVBk_seED1" @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "metadata": { "cellView": "form", "id": "N_fMsQ-N8I7j" @@ -136,40 +136,22 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "metadata": { "id": "RzIOVSdnMYyO" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (6.0.2)\n", - "Requirement already satisfied: h5py in /usr/local/lib/python3.10/dist-packages (3.11.0)\n", - "Requirement already satisfied: numpy>=1.17.3 in /usr/local/lib/python3.10/dist-packages (from h5py) (1.26.4)\n" - ] - } - ], + "outputs": [], "source": [ "!pip install pyyaml h5py # Required to save models in HDF5 format" ] }, { "cell_type": "code", - "execution_count": 57, + "execution_count": null, "metadata": { "id": "7Nm7Tyb-gRt-" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.17.0\n" - ] - } - ], + "outputs": [], "source": [ "import os\n", "\n", @@ -192,7 +174,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "metadata": { "id": "9rGfFwE9XVwz" }, @@ -227,93 +209,11 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "metadata": { "id": "0HZbJIjxyX1S" }, - "outputs": [ - { - "data": { - "text/html": [ - "
Model: \"sequential_18\"\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1mModel: \"sequential_18\"\u001b[0m\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n",
-              "┃ Layer (type)                          Output Shape                         Param # ┃\n",
-              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n",
-              "│ dense_36 (Dense)                     │ (None, 512)                 │         401,920 │\n",
-              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
-              "│ dropout_18 (Dropout)                 │ (None, 512)                 │               0 │\n",
-              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
-              "│ dense_37 (Dense)                     │ (None, 10)                  │           5,130 │\n",
-              "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n",
-              "
\n" - ], - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n", - "│ dense_36 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m512\u001b[0m) │ \u001b[38;5;34m401,920\u001b[0m │\n", - "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", - "│ dropout_18 (\u001b[38;5;33mDropout\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m512\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n", - "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", - "│ dense_37 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m10\u001b[0m) │ \u001b[38;5;34m5,130\u001b[0m │\n", - "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Total params: 407,050 (1.55 MB)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m407,050\u001b[0m (1.55 MB)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Trainable params: 407,050 (1.55 MB)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m407,050\u001b[0m (1.55 MB)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Non-trainable params: 0 (0.00 B)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "# Define a simple sequential model\n", "def create_model():\n", @@ -360,68 +260,11 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "metadata": { "id": "IFPuhwntH8VH" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Epoch 1/10\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 29ms/step - loss: 1.6564 - sparse_categorical_accuracy: 0.4722\n", - "Epoch 1: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 65ms/step - loss: 1.6423 - sparse_categorical_accuracy: 0.4774 - val_loss: 0.7733 - val_sparse_categorical_accuracy: 0.7540\n", - "Epoch 2/10\n", - "\u001b[1m25/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━\u001b[0m\u001b[37m━━━━━\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.4964 - sparse_categorical_accuracy: 0.8321 \n", - "Epoch 2: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 9ms/step - loss: 0.4887 - sparse_categorical_accuracy: 0.8384 - val_loss: 0.5135 - val_sparse_categorical_accuracy: 0.8430\n", - "Epoch 3/10\n", - "\u001b[1m22/32\u001b[0m \u001b[32m━━━━━━━━━━━━━\u001b[0m\u001b[37m━━━━━━━\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.2988 - sparse_categorical_accuracy: 0.9315 \n", - "Epoch 3: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 9ms/step - loss: 0.2948 - sparse_categorical_accuracy: 0.9302 - val_loss: 0.4469 - val_sparse_categorical_accuracy: 0.8640\n", - "Epoch 4/10\n", - "\u001b[1m22/32\u001b[0m \u001b[32m━━━━━━━━━━━━━\u001b[0m\u001b[37m━━━━━━━\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.1931 - sparse_categorical_accuracy: 0.9680 \n", - "Epoch 4: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 7ms/step - loss: 0.1945 - sparse_categorical_accuracy: 0.9649 - val_loss: 0.4242 - val_sparse_categorical_accuracy: 0.8670\n", - "Epoch 5/10\n", - "\u001b[1m21/32\u001b[0m \u001b[32m━━━━━━━━━━━━━\u001b[0m\u001b[37m━━━━━━━\u001b[0m \u001b[1m0s\u001b[0m 3ms/step - loss: 0.1597 - sparse_categorical_accuracy: 0.9647 \n", - "Epoch 5: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 7ms/step - loss: 0.1592 - sparse_categorical_accuracy: 0.9639 - val_loss: 0.4343 - val_sparse_categorical_accuracy: 0.8580\n", - "Epoch 6/10\n", - "\u001b[1m 1/32\u001b[0m \u001b[37m━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[1m2s\u001b[0m 88ms/step - loss: 0.0780 - sparse_categorical_accuracy: 1.0000\n", - "Epoch 6: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 5ms/step - loss: 0.1188 - sparse_categorical_accuracy: 0.9747 - val_loss: 0.4047 - val_sparse_categorical_accuracy: 0.8660\n", - "Epoch 7/10\n", - "\u001b[1m 1/32\u001b[0m \u001b[37m━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[1m0s\u001b[0m 21ms/step - loss: 0.1331 - sparse_categorical_accuracy: 0.9375\n", - "Epoch 7: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 5ms/step - loss: 0.0930 - sparse_categorical_accuracy: 0.9877 - val_loss: 0.4240 - val_sparse_categorical_accuracy: 0.8620\n", - "Epoch 8/10\n", - "\u001b[1m30/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m━━\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.0584 - sparse_categorical_accuracy: 0.9994 \n", - "Epoch 8: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 5ms/step - loss: 0.0586 - sparse_categorical_accuracy: 0.9990 - val_loss: 0.3940 - val_sparse_categorical_accuracy: 0.8690\n", - "Epoch 9/10\n", - "\u001b[1m 1/32\u001b[0m \u001b[37m━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[1m0s\u001b[0m 21ms/step - loss: 0.0406 - sparse_categorical_accuracy: 1.0000\n", - "Epoch 9: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 4ms/step - loss: 0.0461 - sparse_categorical_accuracy: 0.9958 - val_loss: 0.3908 - val_sparse_categorical_accuracy: 0.8740\n", - "Epoch 10/10\n", - "\u001b[1m 1/32\u001b[0m \u001b[37m━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[1m0s\u001b[0m 21ms/step - loss: 0.1110 - sparse_categorical_accuracy: 0.9375\n", - "Epoch 10: saving model to training_1/cp.weights.h5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 4ms/step - loss: 0.0493 - sparse_categorical_accuracy: 0.9917 - val_loss: 0.3984 - val_sparse_categorical_accuracy: 0.8740\n" - ] - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 61, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "checkpoint_path = \"training_1/cp.weights.h5\" # we are only saving weights therefore we need to use .weights.h5 extension instead we would use .keras for whole model\n", "checkpoint_dir = os.path.dirname(checkpoint_path)\n", @@ -454,22 +297,11 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": { "id": "gXG5FVKFOVQ3" }, - "outputs": [ - { - "data": { - "text/plain": [ - "['cp.weights.h5']" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "os.listdir(checkpoint_dir)" ] @@ -487,20 +319,11 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": null, "metadata": { "id": "Fp5gbuiaPqCT" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "32/32 - 1s - 31ms/step - loss: 2.3965 - sparse_categorical_accuracy: 0.1220\n", - "Untrained model, accuracy: 12.20%\n" - ] - } - ], + "outputs": [], "source": [ "# Create a basic model instance\n", "model = create_model()\n", @@ -521,28 +344,11 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "metadata": { "id": "2IZxbwiRRSD2" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "32/32 - 0s - 2ms/step - loss: 0.3984 - sparse_categorical_accuracy: 0.8740\n", - "Restored model, accuracy: 87.40%\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py:576: UserWarning: Skipping variable loading for optimizer 'adam', because it has 2 variables whereas the saved optimizer has 10 variables. \n", - " saveable.load_own_variables(weights_store.get(inner_path))\n" - ] - } - ], + "outputs": [], "source": [ "# Loads the weights\n", "model.load_weights(checkpoint_path)\n", @@ -567,48 +373,11 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "metadata": { "id": "mQF_dlgIVOvq" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Epoch 5: saving model to training_2/cp-0005.weights.h5\n", - "\n", - "Epoch 10: saving model to training_2/cp-0010.weights.h5\n", - "\n", - "Epoch 15: saving model to training_2/cp-0015.weights.h5\n", - "\n", - "Epoch 20: saving model to training_2/cp-0020.weights.h5\n", - "\n", - "Epoch 25: saving model to training_2/cp-0025.weights.h5\n", - "\n", - "Epoch 30: saving model to training_2/cp-0030.weights.h5\n", - "\n", - "Epoch 35: saving model to training_2/cp-0035.weights.h5\n", - "\n", - "Epoch 40: saving model to training_2/cp-0040.weights.h5\n", - "\n", - "Epoch 45: saving model to training_2/cp-0045.weights.h5\n", - "\n", - "Epoch 50: saving model to training_2/cp-0050.weights.h5\n" - ] - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Include the epoch in the file name (uses `str.format`)\n", "checkpoint_path = \"training_2/cp-{epoch:04d}.weights.h5\"\n", @@ -655,57 +424,22 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "metadata": { "id": "p64q3-V4sXt0" }, - "outputs": [ - { - "data": { - "text/plain": [ - "['cp-0035.weights.h5',\n", - " 'cp-0000.weights.h5',\n", - " 'cp-0030.weights.h5',\n", - " 'cp-0025.weights.h5',\n", - " 'cp-0050.weights.h5',\n", - " 'cp-0015.weights.h5',\n", - " 'cp-0005.weights.h5',\n", - " 'cp-0040.weights.h5',\n", - " 'cp-0010.weights.h5',\n", - " 'cp-0020.weights.h5',\n", - " 'cp-0045.weights.h5']" - ] - }, - "execution_count": 66, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "os.listdir(checkpoint_dir)" ] }, { "cell_type": "code", - "execution_count": 67, + "execution_count": null, "metadata": { "id": "1AN_fnuyR41H" }, - "outputs": [ - { - "data": { - "application/vnd.google.colaboratory.intrinsic+json": { - "type": "string" - }, - "text/plain": [ - "'training_2/cp-0050.weights.h5'" - ] - }, - "execution_count": 67, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def load_latest_checkpoint(checkpoint_dir):\n", " latest = max(os.listdir(checkpoint_dir), key=lambda f: int(f.split('-')[1].split('.')[0]))\n", @@ -728,20 +462,11 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": null, "metadata": { "id": "3M04jyK-H3QK" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "32/32 - 1s - 34ms/step - loss: 0.4968 - sparse_categorical_accuracy: 0.8730\n", - "Restored model, accuracy: 87.30%\n" - ] - } - ], + "outputs": [], "source": [ "# Create a new model instance\n", "model = create_model()\n", @@ -789,20 +514,11 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": null, "metadata": { "id": "R7W5plyZ-u9X" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "32/32 - 1s - 23ms/step - loss: 0.4968 - sparse_categorical_accuracy: 0.8730\n", - "Restored model, accuracy: 87.30%\n" - ] - } - ], + "outputs": [], "source": [ "# Save the weights\n", "model.save_weights('./checkpoints/my_checkpoint.weights.h5')\n", @@ -869,28 +585,11 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": null, "metadata": { "id": "3f55mAXwukUX" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Epoch 1/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 42ms/step - loss: 1.5987 - sparse_categorical_accuracy: 0.5177\n", - "Epoch 2/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 3ms/step - loss: 0.4480 - sparse_categorical_accuracy: 0.8843\n", - "Epoch 3/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 3ms/step - loss: 0.2776 - sparse_categorical_accuracy: 0.9279\n", - "Epoch 4/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 3ms/step - loss: 0.2168 - sparse_categorical_accuracy: 0.9447\n", - "Epoch 5/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 3ms/step - loss: 0.1379 - sparse_categorical_accuracy: 0.9763\n" - ] - } - ], + "outputs": [], "source": [ "# Create and train a new model instance.\n", "model = create_model()\n", @@ -911,106 +610,11 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": null, "metadata": { "id": "HyfUMOZwux_-" }, - "outputs": [ - { - "data": { - "text/html": [ - "
Model: \"sequential_23\"\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1mModel: \"sequential_23\"\u001b[0m\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n",
-              "┃ Layer (type)                          Output Shape                         Param # ┃\n",
-              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n",
-              "│ dense_46 (Dense)                     │ (None, 512)                 │         401,920 │\n",
-              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
-              "│ dropout_23 (Dropout)                 │ (None, 512)                 │               0 │\n",
-              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
-              "│ dense_47 (Dense)                     │ (None, 10)                  │           5,130 │\n",
-              "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n",
-              "
\n" - ], - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n", - "│ dense_46 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m512\u001b[0m) │ \u001b[38;5;34m401,920\u001b[0m │\n", - "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", - "│ dropout_23 (\u001b[38;5;33mDropout\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m512\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n", - "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", - "│ dense_47 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m10\u001b[0m) │ \u001b[38;5;34m5,130\u001b[0m │\n", - "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Total params: 1,221,152 (4.66 MB)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m1,221,152\u001b[0m (4.66 MB)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Trainable params: 407,050 (1.55 MB)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m407,050\u001b[0m (1.55 MB)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Non-trainable params: 0 (0.00 B)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Optimizer params: 814,102 (3.11 MB)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Optimizer params: \u001b[0m\u001b[38;5;34m814,102\u001b[0m (3.11 MB)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "new_model = tf.keras.models.load_model('my_model.keras')\n", "\n", @@ -1029,22 +633,11 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "metadata": { "id": "8BT4mHNIvMdW" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "32/32 - 1s - 33ms/step - loss: 0.4288 - sparse_categorical_accuracy: 0.8620\n", - "Restored model, accuracy: 86.20%\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 3ms/step\n", - "(1000, 10)\n" - ] - } - ], + "outputs": [], "source": [ "# Evaluate the restored model\n", "loss, acc = new_model.evaluate(test_images, test_labels, verbose=2)\n", @@ -1073,42 +666,11 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": null, "metadata": { "id": "sI1YvCDFzpl3" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Epoch 1/5\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n", - " super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 30ms/step - loss: 1.5705 - sparse_categorical_accuracy: 0.5231\n", - "Epoch 2/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.4329 - sparse_categorical_accuracy: 0.8752 \n", - "Epoch 3/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.2856 - sparse_categorical_accuracy: 0.9221 \n", - "Epoch 4/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.1993 - sparse_categorical_accuracy: 0.9594 \n", - "Epoch 5/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.1284 - sparse_categorical_accuracy: 0.9760 \n" - ] - } - ], + "outputs": [], "source": [ "# Create and train a new model instance.\n", "model = create_model()\n", @@ -1130,20 +692,11 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": null, "metadata": { "id": "sq8fPglI1RWA" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "my_model\n", - "assets\tfingerprint.pb\tsaved_model.pb\tvariables\n" - ] - } - ], + "outputs": [], "source": [ "# my_model directory\n", "!ls saved_model\n", @@ -1163,22 +716,11 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": null, "metadata": { "id": "0YofwHdN0pxa" }, - "outputs": [ - { - "data": { - "text/plain": [ - "._UserObject at 0x7c84e32913c0>" - ] - }, - "execution_count": 78, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "saved_model = tf.saved_model.load('saved_model/my_model')\n", "saved_model" @@ -1197,49 +739,11 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": null, "metadata": { "id": "m2dkmJVCGUia" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Epoch 1/5\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n", - " super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 31ms/step - loss: 1.6114 - sparse_categorical_accuracy: 0.5003\n", - "Epoch 2/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 2ms/step - loss: 0.4265 - sparse_categorical_accuracy: 0.8682\n", - "Epoch 3/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 1ms/step - loss: 0.3224 - sparse_categorical_accuracy: 0.9086 \n", - "Epoch 4/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.2046 - sparse_categorical_accuracy: 0.9593 \n", - "Epoch 5/5\n", - "\u001b[1m32/32\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.1646 - sparse_categorical_accuracy: 0.9715\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. \n" - ] - } - ], + "outputs": [], "source": [ "# Create and train a new model instance.\n", "model = create_model()\n", @@ -1261,113 +765,11 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": null, "metadata": { "id": "5NDMO_7kS6Do" }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.\n" - ] - }, - { - "data": { - "text/html": [ - "
Model: \"sequential_28\"\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1mModel: \"sequential_28\"\u001b[0m\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n",
-              "┃ Layer (type)                          Output Shape                         Param # ┃\n",
-              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n",
-              "│ dense_56 (Dense)                     │ (None, 512)                 │         401,920 │\n",
-              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
-              "│ dropout_28 (Dropout)                 │ (None, 512)                 │               0 │\n",
-              "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n",
-              "│ dense_57 (Dense)                     │ (None, 10)                  │           5,130 │\n",
-              "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n",
-              "
\n" - ], - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩\n", - "│ dense_56 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m512\u001b[0m) │ \u001b[38;5;34m401,920\u001b[0m │\n", - "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", - "│ dropout_28 (\u001b[38;5;33mDropout\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m512\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │\n", - "├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤\n", - "│ dense_57 (\u001b[38;5;33mDense\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m10\u001b[0m) │ \u001b[38;5;34m5,130\u001b[0m │\n", - "└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Total params: 407,052 (1.55 MB)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m407,052\u001b[0m (1.55 MB)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Trainable params: 407,050 (1.55 MB)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m407,050\u001b[0m (1.55 MB)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Non-trainable params: 0 (0.00 B)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
 Optimizer params: 2 (12.00 B)\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[1m Optimizer params: \u001b[0m\u001b[38;5;34m2\u001b[0m (12.00 B)\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "# Recreate the exact same model, including its weights and the optimizer\n", "new_model = tf.keras.models.load_model('my_model.h5')\n", @@ -1387,20 +789,11 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": null, "metadata": { "id": "jwEaj9DnTCVA" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "32/32 - 1s - 25ms/step - loss: 0.4343 - sparse_categorical_accuracy: 0.8610\n", - "Restored model, accuracy: 86.10%\n" - ] - } - ], + "outputs": [], "source": [ "loss, acc = new_model.evaluate(test_images, test_labels, verbose=2)\n", "print('Restored model, accuracy: {:5.2f}%'.format(100 * acc))"