Skip to content

Commit

Permalink
Fix notebook failure with Keras 3.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 625072490
  • Loading branch information
tensorflower-gardener authored and copybara-github committed Apr 15, 2024
1 parent e99aab9 commit 9efadf0
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions site/en/tutorials/generative/autoencoder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
"id": "Ndo4ERqnwQOU"
},
"source": [
"##### Copyright 2020 The TensorFlow Authors."
"##### Copyright 2024 The TensorFlow Authors."
]
},
{
"metadata": {
"id": "13rwRG5Jec7n"
},
"cell_type": "markdown",
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -76,7 +83,7 @@
"source": [
"This tutorial introduces autoencoders with three examples: the basics, image denoising, and anomaly detection.\n",
"\n",
"An autoencoder is a special type of neural network that is trained to copy its input to its output. For example, given an image of a handwritten digit, an autoencoder first encodes the image into a lower dimensional latent representation, then decodes the latent representation back to an image. An autoencoder learns to compress the data while minimizing the reconstruction error. \n",
"An autoencoder is a special type of neural network that is trained to copy its input to its output. For example, given an image of a handwritten digit, an autoencoder first encodes the image into a lower dimensional latent representation, then decodes the latent representation back to an image. An autoencoder learns to compress the data while minimizing the reconstruction error.\n",
"\n",
"To learn more about autoencoders, please consider reading chapter 14 from [Deep Learning](https://www.deeplearningbook.org/) by Ian Goodfellow, Yoshua Bengio, and Aaron Courville."
]
Expand Down Expand Up @@ -117,7 +124,7 @@
},
"source": [
"## Load the dataset\n",
"To start, you will train the basic autoencoder using the Fashion MNIST dataset. Each image in this dataset is 28x28 pixels. "
"To start, you will train the basic autoencoder using the Fashion MNIST dataset. Each image in this dataset is 28x28 pixels."
]
},
{
Expand Down Expand Up @@ -169,7 +176,7 @@
" layers.Dense(latent_dim, activation='relu'),\n",
" ])\n",
" self.decoder = tf.keras.Sequential([\n",
" layers.Dense(tf.math.reduce_prod(shape), activation='sigmoid'),\n",
" layers.Dense(tf.math.reduce_prod(shape).numpy(), activation='sigmoid'),\n",
" layers.Reshape(shape)\n",
" ])\n",
"\n",
Expand Down Expand Up @@ -331,8 +338,8 @@
"outputs": [],
"source": [
"noise_factor = 0.2\n",
"x_train_noisy = x_train + noise_factor * tf.random.normal(shape=x_train.shape) \n",
"x_test_noisy = x_test + noise_factor * tf.random.normal(shape=x_test.shape) \n",
"x_train_noisy = x_train + noise_factor * tf.random.normal(shape=x_train.shape)\n",
"x_test_noisy = x_test + noise_factor * tf.random.normal(shape=x_test.shape)\n",
"\n",
"x_train_noisy = tf.clip_by_value(x_train_noisy, clip_value_min=0., clip_value_max=1.)\n",
"x_test_noisy = tf.clip_by_value(x_test_noisy, clip_value_min=0., clip_value_max=1.)"
Expand Down Expand Up @@ -657,7 +664,7 @@
"id": "wVcTBDo-CqFS"
},
"source": [
"Plot a normal ECG. "
"Plot a normal ECG."
]
},
{
Expand Down Expand Up @@ -721,12 +728,12 @@
" layers.Dense(32, activation=\"relu\"),\n",
" layers.Dense(16, activation=\"relu\"),\n",
" layers.Dense(8, activation=\"relu\")])\n",
" \n",
"\n",
" self.decoder = tf.keras.Sequential([\n",
" layers.Dense(16, activation=\"relu\"),\n",
" layers.Dense(32, activation=\"relu\"),\n",
" layers.Dense(140, activation=\"sigmoid\")])\n",
" \n",
"\n",
" def call(self, x):\n",
" encoded = self.encoder(x)\n",
" decoded = self.decoder(encoded)\n",
Expand Down Expand Up @@ -763,8 +770,8 @@
},
"outputs": [],
"source": [
"history = autoencoder.fit(normal_train_data, normal_train_data, \n",
" epochs=20, \n",
"history = autoencoder.fit(normal_train_data, normal_train_data,\n",
" epochs=20,\n",
" batch_size=512,\n",
" validation_data=(test_data, test_data),\n",
" shuffle=True)"
Expand Down Expand Up @@ -908,7 +915,7 @@
"id": "uEGlA1Be50Nj"
},
"source": [
"Note: There are other strategies you could use to select a threshold value above which test examples should be classified as anomalous, the correct approach will depend on your dataset. You can learn more with the links at the end of this tutorial. "
"Note: There are other strategies you could use to select a threshold value above which test examples should be classified as anomalous, the correct approach will depend on your dataset. You can learn more with the links at the end of this tutorial."
]
},
{
Expand All @@ -917,7 +924,7 @@
"id": "zpLSDAeb51D_"
},
"source": [
"If you examine the reconstruction error for the anomalous examples in the test set, you'll notice most have greater reconstruction error than the threshold. By varing the threshold, you can adjust the [precision](https://developers.google.com/machine-learning/glossary#precision) and [recall](https://developers.google.com/machine-learning/glossary#recall) of your classifier. "
"If you examine the reconstruction error for the anomalous examples in the test set, you'll notice most have greater reconstruction error than the threshold. By varing the threshold, you can adjust the [precision](https://developers.google.com/machine-learning/glossary#precision) and [recall](https://developers.google.com/machine-learning/glossary#recall) of your classifier."
]
},
{
Expand Down Expand Up @@ -992,8 +999,18 @@
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "autoencoder.ipynb",
"gpuType": "T4",
"private_outputs": true,
"provenance": [
{
"file_id": "17gKB2bKebV2DzoYIMFzyEXA5uDnwWOvT",
"timestamp": 1712793165979
},
{
"file_id": "https://github.com/tensorflow/docs/blob/master/site/en/tutorials/generative/autoencoder.ipynb",
"timestamp": 1712792176273
}
],
"toc_visible": true
},
"kernelspec": {
Expand Down

0 comments on commit 9efadf0

Please sign in to comment.