From 687a1d2f115d6c600843b6837def73e8e2ff71d2 Mon Sep 17 00:00:00 2001 From: Sachin Prasad Date: Sat, 3 Aug 2024 13:10:18 -0700 Subject: [PATCH] Fix shape issue (#1905) * fix torch guide * trim .md output --- guides/custom_train_step_in_torch.py | 4 +- guides/ipynb/custom_train_step_in_torch.ipynb | 17 +----- guides/md/custom_train_step_in_torch.md | 55 +++++++++++-------- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/guides/custom_train_step_in_torch.py b/guides/custom_train_step_in_torch.py index 07a89cfed4..b493411ab5 100644 --- a/guides/custom_train_step_in_torch.py +++ b/guides/custom_train_step_in_torch.py @@ -2,7 +2,7 @@ Title: Customizing what happens in `fit()` with PyTorch Author: [fchollet](https://twitter.com/fchollet) Date created: 2023/06/27 -Last modified: 2023/06/27 +Last modified: 2024/08/01 Description: Overriding the training step of the Model class with PyTorch. Accelerator: GPU """ @@ -397,7 +397,7 @@ def compile(self, d_optimizer, g_optimizer, loss_fn): def train_step(self, real_images): device = "cuda" if torch.cuda.is_available() else "cpu" - if isinstance(real_images, tuple): + if isinstance(real_images, tuple) or isinstance(real_images, list): real_images = real_images[0] # Sample random points in the latent space batch_size = real_images.shape[0] diff --git a/guides/ipynb/custom_train_step_in_torch.ipynb b/guides/ipynb/custom_train_step_in_torch.ipynb index 0ae7a8b9f0..9f3d3576d1 100644 --- a/guides/ipynb/custom_train_step_in_torch.ipynb +++ b/guides/ipynb/custom_train_step_in_torch.ipynb @@ -10,7 +10,7 @@ "\n", "**Author:** [fchollet](https://twitter.com/fchollet)
\n", "**Date created:** 2023/06/27
\n", - "**Last modified:** 2023/06/27
\n", + "**Last modified:** 2024/08/01
\n", "**Description:** Overriding the training step of the Model class with PyTorch." ] }, @@ -50,17 +50,6 @@ "Let's see how that works." ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab_type": "code" - }, - "outputs": [], - "source": [ - "!pip install keras --upgrade --quiet" - ] - }, { "cell_type": "markdown", "metadata": { @@ -282,7 +271,7 @@ "outputs = keras.layers.Dense(1)(inputs)\n", "model = CustomModel(inputs, outputs)\n", "\n", - "# We don't passs a loss or metrics here.\n", + "# We don't pass a loss or metrics here.\n", "model.compile(optimizer=\"adam\")\n", "\n", "# Just use `fit` as usual -- you can use callbacks, etc.\n", @@ -531,7 +520,7 @@ "\n", " def train_step(self, real_images):\n", " device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", - " if isinstance(real_images, tuple):\n", + " if isinstance(real_images, tuple) or isinstance(real_images, list):\n", " real_images = real_images[0]\n", " # Sample random points in the latent space\n", " batch_size = real_images.shape[0]\n", diff --git a/guides/md/custom_train_step_in_torch.md b/guides/md/custom_train_step_in_torch.md index c112a44be6..0b62d818f0 100644 --- a/guides/md/custom_train_step_in_torch.md +++ b/guides/md/custom_train_step_in_torch.md @@ -2,7 +2,7 @@ **Author:** [fchollet](https://twitter.com/fchollet)
**Date created:** 2023/06/27
-**Last modified:** 2023/06/27
+**Last modified:** 2024/08/01
**Description:** Overriding the training step of the Model class with PyTorch. @@ -142,14 +142,14 @@ model.fit(x, y, epochs=3)
``` -Epoch 1/3 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 551us/step - mae: 0.6533 - loss: 0.6036 +Epoch 1/3 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - mae: 0.3410 - loss: 0.1772 Epoch 2/3 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 522us/step - mae: 0.4013 - loss: 0.2522 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - mae: 0.3336 - loss: 0.1695 Epoch 3/3 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 516us/step - mae: 0.3813 - loss: 0.2256 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - mae: 0.3170 - loss: 0.1511 - + ```
@@ -238,17 +238,17 @@ model.fit(x, y, epochs=5)
``` Epoch 1/5 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 461us/step - loss: 0.2470 - mae: 0.3953 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.6173 - mae: 0.6607 Epoch 2/5 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 456us/step - loss: 0.2386 - mae: 0.3910 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.2340 - mae: 0.3883 Epoch 3/5 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 456us/step - loss: 0.2359 - mae: 0.3901 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.1922 - mae: 0.3517 Epoch 4/5 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 480us/step - loss: 0.2013 - mae: 0.3572 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.1802 - mae: 0.3411 Epoch 5/5 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 463us/step - loss: 0.1903 - mae: 0.3480 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.1862 - mae: 0.3505 - + ```
@@ -328,13 +328,13 @@ model.fit(x, y, sample_weight=sw, epochs=3)
``` Epoch 1/3 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 499us/step - mae: 1.4332 - loss: 1.0769 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - mae: 0.3216 - loss: 0.0827 Epoch 2/3 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 520us/step - mae: 0.9250 - loss: 0.5614 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - mae: 0.3156 - loss: 0.0803 Epoch 3/3 - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 502us/step - mae: 0.6069 - loss: 0.2653 + 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - mae: 0.3085 - loss: 0.0760 - + ```
@@ -378,11 +378,23 @@ y = np.random.random((1000, 1)) model.evaluate(x, y) ``` + + 1/32 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - mae: 0.8706 - loss: 0.9344 +
``` - 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 325us/step - mae: 0.4427 - loss: 0.2993 + +``` +
+ 32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - mae: 0.8959 - loss: 0.9952 + + -[0.2726495862007141, 0.42286917567253113] + + +
+``` +[1.0077838897705078, 0.8984771370887756] ```
@@ -463,7 +475,7 @@ class GAN(keras.Model): def train_step(self, real_images): device = "cuda" if torch.cuda.is_available() else "cpu" - if isinstance(real_images, tuple): + if isinstance(real_images, tuple) or isinstance(real_images, list): real_images = real_images[0] # Sample random points in the latent space batch_size = real_images.shape[0] @@ -556,9 +568,8 @@ gan.fit(dataloader, epochs=1)
``` - 1094/1094 ━━━━━━━━━━━━━━━━━━━━ 1582s 1s/step - d_loss: 0.3581 - g_loss: 2.0571 - - + 1094/1094 ━━━━━━━━━━━━━━━━━━━━ 394s 360ms/step - d_loss: 0.2436 - g_loss: 4.7259 + ```