Xinyi Zhao
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- lang-ref: ch.08-3 title: Generative Models - Autoencoders lecturer: Alfredo Canziani authors: Nandhitha Raghuram, Xinyi Zhao date: 25 March 2021 --- ## Recap: Predictive Latent Variable Model As we know latent variables are useful in providing additional information that help in predicting multiple values of the target $\boldsymbol{y}$, assiociated with the input $x$. We have seen earlier, in order to predict the target variable $\boldsymbol{y}$ from latent variable $z$, the energy function is given as, $$ E(\boldsymbol{y},z) = [y_1 - g_1(z)]^2 + [y_2 - g_2(z)]^2, \boldsymbol{y} \in \boldsymbol{Y} $$ To be clear, we show the unit of $\boldsymbol{g}$: $$ \boldsymbol{g} = [g_{1}, g_{2}]^\top, \mathbb{R} \rightarrow \mathbb{R}^{2} \\ \boldsymbol{g}(z) = [w_{1}\cos(z), w_{2}\sin(z)] $$ In this case, the the latent variable $z$ is one dimensional. However, if the $z$ has a higher dimension, this could lead to an energy function of $0$ everywhere. Hence, in order to ensure lower energy values for more compatible predictions we introduce a regularized factor. ### Training recap Given an observation $\boldsymbol{y}$, training the regualarized latent variable model $$ E(\boldsymbol{y},z) = C(\boldsymbol{y},\boldsymbol{\tilde{y}}) + R(z) $$ where $\boldsymbol{\tilde{y}} = \text{Dec}(z)$, - compute $$ F_{\beta}(\boldsymbol{y}) = \underset{z}{\text{softmin}_{\beta}}[E(\boldsymbol{y},z)] $$ - Minimise $$ \mathcal{L}(F_{\beta}(\cdot), Y) $$ At zero temperature limit, - Compute $$ \check{z} = \underset{z}{\arg\min}{E(\boldsymbol{y},z)} \\ F_{\infty}(\boldsymbol{y}) = \underset{z}{\min}{E(\boldsymbol{y},\check{z})} $$ - Minimise $$ \mathcal{L}(F_{\infty}(\cdot), Y) $$ ### Target propagation <center> <img src="{{site.baseurl}}/images/week08/08-3/target_prop.png" style="background-color:#DCDCDC;" /><br> <b>Fig. 1:</b> Target propagation </center> *Fig. 1* shows the architecture of target propagation. Given an observation $\boldsymbol{y}$, we first compute $\tilde{z}$ which is the initial guess of the latent variable $z$ $$ \tilde{z} = \text{Enc}(\boldsymbol{y}) $$ We then initialize $z$ to $\tilde{z}$, here we must ensure that these values are not too far apart from each other. $$ \check{z} = \underset{z}{\arg\min}{E(\boldsymbol{y},z)} \\ E(\boldsymbol{y},z) = C(\boldsymbol{y}, \boldsymbol{\tilde{y}}) + R(z) + D(z,\tilde{z}) $$ We now minimise the loss functional in two steps, minimise $\mathcal{L}(F_{\infty}(\cdot), Y)$: $$ \theta_{\text{Dec}} = \theta_{\text{Dec}} - \triangledown_{\theta_{\text{Dec}}}C(\boldsymbol{y},\boldsymbol{\tilde{y}})\\ \\ \theta_{\text{Enc}} = \theta_{\text{Enc}} - \triangledown_{\theta_{\text{Enc}}}D(\check{z},\tilde{z}) $$ In this way $z$ is constrained in taking only a set of values. ## Autoencoder (AE) An autoencoder is a type of artificial neural network used to learn efficient data codings in an unsupervised manner. The aim is to first learn encoded representations of our data and then generate the input data (as closely as possible) from the learned encoded representations. Thus, the output of an autoencoder is its prediction for the input. <center> <img src="{{site.baseurl}}/images/week08/08-3/AE.png" style="background-color:#DCDCDC;" /><br> <b>Fig. 2</b>: Architecture of a basic autoencoder </center> *Fig. 2* shows the architecture of a basic unconditional autoencoder. To summarize at a high level, a very simple form of AE is as follows: - First, the autoencoder takes in an input and maps it to a hidden state through an affine transformation $\boldsymbol{h} = f(\boldsymbol{W}_h \boldsymbol{y} + \boldsymbol{b}_h)$, where $f$ is an (element-wise) activation function. This is the **encoder** stage. Note that $\boldsymbol{h}$ is also called the **code**. - Next, $\tilde{\boldsymbol{y}} = g(\boldsymbol{W}_x \boldsymbol{y} + \boldsymbol{b}_y)$, where $g$ is an activation function. This is the **decoder** stage. - Finally, the energy is the sum of the reconstruction and the regularization, $F(\boldsymbol{y}) = C(\boldsymbol{y},\tilde{\boldsymbol{y}})+ R(\boldsymbol{h})$. ### Reconstruction costs When the input is real-valued we use mean squared error loss, $$ C(\boldsymbol{y},\boldsymbol{\tilde{y}}) = \Vert \boldsymbol{y} - \boldsymbol{\tilde{y}} \Vert^{2} = \Vert{\boldsymbol{y} - \text{Dec}[\text{Enc}(\boldsymbol{y})]}\Vert^{2} $$ When the input is categorical we use cross entropy loss, $$ C(\boldsymbol{y},\boldsymbol{\tilde{y}}) = -\sum_{i = 1}^{n} [y_{i} \log(\tilde{y_{i}}) + (1 - y_{i})\log(1 - \tilde{y_{i}})] $$ ### Loss functional The loss functional is the average of the per sample loss. $$ \mathcal{L}(F(\cdot), Y) = \frac{1}{m} \sum_{j = 1}^{m} \ell(F(\cdot),y^{(j)}) \in \mathbb{R} \\ \ell_\text{energy}(F(\cdot),\boldsymbol{y}) = F(\boldsymbol{y}) $$ ### Under-/over-complete hidden layer When the dimensionality of the hidden layer is less than the dimensionality of the input then it is under complete hidden layer. When the dimensionality of the hidden layer is greater than the dimensionality of the input then it is over complete. The dimensionality of the hidden layer is larger than the input so that they can be linearly seperable. However, this can lead to a collapsed representation. Since by reconstructing the input, the model can copy all the features. In order to avoid this there are a few techniques such as denoising autoencoder, contractive autoencoder, variation autoencoder, etc. ## Denoising Autoencoder Denoising Autoencoder is a contrastive technique. *Fig. 3* below shows the architecture and manifold of the denoising autoencoder and the intuition of how it works. <center> <img src="{{site.baseurl}}/images/week08/08-3/DAE.png" style="background-color:#DCDCDC;" /><br> <b>Fig. 3</b>: Denoising Autoencoder </center> In this model, we corrupt the input data <span style="color:#80b1d3">$\boldsymbol{y}$</span>, in blue, because it's cold / low energy, and get the sample <span style="color:#fb8072">$\boldsymbol{\hat{y}}$</span>, in red, because it's hot / high energy. The energy associated to $\boldsymbol{\tilde y}$ is squared euclidian distance from its original value. Then $\boldsymbol{\tilde y}$ is encoded back to the hidden variable $h$ and then passed back to the decoder producing $\boldsymbol{\tilde y}$ which should be close to the target <span style="color:#80b1d3">$\boldsymbol{y}$</span>. Hence irrespective of what noise is added to the original data, the autoencoder needs to learn to separate the noise and retrieve the original value of <span style="color:#80b1d3">$\boldsymbol{y}$</span>. ## Contractive Autoencoder A contractive autoencoder is an unsupervised deep learning technique that helps a neural network encode unlabeled training data. It makes this encoding less sensitive to small variations in its training dataset. This is accomplished by adding a regularizer, or penalty term, to whatever cost or objective function the algorithm is trying to minimize. The end result is to reduce the learned representation’s sensitivity towards the training input. <center> <img src="{{site.baseurl}}/images/week08/08-3/contractiveAE.png" style="background-color:#DCDCDC;" /><br> <b>Fig. 4</b>: Contactive autoencoder </center> As shown in *Fig. 4*, $C(\boldsymbol{y}, \boldsymbol{\tilde y})$ penalises sensitivities to reconstructions along the manifold while $R(\boldsymbol{h})$ penalises how much the wiggling of $\boldsymbol{h}$ comes from the wiggling of $\boldsymbol{y}$. In this manner, you basically get to push up the energies only in the direction that are not used for the reconstruction. ## Variational Autoencoder (VAE) **Intuition behind VAE and a comparison with classic autoencoders** Next, we introduce Variational Autoencoders (or VAE), a type of generative model. But why do we even care about generative models? To answer the question, discriminative models learn to make predictions given some observations, but generative models aim to simulate the data generation process. One effect is that generative models can better understand the underlying causal relations which leads to better generalization. ### What's the difference between variational autencoder (VAE) and basic autoencoder (AE)? Note that although VAE has "Autoencoders" (AE) in its name (because of structural or architectural similarity to auto-encoders), the formulations between VAEs and AEs are very different. *Fig. 5* below shows the architectures of VAE and basic AE. <center> <img src="{{site.baseurl}}/images/week08/08-3/VAE.png" style="background-color:#DCDCDC;" /><br> <b>Fig. 5</b>: VAE vs. Basic AE </center> For VAE: - First, the encoder stage: we pass the input $\boldsymbol{y}$ to the encoder. Instead of generating a hidden representation $\boldsymbol{h}$ in AE, the hidden representation in VAE comprises two parts: $\boldsymbol{\mu}$ and $\boldsymbol{v}$. And similar to using regularisation factor $R$ for $\boldsymbol{h}$, we now use regularisation factors $U$ and $V$ for $\boldsymbol{\mu}$ and $\boldsymbol{v}$, respectively. - Next, we use a sampler to sample $\boldsymbol{z}$ which is the latent random variable following a Gaussian distribution with $\boldsymbol{\mu}$ and $\boldsymbol{v}$. Note that people use Gaussian distributions as the encoded distribution in practice, but other distributions can be used as well. - Finally, $\boldsymbol{z}$ is passed into the decoder to generate $\tilde{\boldsymbol{y}}$. - The decoder will be a function from $\mathcal{Z}$ to $\mathbb{R}^{n}$: $\boldsymbol{z} \mapsto \boldsymbol{\tilde{y}}$. In fact, for basic autoencoder, we can think of $\boldsymbol{h}$ as just the vector $\boldsymbol{\mu}$ in the VAE formulation, with the variance set to zero. In short, the main difference between VAEs and AEs is that VAEs have a good latent space that enables generative process. ### What's the difference between variational autencoder (VAE) and denoising autoencoder (DAE)? Now let's have compare the difference between VAE and DAE. See *Fig. 6* below. <center> <img src="{{site.baseurl}}/images/week08/08-3/VAE_DAE.png" style="background-color:#DCDCDC;" /><br> <b>Fig. 6</b>: VAE vs. Denoising AE </center> For DAE as we mentioned before, the sampling was happening between <span style="color:#80b1d3">$\boldsymbol{y}$</span>, in blue, because it’s cold / low energy, and <span style="color:#fb8072">$\boldsymbol{\hat{y}}$</span>, in red, because it's hot / high energy. So we move the input and then we decode everything into $\boldsymbol{\tilde{y}}$. For VAD we encode the input and we add noise in the hidden , so we just switch the position of the encoder in the sampler more or less. ### The VAE objective (loss) function <center> <img src="{{site.baseurl}}/images/week08/08-3/VAEloss.png" style="background-color:#DCDCDC;" /><br> <b>Fig. 7</b>: Mapping from input space to latent space </center> First, we encode from input space (left) to latent space (right), through encoder and noise. Next, we decode from latent space (right) to output space (left). To go from the latent to input space (the generative process) we will need to either learn the distribution (of the latent code) or enforce some structure. In our case, VAE enforces some structure to the latent space. As usual, to train VAE, we minimize a loss function. The loss function is therefore composed of a reconstruction term as well as a regularization term. - The reconstruction term is on the final layer (left side of the figure). This corresponds to $C(\boldsymbol{y}, \tilde{\boldsymbol{y}})$ in the figure. - The regularization term is on the latent layer, to enforce some specific Gaussian structure on the latent space (right side of the figure). We do so by using a penalty term $D_{KL}(\mathcal{N}(\boldsymbol{\mu}, \boldsymbol{v}) \mathrel{\Vert} \mathcal{N}(\boldsymbol{0}, \boldsymbol{1}))$. Without this term, VAE will act like a basic autoencoder, which may lead to overfitting, and we won't have the generative properties that we desire. ### Discussion on sampling $\boldsymbol{z}$ (reparameterisation trick) How do we add this noise as mensioned above? We here introduce *reparameterization trick*. From above we know that, we sample from the Gaussian distribution, in order to obtain latent variable $\boldsymbol{z}$. However, this is problematic, because when we do gradient descent to train the VAE model, we don't know how to do backpropagation through the sampling module. We can simply say that the new latent $\boldsymbol{z}$ is going to be $\boldsymbol{\mu}$ which is the guess for the mean plus a epsilon that was sampled from a gaussian whose amplitude is changed by the square root of the the standard deviation $\boldsymbol{v}$. So in this way, we can get gradients back flowing in these encoder. ### Visualizing Latent Variable Estimates and Reconstruction Loss <center> <img src="{{site.baseurl}}/images/week08/08-3/bubbles_z.png" style="background-color:#DCDCDC;" /><br> <b>Fig. 8</b>: Visualizing vector $z$ as bubbles in the latent space </center> *In Fig. 8 above, each bubble represents an estimated region of $\boldsymbol{z}$, and the arrows represent how the reconstruction term pushes each estimated value away from the others, which is explained more below.* As stated above, the Free Energy for the VAE contains two terms: a reconstruction term and a regularization term. We can write this as $$ \tilde{F}(\boldsymbol{y}) = C(\boldsymbol{y}, \tilde{\boldsymbol{y}}) + \beta D_{\text{KL}}(\mathcal{N}(\boldsymbol{\mu}, \boldsymbol{v}) \mathrel{\Vert} \mathcal{N}(\boldsymbol{0}, \boldsymbol{1})) $$ To visualize the purpose of each term in the free energy, we can think of each estimated $\boldsymbol{z}$ value as a circle in $2d$ space, where the centre of the circle is $\boldsymbol{\mu}$ and the surrounding area are the possible values of $\boldsymbol{z}$ determined by $\boldsymbol{v}$. If there is an overlap between any two estimates of $z$, (visually, if two bubbles overlap) this creates ambiguity for reconstruction because the points in the overlap can be mapped to both original inputs. The first term $C(\boldsymbol{y}, \tilde{\boldsymbol{y}})$ forces to reconstructing these bubbles to the correct locations. - The reconstruction loss $C(\boldsymbol{y}, \tilde{\boldsymbol{y}})$ will push the points away from one another such there is no overlap. - Another option for $C(\boldsymbol{y}, \tilde{\boldsymbol{y}})$ to avoid overlap is to make the variance zero and then there are just points other than bubbles so there's no more overlap. However, if we use just the reconstruction loss, the estimates will continue to be pushed away from each other and the system could blow up. This is where the penalty term comes in. **The penalty term** The second term is the relative entropy (a measure of the distance between two distributions) between a Gaussian with mean $\boldsymbol{\mu}$, variance $\boldsymbol{\mu}$ and the standard normal distribution. If we expand this second term in the VAE loss function we get: $$ \beta D_{\text{KL}}(\mathcal{N}(\boldsymbol{\mu}, \boldsymbol{v}) \mathrel{\Vert} \mathcal{N}(\boldsymbol{0}, \boldsymbol{1})) = \frac{\beta}{2} \sum\limits_{i=1}^dv_i - \log{(v_i)} - 1 + \mu_i^2 $$ where the expression in the summation has four terms. Let's use $V$ for the first three terms. $$ V = v_i - \log{(v_i)} - 1 $$ From the left bottom plot in Fig. 8 above, we can see that this expression is minimized when $v_i$ is 1. Therefore our penalty loss will keep the variance of our estimated latent variables at around 1. Visually, this means our "bubbles" from above will have a radius of around 1. The last term, $\mu_i^2$, minimizes the distance between the bubbles and therefore prevents the "exploding" encouraged by the reconstruction term. **Note:** The $\beta$ in the VAE loss function is a hyperparameter that dictates how to weight the reconstruction and penalty terms.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully