{"id":210993,"date":"2024-08-26T12:06:54","date_gmt":"2024-08-26T16:06:54","guid":{"rendered":"https:\/\/ibkrcampus.com\/campus\/?p=210993"},"modified":"2025-04-21T10:44:37","modified_gmt":"2025-04-21T14:44:37","slug":"tensorflow-variational-autoencoder-vae-for-mnist-digits","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/","title":{"rendered":"TensorFlow: Variational Autoencoder (VAE) for MNIST Digits"},"content":{"rendered":"\n<p><em><strong>Excerpt<\/strong><\/em><\/p>\n\n\n\n<p>This post demonstrates the implementation of TensorFlow code for Variational Autoencoder (VAE) using a well-established example with MNIST digit data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-vae-in-tensorflow\">VAE in TensorFlow<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-variational-autoencoder-vae\">Variational Autoencoder (VAE)<\/h3>\n\n\n\n<p>The Variational Autoencoder (VAE) is a generative model that allows us to learn a probabilistic representation of data.<\/p>\n\n\n\n<p>The VAE architecture consists of an encoder and a decoder. The encoder maps input data to a probability distribution in a latent space, while the decoder generates data from samples drawn from the latent space.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"825\" height=\"265\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech.png\" alt=\"\" class=\"wp-image-210996 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech.png 825w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-700x225.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-300x96.png 300w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-768x247.png 768w\" data-sizes=\"(max-width: 825px) 100vw, 825px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 825px; aspect-ratio: 825\/265;\" \/><\/figure>\n\n\n\n<p>The core concept of VAE is the latent space, which is represented by the mean and variance of a Gaussian distribution. The equations for VAE are as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"569\" height=\"92\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-2.png\" alt=\"\" class=\"wp-image-210998 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-2.png 569w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-2-300x49.png 300w\" data-sizes=\"(max-width: 569px) 100vw, 569px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 569px; aspect-ratio: 569\/92;\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center\"><\/p>\n\n\n\n<p>The loss function for VAE includes a reconstruction loss and a regularization term to encourage the latent space to be normally distributed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"567\" height=\"70\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-3.png\" alt=\"\" class=\"wp-image-210999 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-3.png 567w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-3-300x37.png 300w\" data-sizes=\"(max-width: 567px) 100vw, 567px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 567px; aspect-ratio: 567\/70;\" \/><\/figure>\n\n\n\n<p>I&#8217;m omitting the derivation of the aforementioned loss function as there are abundant educational resources on Google. Numerous high-quality materials provide a better explanation than I can offer.<\/p>\n\n\n\n<p>The reparameterization trick allows the training of generative models with stochastic elements while maintaining differentiability. It is crucial when working with continuous latent variables.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"556\" height=\"44\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-4.png\" alt=\"\" class=\"wp-image-211000 lazyload\" style=\"--smush-placeholder-width: 556px; aspect-ratio: 556\/44;width:556px;height:auto\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-4.png 556w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/vae2-shfintech-4-300x24.png 300w\" data-sizes=\"(max-width: 556px) 100vw, 556px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p>here,\u00a0<strong><em>\u03bc<\/em><\/strong>\u00a0and\u00a0<strong><em>\u03c3<\/em><\/strong> are mean and standard deviation of the distribution of the latent variable\u00a0<strong><em>z<\/em><\/strong>.\u00a0<strong><em>\u03f5<\/em><\/strong>\u00a0is sampled from a fixed distribution, typically a standard Gaussian distribution,\u00a0<strong><em>N(0,1)<\/em><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1100\" height=\"515\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/repara_trick-shfintech-1100x515.jpg\" alt=\"\" class=\"wp-image-211003 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/repara_trick-shfintech-1100x515.jpg 1100w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/repara_trick-shfintech-700x328.jpg 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/repara_trick-shfintech-300x140.jpg 300w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/repara_trick-shfintech-768x360.jpg 768w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/repara_trick-shfintech-1536x719.jpg 1536w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/08\/repara_trick-shfintech.jpg 1600w\" data-sizes=\"(max-width: 1100px) 100vw, 1100px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1100px; aspect-ratio: 1100\/515;\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python Jupyter Notebook Code<\/h3>\n\n\n\n<p>A well-established example of VAE&#8217;s application is with MNIST digits. The following code reads MNIST data and performs some preprocessing.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\nimport matplotlib.pyplot as plt\n \nfrom keras.datasets import mnist\nfrom keras.layers import Input, Lambda, Dense\nfrom keras.models import Model\nfrom keras import backend as K\nfrom keras.utils import plot_model\nfrom keras.losses import binary_crossentropy\n \n# network parameters\nrec_dim=784\ninput_shape = (rec_dim,)\nint_dim = 512\nlat_dim = 2\n \n# Load the MNIST data\n(x_tr, y_tr), (x_te, y_te) = mnist.load_data()\n \n# normalize values of image pixels between 0 and 1f\nx_tr = x_tr.astype('float32') \/ 255.\nx_te = x_te.astype('float32') \/ 255.\n \n# 28x28 2D matrix --&gt; 784x1 1D vector\nx_tr = x_tr.reshape((len(x_tr), np.prod(x_tr.shape[1:])))\nx_te = x_te.reshape((len(x_te), np.prod(x_te.shape[1:])))\n \nprint(x_tr.shape, x_te.shape)<\/pre>\n\n\n\n<p>The following code includes both the encoder and decoder. The encoder portion involves sampling latent factors using their mean and variance through the reparameterization trick.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#=======================\n# Encoder\n#=======================\n# Z sampling function\ndef sampling(args):\n    z_mean, z_log_var = args\n    batch = K.shape(z_mean)[0]\n    dim = K.int_shape(z_mean)[1]\n    \n    # Reparameterization Trick\n    # draw random sample \u03b5 from Gussian(=normal) distribution\n    # by default, random_normal has mean = 0 and std = 1.0\n    epsilon = K.random_normal(shape=(batch, dim))\n    \n    return z_mean + K.exp(0.5 * z_log_var) * epsilon\n \n# Input shape\ninputs = Input(shape=input_shape)\nenc_x  = Dense(int_dim, activation='relu')(inputs)\n \nz_mean    = Dense(lat_dim)(enc_x)\nz_log_var = Dense(lat_dim)(enc_x)\n \n# sampling z\nz_sampling = Lambda(sampling, (lat_dim,))([z_mean, z_log_var])\n \n# encoder model has multi-output so a list is used\nencoder = Model(inputs,[z_mean,z_log_var,z_sampling])\nencoder.summary()\n \n#=======================\n# Decoder\n#=======================\n# Input of decoder is z\ninput_z = Input(shape=(lat_dim,))\ndec_h   = Dense(int_dim, activation='relu')(input_z)\noutputs = Dense(rec_dim, activation='sigmoid')(dec_h)\n \n# z is the input and the reconstructed image is the output\ndecoder = Model(input_z, outputs)\ndecoder.summary()<\/pre>\n\n\n\n<p>After constructing the VAE model, which encompasses both the encoder and decoder, the VAE loss, also referred to as the Evidence Lower Bound (ELBO), is calculated as the combination of the reconstruction loss and the Kullback-Leibler (KL) loss. Notably, in the case of beta-VAE, the KL loss is adjusted using a scaling factor, beta, to strike a balance between these two components.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#=======================\n# VAE model\n#=======================\noutputs = decoder(encoder(inputs)[2])\nvae = Model(inputs, outputs)\n \n#--------------------------------------------------\n# VAE_loss = ELBO\n#--------------------------------------------------\n# (1)Reconstruct loss (Marginal_likelihood) : Cross-entropy \nrec_loss = binary_crossentropy(inputs,outputs)\nrec_loss *= rec_dim\n# (2) KL divergence(Latent_loss)\nkl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)\nkl_loss = -0.5*K.sum(kl_loss, 1)\n# (3) ELBO\nvae_loss = K.mean(rec_loss + kl_loss)\n#--------------------------------------------------\n \nvae.add_loss(vae_loss)\nvae.compile(optimizer='adam')\nvae.summary()\n \nhistory = vae.fit(x_tr, x_tr, shuffle=True, \n                  epochs=30, batch_size=64, \n                  validation_data=(x_te, x_te))<\/pre>\n\n\n\n<p><em>Visit <a href=\"https:\/\/shleeai.blogspot.com\/2023\/11\/tensorflow-keras-variational.html\">SHLee AI Financial Model<\/a> for details on how to visualize the training and validation losses across epochs.<\/em><\/p>\n\n\n\n<p><em>Originally posted on <a href=\"https:\/\/shleeai.blogspot.com\/2023\/11\/tensorflow-keras-variational.html\">SHLee AI Financial Model<\/a><\/em> <em>blog.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post demonstrates the implementation of TensorFlow code for Variational Autoencoder (VAE) using a well-established example with MNIST digit data.<\/p>\n","protected":false},"author":662,"featured_media":191319,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338],"tags":[806,6614,827,4659,1225,595,924,17618],"contributors-categories":[13728],"class_list":{"0":"post-210993","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"tag-data-science","12":"tag-jupyter-notebook","13":"tag-keras","14":"tag-matplotlib","15":"tag-numpy","16":"tag-python","17":"tag-tensorflow","18":"tag-variational-autoencoder-vae","19":"contributors-categories-sh-fintech-modeling"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>TensorFlow: Variational Autoencoder (VAE) for MNIST Digits<\/title>\n<meta name=\"description\" content=\"This post demonstrates the implementation of TensorFlow code for Variational Autoencoder (VAE) using a well-established example with MNIST digit data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/210993\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TensorFlow: Variational Autoencoder (VAE) for MNIST Digits\" \/>\n<meta property=\"og:description\" content=\"This post demonstrates the implementation of TensorFlow code for Variational Autoencoder (VAE) using a well-established example with MNIST digit data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-26T16:06:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-21T14:44:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-blue-background-digits.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sang-Heon Lee\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sang-Heon Lee\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Sang-Heon Lee\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/0a959ff9de7f0465a07baa1fe1ae0200\"\n\t            },\n\t            \"headline\": \"TensorFlow: Variational Autoencoder (VAE) for MNIST Digits\",\n\t            \"datePublished\": \"2024-08-26T16:06:54+00:00\",\n\t            \"dateModified\": \"2025-04-21T14:44:37+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/\"\n\t            },\n\t            \"wordCount\": 355,\n\t            \"commentCount\": 0,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/06\\\/python-blue-background-digits.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"Jupyter Notebook\",\n\t                \"Keras\",\n\t                \"Matplotlib\",\n\t                \"NumPy\",\n\t                \"Python\",\n\t                \"TensorFlow\",\n\t                \"Variational Autoencoder (VAE)\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/\",\n\t            \"name\": \"TensorFlow: Variational Autoencoder (VAE) for MNIST Digits | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/06\\\/python-blue-background-digits.jpg\",\n\t            \"datePublished\": \"2024-08-26T16:06:54+00:00\",\n\t            \"dateModified\": \"2025-04-21T14:44:37+00:00\",\n\t            \"description\": \"This post demonstrates the implementation of TensorFlow code for Variational Autoencoder (VAE) using a well-established example with MNIST digit data.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/tensorflow-variational-autoencoder-vae-for-mnist-digits\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/06\\\/python-blue-background-digits.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/06\\\/python-blue-background-digits.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/0a959ff9de7f0465a07baa1fe1ae0200\",\n\t            \"name\": \"Sang-Heon Lee\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/sang-heonlee\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"TensorFlow: Variational Autoencoder (VAE) for MNIST Digits","description":"This post demonstrates the implementation of TensorFlow code for Variational Autoencoder (VAE) using a well-established example with MNIST digit data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/210993\/","og_locale":"en_US","og_type":"article","og_title":"TensorFlow: Variational Autoencoder (VAE) for MNIST Digits","og_description":"This post demonstrates the implementation of TensorFlow code for Variational Autoencoder (VAE) using a well-established example with MNIST digit data.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/","og_site_name":"IBKR Campus US","article_published_time":"2024-08-26T16:06:54+00:00","article_modified_time":"2025-04-21T14:44:37+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-blue-background-digits.jpg","type":"image\/jpeg"}],"author":"Sang-Heon Lee","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Sang-Heon Lee","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/#article","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/"},"author":{"name":"Sang-Heon Lee","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/0a959ff9de7f0465a07baa1fe1ae0200"},"headline":"TensorFlow: Variational Autoencoder (VAE) for MNIST Digits","datePublished":"2024-08-26T16:06:54+00:00","dateModified":"2025-04-21T14:44:37+00:00","mainEntityOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/"},"wordCount":355,"commentCount":0,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-blue-background-digits.jpg","keywords":["Data Science","Jupyter Notebook","Keras","Matplotlib","NumPy","Python","TensorFlow","Variational Autoencoder (VAE)"],"articleSection":["Data Science","Programming Languages","Python Development","Quant"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/","url":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/","name":"TensorFlow: Variational Autoencoder (VAE) for MNIST Digits | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/#primaryimage"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-blue-background-digits.jpg","datePublished":"2024-08-26T16:06:54+00:00","dateModified":"2025-04-21T14:44:37+00:00","description":"This post demonstrates the implementation of TensorFlow code for Variational Autoencoder (VAE) using a well-established example with MNIST digit data.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/tensorflow-variational-autoencoder-vae-for-mnist-digits\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-blue-background-digits.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-blue-background-digits.jpg","width":1000,"height":563,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/0a959ff9de7f0465a07baa1fe1ae0200","name":"Sang-Heon Lee","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/sang-heonlee\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/06\/python-blue-background-digits.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/210993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/662"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=210993"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/210993\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/191319"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=210993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=210993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=210993"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=210993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}