From 7b3dba32e96d9b7b2383742d3c92c96d30cd695b Mon Sep 17 00:00:00 2001 From: Boyd Kane <33420535+beyarkay@users.noreply.github.com> Date: Tue, 4 Apr 2023 14:39:09 +0200 Subject: [PATCH 1/5] Make autoencoder easier to copy-paste This commit simply removes the explicit `(28, 28)` and `784` which represented the shape of the FMNIST dataset. It rather uses a `shape` member variable which is passed into the constructor for the Autoencoder. This `shape` variable is set to be `shape = x_test.shape[1:]` by default. --- .../en/tutorials/generative/autoencoder.ipynb | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/site/en/tutorials/generative/autoencoder.ipynb b/site/en/tutorials/generative/autoencoder.ipynb index b68b8e934a7..f90ec24d521 100644 --- a/site/en/tutorials/generative/autoencoder.ipynb +++ b/site/en/tutorials/generative/autoencoder.ipynb @@ -159,27 +159,29 @@ }, "outputs": [], "source": [ - "latent_dim = 64 \n", - "\n", - "class Autoencoder(Model):\n", - " def __init__(self, latent_dim):\n", + "class Autoencoder(keras.Model):\n", + " def __init__(self, latent_dim, shape):\n", " super(Autoencoder, self).__init__()\n", - " self.latent_dim = latent_dim \n", + " self.latent_dim = latent_dim\n", + " self.shape = shape\n", " self.encoder = tf.keras.Sequential([\n", " layers.Flatten(),\n", " layers.Dense(latent_dim, activation='relu'),\n", " ])\n", " self.decoder = tf.keras.Sequential([\n", - " layers.Dense(784, activation='sigmoid'),\n", - " layers.Reshape((28, 28))\n", + " layers.Dense(tf.math.reduce_prod(shape), activation='sigmoid'),\n", + " layers.Reshape(shape)\n", " ])\n", "\n", " def call(self, x):\n", " encoded = self.encoder(x)\n", " decoded = self.decoder(encoded)\n", " return decoded\n", - " \n", - "autoencoder = Autoencoder(latent_dim) " + "\n", + "\n", + "shape = x_test.shape\n", + "latent_dim = 64\n", + "autoencoder = Autoencoder(latent_dim, shape)\n", ] }, { From 61794fdd1f21968857711687a1a16343f7660cfe Mon Sep 17 00:00:00 2001 From: Boyd Kane <33420535+beyarkay@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:00:57 +0200 Subject: [PATCH 2/5] Fix: linting --- site/en/tutorials/generative/autoencoder.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/en/tutorials/generative/autoencoder.ipynb b/site/en/tutorials/generative/autoencoder.ipynb index f90ec24d521..8fb436aa5b6 100644 --- a/site/en/tutorials/generative/autoencoder.ipynb +++ b/site/en/tutorials/generative/autoencoder.ipynb @@ -182,7 +182,7 @@ "shape = x_test.shape\n", "latent_dim = 64\n", "autoencoder = Autoencoder(latent_dim, shape)\n", - ] + ], }, { "cell_type": "code", From f0cec7602266394afb1ac02bc280747284e0d4f2 Mon Sep 17 00:00:00 2001 From: beyarkay Date: Tue, 4 Apr 2023 15:06:56 +0200 Subject: [PATCH 3/5] Fix: remove trailing comma --- site/en/tutorials/generative/autoencoder.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/site/en/tutorials/generative/autoencoder.ipynb b/site/en/tutorials/generative/autoencoder.ipynb index 8fb436aa5b6..c2c3125fbe4 100644 --- a/site/en/tutorials/generative/autoencoder.ipynb +++ b/site/en/tutorials/generative/autoencoder.ipynb @@ -181,8 +181,8 @@ "\n", "shape = x_test.shape\n", "latent_dim = 64\n", - "autoencoder = Autoencoder(latent_dim, shape)\n", - ], + "autoencoder = Autoencoder(latent_dim, shape)\n" + ] }, { "cell_type": "code", From c618f464c618443f72dfcaefca505c6525b4bc1a Mon Sep 17 00:00:00 2001 From: Boyd Kane <33420535+beyarkay@users.noreply.github.com> Date: Mon, 14 Aug 2023 09:04:35 +0200 Subject: [PATCH 4/5] keras.Model -> Model --- site/en/tutorials/generative/autoencoder.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/en/tutorials/generative/autoencoder.ipynb b/site/en/tutorials/generative/autoencoder.ipynb index c2c3125fbe4..508b136307f 100644 --- a/site/en/tutorials/generative/autoencoder.ipynb +++ b/site/en/tutorials/generative/autoencoder.ipynb @@ -159,7 +159,7 @@ }, "outputs": [], "source": [ - "class Autoencoder(keras.Model):\n", + "class Autoencoder(Model):\n", " def __init__(self, latent_dim, shape):\n", " super(Autoencoder, self).__init__()\n", " self.latent_dim = latent_dim\n", From f1da8233eebb5c4e79ffef1b4509f70f408a7247 Mon Sep 17 00:00:00 2001 From: Boyd Kane <33420535+beyarkay@users.noreply.github.com> Date: Mon, 14 Aug 2023 10:32:03 +0200 Subject: [PATCH 5/5] Set autoencoder's shape to x_test.shape[1:] Co-authored-by: Mark McDonald --- site/en/tutorials/generative/autoencoder.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/en/tutorials/generative/autoencoder.ipynb b/site/en/tutorials/generative/autoencoder.ipynb index 508b136307f..d81628fb401 100644 --- a/site/en/tutorials/generative/autoencoder.ipynb +++ b/site/en/tutorials/generative/autoencoder.ipynb @@ -179,7 +179,7 @@ " return decoded\n", "\n", "\n", - "shape = x_test.shape\n", + "shape = x_test.shape[1:]\n", "latent_dim = 64\n", "autoencoder = Autoencoder(latent_dim, shape)\n" ]