<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Adam Kosiorek</title>
    <description>Understanding Biology through AI
</description>
    <link>http://akosiorek.github.io/</link>
    <atom:link href="http://akosiorek.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 08 Feb 2026 09:01:02 +0000</pubDate>
    <lastBuildDate>Sun, 08 Feb 2026 09:01:02 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Geometry in Text-to-Image Diffusion Models</title>
        <description>&lt;!-- takeoff of large-scale text-to-image generative models: diffusion + big data --&gt;
&lt;!-- Generative modeling is as old as machine learning. But until recently, generative models were a bit like neural nets pre-2012 when AlexNet came out. People knew about them but kept asking what you could really use them for. [DALL-E](https://openai.com/research/dall-e), StableDiffusion (it even has a [Wiki page](https://en.wikipedia.org/wiki/Stable_Diffusion)), and later, ChatGPT changed this. They mark the AlexNet moment for generative modeling--10 years after the original AlexNet. The revolution isn&apos;t in the technology (which was present for a few years) but rather in the general capability of the models due to their scale and the size of the datasets used, and in their public availability.


Generative models are extremely versatile, but it takes creativity to figure out what to do with them. It is thanks to the public availability of these models that we are discovering new use cases every day--far beyond what anyone could have expected.
One such use case is automated [prompt engineering](https://stable-diffusion-art.com/chatgpt-prompt/)[^prompt_engineering]: using a language model to seed the text-to-image model with &quot;interesting&quot; prompts to get nice- or interesting-looking pictures. But we can also ask the language model to tell a story while describing the scenery in detail every time it changes. Using a text-to-image model we can then translate that story into a movie. This should result in a complex environment that gradually changes according to the changing prompts. But will it work? Yes, but not out of the box, because these models do not have any mechanisms for generating 3D-consistent scenes. This blog will explore how we can use text-to-image models for generating 3D scenes--without retraining these models. --&gt;
&lt;!-- takeoff of large-scale text-to-image generative models: diffusion + big data --&gt;

&lt;p&gt;Until recently, generative models were a bit like neural nets pre-2012 when AlexNet came out. People knew about them but kept asking what you could really use them for. Text-to-image models &lt;a href=&quot;https://openai.com/research/dall-e&quot;&gt;DALL-E&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Stable_Diffusion&quot;&gt;StableDiffusion&lt;/a&gt;, and the language model ChatGPT changed this–these models mark the AlexNet moment for generative modeling. The best part? These models are publicly available.
So you can ask ChatGPT to tell a story while describing the scenery in detail every time it changes. Using a text-to-image model you can then translate that story into a movie&lt;sup id=&quot;fnref:prompt_engineering&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:prompt_engineering&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;, right?
If this works, the movie will most likely contain changing cameras showing different parts of a 3D scene. As the camera moves, the scene might change according to the changing prompts used to generate the corresponding images. But will it work? Yes, kind of, but not out of the box, because these models do not have any mechanisms for generating 3D-consistent scenes. This blog will explore how we can use text-to-image models for generating 3D scenes–without retraining these models.&lt;/p&gt;

&lt;p&gt;While no in-depth knowledge is required, it will be helpful to know what diffusion models and NeRF are. If you’d like to dig deeper, I recommend Sander Dieleman’s blog for an &lt;a href=&quot;https://sander.ai/2022/01/31/diffusion.html&quot;&gt;intro to diffusion models&lt;/a&gt; and a guide on &lt;a href=&quot;https://sander.ai/2022/05/26/guidance.html&quot;&gt;how to make them conditional&lt;/a&gt;. For NeRF, check out the &lt;a href=&quot;https://www.matthewtancik.com/nerf&quot;&gt;project website&lt;/a&gt;, and Frank Dellaert’s &lt;a href=&quot;https://dellaert.github.io/NeRF/&quot;&gt;NeRF Explosion 2020&lt;/a&gt; blog which provides a great overview of the history behind NeRF and its various extensions.&lt;/p&gt;

&lt;!-- - inpainting with these models: a cool feature that comes prepackaged --&gt;
&lt;p&gt;Coming back to stitching a movie from images: this is something you can use a text-to-image diffusion model for.
Such image chaining is possible with diffusion models due to their ability to inpaint missing information (or to do image-to-image translation). We can just mask (think erase) a part of an image and ask a diffusion model to fill in the blank. The blank will generally be compatible with the unmasked parts of the image and the text prompt used to condition the model&lt;sup id=&quot;fnref:seeding_masked_parts&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:seeding_masked_parts&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;. See &lt;a href=&quot;https://ahrm.github.io/jekyll/update/2023/01/02/three-eyed-forehead.html&quot;&gt;here for a cool demo&lt;/a&gt; of inpainting with StableDiffusion. This &lt;a href=&quot;https://stable-diffusion-art.com/how-stable-diffusion-work/&quot;&gt;tutorial&lt;/a&gt; says a bit more about how StableDiffusion works and how inpainting is done.
Note that if you can inpaint, you can also outpaint: by simply translating the image to the left, you can pretend that you masked the right side of the image (which is non-existent, but it doesn’t matter). The model will complete that right side of the image, effectively extending it.&lt;/p&gt;

&lt;!-- notes on inpainting:
- mask a part of the image, add noise (strength in [0, 1]), and denoise
- masking can be black, but probably better fill-in with nearest neighbour colour (flood-fill-like algo)
- the masked region can be seeded by another image; if the masked area is smaller than the unmasked area,
the content will be style-adapted to the unmasked area
- often after inpainting there may be a visible boundary between the masked and unmasked regions; you can then
mask the boundary and inpaint it to have it blend nicely
- inpainting can be conditional as well, usually on text; StableDiffusion v2 allows conditioning on depth (via additional input, it&apos;s not trained to model RGBD); it uses [MiDaS](https://github.com/isl-org/MiDaS)

[how stable diffusion works](https://stable-diffusion-art.com/how-stable-diffusion-work/)
[depth to image with SD](https://stable-diffusion-art.com/depth-to-image/): it allows to preserve composition while completely changing the styles
--&gt;

&lt;!-- why we need 3d instead of just images --&gt;
&lt;p&gt;So if you wanted to create an illusion of moving in a 3D scene represented by an image, you could just downscale that image (to move away) or upscale it (to move closer), and have the diffusion model fix any artifacts, right?
The issue is that zooming out scales down everything the same way, but scaling as you move should depend on the distance from the camera (depth); Also you cannot walk forward, walk through doors, model occlusions or walk around and come back to the same place–the result would not be consistent with the previously-generated images. Here’s an example of what zooming out ad infinitum looks like.&lt;/p&gt;

&lt;div style=&quot;max-width: 400px; display: block; margin: auto;&quot;&gt;
  &lt;blockquote class=&quot;twitter-tweet&quot; data-lang=&quot;en&quot;&gt;
    &lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;
      &lt;a href=&quot;https://twitter.com/hardmaru/status/1611569188144807943&quot;&gt;&lt;/a&gt;
    &lt;/p&gt;
  &lt;/blockquote&gt;
  &lt;script async=&quot;&quot; src=&quot;//platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;p&gt;To make the above work well, we would need to model not only the views of a given scene (images), but also the geometry (where things are, and where the camera that captured those views was). If we have the geometry, we can explicitly move the camera into a new position and capture the next image from there. If you can do this, you unlock a plethora of additional applications like generating whole scenes or 3D assets for virtual reality, computer games, or special effects in movies, for interior design, or any other artistic endeavor, really.&lt;/p&gt;

&lt;p&gt;But building generative models of 3D scenes or objects is not easy. In my work, I focused on VAE-based generative models of NeRFs (&lt;a href=&quot;https://arxiv.org/abs/2104.00587&quot;&gt;NeRF-VAE&lt;/a&gt; and &lt;a href=&quot;https://laser-nv-paper.github.io/index.html&quot;&gt;Laser-NV&lt;/a&gt;). In principle, these models offer very similar capabilities&lt;sup id=&quot;fnref:nerf_vae_text_cond&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:nerf_vae_text_cond&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;. In practice, the quality of the generated 3D content is far far behind what text-to-image diffusion models generate these days. One reason is a different framework: &lt;a href=&quot;https://arxiv.org/abs/2207.13751&quot;&gt;GAUDI&lt;/a&gt; employs the diffusion modeling techniques used for image generation and applies them to 3D, which does result in better results than VAEs can provide. However, the model quality is still limited by the lack of high-quality 3D data.&lt;/p&gt;

&lt;!-- no 3d data--&gt;
&lt;p&gt;While it is easy to scrape billions of images and associated text captions from the Internet, this isn’t the case for 3D. To do 3D modeling with NeRF (used in my work and in GAUDI above), you need several images and associated camera viewpoints for every scene, and, if you want to reach the scale of text-to-image models, you need millions if not billions of scenes in your dataset. This data does not exist on the Internet, because that’s not how people take (or post) pictures. Considering the scale, manually capturing such datasets is out of the question.
The only respite is video, where different frames are captured from slightly different viewpoints, but video modeling opens up another can of worms: since the scene isn’t static, it is difficult to learn a scene representation that will be consistent across views (that preserves the geometry). The video diffusion models certainly do not offer multi-view consistency (&lt;a href=&quot;https://imagen.research.google/video/&quot;&gt;Imagen Video&lt;/a&gt;, &lt;a href=&quot;https://makeavideo.studio/&quot;&gt;Make-a-Video&lt;/a&gt;). Nevertheless, video modeling with NeRF-based generative models is the most promising direction for future large-scale 3D models.&lt;/p&gt;

&lt;h3 id=&quot;text-to-image-models-know-about-geometry&quot;&gt;Text-to-Image Models Know About Geometry&lt;/h3&gt;

&lt;!-- - but do we really need to train from 3d data? clearly, the 2d image models know about geometry --&gt;
&lt;p&gt;But here’s the thing. We can play with the text-to-image models by manipulating the text prompt, which then shows that these models know about geometry. Perhaps the best example of this is &lt;a href=&quot;https://dreambooth.github.io/&quot;&gt;DreamBooth&lt;/a&gt;.&lt;/p&gt;

&lt;figure id=&quot;dreambooth&quot;&gt;
  &lt;img style=&quot;width: 100%; display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/3d_diffusion/dreambooth.png&quot; alt=&quot;DreamBooth&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 4:&lt;/b&gt; &lt;a href=&quot;https://dreambooth.github.io/&quot;&gt;DreamBooth&lt;/a&gt; allows one to associate a specific object with a text token and then place that token within different text prompts.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If text-to-image models really know about 3D geometry, maybe we don’t need all that 3D data. Maybe we can just use the image models and either extract their 3D knowledge or perhaps somehow nudge them to preserve geometry across multiple generated images.
&lt;!-- - it turns out that we can, at least in some cases, leverage pretrained image diffusion models as priors for 3D --&gt;
It turns out that both approaches are possible, do not require re-training of the text-to-image models, and correspond to extracting geometry from an image model (&lt;a href=&quot;https://dreamfusion3d.github.io&quot;&gt;DreamFusion&lt;/a&gt; and &lt;a href=&quot;https://pals.ttic.edu/p/score-jacobian-chaining&quot;&gt;Score Jacobian Chaining (SJC)&lt;/a&gt;), and injecting geometry into an image model (&lt;a href=&quot;https://scenescape.github.io&quot;&gt;SceneScape&lt;/a&gt;), respectively.&lt;/p&gt;

&lt;h3 id=&quot;extracting-geometry-from-an-image-model&quot;&gt;Extracting Geometry from an Image Model&lt;/h3&gt;
&lt;p&gt;Given that text-to-image diffusion models&lt;sup id=&quot;fnref:not_dreambooth&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:not_dreambooth&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt; can generate pretty pictures and know about geometry, it is natural to ask if we can extract that geometry from these models. That is, can we lift a generated 2D picture to a full 3D scene?
The answer is, of course, yes. But why does it work?
Because any 2D rendering of a 3D representation is an image, and if that representation contains a scene familiar to the image model (i.e. in the model distribution), that rendered image should have a high likelihood under the image model. Conversely, if the represented scene is not familiar to the image model, the rendered image will have a low likelihood. Therefore, if we start from a random scene, the rendered images will have a low likelihood under the image model. But if we then manage to compute the gradients of the image model likelihood with respect to the 3D representation, we’ll be able to nudge the 3D representation into something that has a bit higher likelihood under that image model. 
Although they differ in derivations, both DreamFusion and SJC come up with novel image-space losses that capture the score (the derivative of the log probability) of a NeRF-rendered image under a pre-trained large-scale text-to-image diffusion model that is then back-propagated onto the NeRF parameters.&lt;/p&gt;

&lt;p&gt;In theory, you don’t even have to use a diffusion model: any image model that can score a rendered image will do, including a VAE or any energy-based model including a GAN discriminator, a contrastive model such as &lt;a href=&quot;https://openai.com/research/clip&quot;&gt;CLIP&lt;/a&gt; or even &lt;a href=&quot;https://arxiv.org/abs/1912.03263&quot;&gt;a classifier&lt;/a&gt;. Check out &lt;a href=&quot;https://arxiv.org/abs/2112.01455&quot;&gt;DreamFields&lt;/a&gt; which uses CLIP to generate images and the &lt;a href=&quot;https://dreamfusion3d.github.io&quot;&gt;DreamFusion&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/2302.10663&quot;&gt;RealFusion&lt;/a&gt; papers (described below), which compare diffusion score against CLIP for training a NeRF. As Ben Poole pointed out, this may not work well in practice, since modes do not usually look like samples (see &lt;a href=&quot;https://sander.ai/2020/09/01/typicality.html&quot;&gt;Sander’s blog on typicality&lt;/a&gt;), and likelihood from a VAE or EBM may fail in high dimensions.&lt;/p&gt;

&lt;p&gt;The next few subsections describe technical details and follow-ups that are self-contained and not necessary for understanding the remainder of the blog. Feel free to skip some of them (but do take a look at the figures to see the results).&lt;/p&gt;

&lt;h4 id=&quot;dreamfusionsjc-algorithm&quot;&gt;DreamFusion/SJC Algorithm&lt;/h4&gt;

&lt;figure id=&quot;dreamfusion_algo&quot;&gt;
  &lt;img style=&quot;width: 100%; display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/3d_diffusion/dreamfusion_algo.png&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 5:&lt;/b&gt; Extracting geometry from a text-to-image model into a NeRF, taken from &lt;a href=&quot;https://dreamfusion3d.github.io&quot;&gt;DreamFusion&lt;/a&gt;.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The simplified algorithm is as follows (the DreamFusion version):&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Initialize a random NeRF and pick a text prompt for the diffusion model.&lt;/li&gt;
  &lt;li&gt;Pick a random camera pose.&lt;/li&gt;
  &lt;li&gt;Render an image at that camera pose using the NeRF.&lt;/li&gt;
  &lt;li&gt;Compute the score-matching loss under a pre-trained diffusion model.&lt;/li&gt;
  &lt;li&gt;Use the score-matching loss as a gradient with respect to the rendered image, and backpropagate it to NeRF’s parameters.&lt;/li&gt;
  &lt;li&gt;Go to step 2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Of course, life is never that easy, and DreamFusion comes with several hacks, including changing the text prompt based on the sampled camera pose, clipping the scene represented by the NeRF to a small ball around the origin (any densities outside of the ball are set to zero), putting the rendered object on different backgrounds, additional losses that ensure e.g. that most of the space is unoccupied or that normals are well-behaved. Most of these tricks are designed to reveal bad learned geometry under the NeRF.e&lt;/p&gt;

&lt;h4 id=&quot;why-does-extracting-geometry-lead-to-cartoonish-objects&quot;&gt;Why Does Extracting Geometry Lead to Cartoonish Objects?&lt;/h4&gt;

&lt;figure id=&quot;sjc_examples&quot;&gt;
  &lt;img style=&quot;width: 100%; display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/3d_diffusion/sjc_examples.png&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 6:&lt;/b&gt; Images + depth maps generated by extracting geometry from StableDiffusion, taken from &lt;a href=&quot;https://pals.ttic.edu/p/score-jacobian-chaining&quot;&gt;Score Jacobian Chaining&lt;/a&gt;.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;As you can see in the above examples, extracting geometry from image models can produce nice but cartoon-ish looking 3D models of single objects which are rather poor quality. You can get higher quality with heavily engineered approaches like  &lt;a href=&quot;https://research.nvidia.com/labs/dir/magic3d/&quot;&gt;Magic3D&lt;/a&gt;, but the algorithm is not as pretty.&lt;/p&gt;

&lt;p&gt;Why does the simple version not work that well? While no one really knows, I have some theories.
First, the 3D representation is initialized with a random NeRF, which leads to rendered images that look like random noise. In this case, the diffusion model will denoise each of these images towards a different image as opposed to different views of the same scene. This makes it difficult to get the optimization off the ground, which may lead to training instabilities and lower final quality.
Second, this approach relies on classifier-free guidance with a very high guidance weight, which decreases the variance of the distribution (and its multimodality, see the end of this blog for a further discussion).&lt;/p&gt;

&lt;h4 id=&quot;why-only-objects-what-happened-to-full-3d-scenes&quot;&gt;Why Only Objects? What Happened to Full 3D Scenes?&lt;/h4&gt;
&lt;p&gt;Beyond just the low-ish quality, the “scenes” generated by extracting geometry into a NeRF show single objects as opposed to full open-ended outdoor or indoor scenes. This is at least partly associated with the distribution of the cameras. If you are trying to model a general 3D scene (a part of a city or an apartment), the distribution of viable cameras is tightly coupled to the layout of the scene. In an apartment, say, randomly sampling cameras will yield cameras that are within walls and other objects. This will result in an empty image, which is unlikely under the model. Optimization in such a case will lead to removing any objects that occlude the scene from the camera: in this case, it will remove everything, resulting in an empty scene. This is precisely why &lt;a href=&quot;https://arxiv.org/abs/2207.13751&quot;&gt;GAUDI&lt;/a&gt; models the joint distribution of indoor scenes and camera distributions (private correspondence with the authors).&lt;/p&gt;

&lt;h4 id=&quot;view-conditioned-follow-ups&quot;&gt;View-Conditioned Follow-ups&lt;/h4&gt;

&lt;p&gt;Next, I’d like to describe RealFusion and NerfDiff: two different takes at extracting geometry from a diffusion model but in such a way that extracted geometry (NeRF) is consistent with a provided image.
&lt;a href=&quot;https://arxiv.org/abs/2302.10663&quot;&gt;RealFusion&lt;/a&gt; is a view-conditioned version of DreamFusion. It does everything that DreamFusion does, but instead of a vanilla text-to-image diffusion model, the authors use DreamBooth to constrain the diffusion model to a specific object shown by a target image. In addition to forcing the NeRF to represent that object, it should result in lower-variance gradients for the NeRF and therefore better NeRF quality.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2302.10109&quot;&gt;NerfDiff&lt;/a&gt; is similar, but instead of fitting a NeRF from scratch, the authors train a view-conditioned (amortized) NeRF. Another difference is that instead of using a pretrained text-to-image diffusion model, NerfDiff fits a custom view-conditioned (not text-conditioned) diffusion model jointly with the amortized NeRF on the target dataset of scenes. Why? Because diffusion models tend to achieve much better image quality than amortized NeRFs at the cost of not being consistent across different views. The amortized NeRF allows a fast NeRF initialization from a single image, which is then fine-tuned with distillation from the diffusion model. The authors also introduce a novel distillation algorithm that improves on DreamFusion/SJC quite a bit (but is quite a bit more expensive). NerfDiff can produce NeRFs only from images that are similar to the training images; RealFusion doesn’t have this issue because it uses a pretrained large-scale diffusion model.&lt;/p&gt;

&lt;h3 id=&quot;injecting-geometry-into-an-image-model&quot;&gt;Injecting Geometry into an Image Model&lt;/h3&gt;
&lt;p&gt;This idea is almost the polar opposite: instead of distilling geometry from the image model and putting it somewhere else, we will use our understanding of 3D geometry to guide the image model to generate images that look like they represent the same scene but are generated from different camera poses.&lt;/p&gt;

&lt;p&gt;The main insight behind the SceneScape algorithm is that an image diffusion model can correct image imperfections with its superb inpainting abilities. Now imagine that we have an image captured from a given camera position, and we pretend to move to a different camera position. Can you imagine how that image would look from the new viewpoint? You will mostly see the same things, just from a different distance and angle; some things will now be missing, and you will see some parts of the scene that you were not able to see before. It turns out that you can do this operation analytically by warping the original image into the new viewpoint. Warping results in an imperfect image:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Specularities and other view-dependent lighting effects will be incorrect.&lt;/li&gt;
  &lt;li&gt;It will have holes because not everything was observed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But mostly, the image will look ok. The diffusion model can fill in the holes, and possibly even fix the lighting artifacts: there you go, we just created a new image, taken from a different camera position, that is geometrically consistent (distances are respected) and semantically consistent (the things visible in the first image are still there and are the same). The best part? We used an off-the-shelf pretrained image model. It doesn’t even have to be a diffusion model: all we need is the inpainting ability.&lt;/p&gt;

&lt;figure id=&quot;scenescape_examples&quot;&gt;
  &lt;img style=&quot;width: 100%; display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/3d_diffusion/scenescape_example.png&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 7:&lt;/b&gt; &lt;a href=&quot;https://scenescape.github.io/&quot;&gt;SceneScape&lt;/a&gt; is a bit more advanced than the simplified algorithm described above, but the idea is the same.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h4 id=&quot;technical-scenescape-algorithm&quot;&gt;Technical: SceneScape Algorithm&lt;/h4&gt;

&lt;!-- - algorithm --&gt;
&lt;p&gt;A naive version of the &lt;a href=&quot;https://scenescape.github.io&quot;&gt;SceneScape&lt;/a&gt; algorithm requires:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;a pretrained text-to-image diffusion model capable of inpainting missing values,&lt;/li&gt;
  &lt;li&gt;a pretrained depth-from-a-single-image predictor (required for warping (above) or mesh building (below)),&lt;/li&gt;
  &lt;li&gt;a text prompt,&lt;/li&gt;
  &lt;li&gt;and optionally an image to start from,&lt;/li&gt;
  &lt;li&gt;and a method to infer intrinsic camera parameters for an RGBD image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We then do the following:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Generate an initial image (or use the one you want to start with). Initialize the camera position and orientation to an arbitrary value.&lt;/li&gt;
  &lt;li&gt;Predict the depth for that image.&lt;/li&gt;
  &lt;li&gt;Infer intrinsics for the RGBD image that you now have. You will only have to do this once as hopefully, the diffusion model will preserve the camera parameters when inpainting missing values.&lt;/li&gt;
  &lt;li&gt;Change the camera position and orientation.&lt;/li&gt;
  &lt;li&gt;Project the previously-generated RGBD images onto the new camera pose (this is where intrinsics come into play). It will contain holes.&lt;/li&gt;
  &lt;li&gt;Feed the projected RGB image into the diffusion model and fill in any missing values. Go to step 2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the paper, the authors start by generating an image from a text prompt.
Camera intrinsics are necessary to render previously-generated RGBD images onto a new camera position.
The paper assumes just an arbitrary fixed camera model, which introduces errors, but apparently the diffusion model is able to fix that, too. 
I augmented the algorithm a little to allow starting from a real image and to reduce the reprojection errors from incorrect camera intrinsics.&lt;/p&gt;

&lt;figure id=&quot;scenescape&quot;&gt;
  &lt;img style=&quot;width: 100%; display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/3d_diffusion/scenescapes.png&quot; alt=&quot;SceneScape&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 8:&lt;/b&gt; &lt;a href=&quot;https://scenescape.github.io/&quot;&gt;SceneScape&lt;/a&gt; is a bit more advanced than the simplified algorithm described above, but the idea is the same.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;!-- things needed to make it work --&gt;
&lt;p&gt;Only it turns out that there are rough edges that need to be smoothed out (as done in the paper):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Reprojection from previously captured RGBD images is not great and is much better done by building a mesh as a global scene representation.&lt;/li&gt;
  &lt;li&gt;The depth predicted from single images is inconsistent across the images (the differences between depth do not respect the changes in camera position), so the authors fine-tune the depth predictor: after projecting the mesh on a new camera they fine-tune the depth predictor to agree with the depth that came out from that projection. Once the depth predictor agrees with the mesh, we can predict the values for the holes in the depth map. This requires optimization of the depth predictor at every generated frame. The authors don’t mention how many gradient steps it takes.&lt;/li&gt;
  &lt;li&gt;The authors use StableDiffusion as their text-to-image model, which is a &lt;a href=&quot;https://arxiv.org/abs/2112.10752&quot;&gt;Latent Diffusion&lt;/a&gt; model operating on embeddings of a VAE trained with perceptual and adversarial losses. Since the VAE did not optmize reconstruction error, autoencoding results in somewhat low reconstruction quality. Therefore, to reconstruct an image that fits visually with previously-observed frame, the authors need to finetune the VAE decoder to improve its reconstruction quality. Similarly to the depth predictor, they first optimize it so that it agrees on these parts of the image that are reprojected from the mesh and then use the finetuned decoder to fill in any holes (RGB and depth will have the same holes).&lt;/li&gt;
  &lt;li&gt;Lastly, the inpainted part of the frame may not agree semantically with the text prompt very well; they generate multiple frames and then use cosine distance between the CLIP embeddings of the text and the generated frames to choose the frame that is best aligned with the prompt.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Limitations:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The mesh representation doesn’t work well for outdoor scenes (depth discontinuities between objects and the sky).&lt;/li&gt;
  &lt;li&gt;There is error accumulation in long generated sequences that sometimes lead to less-than-realistic results.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While not stated in the paper, Rafail mentioned that they finetune the depth predictor and the VAE decoder for 300 and 100 gradients steps, respectively. It takes about an hour to generate 50 frames on a Tesla V100.&lt;/p&gt;

&lt;h3 id=&quot;why-is-it-important-for-the-image-model-to-be-text-conditioned&quot;&gt;Why is it important for the image model to be text conditioned?&lt;/h3&gt;
&lt;p&gt;I left this discussion until after describing the two approaches of extracting and injecting geometry because it requires understanding some technical details about how these methods work.&lt;/p&gt;

&lt;p&gt;Generally speaking, modeling conditional probability distributions is easier than modeling unconditional ones. This may seem counter-intuitive at first, because to modal a conditional probability \(p(x \mid z)\) you have to learn the relationship between \(x\) and \(z\), which you don’t have to do if you are modeling just \(p(x)\). While that is true, \(p(x)\) is generally a much more complicated object than \(p(x \mid z)\). To see this, look at a Gaussian mixture with K components. In this case, to recover the true \(p(x)\) with a learned \(\widetilde{p}(x)\), we have to parametrize \(\widetilde{p}(x)\) with a family of distributions expressive enough to cover the 10 different modes.  If, however, we model the conditional \(p(x \mid z)\) where \(z\) now is an index telling us which mode we care about, the learned \(\widetilde{p}(x \mid z)\) has to model just one mode at a time. In this example, it can be just a Gaussian. A larger-scale example is that of ImageNet with 1000 different classes. In that case, you can think of the data distribution as a mixture of 1000 components, but now the components are very high-dimensional (images of shape 224x224x3), and the individual components are highly non-Gaussian, so the problem is much more difficult. Modeling conditionals in this case is way simpler.&lt;/p&gt;

&lt;p&gt;So what does this have to do with image models and geometry?&lt;/p&gt;

&lt;p&gt;I did some experiments with a DreamFusion-like setup, where I played with an unconditional and a view-conditional image model trained from scratch on a smaller dataset. It turns out that if the image model is unconditional, the gradients that it produces to train the NeRF point in a multitude of different directions. What happens in practice is that the NeRF initially starts to represent a scene, but eventually that scene disappears and the NeRF represents just empty space. This changes when we introduce conditioning: either a text prompt describing an object (like in DreamFusion or SJC), or an image (like in RealFusion or NerfDiff). The bottom line: too many modes lead to too high a variance of the gradients used to train the NeRF. Decreasing the number of modes leads to better-behaved gradients and thus learning.&lt;/p&gt;

&lt;p&gt;A very similar argument applies to injecting geometry into an image model. One of the limitations of SceneScape is the accumulation of errors. This is partly mitigated by generating more than just one inpainting of the image from a new camera position, and then choosing the one that best aligns with the &lt;strong&gt;text prompt&lt;/strong&gt; under CLIP similarity. So if the distribution of the image model had many more modes (if it was unconditional), it would be much more likely to inpaint missing parts of the image in a way that is not very consistent with the presented image, leading to faster error accumulation. If the model wasn’t text-conditioned, the authors couldn’t have done the CLIP trick of choosing the most suitable image in the first place, which would have significantly exacerbated the error accumulation.&lt;/p&gt;

&lt;p&gt;So we see that the ability to model insanely complex distributions (unconditional distributions of real images) is counter-productive. Perhaps that’s ok because whenever we want to generate an image, we would like to have some control over what we’re generating. However, this suggests a future failure case. As the generative models get bigger, more expressive, and trained on more data, they will represent distributions with more and more modes. This is true even for conditional models. Does it mean that, with the advances in generative modeling, the approaches of injecting and extracting geometry (and anything that requires constraining the variance of the distribution) will stop working? As with anything, there will be workarounds. But it’s an interesting failure case to keep in mind.&lt;/p&gt;

&lt;h3 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h3&gt;

&lt;p&gt;While I’m not sure what I said with this blog, what I wanted to say is this&lt;sup id=&quot;fnref:gaiman&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:gaiman&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;. There is value in making generative models. Ideally, we would be able to train such models on large datasets of 3D assets, or from videos. But this is difficult because there isn’t enough 3D data, and modeling videos while also modeling the geometry of the underlying scenes is tricky. So if it suits your application, why not try a simpler approach? Maybe you can take an off-the-shelf text-to-image diffusion model, and then massage it a bit so that it gives you a 3D model instead of just a 2D image. There you go.&lt;/p&gt;

&lt;h4 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h4&gt;
&lt;p&gt;I would like to thank &lt;a href=&quot;https://scholar.google.co.uk/citations?user=QFseZ2gAAAAJ&amp;amp;hl=en&quot;&gt;Heiko Strathmann&lt;/a&gt; and &lt;a href=&quot;https://scholar.google.com/citations?user=UGlyhFMAAAAJ&amp;amp;hl=en&quot;&gt;Danilo J. Rezende&lt;/a&gt; for numerous discussions about topics covered in this blog. I also thank &lt;a href=&quot;https://yugeten.github.io/&quot;&gt;Jimmy Shi&lt;/a&gt;, &lt;a href=&quot;https://hyunjik11.github.io/&quot;&gt;Hyunjik Kim&lt;/a&gt;, &lt;a href=&quot;https://leonard-hasenclever.github.io/&quot;&gt;Leonard Hasenclever&lt;/a&gt;, &lt;a href=&quot;http://adamgol.me/&quot;&gt;Adam Goliński&lt;/a&gt;, and Heiko for feedback on an initial version of this post.&lt;/p&gt;

&lt;p&gt;Also thanks to Rafail Fridman and &lt;a href=&quot;https://research.google/people/BenPoole/&quot;&gt;Ben Poole&lt;/a&gt; who provided feedback on the SceneScape and DreamFusion coverage in this blog, respectively.&lt;/p&gt;

&lt;h3 id=&quot;footnotes&quot;&gt;Footnotes&lt;/h3&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:prompt_engineering&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Getting a nice picture out of a text-to-image model may require tinkering with the prompt a bit. It’s not as easy as one might think. It’s called &lt;a href=&quot;https://stable-diffusion-art.com/chatgpt-prompt/&quot;&gt;prompt engineering&lt;/a&gt;. The example above works in principle because it’s just an elaborate example of prompt engineering. &lt;a href=&quot;#fnref:prompt_engineering&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:seeding_masked_parts&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;You can also place a fragment of a different image in the masked part to seed the result. E.g. in the demo above the author erases a part of the foreground, puts a lamp in there, and lets the model do its magic. The result is a lamp that fits stylistically with the rest of the image. &lt;a href=&quot;#fnref:seeding_masked_parts&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:nerf_vae_text_cond&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;We never did text-conditional modeling, but it’s easy to add text-conditioning to the prior if you have paired text-3D data. &lt;a href=&quot;#fnref:nerf_vae_text_cond&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:not_dreambooth&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;It doesn’t even have to be DreamBooth; standard text-to-image models know just as much about geometry. Unlike in DreamBooth, though, diffusion models will render different scenes for different prompts, so it’s harder to verify that different prompts do, in fact, correspond to different views. &lt;a href=&quot;#fnref:not_dreambooth&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:gaiman&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This is a paraphrase of Neil Gaiman from one of his speeches, taken from his book “The View from the Cheap Seats: Selected Nonfiction”. &lt;a href=&quot;#fnref:gaiman&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Wed, 08 Mar 2023 16:23:00 +0000</pubDate>
        <link>http://akosiorek.github.io/geometry_in_image_diffusion/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/geometry_in_image_diffusion/</guid>
        
        
        <category>ml</category>
        
      </item>
    
      <item>
        <title>Masking for Representation Learning in Vision</title>
        <description>&lt;!-- # On Masking for Representation Learning in Vision --&gt;

&lt;p&gt;Masked-image modeling (MIM) is about inpainting; that is, covering parts of an image and then trying to recover what was hidden from what is left.
Recently, it has led to state-of-the-art representation learning in images&lt;sup id=&quot;fnref:sota_repr_learn&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:sota_repr_learn&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;.
In this blog, I will dive into why masked images deliver such a powerful learning signal, think about what may constitute a good mask, and discuss my recent paper (&lt;a href=&quot;https://arxiv.org/abs/2201.13100&quot;&gt;ADIOS&lt;/a&gt;) which attempts to learn good masks for representation learning.
But let’s start with some motivation.&lt;/p&gt;

&lt;h1 id=&quot;masking-and-the-brain&quot;&gt;Masking and the Brain&lt;/h1&gt;

&lt;p&gt;Have you ever covered an object you saw with your hand and tried to imagine what the covered part looks like?
If not, why not give it a try?
You may be unable to draw or paint it since that requires considerable skill.
You may not even be able to see it clearly in your mind’s eye.
Yet, you know what it is or what it can be used for—in other words, you have a good representation of it.
Getting such representations is, roughly, the goal behind masked-image modeling (MIM).&lt;/p&gt;

&lt;p&gt;Reconstructing the hidden part from the visible parts is called image inpainting, or more generally, missing-data imputation.&lt;sup id=&quot;fnref:VAE-AC&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:VAE-AC&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;
While MIM models are usually trained via image inpainting, we will see later on that reconstruction is not always necessary for learning good represetations.
But actually, this is what your brain is doing all the time!&lt;/p&gt;

&lt;figure id=&quot;blind_spot&quot;&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/masked_image_modelling/blind_spot.webp&quot; alt=&quot;blind spot&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 1:&lt;/b&gt; Blind spot of the human eye. The illustration is thanks to &lt;a href=&quot;http://george-retseck.squarespace.com/&quot;&gt;George Retseck&lt;/a&gt;.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Each of your eyes has a visual blind spot; as shown in the figure above.
It’s roughly in the middle vertically, and slightly off-center to the outside for each eye.
You don’t see anything there because it’s the place where the optic nerve connects to the eye, leaving no place for photoreceptors.
And yet, you are unaware that any information is missing: you seem to see what is hidden.
See for yourself!&lt;/p&gt;

&lt;figure id=&quot;blind_spot_test&quot;&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/masked_image_modelling/blind_spot_test.png&quot; alt=&quot;blind spot test&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 2:&lt;/b&gt; Test your blind spot: cover your left eye, and focus your right eye on the plus (or do the opposite for the left eye). Move closer to the screen, such that the distance to your face is roughly three times the distance between symbols. Move your head back and forth. At some point, the circle should disappear. That&apos;s your blind spot!
  Inspired by &lt;a href=&quot;https://en.wikipedia.org/wiki/Blind_spot_(vision)&quot;&gt;Wikipedia&lt;/a&gt;.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If you followed the test in in Fig. 2, you know that the blind spot exist—and that we should be experiencing its effects whenever we use our eyes to observe the world.
How come this is not so?
This is where the magic of unconscious perception comes in: our brain inpaints the “blinded” area for us, to the point where we don’t even know that blind spots exist!
This may be based on what is around that area but also using the view from the other eye (novel-view synthesis&lt;sup id=&quot;fnref:nvs_brain&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:nvs_brain&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;) and what the brain is expecting to see in a given context.&lt;/p&gt;

&lt;p&gt;I expect that the ability to inpaint in the brain is not innate and that the brain has to &lt;em&gt;learn&lt;/em&gt; how to do it.
If this is the case, is this something that guides the brain in learning good visual representations?
Given the very impressive representation learning results of recent MIM models, I wouldn’t be surprised if it was the case.&lt;/p&gt;

&lt;p&gt;It is also interesting that even though the brain is really good at inpainting (people don’t usually know about their blind spots) or imagining (e.g., vivid dreams), this is not a capability we control consciously.
Think about that object you covered: you know what it is, but you probably cannot project a pixel-perfect rendering in your mind.
This is rarely problematic, because conscious reasoning relies on high-level abstractions, not pixel-level detail.
Since the representations we try to learn are usually used in such higher-level reasoning tasks, perhaps reconstruction is not the right way to go?&lt;/p&gt;

&lt;p&gt;We will come back to this question later.
For now, we will look at a few methods that do involve reconstruction.&lt;/p&gt;

&lt;h1 id=&quot;bert-or-why-inpaint-for-representation-learning&quot;&gt;BERT or Why Inpaint for Representation Learning?&lt;/h1&gt;

&lt;p&gt;Because it works—as shown by &lt;a href=&quot;https://arxiv.org/abs/1810.04805&quot;&gt;BERT of Devlin et al.&lt;/a&gt; in 2018.
BERT is a large transformer trained to fill in missing words in natural language sentences based on the available words.
Why is this useful?
Because words typically represent concrete objects or abstract entities, their properties, and relations between them.
To predict which word makes sense in the presence of other words, is to analyze what objects and with what properties are represented in that sentence, and what the relations between them are.
A model that learns to do that learns many truths about the world&lt;sup id=&quot;fnref:world_truths&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:world_truths&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;So why not do this for vision?
You can, but it is not as straightforward as pushing a masked image through a CNN.
If that’s what you do, you get the &lt;a href=&quot;https://arxiv.org/abs/1604.07379&quot;&gt;Context Encoder (CE) by Pathak et al.&lt;/a&gt;, which came out in 2016, two years before BERT.
CE used a small CNN (AlexNet-based) in an encoder-decoder setup.
The images are either masked by a single large-ish rectangle, multiple smaller rectangles, or the ground-truth segmentation mask from another image.
While the learned representations are ok, their performance is far behind supervised models of the time, even when fine-tuned.&lt;/p&gt;

&lt;figure id=&quot;context_encoder_in_out&quot;&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/masked_image_modelling/context_encoder_input_output.png&quot; alt=&quot;blind spot&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 3:&lt;/b&gt; Context Encoder; from left: masked input, reconstruction, three examples of different masks used for CE.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Why?
First, there is an architectural issue.
CNNs are great at correlating pixels.
But filling-in missing words is about reasoning about objects, parts, properties, and relations.
This is what transformers are really good at, but at the time, there was no good way of using transformers for vision.
Second, there is a representation issue.
Words in natural language are fundamentally different from pixels in images.
So masking just a few random rectangles is unlikely to bear similar results to masking words.&lt;/p&gt;

&lt;p&gt;It was the &lt;a href=&quot;https://arxiv.org/abs/2111.06377&quot;&gt;Masked Autoencoder (MAE) by He et al.&lt;/a&gt; that finally proved that image inpainting can lead to state-of-the-art representations for images.
Coming five years after CE, it brought in recent advances.
The encoder is a large vision transformer (&lt;a href=&quot;https://arxiv.org/abs/2010.11929&quot;&gt;ViT, Dosovitskiy et al.&lt;/a&gt;).
The image is split into a rectangular grid, as in ViT, and a number of grid elements are masked.
This paper provides two insights:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The representation quality improves with the fraction of the image masked (up to a point).&lt;/li&gt;
  &lt;li&gt;Instead of feeding an image with masked parts to the encoder, it is better to just not use the masked parts as an input&lt;sup id=&quot;fnref:not_feeding_masked_patches&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:not_feeding_masked_patches&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;.
This is easy to do for an image-divided-into-patches and a transformer like in ViT, but next to impossible for a CNN.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure id=&quot;mae&quot;&gt;
  &lt;img style=&quot;width: 75%; display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/masked_image_modelling/mae.png&quot; alt=&quot;MAE architecture&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 4:&lt;/b&gt; MAE architecture; note that the masked patches are not fed into the encoder.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;MAE masks consist of small, randomly-scattered rectangles corresponding to the ViT image patches.
They cover 75% of the image, which is significantly more than in CE&lt;sup id=&quot;fnref:scattered_mask&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:scattered_mask&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Why is this important?
Because masking a large proportion of the image makes it more likely to mask visual words.&lt;/p&gt;

&lt;h1 id=&quot;what-is-a-visual-word&quot;&gt;What is a Visual Word?&lt;/h1&gt;

&lt;p&gt;A word typically represents an entity, its property, or a relation between entities.
A pixel represents a color.
A visual word is a group of pixels, but it is not a random group.
Rather, it’s a group of pixels that represents something meaningful like an object, but also a property or a relation.&lt;sup id=&quot;fnref:pixel_representing_relation&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:pixel_representing_relation&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;7&lt;/a&gt;&lt;/sup&gt;.
Imagine a man wearing a red jacket.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;To mask “red”, we need to occlude most of a jacket, perhaps leaving its outline.&lt;/li&gt;
  &lt;li&gt;To mask “jacket” without masking its color, we can mask its outline but leave a pixel here or there.&lt;/li&gt;
  &lt;li&gt;To mask the fact that someone is wearing the jacket, we need to mask out a person while leaving fragments of the jacket.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Such groupings are far from random and are extremely unlikely to occur with random masks.
Masking a significant area of the image, like in MAE, makes it easier to occlude visual words (whole entities, say).
As the paper shows, such masks are also better for representation learning.
Still, masking properties or relations remains difficult under that scheme.&lt;/p&gt;

&lt;h1 id=&quot;finding-visual-words&quot;&gt;Finding Visual Words&lt;/h1&gt;

&lt;p&gt;Let’s assume that, for representation learning, masking single words in natural language sentences is the best thing to do.
How do we get such visual-word masks for images?&lt;/p&gt;

&lt;p&gt;We would need to identify image regions that are similar in meaning to words.
Object bounding boxes or segmentation masks would be a good choice if not for two issues.
First, they usually cover objects, with no masks or boxes describing relations between objects or parts thereof&lt;sup id=&quot;fnref:mask_editing&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:mask_editing&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;8&lt;/a&gt;&lt;/sup&gt;.
Second, they are human-generated, which defeats the purpose of unsupervised learning.
Let’s explore alternatives.&lt;/p&gt;

&lt;h4 id=&quot;visual-words-from-before-deep-learning&quot;&gt;Visual Words from Before Deep-Learning&lt;/h4&gt;
&lt;p&gt;The concept of a visual word has been studied before in the pre-deep-learning era.
Inspired by &lt;a href=&quot;https://en.wikipedia.org/wiki/Bag-of-words_model&quot;&gt;bag-of-words&lt;/a&gt; classifiers for natural language (e.g., an SVM operating on word histograms, the so-called bags of words), people constructed &lt;a href=&quot;https://medium.com/analytics-vidhya/bag-of-visual-words-bag-of-features-9a2f7aec7866&quot;&gt;visual bag-of-words&lt;/a&gt; classifiers.&lt;/p&gt;

&lt;figure id=&quot;visual_bag_of_wrds&quot;&gt;
  &lt;img style=&quot;display: box; margin: auto; width: 65%;&quot; src=&quot;http://akosiorek.github.io/resources/masked_image_modelling/bag_of_visual_words.png&quot; alt=&quot;visual bag of words&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 5:&lt;/b&gt; The visual bag-of-words framework.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Dictionaries of visual words were built by running a &lt;a href=&quot;http://www.cs.ubc.ca/~lowe/papers/ijcv04.pdf&quot;&gt;SIFT&lt;/a&gt; or SURF keypoint detector on a dataset of images, describing these keypoints with relevant descriptors (&lt;a href=&quot;http://www.cs.ubc.ca/~lowe/papers/iccv99.pdf&quot;&gt;SIFT&lt;/a&gt;, SURF, &lt;a href=&quot;https://en.wikipedia.org/wiki/Histogram_of_oriented_gradients&quot;&gt;HOG&lt;/a&gt;), and then clustering them.
The cluster centroids represented a new visual grammar.
A new image could be classified by creating a histogram of such visual words and feeding it into an SVM, say.
A visual word like that could correspond to an eye or a car wheel.
While I haven’t tried it, it would be interesting to adapt this paradigm for MIM.&lt;/p&gt;

&lt;h4 id=&quot;learning-visual-words&quot;&gt;Learning Visual Words&lt;/h4&gt;
&lt;p&gt;The modern alternative is to learn what a visual word is. To understand how visual words can be learned, let’s think about what categories of masks we can expect.
We can do it by looking at some air balloons.&lt;/p&gt;

&lt;figure id=&quot;masked_balloons&quot;&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/masked_image_modelling/masked_balloons.png&quot; alt=&quot;masked balloons&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 6:&lt;/b&gt; Inpainting a part of an object or background is easy. Inpainting a whole object is difficult. Adapted from &lt;a href=&quot;https://slideslive.com/38930701/what-are-objects&quot;&gt;Klaus Greff&apos;s talk &quot;What are Objects?&quot;&lt;/a&gt;.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;!-- Look at the air balloon figure above. --&gt;
&lt;p&gt;If you occlude a piece of the background, in this case, an empty sky, you can easily fill that piece in.
If you hide a random part of an object, you can easily imagine that hidden part—its contents are largely defined by the visible parts of the object.
If you hide a semantically-meaningful piece of an object, e.g., the balloon part of an air balloon, you have a somewhat harder task.
Now you know that there should be a balloon because you can see a basket.
Based on the context, you know that it probably belongs under a balloon.
But the balloon can have a range of sizes and can be painted in many different ways, which increases the difficulty of the task.
Finally, you can hide the whole object.
This is virtually indistinguishable from hiding a piece of the background.
You will have a hard time figuring out what the object was or if there was an object at all.
The only way to do this is to check if it would make sense for any particular object to be there, given the visible surroundings.&lt;/p&gt;

&lt;p&gt;This gradation of difficulty in different masking scenarios stems from the fact that some pixels are predictable&lt;sup id=&quot;fnref:correlated&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:correlated&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;9&lt;/a&gt;&lt;/sup&gt; from each other, while others are not.
For a longer discussion, see section 4.1.1. of &lt;a href=&quot;https://arxiv.org/abs/2012.05208&quot;&gt;Greff et al, “On the Binding Problem in Artificial Neural Networks”&lt;/a&gt;.
For now:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Pixels belonging to an object are strongly correlated with each other.&lt;/li&gt;
  &lt;li&gt;Pixels belonging to different objects or an object and the background are not correlated or are correlated only very weakly&lt;sup id=&quot;fnref:bg_correlation&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:bg_correlation&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;10&lt;/a&gt;&lt;/sup&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By now, this is a widely-accepted view.
I would go a step further and say that pixels representing a relation&lt;sup id=&quot;fnref:pixel_representing_relation:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:pixel_representing_relation&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;7&lt;/a&gt;&lt;/sup&gt; (e.g., two objects that often appear together), or a property, are also strongly correlated; therefore, they are possible to infer from a partial observation.&lt;/p&gt;

&lt;p&gt;The above intuition can be formalized as a training objective.
This is exactly what we do in &lt;a href=&quot;https://arxiv.org/abs/2201.13100&quot;&gt;Shi et al., “Adversarial Masking for Self-Supervised Learning”, ICML 2022&lt;/a&gt; (&lt;a href=&quot;https://github.com/YugeTen/adios&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;code&lt;/code&gt;&lt;/a&gt;).&lt;/p&gt;

&lt;h1 id=&quot;adversarial-inference-occlusion-self-supervision-adios&quot;&gt;&lt;strong&gt;Ad&lt;/strong&gt;versarial &lt;strong&gt;I&lt;/strong&gt;nference-&lt;strong&gt;O&lt;/strong&gt;cclusion &lt;strong&gt;S&lt;/strong&gt;elf-supervision (ADIOS)&lt;/h1&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2201.13100&quot;&gt;ADIOS&lt;/a&gt; is a reconstruction-free MIM model that learns to mask in an adversarial fashion.&lt;/p&gt;

&lt;p&gt;Imagine a setup where you try to inpaint an image with some parts occluded.
To get the mask, we instantiate a masking model whose job is to make inpainting as difficult as possible, subject to some constraints (see below).
The result?
You get masks that seem to hide objects or their parts.
You also get better representation learning results than with using MAE’s masks&lt;sup id=&quot;fnref:learned_masks_for_mae&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:learned_masks_for_mae&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;11&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;What constrains masking whole objects, or the entire image for that matter?
First, we predict several masks while making sure that each pixel is masked only once.
Second, we penalize the masks so that they cannot be all black or all white.
These two constraints mean that none of the predicted masks can cover the whole image and that the image must be partitioned between all masks.
Third, there are built-in inductive biases in the form of the masking net architecture (Convolutional UNet pays more attention to texture than semantics) and the encoder architecture (ViT seems to result in masks that look more semantically-meaningful than when a ResNet is used).&lt;/p&gt;

&lt;p&gt;Recall that MIM models are trained by reconstructing occluded images, similar to how the brain inpaints the visual blind spot.
But since we are not interested in pixel-perfect detail but rather high-level, conscious-like reasoning abilities, we may be able to get away without reconstruction.
That’s why we resort to reconstruction-free representation learning (RFL)&lt;sup id=&quot;fnref:RFL&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:RFL&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;12&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;figure id=&quot;adios_masks&quot;&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/masked_image_modelling/adios_masks.png&quot; alt=&quot;ADIOS masks&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 7:&lt;/b&gt; Masks generated by ADIOS on the &lt;a href=&quot;https://cs.stanford.edu/~acoates/stl10/&quot;&gt;STL-10 dataset&lt;/a&gt;. There are six color-coded masks for each image. While some parts appear random, some clearly cover object parts.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;ADIOS applies to any siamese-style representation learning algorithm (contrastive or otherwise) where the training is done by minimizing some distance between representations.
Here we compare a generic algorithm with its ADIOS-augmented version.
The ADIOS-specific parts are highlighted in green.&lt;/p&gt;

&lt;div&gt;
  &lt;div style=&quot;float: left; width: 50%;&quot;&gt;
    &lt;center&gt;&lt;b&gt;Generic Reconstruction-Free Learning&lt;/b&gt;&lt;/center&gt;
    &lt;ol&gt;
      &lt;li&gt;Take an image x.&lt;/li&gt;
      &lt;li&gt;Create two views of that image, a and b.&lt;/li&gt;
      &lt;li&gt;&lt;span style=&quot;color:green&quot;&gt;skip&lt;/span&gt;&lt;/li&gt;
      &lt;li&gt;Encode the views with a neural net with parameters 	&amp;theta; to get two representations z_a and z_b.&lt;/li&gt;
      &lt;li&gt;Compute a loss L(z_a, z_b).&lt;/li&gt;
      &lt;li&gt;Update the parameters of the neural net(s) by minimising that loss with respect to &amp;theta;.&lt;/li&gt;
    &lt;/ol&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &lt;/div&gt;
  &lt;div style=&quot;float: right; width: 50%;&quot;&gt;
  &lt;center&gt;&lt;b&gt;ADIOS&lt;/b&gt;&lt;/center&gt;
  &lt;ol&gt;
    &lt;li&gt;Take an image x.&lt;/li&gt;
    &lt;li&gt;Create two views of that image, a and b.&lt;/li&gt;
    &lt;li&gt;&lt;span style=&quot;color:green&quot;&gt;Predict a mask m = mask(b) with a neural net with parameters &amp;phi;. Apply that mask to b.&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;Encode the views with a neural net with parameters 	&amp;theta; to get two representations z_a and z_b.&lt;/li&gt;
    &lt;li&gt;Compute a loss L(z_a, z_b).&lt;/li&gt;
    &lt;li&gt;Update the parameters of the neural net(s) by minimising that loss with respect to &amp;theta;&lt;span style=&quot;color:green&quot;&gt; and maximising with respect to &amp;phi;&lt;/span&gt;.&lt;/li&gt;
  &lt;/ol&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;In ADIOS, we want one of the image views, say b, to be masked.
The mask m = mask(b) is conditioned on the image and is predicted by another neural net with parameters \(\phi\).
We get a masked image \(b^m = b \circ m\) by applying the mask to the image (via element-wise multiplication \(\circ\)), and extract representation \(z_b^m\).
At the end, in addition to updating the encoder’s parameters, we also update the parameters of the masking neural net by maximizing the loss L with respect to \(\phi\).&lt;/p&gt;

&lt;p&gt;That’s it! It’s simple, isn’t it? A cool thing is that is works with many different RFL objectives (we tried BYOL, SimCLR, and SimSiam), and it improves representation learning performance on every dataset and task we tried.
Additionally, ADIOS improves robustness to non-adversarial attacks (e.g., changing the background behind an object), presumably due to decreasing sensitivity to spurious correlations (these are often masked separately from the object due to the correlation structure discussed above).&lt;/p&gt;

&lt;h1 id=&quot;how-does-masking-apply-to-reconstruction-free-learning-rfl&quot;&gt;How Does Masking Apply to Reconstruction-Free Learning (RFL)?&lt;/h1&gt;
&lt;p&gt;RFL minimizes the distance between representations extracted from two views of the same image.
That distance is minimized when the encoders are invariant to the transformations applied to the source image.
Here is a simple example: if we use a color image and a grayscale version of that same image, we will get a representation that encodes the content (e.g., objects) and even brightness, but not the hue.
Hence, we say, the representation is invariant to hue variations.
See &lt;a href=&quot;https://fabianfuchsml.github.io/equivariance1of2/&quot;&gt;Fabian Fuchs’ blog&lt;/a&gt; for a longer discussion of equivariance and invariance.&lt;/p&gt;

&lt;p&gt;Using a masked image as one of the views means that we want a representation that is invariant to masking.
There are two ways to do this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Ignore any region that can be masked.&lt;/li&gt;
  &lt;li&gt;If a region is masked, try to predict what was there before masking.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Option 1. means encoding no information (representation collapse) and is usually incompatible with any good learning objective.
That leaves option 2. and forces the model to reason about occluded parts.
The masking model is trying to make 2. more difficult. Hence it learns to mask strongly-correlated groups of pixels, which often correspond to semantically-meaningful object parts, but do not necessarily correspond to objects—as discussed.&lt;/p&gt;

&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;/h1&gt;

&lt;p&gt;That’s it! If you got this far, you learned about visual blind spots and (hopefully) found your own, which gives you a pretty good idea how much inpainting our brains do.
This is similar to masking and then inpainting images, which leads to some state-of-the-art representation learning.
You also know that semantically-meaningful masks lead to even stronger results than random masks, and you’ve seen a couple of ways to get such masks.&lt;/p&gt;

&lt;p&gt;So is pixel-level reconstruction the right way to go if you want to get good representations?
While we do not have a definitive answer, we show through ADIOS that reconstructions are not always necessary, and that the motivation behind reconstruction-based MIM models does extend to the reconstruction-free setting.&lt;/p&gt;

&lt;p&gt;If you’re interested in more details behind ADIOS, have a look at the &lt;a href=&quot;https://arxiv.org/abs/2201.13100&quot;&gt;paper&lt;/a&gt;, and play with the &lt;a href=&quot;https://github.com/YugeTen/adios&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;code&lt;/code&gt;&lt;/a&gt;!
Here are a few things you could try:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Figure out how to learn masks for MAE without processing the whole image with the encoder, and perhaps with higher granularity than afforded by masking individual patches.&lt;/li&gt;
  &lt;li&gt;Experiment with stronger inductive biases for the masking model like &lt;a href=&quot;https://proceedings.neurips.cc/paper/2020/hash/8511df98c02ab60aea1b2356c013bc0f-Abstract.html&quot;&gt;slot-attention&lt;/a&gt; or &lt;a href=&quot;https://arxiv.org/abs/1907.13052&quot;&gt;GENESIS&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Further reading:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2206.10207&quot;&gt;SemMAE&lt;/a&gt;, which came out a few days ago, provides an alternative way of learning visual-word-like masks by using arg-maxed attention from another transformer.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2012.05208&quot;&gt;“On the Binding Problem in Artificial Neural Networks”&lt;/a&gt; from &lt;a href=&quot;https://qwlouse.github.io/&quot;&gt;Klaus Greff&lt;/a&gt; and
&lt;a href=&quot;https://www.sjoerdvansteenkiste.com/&quot;&gt;Sjoerd van Steenkiste&lt;/a&gt; discusses at length what objects are and how to represent them in neural networks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h4&gt;
&lt;p&gt;Huge thanks to &lt;a href=&quot;https://yugeten.github.io/&quot;&gt;Yuge Shi&lt;/a&gt; for doing most of the work behind the ADIOS paper.
I would also like to thank &lt;a href=&quot;https://yugeten.github.io/&quot;&gt;Yuge Shi&lt;/a&gt;, &lt;a href=&quot;https://shhuang.github.io/&quot;&gt;Sandy Huang&lt;/a&gt;, &lt;a href=&quot;https://fabianfuchsml.github.io/&quot;&gt;Fabian Fuchs&lt;/a&gt;, &lt;a href=&quot;https://qwlouse.github.io/&quot;&gt;Klaus Greff&lt;/a&gt;, and &lt;a href=&quot;https://www.sjoerdvansteenkiste.com/&quot;&gt;Sjoerd van Steenkiste&lt;/a&gt; for proofreading and providing helpful suggestions for this blog.&lt;/p&gt;

&lt;h4 id=&quot;footnotes&quot;&gt;Footnotes&lt;/h4&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:sota_repr_learn&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2111.06377&quot;&gt;MAE&lt;/a&gt;, &lt;a href=&quot;https://arxiv.org/abs/2106.08254&quot;&gt;BEiT&lt;/a&gt;, &lt;a href=&quot;https://arxiv.org/abs/2206.10207&quot;&gt;SemMAE&lt;/a&gt; as well as our paper &lt;a href=&quot;https://arxiv.org/abs/2201.13100&quot;&gt;ADIOS&lt;/a&gt;, which is discussed further below. &lt;a href=&quot;#fnref:sota_repr_learn&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:VAE-AC&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1806.02382&quot;&gt;“Variational Autoencoder with Arbitrary Conditioning” by Ivanov et al.&lt;/a&gt; was the first paper that got me thinking about image inpainting. &lt;a href=&quot;#fnref:VAE-AC&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:nvs_brain&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2206.06922&quot;&gt;Sajjadi et al.&lt;/a&gt; show that novel-view synthesis helps to learn object segmentation in an unsupervised setting. A long shot and a topic for another blog, but I wonder if the blind-spot inpainting in the brain could help with object perception. &lt;a href=&quot;#fnref:nvs_brain&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:world_truths&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I know this may sound unscientific and overly hyped. It is. I like this rhetoric, though. &lt;a href=&quot;#fnref:world_truths&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:not_feeding_masked_patches&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;If the masked patches are used as input, the model has to learn to ignore them. Since MAE masks 75% of the image, there is probably no benefit to representing which areas of the image are masked (they are represented implicitly, since there is no contribution from the masked patches). By asking the model to learn-to-ignore, we are wasting model capacity while also risking falling into a local minimum where the masked patches are not totally ignored. Note that in transformers we can hardcode to ignore masked patches while feeding them as input, but this is more computationally-expensive and requires changing the implementation; for a convnet this may be impossible. &lt;a href=&quot;#fnref:not_feeding_masked_patches&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:scattered_mask&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;It is unclear what the impact of such scattered masks is. It might force the model to reason about multiple things in every image. It may also reduce the variance of the gradients because total occlusion of a certain object is less likely with such scattered masks. &lt;a href=&quot;#fnref:scattered_mask&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:pixel_representing_relation&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://www.sjoerdvansteenkiste.com/&quot;&gt;Sjoerd van Steenkiste&lt;/a&gt; pointed out that there may be no such thing as a pixel representing a relation, e.g., no group of pixels may represent “heavier than” or even “bigger than”. While I agree, I’d like to note that masking pixels can obscure such relations. In case of “bigger than”, a mask can occlude a part of an object making its size difficult to determine. This may be useful for representation learning. &lt;a href=&quot;#fnref:pixel_representing_relation&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt; &lt;a href=&quot;#fnref:pixel_representing_relation:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:mask_editing&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The latter could be perhaps circumvented by editing ground-truth masks, e.g., taking a union of two object masks, diluting or eroding masks, etc. &lt;a href=&quot;#fnref:mask_editing&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:correlated&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I usually use “correlated” to describe such pixels, but as helpfully pointed out by &lt;a href=&quot;https://qwlouse.github.io/&quot;&gt;Klaus Greff&lt;/a&gt;, this is wrong, because it relates to particular pixel values and not to random variables as such. Instead of “correlated”, it is more accurate to say that such pixels have high pointwise mutual information. “Predictable” here is a shorthand. &lt;a href=&quot;#fnref:correlated&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:bg_correlation&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;See that, according to above, the background behaves just like a big object behind the objects in the foreground. &lt;a href=&quot;#fnref:bg_correlation&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:learned_masks_for_mae&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The caveat is that using such learned masks requires feeding the whole image into the encoder. This results in a significantly increased computation cost for MAE and might not be practical. &lt;a href=&quot;#fnref:learned_masks_for_mae&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:RFL&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;While I don’t like creating acronyms, I find that the currently available options are somewhat lacking. All representation learning algorithms we care about are unsupervised (self-supervised &lt;strong&gt;is&lt;/strong&gt; unsupervised). The ones that require image reconstruction (inpainting, e.g., MAE) use one encoder and one decoder. The ones that do not require reconstruction (e.g., SimCLR) use two encoders and no decoder. The latter were called contrastive (but some methods do not use negative examples) and later self-supervised learning (SSL; but this is too broad since MAE is also SSL). Hence, I adopt “reconstruction-free learning (RFL)” to distinguish these two paradigms. An alternative that focuses on architecture would be “Siamese-Style Learning”—maybe this is better because it uses the same acronym? &lt;a href=&quot;#fnref:RFL&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Mon, 04 Jul 2022 10:59:00 +0000</pubDate>
        <link>http://akosiorek.github.io/masking_repr_learning_vision/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/masking_repr_learning_vision/</guid>
        
        
        <category>ml</category>
        
      </item>
    
      <item>
        <title>Machine Learning of Sets</title>
        <description>&lt;p&gt;In machine learning, we typically work with input pairs (x, y), and we try to figure out how x and y depend on each other.
To do so, we gather many such pairs and hope that the dependence will reveal itself if a) we have enough data, b) our model is expressive enough to approximate this dependency, and c) we get the hyperparameters right.
In the simplest case, both x and y are just scalar values (or vectors \(\mathbf{x}, \mathbf{y}\)); for example, given some measurements of a plant’s shape, we might want to predict its species. The measurements here are real vectors \(\mathbf{x} \in \mathcal{X}\), where the input space \(\mathcal{X} = \mathbb{R}^d\) is usually Euclidean, and the species is a label \(\mathbf{y} \in \mathcal{Y}\) (usually an integer or a one-hot vector), but it is common for \(\mathbf{x}\) and \(\mathbf{y}\) to have more structure.&lt;/p&gt;

&lt;p&gt;One of the main assumptions we rely on is that the pairs of (x, y) points are &lt;a href=&quot;https://en.wikipedia.org/wiki/Independent_and_identically_distributed_random_variables&quot;&gt;independent and identically distributed (i.i.d.) random variables&lt;/a&gt;.
Let us unpack this a bit, starting from the end,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random variable&lt;/code&gt;: there exists some stochastic generative process from which the variables were randomly sampled,&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;identically&lt;/code&gt;: all samples come from the same probability distribution,&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;independent&lt;/code&gt;: the generative process has no memory of generated samples, and hence any generated sample does not change the distribution over future generated samples.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any structure in \(\mathbf{x}, \mathbf{y}\), or both introduces constraints, and a successful application of an algorithm to a particular problem does heavily depend on whether or not this algorithm takes the relevant constraints into account.
A common constraint in image-related problems is translation equivariance&lt;sup id=&quot;fnref:cnnequiv&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:cnnequiv&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;—the output of the algorithm should shift with any shifts applied to the image (you can read more about equvariances in &lt;a href=&quot;https://fabianfuchsml.github.io/equivariance1of2/&quot;&gt;this excellent blog post&lt;/a&gt;).
In natural language-related problems, a typical constraint is causality: a token at position t can depend on any previous tokens at position 1:t-1, but it cannot depend on any future tokens&lt;sup id=&quot;fnref:languecausality&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:languecausality&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;In the above examples, the dependencies between points (e.g., autoregressive dependence in NLP) are clear from the context.
However, if a data point is not a vector, matrix, or a sequence of vectors, but it is a &lt;strong&gt;set of vectors&lt;/strong&gt;, these dependencies become less clear.
In particular, elements in an input set resemble elements in a dataset (i.e., lack of order), but the critical difference is that they are &lt;strong&gt;not independent&lt;/strong&gt;, therefore breaking the i.i.d. assumption.
Accounting for this specific structure in inputs or outputs of an ML model leads to a family of set learning problems, which have recently gained considerable attention in the machine learning community.
I thought it would be useful to delve into the machine learning of sets.
In the following, we will consider set-to-vector, vector-to-set, and set-to-set problems and provide implementations of simple algorithms in &lt;a href=&quot;https://github.com/google/jax&quot;&gt;JAX&lt;/a&gt; and &lt;a href=&quot;https://github.com/deepmind/dm-haiku&quot;&gt;haiku&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First some imports:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import jax
import jax.numpy as jnp
import haiku as hk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;notation&quot;&gt;Notation&lt;/h1&gt;
&lt;p&gt;Before we start, it is useful to introduce some notation.
Let \(\mathbf{x} \in \mathbb{R}^d\) be an input vector, \(\mathbf{y} \in \mathbb{R}^k\) the output vector, and let \(X = \{\mathbf{x}_i\}_{i=1}^M\) and \(Y = \{\mathbf{y}_j\}_{j=1}^N\) be sets of \(M\) and \(N\) elements, respectively.
Note that, until now, \(y\) or \(\mathbf{y}\) were simply labels.
From now on, however, \(\mathbf{x}\) and \(\mathbf{y}\) can live in the same space, and simply be elements of different sets.
I will also use \(\mathcal{L}(X, Y)\) as a loss function operating on two sets, and \(l(\mathbf{x}, \mathbf{y})\) will be a loss function for pairs of elements.&lt;/p&gt;

&lt;h1 id=&quot;set-to-vector&quot;&gt;Set To Vector&lt;/h1&gt;
&lt;p&gt;This is perhaps the simplest set-learning problem since it only requires permutation invariance.
A function \(f\) is invariant to permutations \(\pi\) if \(\forall \pi\): \(f(X) = f(\pi X)\).
Permutation invariance has always been known in machine learning, as loss functions we use almost never&lt;sup id=&quot;fnref:acn&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:acn&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; depend on the ordering of elements in our datasets or minibatches.
This is not for the lack of order: to create a minibatch, we stack multiple data elements in an array; this pairs every element in the minibatch with its minibatch index, therefore implicitly creating an order.
Loss functions tend to discard information about the order, usually by taking the mean over data examples.
We can create permutation-invariant functions by following a similar logic.&lt;/p&gt;

&lt;p&gt;Examples in a minibatch are processed independently (which reflects their i.i.d. nature), but if each entry in the minibatch contains more than just a single data point (many pixels in an image, points in a point cloud, tokens in a language sentence), then flattening these points into a vector and feeding it into an MLP or a CNN results in different parameters being used for processing different data points, and hence order is used implicitly; feeding the points into an RNN reuses parameters, but introduces an explicit dependence on the order.&lt;/p&gt;

&lt;p&gt;A straightforward solution to this issue is to treat points in a single example in the same way we treat examples in the minibatch: treat them independently.
This approach, followed by a permutation-invariant pooling operation such as max or mean pooling, is explored in &lt;a href=&quot;https://arxiv.org/abs/1703.06114&quot;&gt;Zaheer et al., “Deep Sets”, NeurIPS 2017&lt;/a&gt; and is proven to be a universal set-function approximator&lt;sup id=&quot;fnref:deepsetdim&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:deepsetdim&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class DeepSet(hk.Module):

  def __init__(self, encoder, decoder):
    super().__init__()
    self._encoder = encoder
    self._decoder = decoder
    
  def __call__(self, x):
    &quot;&quot;&quot;Compute the DeepSet embedding.

    Args:
      x: Tensor of shape [batch_size, n_elems, n_dim].
    &quot;&quot;&quot;
    return self._decoder(self._encoder(x).mean(1))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While newer approaches with better empirical performance exist, they all draw from the Deep Sets framework&lt;sup id=&quot;fnref:setembeddings&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:setembeddings&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;.
Another factor contributing to the fact that the set-to-vector problem is quite easy is that pooling operations naturally work with variable-sized sets–there is nothing extra we have to do to handle sets of variable cardinality.
This is not the case in the following two problems, where we have to take the set size into account explicitly.&lt;/p&gt;

&lt;h1 id=&quot;vector-to-set&quot;&gt;Vector To Set&lt;/h1&gt;
&lt;p&gt;In vector-to-set, the task is to generate a set of real vectors from some (usually vector-valued) conditioning.&lt;/p&gt;

&lt;p&gt;The majority of approaches out there focus on generating ordered sequences instead of unordered sets, and usually of fixed or at least known size.
This allows using MLPs&lt;sup id=&quot;fnref:setae&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:setae&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;6&lt;/a&gt;&lt;/sup&gt; and RNNs&lt;sup id=&quot;fnref:order_matters&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:order_matters&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;7&lt;/a&gt;&lt;/sup&gt; to predict fixed- and variable-length sets, respectively, but at the price of having to learn permutation-equivariance from data.
Learning permutation-equivariance can be induced by data augmentation. It is easy to generate different permutations, but usually comes at a decreased performance and/or longer training times compared to truly permutation-equivariant methods&lt;sup id=&quot;fnref:data_augmentation&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:data_augmentation&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;8&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  def set_mlp(conditioning, decoder, n_elements):
    &quot;&quot;&quot;Predicts a set.

    Args:
      conditioning: tensor of shape [batch_size, n_dim].
      decoder: callable, e.g. an MLP.
      n_elements: int.
    &quot;&quot;&quot;
    z = decoder(conditioning)
    batch_size = conditioning.shape[0]
    # all we can do here is reshape!
    return z.reshape(batch_size, n_elements, -1)

  def set_rnn(conditioning, state, rnn, n_elements):
    &quot;&quot;&quot;Predicts a set.

    Args:
      conditioning: tensor of shape [batch_size, n_dim].
      state: initial state for the rnn.
      rnn: rnn core.
      n_elements: int.
    &quot;&quot;&quot;
    zs = []
    for _ in range(n_elements):
      z, state = rnn(conditioning, state)
      zs.append(z[:, None])  # add an axis

    return jnp.concatenate(zs, 1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;permutation-invariant-loss-functions&quot;&gt;Permutation-Invariant Loss Functions&lt;/h4&gt;
&lt;p&gt;Learning to generate sets based on some conditioning typically requires scoring that set against the conditioning.
If we have ground-truth sets at our disposal, we can compare the generated sets against the ground-truth ones for the same conditioning.
This can take the form of supervised learning (think of detecting objects in an image, where we need to generate a set of bounding boxes) or unsupervised learning (autoencoding point-clouds, say).
Since we generally have no guarantee that the generated sets will obey any ordering (why should they?), we have to apply losses invariant to that ordering.
We have two options here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We can find an optimal matching between two sets&lt;sup id=&quot;fnref:bipartite_matching&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:bipartite_matching&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;9&lt;/a&gt;&lt;/sup&gt;, which comes down to finding a permutation \(\pi\) of one of the sets that minimizes the computed loss, that is: \(\pi^\star = \arg \min_\pi \mathcal{L}( \pi X, Y)\), with \(\mathcal{L}( \pi X, Y) = \sum_i l(\mathbf{x}_{\pi(i)}, \mathbf{y}_i)\). This can be done exactly using the cubic &lt;a href=&quot;https://en.wikipedia.org/wiki/Hungarian_algorithm&quot;&gt;Hungarian matching&lt;/a&gt; algorithm, or approximately using e.g. &lt;a href=&quot;https://arxiv.org/abs/1106.1925&quot;&gt;optimal-transport&lt;/a&gt;- or &lt;a href=&quot;https://web.stanford.edu/~bayati/papers/bpmwmIT.pdf&quot;&gt;message-passing&lt;/a&gt;-based algorithms.&lt;/li&gt;
  &lt;li&gt;Instead of finding a matching, we can find a lower bound on what the matched loss would be. A popular choice here is the Chamfer loss&lt;sup id=&quot;fnref:chamfer&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:chamfer&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;10&lt;/a&gt;&lt;/sup&gt;, which computes \(\sum_{x \in X} \min_{y \in Y} l(x, y) + \sum_{y \in Y} \min_{x \in X} l(x, y)\). For every element in one set, it finds the element in the other set that results in the lowest pairwise loss. This loss does not work for multisets as elements can be repeated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we do not have ground-truth for each conditioning (we have just sets), or if we have many possible sets for each conditioning (e.g., a group of possible sets for one of a few labels), we can instead learn by matching distributions e.g., in the GAN setting.
If we take this approach, we have two problems, really: that of vector-to-set for the generator and set-to-vector for the discriminator. 
Fortunately, we know how to solve the set-to-vector problem with a permutation-invariant neural net, and shortly I am going to describe some permutation-equivariant methods for generation.
This is precisely what we recently explored in &lt;a href=&quot;https://oolworkshop.github.io/program/ool_32.html&quot;&gt;Stelzner et al., “Generative Adversarial Set Transformers”, ICML 2020 Object-Oriented Learning Workshop&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Coincidentally, sometimes we have to deal with a set of latent variables inside a model. For example in Attend-Infer-Repeat (AIR, &lt;a href=&quot;https://papers.nips.cc/paper/6230-attend-infer-repeat-fast-scene-understanding-with-generative-models&quot;&gt;paper&lt;/a&gt;, &lt;a href=&quot;http://akosiorek.github.io/ml/2017/09/03/implementing-air.html&quot;&gt;blog&lt;/a&gt;), a set of object-centered latent variables was used to render an image.
We did not need to worry about permutations of these variables, though, since the rendering process was permutation-invariant, and any loss applied to the final image carried over to the latent variables in a permutation-invariant way, too!&lt;/p&gt;

&lt;h4 id=&quot;gradient-descent-to-the-rescue&quot;&gt;Gradient Descent to the Rescue!&lt;/h4&gt;
&lt;p&gt;Until recently, there was no accepted method able to predict variable-sized sets in a permutation-equivariant manner.
For completness, note that a function g is equivariant to permutations \(\pi\) if \(\forall \pi\): \(\pi g(X) = g(\pi X)\).
&lt;a href=&quot;https://arxiv.org/abs/1906.06565&quot;&gt;Zhang et al., “Deep Set Prediction Networks”, NeurIPS 2019&lt;/a&gt; used the well-known (but still pretty cool!) observation that the gradient of a permutation-invariant function (such as the DeepSet embedding) is permutation equivariant to the input set&lt;sup id=&quot;fnref:invgrad&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:invgrad&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;11&lt;/a&gt;&lt;/sup&gt;.
Their introduced model, DSPN, uses a fixed initial set adapted via a nested loop of gradient-descent on a learned loss function.
This loss function compares the currently-generated set and the conditioning, telling us how well the current set and the conditioning match.
DSPN achieved quite good results on point-cloud generation (but only MNIST) and showed proof-of-concept results to object detection in images.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class DeepSetPredictionNetwork(hk.Module):

  def __init__(self, set_encoder, max_n_points, n_dim,
              n_updates=5, step_size=1., repr_loss_func):
    &quot;&quot;&quot;Builds the module.

    Args:
      set_encoder: An encoder for sets, e.g. a DeepSet.
      max_n_points: an integer.
      n_dim: dimensionality of the set elements.
      n_updates: The number of gradient updates applied to the initial set.
      step_size: Learning rate for the inner gradient descent loop.
      repr_loss_func: A loss function used to compare the embedding of a
        generated set and an embedding of the conditioning, e.g. squared-error.
    &quot;&quot;&quot;
    
    super().__init__()
    self._set_encoder = set_encoder
    self._max_n_points = max_n_points
    self._n_dim = n_dim
    self._n_updates = n_updates
    self._step_size = step_size

    self._clip_pres = lambda x: jnp.clip(x, 0., 1.)

    def repr_loss(inputs, target):
      h = self._set_encoder(*inputs)
      # We take a mean over the number of points.
      return repr_loss_func(h, target).mean(1).sum()

    self._repr_loss_grad = hk.grad(repr_loss)

  def __call__(self, z):     
    # create the initial set and presence variables
    current_set = hk.get_parameter(&apos;init_set&apos;,
                            shape=(self._max_n_points, self._n_dim),
                            init=hk.initializers.RandomUniform(0., 1.)
    )
    
    current_pres = self._clip_pres(hk.get_parameter(&apos;init_pres&apos;,
                                    shape=(self._max_n_points, 1),
                                    init=hk.initializers.Constant(.5),
    ))

    # DSPN returns the starting set/pres and apparently puts loss on it.
    all_sets, all_pres = [current_set], [current_pres]
    for _ in range(self._n_updates):
      set_grad, pres_grad = self._repr_loss_grad((current_set, current_pres), z)

      current_set = current_set - self._step_size * set_grad
      current_pres = current_pres - self._step_size * pres_grad
      # We need to make sure that the presence is valid after each update.
      current_pres = self._clip_pres(current_pres)

      all_sets.append(current_set)
      all_pres.append(current_pres)

    return all_sets, all_pres
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figure id=&quot;DSPN_flow&quot;&gt;
  &lt;div align=&quot;center&quot; style=&quot;max-width: 800px; display: box; float: margin: auto;&quot;&gt;
    &lt;img style=&quot;width: 800px; padding: 5px;&quot; src=&quot;http://akosiorek.github.io/resources/DSPN_flow.png&quot; /&gt;
  &lt;/div&gt;

  &lt;figcaption align=&quot;center&quot;&gt;
    &lt;b&gt;Fig. 1:&lt;/b&gt; &lt;a href=&quot;https://arxiv.org/abs/1906.06565&quot;&gt;DSPN&lt;/a&gt; iteratively transforms an initial set (left) into the final prediction (2nd from the right) by gradient descent.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;While a cool idea, the gradient iteration learned by DSPN is a flow field (see &lt;a href=&quot;#DSPN_flow&quot;&gt;Fig. 1&lt;/a&gt;), and it necessarily requires many iterations to reach the final prediction.
Instead, we can learn a permutation-equivariant operator that directly outputs the required set.&lt;/p&gt;

&lt;h4 id=&quot;attention-is-all-you-need-really&quot;&gt;Attention is All You Need, Really&lt;/h4&gt;
&lt;p&gt;Not too long ago, &lt;a href=&quot;https://arxiv.org/abs/1706.03762&quot;&gt;Vaswani et al. showed that we could replace RNNs with attention, causal masking, and position embeddings&lt;/a&gt;.
It turns out that discarding causal masking and position embeddings leads to self-attention that is permutation-equivariant, as explored in &lt;a href=&quot;https://arxiv.org/abs/1810.00825&quot;&gt;Lee et al., “Set Transformer”, ICML 2019&lt;/a&gt;.
If this is the case, can we build a model similar to DSPN, but with a transformer instead of the inner gradient-descent inner loop?
Of course, we can!
There are several advantages:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The initial set can be higher-dimensional (in DSPN, it has to be the same dimensionality as the output set), leading to more degrees of freedom.&lt;/li&gt;
  &lt;li&gt;Transformer layers can operate on the set of different dimensionality, and they do not have to project it to the output dimensionality between layers. This might seem trivial, but it relaxes the flow-field constraint, and in practice, creates transformations that can hold on to some additional state, akin to RNNs.&lt;/li&gt;
  &lt;li&gt;DSPN captures dependencies between individual points only via a pooling operation in its DeepSet encoder. Transformers are all about relational reasoning, and can directly use interdependencies between points to generate the final set.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure id=&quot;tspn&quot;&gt;
  &lt;div align=&quot;center&quot; style=&quot;max-width: 800px; display: box; float: margin: auto;&quot;&gt;
    &lt;img style=&quot;width: 500px; padding: 5px;&quot; src=&quot;http://akosiorek.github.io/resources/tspn.svg&quot; /&gt;
  &lt;/div&gt;

  &lt;figcaption align=&quot;center&quot;&gt;
    &lt;b&gt;Fig. 2:&lt;/b&gt; &lt;a href=&quot;https://arxiv.org/abs/2006.16841&quot;&gt;TSPN&lt;/a&gt; uses a Transformer to directly transform a random point cloud.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;We explored this idea in two recent papers; both published at the &lt;a href=&quot;https://oolworkshop.github.io/&quot;&gt;ICML 2020 Object-Oriented Learning workshop&lt;/a&gt;,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2006.16841&quot;&gt;Kosiorek, Kim, and Rezende, “Conditional Set Generation with Transformers”&lt;/a&gt;, where we introduce the Transformer Set Prediction Network (TSPN). TSPN uses an MLP to predict the required number of points from a conditioning, samples the required number of points from a base distribution, and transforms them using a Transformer, see &lt;a href=&quot;#tspn&quot;&gt;Fig. 2&lt;/a&gt; for an overview.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://oolworkshop.github.io/program/ool_32.html&quot;&gt;Stelzner, Kersting, and Kosiorek, “Generative Adversarial Set Transformers”&lt;/a&gt; introduces GAST: a similar idea, where a number of points from a base distribution are conditionally-transformed (based on a global noise vector) using a Transormer. We then use a Set Transformer to discriminate between the generated and real sets.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same idea was concurrently explored by at least two other groups&lt;sup id=&quot;fnref:other_set_att_papers&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:other_set_att_papers&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;12&lt;/a&gt;&lt;/sup&gt;.
While details differ, the main finding is that an initial set (randomly-sampled or deterministic and learned) passed through several layers of attention leads to state-of-the-art set generation.
The general architecture is as follows:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Some (big) neural net encoder for processing the conditioning, e.g., a ResNet for images.&lt;/li&gt;
  &lt;li&gt;The encoder produces some key-and-value vectors.&lt;/li&gt;
  &lt;li&gt;We take either a deterministic or randomly-sampled set of queries and attend over the key-and-value pairs.&lt;/li&gt;
  &lt;li&gt;The result might be post-processed by self-attention and/or point-wise MLPs.&lt;/li&gt;
  &lt;li&gt;We apply a permutation-invariant loss function, one of the described above. Hungarian matching seems to give the best results.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure id=&quot;slot_attention&quot;&gt;
  &lt;div align=&quot;center&quot; style=&quot;max-width: 800px; display: box; float: margin: auto;&quot;&gt;
    &lt;img style=&quot;width: 400px; padding: 5px;&quot; src=&quot;http://akosiorek.github.io/resources/slot_attention.png&quot; /&gt;
  &lt;/div&gt;

  &lt;figcaption align=&quot;center&quot;&gt;
    &lt;b&gt;Fig. 3:&lt;/b&gt; &lt;a href=&quot;https://arxiv.org/abs/2006.15055&quot;&gt;Slot Attention&lt;/a&gt; induces competition between queries, leading to SOTA unsupervised object segmentation.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The results of &lt;a href=&quot;https://github.com/facebookresearch/detr&quot;&gt;Carion et al.’s DETR&lt;/a&gt; model are particularly impressive. While it still required quite a bit of engineering, this pure set-prediction approach achieves state-of-the-art on large-scale object detection on COCO!
&lt;a href=&quot;https://arxiv.org/abs/2006.15055&quot;&gt;Locatello et al.&lt;/a&gt; show that the particular form of attention required might depend on the task; in their experiments, they normalize attention across the query axis (instead of the key axis), which leads to competition between queries, and provides superior results for unsupervised object segmentation (&lt;a href=&quot;#slot_attention&quot;&gt;Fig. 3&lt;/a&gt;).&lt;/p&gt;

&lt;h4 id=&quot;what-about-those-point-processes&quot;&gt;What about those Point Processes??!!&lt;/h4&gt;
&lt;p&gt;While the above approaches definitely work for generating sets, they make no use of the well-known area of statistics concerned with modeling sets: point processes!
Point processes treat the set size \(k \in \mathbb{N}_+\) as a random variable and model it jointly with the set membership \(X \in \mathcal{X}^k\), thus modeling the joint density \(p(X, k)\).
This is in contrast to some of the previously-described methods; e.g., DSPN uses heuristics to determine the set size, which does or does not work depending on which loss function it is used with (&lt;a href=&quot;https://arxiv.org/abs/2006.16841&quot;&gt;see our TSPN paper for details&lt;/a&gt;).
Our TSPN is not much better in that regard, and casts determining the set size as a classification problem–this works quite well in practice, but it &lt;strong&gt;cannot generalize&lt;/strong&gt; to set sizes not seen in training.
While a detailed description of point process would take too much space to fit in this blog, I would like to highlight one notion, which I learned about from an excellent paper by Vu et al. called &lt;a href=&quot;https://arxiv.org/abs/1703.02155&quot;&gt;“Model-Based Multiple Instance Learning”&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let \(f_k(X) = f_k(x_1, ..., x_i, ..., x_k)\) be a probability density function defined over sets of \(k\) elements, and let this density be invariant to ordering of the elements of the set, that is \(\forall \pi\): \(f(X) = f(\pi X)\).
It turns out that we can use this density to compare sets of the same cardinality with each other in terms of how probable they are (i.e., how high their likelihood is), but, even if we have two such functions for sets of cardinality \(k\) and \(m\), we simply &lt;strong&gt;cannot use them to compare sets of those different cardinalities&lt;/strong&gt;.
Why is that?
Well, comparing sets of two and sets of three elements is a bit like comparing square meters m\(^2\) and cubic meters m\(^3\), or like comparing apples and oranges.
It is not that we cannot compare sets of different cardinality, but we have to first bring them into the same space, which in this case is dimension-less.
To do that, we have to account for a) the number of possible permutations of each set, and b) the unit volume (in case of metric space and comparing m\(^2\) and m\(^3\), we need to figure out how big a meter m\(^1\) is).
This leads to the following definition of the probability density function of a set of size \(k\),&lt;/p&gt;

\[p(\{x_1, ..., x_k\}) = p(X, k) = p_c(k)k!U^k f_k(x_1, ..., x_k)\,,\]

&lt;p&gt;where \(p_c(k)\) is the probability mass function of the set size, \(k!\) accounts for all possible permutations of set elements \(\mathbf{x}_i\), \(U 
\in \mathbb{R}_+\) is the unit volume expressed as a scalar value, and \(f_k\) is the permutation-invariant density of a set of size k.
Interestingly, none of the above set-generation papers take the point-process theory into account when defining their likelihoods over sets.
I would be curious to see if it improves results, as Vu et al. suggest.&lt;/p&gt;

&lt;h1 id=&quot;set-to-set&quot;&gt;Set To Set&lt;/h1&gt;
&lt;p&gt;Given the knowledge of how to solve set-to-vector and vector-to-set problems, it should be quite clear how to solve a set-to-set problem: we can encode a set into a vector, and then decode that vector into a set using one of the above vector-to-set methods.
While correct, this approach forces us to use a bottleneck in the shape of a single vector.
Perhaps a better option is to encode a set to an intermediate set, possibly of smaller cardinality, and use that smaller set as conditioning when generating the output set.
There are many methods of how this can be done, and I will only mention that we explored some such problems in &lt;a href=&quot;https://arxiv.org/abs/1810.00825&quot;&gt;Lee et al., “Set Transformer”, ICML 2019&lt;/a&gt; and encourage curious readers to look at the paper.&lt;/p&gt;

&lt;h1 id=&quot;outlook-and-conclusion&quot;&gt;Outlook and Conclusion&lt;/h1&gt;
&lt;p&gt;Thank you for reaching this far!
We have covered some basics of set-oriented machine learning by taking a look at set-to-vector, vector-to-set, and set-to-set problems and some approaches to solving them.
I find this area of ML incredibly interesting, for the variety of things that we consider in life as sets is endless.
At the same time, the set-learning models tend to be both theoretically- and architecturally- interesting.
Moving forward, I would like to see more models directly based on the point-process theory.
Another area that I have not mentioned, and one that is extremely applicable, is that of normalizing flows.
You can read about &lt;a href=&quot;http://akosiorek.github.io/ml/2018/04/03/norm_flows.html&quot;&gt;the basics of normalizing flows in my previous blog post&lt;/a&gt;, but in short, they are used to transform a simple probability distribution into a more complicated one.
As such, there is nothing preventing us from using flows to transform a distribution over independent variables into a joint distribution over sets.
While there are some papers that use this idea&lt;sup id=&quot;fnref:set_flow_models&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:set_flow_models&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;13&lt;/a&gt;&lt;/sup&gt; to define permutation-invariant likelihoods, none of them uses point-process theory.
I will leave working out how to combine flows and point processes as an exercise to the reader, and I will be looking out for papers doing that :)&lt;/p&gt;

&lt;h1 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h1&gt;
&lt;p&gt;If you want to learn about point processes, I would recommend:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The excellent and yet a very short book &lt;a href=&quot;https://global.oup.com/academic/product/poisson-processes-9780198536932?cc=us&amp;amp;lang=en&amp;amp;&quot;&gt;“Poisson Process” by J. F. C. Kingman&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-262-discrete-stochastic-processes-spring-2011/&quot;&gt;The open MIT course on Discrete Stochastic Processes by Robert Gallager&lt;/a&gt;, which provides a very gentle introduction to point processes without any measure theory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;footnotes&quot;&gt;Footnotes&lt;/h4&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:cnnequiv&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Interestingly, CNNs or even 2D conv filters we often use are NOT equivariant to translations due to discretization artifacts, see &lt;a href=&quot;https://arxiv.org/abs/1904.11486&quot;&gt;here&lt;/a&gt; for a more thorough description and a solution. &lt;a href=&quot;#fnref:cnnequiv&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:languecausality&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;though this does not always apply; a good example is machine translation, where the order of tokens can vary between languages. &lt;a href=&quot;#fnref:languecausality&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:acn&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;See &lt;a href=&quot;https://arxiv.org/abs/1804.02476&quot;&gt;Graves et al., “Associative Compression Networks for Representation Learning”, arXiv 2018&lt;/a&gt; for an example where dataset (or minibatch) items are modeled jointly, and the loss depends on the whole minibatch/dataset. &lt;a href=&quot;#fnref:acn&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:deepsetdim&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;with the caveat that the dimensionality of the embedding produced by the pooling function has to be on the order of the maximum expected set size to achieve universal approximation properties, see more in &lt;a href=&quot;https://arxiv.org/abs/1901.09006&quot;&gt;Wagstaff et al., “On the limitations of representing functions on sets”, ICML 2019&lt;/a&gt;. &lt;a href=&quot;#fnref:deepsetdim&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:setembeddings&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I tend to use &lt;a href=&quot;https://arxiv.org/abs/1810.00825&quot;&gt;Lee et al., “Set Transformer”, ICML 2019&lt;/a&gt;, but as a co-author, I might be biased. &lt;a href=&quot;#fnref:setembeddings&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:setae&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1707.02392&quot;&gt;Achlioptas et. al., “Learning representations andgenerative models for 3D point clouds”, ICML 2018&lt;/a&gt;. &lt;a href=&quot;#fnref:setae&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:order_matters&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1511.06391&quot;&gt;Vinyals et. al., “Order Matters: Sequence to sequence for sets”, ICLR 2015&lt;/a&gt;. &lt;a href=&quot;#fnref:order_matters&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:data_augmentation&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;See, e.g. &lt;a href=&quot;https://arxiv.org/abs/1906.06565&quot;&gt;Zhang et al., “Deep Set Prediction Networks”, NeurIPS 2019&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/1602.07576&quot;&gt;Cohen and Welling, “Group Equivariant Convolutional Networks”, ICML 2016&lt;/a&gt; for comparisons of truly equivariant methods against data augmentation for permutations and rotations, respectively. &lt;a href=&quot;#fnref:data_augmentation&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:bipartite_matching&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Matching elements of two sets in the sense required here is formally known as &lt;a href=&quot;https://en.wikipedia.org/wiki/Matching_(graph_theory)#Maximum-weight_matching&quot;&gt;Maximum Weight Bipartite Graph Matching&lt;/a&gt;. &lt;a href=&quot;#fnref:bipartite_matching&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:chamfer&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Strictly speaking, it would be a lower bound if divided by two. The most popular form of the Chamfer loss omits this division, however. &lt;a href=&quot;#fnref:chamfer&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:invgrad&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;More generally, the gradient of an invariant function is itself an equivariant function, as noted in &lt;a href=&quot;https://arxiv.org/abs/1912.02762&quot;&gt;Papamakarios et al., “Normalizing Flows for Probabilistic Modeling and Inference”, arXiv 2019&lt;/a&gt;. &lt;a href=&quot;#fnref:invgrad&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:other_set_att_papers&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2006.15055&quot;&gt;Locatello et. al., “Object-Centric Learning with Slot Attention”&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/2005.12872&quot;&gt;Carion et. al., “End-to-End Object Detection with Transformers”&lt;/a&gt;. &lt;a href=&quot;#fnref:other_set_att_papers&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:set_flow_models&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;[Wirnsberger et. al, “Targeted free energy estimation via learned mappings”, arXiv 2020] uses a split-coupling flow with a permutation-invariant coupling layer, and &lt;a href=&quot;https://arxiv.org/abs/2008.02676&quot;&gt;Li et. al., “Exchangeable Neural ODE for Set Modeling”, arXiv 2020&lt;/a&gt; use &lt;a href=&quot;https://arxiv.org/abs/1806.07366&quot;&gt;Neural ODEs&lt;/a&gt; with permutation-invariant drift functions, which gives them a permutation-equivariant continuous normalizing flow, how cool! &lt;a href=&quot;#fnref:set_flow_models&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;

&lt;h4 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h4&gt;
&lt;p&gt;I would like to give huge thanks to Fabian Fuchs, Thomas Kipf, Hyunjik Kim, Yan Zhang, George Papamakarios, and Danilo Rezende for insightful and inspiring discussions about the machine learning of sets. I would also like to thank Hyunjik Kim and Fabian Fuchs for their feedback on the initial version of this post.
This post would not happen if not for Juho Lee, who got me interested in sets in the first place.&lt;/p&gt;

</description>
        <pubDate>Wed, 12 Aug 2020 10:15:00 +0000</pubDate>
        <link>http://akosiorek.github.io/machine_learning_of_sets/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/machine_learning_of_sets/</guid>
        
        
        <category>ML</category>
        
      </item>
    
      <item>
        <title>Stacked Capsule Autoencoders</title>
        <description>&lt;p&gt;Objects play a central role in computer vision and, increasingly, machine learning research.
With many applications depending on object detection in images and videos, the demand for accurate and efficient algorithms is high.
More generally, knowing about objects is essential for understanding and interacting with our environments.
Usually, object detection is posed as a supervised learning problem, and modern approaches typically involve training a CNN to predict the likelihood of whether an object exists at a given image location (and maybe the corresponding class), see e.g. &lt;a href=&quot;https://blog.athelas.com/a-brief-history-of-cnns-in-image-segmentation-from-r-cnn-to-mask-r-cnn-34ea83205de4&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While modern methods can achieve human-like performance in object recognition, they need to consume staggering amounts of data to do so. This is in stark contrast to mammals, who learn to recognize and localize objects with no supervision.
It is difficult to say what exactly makes mammals so good at learning, but we can imagine that &lt;em&gt;self-supervision&lt;/em&gt;&lt;sup id=&quot;fnref:selfsupervised&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:selfsupervised&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Inductive_bias&quot;&gt;&lt;em&gt;inductive biases&lt;/em&gt;&lt;/a&gt; present in their sophisticated computing hardware (or rather &lt;a href=&quot;https://en.wikipedia.org/wiki/Wetware_(brain)&quot;&gt;‘wetware’&lt;/a&gt;; that is brains) both play a huge role.
These intuitions have led us to develop an &lt;a href=&quot;https://arxiv.org/abs/1906.06818&quot;&gt;unsupervised version of capsule networks&lt;/a&gt;, see &lt;a href=&quot;#SCA_overview&quot;&gt;Figure 1&lt;/a&gt; for an overview, whose inductive biases give rise to object-centric latent representations, which are learned in a self-supervised way—simply by reconstructing input images.
Clustering learned representations was enough to allow us to achieve unsupervised state-of-the-art classification performance on MNIST (98.5%) and SVHN (55%).
In the remainder of this blog, I will try to explain what those inductive biases are, how they are implemented and what kind of things are possible with this new capsule architecture.
I will also try to explain how this new version differs from previous versions of &lt;a href=&quot;https://openreview.net/forum?id=HJWLfGWRb&quot;&gt;capsule networks&lt;/a&gt;.&lt;/p&gt;

&lt;figure id=&quot;SCA_overview&quot;&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/scae/blocks_v4.svg&quot; alt=&quot;SCAE&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 1:&lt;/b&gt; The Stacked Capsule Autoencoder (SCAE) is composed of a Part Capsule Autoencoder (PCAE) followed by an Object Capsule Autoencoder (OCAE). It can decompose an image into its parts and group parts into objects.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h1 id=&quot;why-do-we-care-about-equivariances&quot;&gt;Why do we care about equivariances?&lt;/h1&gt;
&lt;p&gt;I think it is fair to say that deep learning would not be so popular if not for CNNs and the &lt;a href=&quot;https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf&quot;&gt;2012 AlexNet paper&lt;/a&gt;.
CNNs learn faster than non-convolutional image models due to (1) local connectivity and (2) parameter sharing across spatial locations.
The former restricts what can be learned, but is sufficient to learn correlations between nearby pixels, which turns out to be important for images.
The latter makes learning easier since parameter updates benefit from more signal.
It also results in &lt;em&gt;translation equivariance&lt;/em&gt;, which means that, when the input to a CNN is shifted, the output is shifted by an equal amount, while remaining unchanged otherwise.
Formally, a function \(f(\mathbf{x})\) is &lt;strong&gt;equivariant&lt;/strong&gt; to any transformation \(T \in \mathcal{T}\) if \(\forall_{T \in \mathcal{T}} Tf(\mathbf{x}) = f(T\mathbf{x})\).
That is, applying any transformation to the input of the function has the same effect as applying that transformation to the output of the function.
Invariance is a related notion, and the function \(f\) is &lt;strong&gt;invariant&lt;/strong&gt; if \(\forall_{T \in \mathcal{T}} f(\mathbf{x}) = f(T\mathbf{x})\)—applying transformations to the input does not change the output.&lt;/p&gt;

&lt;p&gt;Being equivariant helps with learning and generalization—for example, a model does not have to see the object placed at every possible spatial location in order to learn how to classify it.
For this reason, it would be great to have neural nets that are equivariant to other affine degrees of freedom like rotation, scale, and shear, but this is not very easy to achieve, see e.g. &lt;a href=&quot;https://arxiv.org/abs/1602.07576&quot;&gt;group equivariant conv nets&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Equivariance to different transformations can be learned approximately, but it requires vast data augmentation and a considerably higher training cost.
&lt;!-- following sentence might be not necessary --&gt;
Augmenting data with random crops or shifts helps even with training translation-equivariant CNNs since these are typically followed by fully-connected layers, which have to learn to handle different positions.
&lt;!--  --&gt;
Other affine transformations&lt;sup id=&quot;fnref:augment&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:augment&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; are not easy to augment with, as they would require access to full three-dimensional scene models.
Even if scene models were available, we would need to augment data with combinations of different transformations, which would result in an absolutely enormous dataset.
The problem is exacerbated by the fact that objects are often composed of parts, and it would be best to capture all possible configurations of object parts.
&lt;a href=&quot;https://arxiv.org/abs/1506.02025&quot;&gt;Spatial Transformer Networks&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/1901.11399&quot;&gt;this followup work&lt;/a&gt; provide one way of learning affine equivariances but do not address the fact that objects can undergo local transformations.&lt;/p&gt;

&lt;h1 id=&quot;capsules-learn-equivariant-object-representations&quot;&gt;Capsules learn equivariant object representations&lt;/h1&gt;

&lt;figure id=&quot;old_capsules&quot;&gt;
   &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/scae/old_capsules.svg&quot; alt=&quot;Capsule Network&quot; /&gt;
   &lt;figcaption align=&quot;center&quot;&gt;
   &lt;b&gt;Fig 2:&lt;/b&gt; Capsule networks work by inferring parts &amp;amp; their poses from an image, and then using parts and poses to reason about objects.
   &lt;/figcaption&gt;
 &lt;/figure&gt;

&lt;p&gt;Instead of building models that are globally equivariant to affine transformations, we can rely on the fact that scenes often contain many complicated objects, which in turn are composed of simpler parts.
&lt;!--  --&gt;
By definition, parts exhibit less variety in their appearance and shape than full objects, and consequently, they should be easier to learn from raw pixels.
&lt;!--  --&gt;
Objects can then be recognized from parts and their poses, given that we can learn how parts come together to form different objects, as in &lt;a href=&quot;#old_capsules&quot;&gt;Figure 2&lt;/a&gt;.
The caveat here is that we still need to learn a part detector, and it needs to predict part poses (i.e. translation, rotation, and scale) too.
We hypothesize that this &lt;em&gt;should&lt;/em&gt; be much simpler than learning an end-to-end object detector with similar capabilities.&lt;/p&gt;

&lt;p&gt;Since poses of any entities present in a scene change with the location of an observer (or rather the chosen coordinate system), then a detector that can correctly identify poses of parts produces a viewpoint-equivariant part representation.
Since object-part relationships do not depend on the particular vantage point, they are viewpoint-invariant.
These two properties, taken together, result in viewpoint-equivariant object representations.&lt;/p&gt;

&lt;p&gt;The issue with the above is that the corresponding inference process, that is, using previously discovered parts to infer objects, is difficult since every part can belong to at most one object.
Previous versions of capsules solved this by iteratively refining the assignment of parts to objects (also known as &lt;em&gt;routing&lt;/em&gt;). This proved to be inefficient in terms of both computation and memory and made it impossible to scale to bigger images.
See e.g. &lt;a href=&quot;https://medium.com/ai%C2%B3-theory-practice-business/understanding-hintons-capsule-networks-part-i-intuition-b4b559d1159b&quot;&gt;here&lt;/a&gt; for an overview of previous capsule networks.&lt;/p&gt;

&lt;h1 id=&quot;can-an-arbitrary-neural-net-learn-capsule-like-representations&quot;&gt;Can an arbitrary neural net learn capsule-like representations?&lt;/h1&gt;
&lt;p&gt;Original capsules are a type of a feed-forward neural network with a specific structure and are trained for classification.
Incidentally, we know that classification corresponds to inference, which is the inverse process of generation, and as such is more difficult.
To see this, think about Bayes’ rule: this is why posterior distributions are often much more complicated than the prior or likelihood terms&lt;sup id=&quot;fnref:simple_posteriors&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:simple_posteriors&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Instead, we can use the principles introduced by capsules to build a generative model (decoder) and a corresponding inference network (encoder).
Generation is simpler, since we can have any object generate arbitrarily many parts, and we do not have to deal with constraints encountered in inference.
Complicated inference can then be left to your favorite neural network, which is going to learn appropriate latent representations.
Since the decoder uses capsules machinery, it is viewpoint equivariant by design.
It follows that the encoder has to learn representations that are also viewpoint equivariant, at least approximately.&lt;/p&gt;

&lt;p&gt;A potential disadvantage is that, even though the latent code might be viewpoint equivariant, in the sense that it explicitly encodes object coordinates, the encoder itself need not be viewpoint equivariant. This means that, if the model sees an object from a very different point of view than it is used to, it may fail to recognize the object.
Interestingly, this seems to be in line with human perception, as noted recently by &lt;a href=&quot;https://youtu.be/VsnQf7exv5I?t=2167&quot;&gt;Geoff Hinton in his Turing Award lecture&lt;/a&gt;, where he uses a thought experiment to illustrate this.
If you are interested, you can watch the below video for about 2.5 minutes.&lt;/p&gt;

&lt;!-- &lt;div align=&apos;center&apos; style=&apos;display: box;&apos;&gt; --&gt;
&lt;div class=&quot;videoWrapper&quot;&gt;
  &lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/VsnQf7exv5I?start=2168&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;
&lt;!-- &lt;br&gt; --&gt;
&lt;p&gt;Here is a simplified version of the example in the video, see &lt;a href=&quot;#diamond_square&quot;&gt;Figure 3&lt;/a&gt;: imagine a square, and tilt it by 45 degrees, look away, and look at it again. Can you see a square? Or does the shape resemble a diamond?
Humans tend to impose coordinate frames on the objects they see, and the coordinate frame is one of the features that let us recognize the objects.
If the coordinate frame is very different from the usual one, we may have problems recognizing the correct shape.&lt;/p&gt;

&lt;figure id=&quot;diamond_square&quot;&gt;
  &lt;img style=&quot;max-width: 450px; display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/scae/square_diamond.svg&quot; alt=&quot;Is it a square, or is it a rhombus?&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 3:&lt;/b&gt; Is it a square, or is it a diamond?
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h1 id=&quot;why-bother&quot;&gt;Why bother?&lt;/h1&gt;
&lt;p&gt;I hope I have managed to convince you that learning capsule-like representations is possible.
Why is it a good idea?
While we still have to scale the method to complicated real-world imagery, the initial results are quite promising.
It turns out that the object capsules can learn to specialize to different types of objects.
When we clustered the presence probabilities of object capsules we found that, to our surprise,  representations of objects from the same class are grouped tightly together.
Simply looking up the label that examples in a given cluster correspond to resulted in state-of-the-art unsupervised classification accuracy on two datasets: MNIST (98.5%) and SVHN (55%).
We also took a model trained on MNIST and simulated unseen viewpoints by performing affine transformations of the digits, and also achieved state-of-the-art unsupervised performance (92.2%), which shows that learned representations are in fact robust to viewpoint changes.
Results on Cifar10 are not quite as good, but still promising.
In future work, we are going to explore more expressive approaches to image reconstruction, instead of using fixed templates, and hopefully, scale up to more complicated data.&lt;/p&gt;

&lt;h1 id=&quot;technical-bits&quot;&gt;Technical bits&lt;/h1&gt;
&lt;p&gt;This is the end of high-level intuitions, and we now proceed to some technical descriptions, albeit also high-level ones.
This might be a good place to stop reading if you are not into that sort of thing.&lt;/p&gt;

&lt;p&gt;In the following, I am going to describe the decoding stack of our model, and you can think of it as a (deterministic) generative model.
Next, I will describe an inference network, which provides the capsule-like representations.&lt;/p&gt;

&lt;h1 id=&quot;how-can-we-turn-object-prototypes-into-an-image&quot;&gt;How can we turn object prototypes into an image?&lt;/h1&gt;
&lt;p&gt;Let us start by defining what a &lt;em&gt;capsule&lt;/em&gt; is.&lt;/p&gt;

&lt;p&gt;We define a &lt;em&gt;capsule&lt;/em&gt; as a specialized part of a model that describes an abstract entity, e.g. a part or an object.
In the following, we will have &lt;em&gt;object capsules&lt;/em&gt;, which recognize objects from parts, and &lt;em&gt;part capsules&lt;/em&gt;, which extract parts and poses from an input image.
Let &lt;em&gt;capsule activation&lt;/em&gt; be a group of variables output by a single capsule.
To describe an object, we would like to know (1) whether it exists, (2) what it looks like and (3) where it is located&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;.
Therefore, for an object capsule \(k\), its activations consist of (1) a presence probability \(a_k\), (2) a feature vector \(\mathbf{c_k}\), and (3) a \(3\times 3\) pose matrix \(OV_k\), which represents the geometrical relationship between the object and the viewer (or some central coordinate system).
Similar features can be used to describe parts, but we will simplify the setup slightly and assume that parts have a fixed appearance and only their pose can vary.
Therefore, for the \(m^\mathrm{th}\) part capsule, its activations consist of the probability \(d_m\) that the part exists, and a \(6\)-dimensional&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt; pose \(\mathbf{x}_m\), which represents the position and orientation of the given part in the image.
As indicated by notation, there are several object capsules and potentially many part capsules.&lt;/p&gt;

&lt;p&gt;Since every object can have several parts, we need a mechanism to turn an object capsule activation into several part capsule activations.
To this end, for every object capsule, we learn a set of \(3\times 3\) transformation matrices \(OP_{k,m}\), representing the geometrical relationship between an object and its parts.
These matrices are encouraged to be constant, although we do allow a weak dependence on the object features \(\mathbf{c}_k\) to account for small deformations.
Since any part can belong to only one object, we gather predictions from all object capsules corresponding to the same part capsule and arrange them into a mixture.
If the model is confident that a particular object should be responsible for a given part, then this will be reflected in the mixing probabilities of the mixture.
In this case, sampling from the mixture will be similar to just taking argmax over the mixing proportions while also accounting for uncertainty in the assignment.
Finally, we explain parts by independent Gaussian mixtures; this is a simplifying assumption saying that a choice of a parent for one part should not influence the choice of parents for other parts.&lt;/p&gt;

&lt;figure id=&quot;mnist_strokes&quot;&gt;
  &lt;div align=&quot;center&quot; style=&quot;max-width: 800px; display: box; float: margin: auto;&quot;&gt;
    &lt;img style=&quot;width: 200px; padding: 5px;&quot; src=&quot;http://akosiorek.github.io/resources/scae/mnist_strokes.png&quot; alt=&quot;Object Capsules&quot; /&gt;
    &lt;!--  --&gt;
    &lt;img style=&quot;max-width: 320px; padding:5px; filter:gray; -webkit-filter: grayscale(1); -webkit-filter: grayscale(100%);&quot; src=&quot;http://akosiorek.github.io/resources/scae/transformed_mnist_strokes.png&quot; alt=&quot;Object Capsules&quot; /&gt;
    &lt;!--  --&gt;
    &lt;img style=&quot;max-width: 64px; padding:5px; filter:gray; -webkit-filter: grayscale(1); -webkit-filter: grayscale(100%);&quot; src=&quot;http://akosiorek.github.io/resources/scae/mnist_rec.png&quot; alt=&quot;Object Capsules&quot; /&gt;
  &lt;/div&gt;

  &lt;figcaption align=&quot;center&quot;&gt;
    &lt;b&gt;Fig 4:&lt;/b&gt; &lt;i&gt;Left&lt;/i&gt;: learned parts, or templates. &lt;i&gt;Center&lt;/i&gt;: a few affine-transformed parts; they do not comprise full objects. &lt;i&gt;Right&lt;/i&gt;: MNIST digits assembled from the transformed parts.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Having generated part poses, we can take parts, apply affine transformations parametrized by the corresponding poses as in &lt;a href=&quot;https://arxiv.org/abs/1506.02025&quot;&gt;Spatial Transformer&lt;/a&gt;, and assemble the transformed parts into an image (&lt;a href=&quot;#mnist_strokes&quot;&gt;Figure 4&lt;/a&gt;).
But wait—we need to get the parts first!
Since we assumed that parts have fixed appearance, we are going to learn a bank of fixed parts by gradient descent.
Each part is like an image, just smaller, and can be seen as a “template”.
To give an example, a good template for MNIST digits would be a stroke, like in &lt;a href=&quot;http://www.sciencemag.org/content/350/6266/1332.short&quot;&gt;the famous paper from Lake et. al.&lt;/a&gt; or the left-hand side of Figure 4.&lt;/p&gt;

&lt;!-- --&gt;
&lt;h1 id=&quot;where-do-we-get-capsule-parameters-from&quot;&gt;Where do we get capsule parameters from?&lt;/h1&gt;
&lt;p&gt;Above, we define a &lt;em&gt;generative process&lt;/em&gt; that can transform object and part capsule activations into images.
But to obtain capsule activations describing a particular image, we need to run some sort of inference.
In this case, we will just use neural networks to amortize inference, like in a VAE, (&lt;a href=&quot;http://akosiorek.github.io/ml/2018/03/14/what_is_wrong_with_vaes.html&quot;&gt;see this post for more details on VAEs&lt;/a&gt;).
In other words, neural nets will predict capsule activations directly from the image.
We will do this in two stages.
Firstly, given the image, we will have a neural net predict pose parameters and presence probabilities for every part from our learnable bank of parts.
Secondly, a separate neural net will look at the part parameters and will try to directly predict object capsule activations.
These two stages correspond to two stages of the generative process we outlined;
we can now pair each of the stages with the corresponding generative stage and arrive at two autoencoders.
The first one, Part Capsule Autoencoder (PCAE), detects parts and recombines them into an image.
The second one, Object Capsule Autoencoder (OCAE), organizes parts into objects.
Below, we describe their architecture and some of our design choices.&lt;/p&gt;

&lt;h4 id=&quot;inferring-parts-and-poses&quot;&gt;Inferring parts and poses&lt;/h4&gt;

&lt;figure&gt;
  &lt;img style=&quot;max-width: 650px; display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/scae/part_capsule_ae.svg&quot; alt=&quot;Part Capsules&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 5:&lt;/b&gt; The Part Capsule Autoencoder (PCAE) detects parts and their poses from the image and reconstructs the image by directly assembling it from affine-transformed parts.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The PCAE uses a CNN-based encoder, but with tweaks.
Firstly, notice that for \(M\) parts, we need \(M \times (6 + 1)\) predicted parameters.
That is, for every part we need \(6\) parameters of an affine transformation \(\mathbf{x}_m\) (we work in two dimensions) and a probability \(d_m\) of the part being present; we could also predict some additional parameters; in fact we do, but we omit the details here for clarity—see the paper for details.
It turns out that using a fully-connected layer after a CNN does not work well here; see the paper for details.
Instead, we project the outputs of the CNN to \(M \times (6 + 1 + 1)\) feature maps using \(1\times 1\) convolutions, where we added an extra feature map for each part capsule.
This extra feature map will serve as an attention mask: we normalize it spatially via a softmax, multiply with the remaining 7 feature maps, and sum each dimension independently across spatial locations.
This is similar to global-average pooling, but allows the model to focus on a specific location; we call it &lt;em&gt;attention-based pooling&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We can then use part presence probabilities and part poses to select and affine-transform learned parts, and assemble them into an image.
Every transformed part is treated as a spatial Gaussian mixture component, and we train PCAE by maximizing the log-likelihood under this mixture.&lt;/p&gt;

&lt;h4 id=&quot;organizing-parts-into-objects&quot;&gt;Organizing parts into objects&lt;/h4&gt;

&lt;figure&gt;
  &lt;img style=&quot;max-width: 500px; display: box; margin: auto;&quot; src=&quot;http://akosiorek.github.io/resources/scae/object_capsule_ae.svg&quot; alt=&quot;Object Capsules&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 6:&lt;/b&gt; The Object Capsule Autoencoder (OCAE) tries to explain part poses as a sparse set of objects, where every present object predicts several parts. It automatically discovers structure in the data, whereby different object capsules specialise to different objects.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Knowing what parts there are in an image and where they are might be useful, but in the end, we care about the objects that they belong to.
&lt;a href=&quot;https://openreview.net/forum?id=HJWLfGWRb&amp;amp;noteId=rk5MadsMf&quot;&gt;Previous capsules&lt;/a&gt; used an EM-based inference procedure to make parts vote for objects.
This way, each part could start by initially disagreeing and voting on different objects, but eventually, the votes would converge to a set of only a few objects.
We can also see inference as compression, where a potentially large set of parts is explained by a potentially very sparse set of objects.
Therefore, we try to predict object capsule activations directly from the poses and presence probabilities of parts.
The EM-based inference tried to cluster part votes around objects.
We follow this intuition and use the &lt;a href=&quot;https://arxiv.org/abs/1810.00825&quot;&gt;Set Transformer&lt;/a&gt; with \(K\) outputs to encode part activations.
Set Transformer has been shown to work well for amortized-clustering-type problems, and it is permutation invariant.
Part capsule activations describe parts, not pixels, which can have arbitrary positions in the image, and in that sense have no order.
Therefore, set-input neural networks seem to be a better choice than MLPs—a hypothesis corroborated by an ablation study we have in the paper.&lt;/p&gt;

&lt;p&gt;Each output of the Set Transformer is fed into a separate MLP, which then outputs all activations for the corresponding object capsule.
We also use a number of sparsity losses applied to the object presence probabilities; these are necessary to make object capsules specialize to different types of objects, please see the paper for details.
The OCAE is trained by maximizing the likelihood of part capsule activations under a Gaussian mixture of predictions from object capsules, subject to sparsity constraints.&lt;/p&gt;

&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;/h1&gt;
&lt;figure&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/scae/blocks_v4.svg&quot; alt=&quot;SCAE&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
  &lt;b&gt;Fig 7:&lt;/b&gt; The Stacked Capsule Autoencoder (SCAE) is composed of a PCAE followed by an OCAE. It can decompose image into its parts and group parts into objects.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In summary, a Stacked Capsule Autoencoder is composed of:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;the PCAE encoder: a CNN with attention-based pooling,&lt;/li&gt;
  &lt;li&gt;the OCAE encoder: a Set Transformer,&lt;/li&gt;
  &lt;li&gt;the OCAE decoder:
    &lt;ul&gt;
      &lt;li&gt;\(K\) MLPs, one for every object capsule, which predicts capsule parameters from Set Transformer’s outputs,&lt;/li&gt;
      &lt;li&gt;\(K \times M\) constant \(3 \times 3\) matrices representing constant object-part relationships,&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;and the PCAE decoder, which is just \(M\) constant part templates, one for each part capsule.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;SCAE defines a new method for representation learning, where an arbitrary encoder learns viewpoint-equivariant representations by inferring parts and their poses and groups them into objects.
This post provides motivation as well as high-level intuitions behind this idea, and an overview of the method.
The major drawback of the method, as of now, is that the part decoder uses fixed templates, which are insufficient to model complicated real-world images.
This is also an exciting avenue for future work, together with deeper hierarchies of capsules and extending capsule decoders to three-dimensional geometry.
If you are interested in the details, I would encourage you to read the original paper: &lt;a href=&quot;https://arxiv.org/abs/1906.06818&quot;&gt;A. R. Kosiorek, S. Sabour, Y.W. Teh and G. E. Hinton, “Stacked Capsule Autoencoders”, arXiv 2019&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;further-reading&quot;&gt;Further reading:&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://medium.com/ai³-theory-practice-business/understanding-hintons-capsule-networks-part-i-intuition-b4b559d1159b&quot;&gt;a series of blog posts&lt;/a&gt; explaining previous capsule networks&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://papers.nips.cc/paper/6975-dynamic-routing-between-capsules&quot;&gt;the original capsule net paper&lt;/a&gt; and &lt;a href=&quot;https://openreview.net/forum?id=HJWLfGWRb&quot;&gt;the version with EM routing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://youtu.be/zRg3IuxaJ6I&quot;&gt;a recent CVPR tutorial on capsules&lt;/a&gt; and &lt;a href=&quot;https://www.crcv.ucf.edu/cvpr2019-tutorial/slides/intro_sara.pptx&quot;&gt;slides&lt;/a&gt; by Sara Sabour&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;footnotes&quot;&gt;Footnotes&lt;/h4&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:selfsupervised&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The term “self-supervised” can be confusing. Here, I mean that the model sees only sensory inputs, e.g. images (without human-generated annotations), and the model is trained by optimizing a loss that depends only on this input. In this sense, learning is unsupervised. &lt;a href=&quot;#fnref:selfsupervised&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:augment&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;It is also easy to augment training data with different scales and rotations around the camera axis, but these can be only applied globally. Rotations around other axes require access to 3D scene models. &lt;a href=&quot;#fnref:augment&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:simple_posteriors&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Though it is possible for the posterior distribution to be much simpler than either the prior or the likelihood, see e.g. &lt;a href=&quot;http://www.cs.toronto.edu/~fritz/absps/ncfast.pdf&quot;&gt;Hinton, Osindero and Teh, “A Fast Learning Algorithm for Deep Belief Nets”. Neural Computation 2006.&lt;/a&gt; &lt;a href=&quot;#fnref:simple_posteriors&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This is very similar to &lt;a href=&quot;https://github.com/akosiorek/attend_infer_repeat&quot;&gt;Attend, Infer, Repeat (AIR)&lt;/a&gt;, also described in &lt;a href=&quot;http://akosiorek.github.io/ml/2017/09/03/implementing-air.html&quot;&gt;my previous blog post&lt;/a&gt;, as well as &lt;a href=&quot;https://github.com/akosiorek/sqair&quot;&gt;SQAIR&lt;/a&gt;, which extends AIR to videos and allows for unsupervised object detection and tracking. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;An affine transformation in two dimensions is naturally expressed as a \(3\times 3\) matrix, but it has only \(6\) degrees of freedom. We express part poses as \(6\)-dimensional vectors, but predictions made by objects are computed as a composition of two affine transformations. Since it is easier to compose transformations in the matrix form, we express object poses as \(3\times 3\) \(OV\) and \(OP\) matrices. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;

&lt;h4 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h4&gt;
&lt;p&gt;This work was done during my internship at Google Brain in Toronto in Geoff Hinton’s team. I would like to thank my collaborators:
 &lt;a href=&quot;https://www.linkedin.com/in/sara-sabour-63019132/?originalSubdomain=ca&quot;&gt;Sara Sabour&lt;/a&gt;,
 &lt;a href=&quot;https://www.stats.ox.ac.uk/~teh/&quot;&gt;Yee Whye Teh&lt;/a&gt; and
 &lt;a href=&quot;http://www.cs.toronto.edu/~hinton/&quot;&gt;Geoff Hinton&lt;/a&gt;.
 I also thank &lt;a href=&quot;http://arkitus.com/research/&quot;&gt;Ali Eslami&lt;/a&gt; and &lt;a href=&quot;https://danijar.com/&quot;&gt;Danijar Hafner&lt;/a&gt; for helpful discussions.
 Big thanks goes to &lt;a href=&quot;https://people.eecs.berkeley.edu/~shhuang/&quot;&gt;Sandy H. Huang&lt;/a&gt; who helped with making figures and editing the paper.
 Sandy, &lt;a href=&quot;http://adamgol.me/&quot;&gt;Adam Goliński&lt;/a&gt; and &lt;a href=&quot;https://ori.ox.ac.uk/ori-people/martin-engelcke/&quot;&gt;Martin Engelcke&lt;/a&gt;  provided extensive feedback on this post.&lt;/p&gt;

</description>
        <pubDate>Sun, 23 Jun 2019 16:15:00 +0000</pubDate>
        <link>http://akosiorek.github.io/stacked_capsule_autoencoders/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/stacked_capsule_autoencoders/</guid>
        
        
        <category>ML</category>
        
      </item>
    
      <item>
        <title>Forge, or how do you manage your machine learning experiments?</title>
        <description>&lt;p&gt;Every time I begin a machine learning (ML) project, I go through more or less the same steps.
I start by quickly hacking a model prototype and a training script.
After a few days, the codebase grows unruly and any modification is starting to take unreasonably long time due to badly-handled dependencies and the general lack of structure.
At this point, I decide that some refactoring is needed:
parts of the model are wrapped into separate, meaningful objects, and the training script gets somewhat general structure, with clearly delineated sections.
Further down the line, I am often faced with the need of supporting multiple datasets and a variety of models, where the differences between model variants are much more than just hyperparameters - they often differ structurally and have different inputs or outputs.
At this point, I start copying training scripts to support model variants.
It is straightforward to set up, but maintenance becomes a nightmare: with copies of the code living in separate files, any modification has to be applied to all the files.&lt;/p&gt;

&lt;p&gt;For me, it is often unclear how to handle this last bit cleanly.
It can be project-dependent.
It is often easy to come up with simple hacks, but they do not generalise and can make code very messy very quickly.
Given that most experiments look similar among the projects I have worked on, there should exist a general solution.
Let’s have a look at the structure of a typical experiment:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;You specify the data and corresponding hyperparameters.&lt;/li&gt;
  &lt;li&gt;You define the model and its hyperparameters.&lt;/li&gt;
  &lt;li&gt;You run the training script and (hopefully) save model checkpoints and logs during training.&lt;/li&gt;
  &lt;li&gt;Once the training has converged, you might want to load a model checkpoint in another script or a notebook for thorough evaluation or deploy the model.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In most projects I have seen, 1. and 2. were split between the training script (dataset and model classes or functions) and external configs (hyperparameters as command-line arguments or config files).
Logging and saving checkpoints &lt;strong&gt;should&lt;/strong&gt; be a part of every training script, and yet it can be time-consuming to set up correctly.
As far as I know, there is no general mechanism to do 4., and it is typically handled by retrieving hyperparameters used in a specific experiment and using the dataset/model classes/functions directly to instantiate them in a script or a notebook.&lt;/p&gt;

&lt;p&gt;If this indeed is a general structure of an experiment, then there should exist tools to facilitate it.
I am not familiar with any, however. Please let me know if such tools exist, or if the structure outlined above does not generally hold.
&lt;a href=&quot;https://github.com/IDSIA/sacred&quot;&gt;Sacred&lt;/a&gt; and &lt;a href=&quot;https://github.com/QUVA-Lab/artemis&quot;&gt;artemis&lt;/a&gt; are great for managing configuration files and experimental results; you can retrieve configuration of an experiment, but if you want to load a saved model in a notebook, for example, you need to know how to instantiate the model using the config. I prefer to automate this, too.
When it comes to &lt;a href=&quot;https://www.tensorflow.org/&quot;&gt;tensorflow&lt;/a&gt;, there is &lt;a href=&quot;https://keras.io/&quot;&gt;keras&lt;/a&gt; and the &lt;a href=&quot;https://www.tensorflow.org/guide/estimators&quot;&gt;estimator api&lt;/a&gt; that simplify model building, fitting and evaluation.
While generally useful, they are rather heavy and make access to low-level model features difficult.
Their lack of flexibility is a no-go for me since I often work on non-standard models and require access to the most private of their parts.&lt;/p&gt;

&lt;p&gt;All this suggests that we could benefit from a lightweight experimental framework for managing ML experiments.
For me, it would be ideal if it satisfied the following requirements.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;It should require minimal setup.&lt;/li&gt;
  &lt;li&gt;It has to be compatible with tensorflow (my primary tool for ML these days).&lt;/li&gt;
  &lt;li&gt;Ideally, it should be usable with non-tensorflow models - software evolves quickly, and my next project might be in &lt;a href=&quot;https://pytorch.org/&quot;&gt;pytorch&lt;/a&gt;. Who knows?&lt;/li&gt;
  &lt;li&gt;Datasets and models should be specified and configured separately so that they can be mixed and matched later on.&lt;/li&gt;
  &lt;li&gt;Hyerparameters and config files should be stored for every experiment, and it would be great if we could browse them quickly, without using non-standard apps to do so (so no databases).&lt;/li&gt;
  &lt;li&gt;Loading a trained model should be possible with minimum overhead, ideally without touching the original model-building code. Pointing at a specific experiment should be enough.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As far as I know, such a framework does not exist.
So how do I go about it?
Since I started my master thesis at &lt;a href=&quot;http://brml.org/brml/index.html&quot;&gt;BRML&lt;/a&gt;, I have been developing tools, including parts of an experimental framework, that meet some of the above requirements.
However, for every new project I started, I would copy parts of the code responsible for running experiments from the previous project.
After doing that for five different projects (from &lt;a href=&quot;https://github.com/akosiorek/hart&quot;&gt;HART&lt;/a&gt; to &lt;a href=&quot;https://github.com/akosiorek/sqair&quot;&gt;SQAIR&lt;/a&gt;), I’ve had enough.
When I was about to start a new project last week, I’ve taken all the experiment-running code, made it project-agnostic, and put it into a separate repo, wrote some docs, and gave it a name. Lo and behold: &lt;a href=&quot;https://github.com/akosiorek/forge&quot;&gt;Forge&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;forge&quot;&gt;Forge&lt;/h1&gt;
&lt;p&gt;While it is very much work in progress, I would like to show you how to set up your project using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge&lt;/code&gt;.
Who knows, maybe it can simplify your workflow, too?&lt;/p&gt;

&lt;h3 id=&quot;configs&quot;&gt;Configs&lt;/h3&gt;

&lt;p&gt;Configs are perhaps the most useful component in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge&lt;/code&gt;.
The idea is that we can specify an arbitrarily complicated config file as a python function, and then we can load it using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge.load(config_file, *args, **kwargs)&lt;/code&gt;, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config_file&lt;/code&gt; is a path on your filesystem.
The convention is that the config file should define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;load&lt;/code&gt; function with the following signature: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;load(config, *args, **kwargs)&lt;/code&gt;.
The arguments and kw-args passed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge.load&lt;/code&gt; are automatically forwarded to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;load&lt;/code&gt; function in the config file.
Why would you load a config by giving its file path? To make code maintenance easier!
Once you write config-loading code in your training/experimentation scripts, it is best not to touch it anymore.
But how do you swap config files?
&lt;strong&gt;Without&lt;/strong&gt; touching the training script:
If we specify file paths as command-line arguments, then we can do it easily.
Here’s an example.
Suppose that our data config file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data_config.py&lt;/code&gt; is the following:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow.examples.tutorials.mnist&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_data&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# The `config` argument is here unused, but you can treat it
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# as a dict of keys and values accessible as attributes - it acts
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# like an AttrDict
&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_data_sets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# download MNIST
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# to the current working dir and load it
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Our model file defines a simple one-layer fully-connected neural net, classification loss and some metrics in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model_config.py&lt;/code&gt;. It can read as follows.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sonnet&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;forge&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;n_hidden&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Number of hidden units.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;process_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# this function should return a minibatch, somehow
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;imgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;process_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;imgs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BatchFlatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MLP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# softmax cross-entropy
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparse_softmax_cross_entropy_with_logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# predicted class and accuracy
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pred_class&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred_class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# put here everything that you might want to use later
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# for example when you load the model in a jupyter notebook
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;artefacts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&apos;mlp&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&apos;logits&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&apos;loss&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&apos;pred_class&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pred_class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&apos;accuracy&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# put here everything that you&apos;d like to be reported every N training iterations
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# as tensorboard logs AND on the command line
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;crossentropy&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;accuracy&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# loss will be minimised with respect to the model parameters
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;artefacts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can write a simple script called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;experiment.py&lt;/code&gt; that loads some data and model config files and does useful things with them.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;osp&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;forge&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;forge&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;# job config
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;data_config&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;data_config.py&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Path to a data config file.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;model_config&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;model_config.py&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Path to a model config file.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;batch_size&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Minibatch size used for training.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# parse command-line flags
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stuff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# ...
# do useful stuff
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here’s the best part.
You can just run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python experiment.py&lt;/code&gt; to run the script with the config files given above.
But if you would like to run a different config, you can execute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python experiment.py --data_config some/config/file/path.py&lt;/code&gt; without touching experimental code.
All this is very lightweight, as config files can return anything and take any arguments you find necessary.&lt;/p&gt;

&lt;h3 id=&quot;smart-checkpoints&quot;&gt;Smart checkpoints&lt;/h3&gt;
&lt;p&gt;Given that we have very general and flexible config files, it should be possible to abstract away model loading.
It would be great, for instance, if we could load a trained model snapshot &lt;strong&gt;without&lt;/strong&gt; pointing to the config files (or model-building code, generally speaking) used to train the model.
We can do it by storing config files with model snapshots.
It can significantly simplify model evaluation and deployment and increase reproducibility of our experiments.
How do we do it?
This feature requires a bit more setup than just using config files, but bear with me - it might be even more useful.&lt;/p&gt;

&lt;p&gt;The smart checkpoint framework depends on the following folder structure.&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;results_dir&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run_name&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;|&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# number of the current run
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;results_dir&lt;/code&gt; is the top-level directory containing potentially many experiment-specific folders, where every experiment has a separate folder denoted by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run_name&lt;/code&gt;.
We might want to re-run a specific experiment, and for this reason, every time we run it, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge&lt;/code&gt; creates a folder, whose name is an integral number - the number of this run.
It starts at one and gets incremented every time we start a new run of the same experiment.
Instead of starting a new run, we can also resume the last one by passing a flag.
In this case, we do not create a new folder for it, but use the highest-numbered folder and load the latest model snapshot.&lt;/p&gt;

&lt;p&gt;First, we need to import &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge.experiment_tools&lt;/code&gt; and define the following flags.&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;osp&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;forge&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;experiment_tools&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fet&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;results_dir&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;../checkpoints&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Top directory for all experimental results.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;run_name&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;test_run&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Name of this job. Results will be stored in a corresponding folder.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFINE_boolean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;resume&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Tries to resume a job if True.&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can then parse the flags and initialise our checkpoint.&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# parse flags
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# initialize smart checkpoint
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logdir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;osp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;results_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logdir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;resume_checkpoint&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logdir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resume&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fet.init_checkpoint&lt;/code&gt; does a few useful things:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Creates the directory structure mentioned above.&lt;/li&gt;
  &lt;li&gt;Copies the data and model config files to the checkpoint folder.&lt;/li&gt;
  &lt;li&gt;Stores all configuration flags &lt;strong&gt;and the hash of the current git commit (if we’re in a git repo, very useful for reproducibility)&lt;/strong&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flags.json&lt;/code&gt;, or restores flags if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;restore&lt;/code&gt; was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Figures out whether there exists a model snapshot file that should be loaded.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logdir&lt;/code&gt; is the path to our checkpoint folder and evaluates to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;results_dir/run_name/&amp;lt;integer&amp;gt;&lt;/code&gt;.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resume_checkpoint&lt;/code&gt; is a path to a checkpoint if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resume&lt;/code&gt; was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt;, typically &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;results_dir/run_name/&amp;lt;integer&amp;gt;/model.ckpt-&amp;lt;maximum global step&amp;gt;&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt; otherwise.&lt;/p&gt;

&lt;p&gt;Now we need to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logdir&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resume_checkpoint&lt;/code&gt; to store any logs and model snapshots.
For example:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# load data/model and do other setup
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Try to restore the model from a checkpoint
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_to_keep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;resume_checkpoint&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Restoring checkpoint from &apos;{}&apos;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resume_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;restore&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;resume_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# somewhere inside the train loop
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;checkpoint_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;global_step&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_itr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we want to load our model snapshot in another script, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eval.py&lt;/code&gt;, say, we can do so in a very straightforward manner.&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;forge&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_from_checkpoint&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;checkpoint_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;../checkpoints/mnist/1&apos;&lt;/span&gt;  
&lt;span class=&quot;n&quot;&gt;checkpoint_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# `data` contains any outputs of the data config file
# `model` contains any outputs of the model config file
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;restore_func&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_from_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpoint_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;checkpoint_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Calling `restore_func` restores all model parameters
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;restore_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# do exciting stuff with the model
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;working-example&quot;&gt;Working Example&lt;/h3&gt;
&lt;p&gt;Code for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge&lt;/code&gt; is available at &lt;a href=&quot;https://github.com/akosiorek/forge&quot;&gt;github.com/akosiorek/forge&lt;/a&gt; and a working example is described in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;closing-thoughts&quot;&gt;Closing thoughts&lt;/h1&gt;
&lt;p&gt;Even though experimental code exhibits very similar structure among experiments, there seem to be no tools to streamline the experimentation process.
This requires ML practitioners to write thousands of lines of boilerplate code, contributes to many errors and generally slows down research progress.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge&lt;/code&gt; is my attempt at introducing some good practices as well as simplifying the process.
Hope you can take something from it for your own purposes.&lt;/p&gt;

&lt;h4 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h4&gt;
&lt;p&gt;I would like to thank &lt;a href=&quot;http://alex.bewley.ai/&quot;&gt;Alex Bewley&lt;/a&gt; for inspiration, &lt;a href=&quot;http://adamgol.me/&quot;&gt;Adam Goliński&lt;/a&gt; for discussions about software engineering in ML and &lt;a href=&quot;https://ori.ox.ac.uk/ori-people/martin-engelcke/&quot;&gt;Martin Engelcke&lt;/a&gt; for his feedback on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forge&lt;/code&gt;.&lt;/p&gt;
</description>
        <pubDate>Wed, 28 Nov 2018 10:15:00 +0000</pubDate>
        <link>http://akosiorek.github.io/forge/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/forge/</guid>
        
        
        <category>ml</category>
        
      </item>
    
      <item>
        <title>Normalizing Flows</title>
        <description>&lt;p&gt;Machine learning is all about probability.
To train a model, we typically tune its parameters to maximise the probability of the training dataset under the model.
To do so, we have to assume some probability distribution as the output of our model.
The two distributions most commonly used are &lt;a href=&quot;https://en.wikipedia.org/wiki/Categorical_distribution&quot;&gt;Categorical&lt;/a&gt; for classification and &lt;a href=&quot;https://en.wikipedia.org/wiki/Normal_distribution&quot;&gt;Gaussian&lt;/a&gt; for regression.
The latter case can be problematic, as the true probability density function (pdf) of real data is often far from Gaussian.
If we use the Gaussian as likelihood for image-generation models, we end up with blurry reconstructions.
We can circumvent this issue by &lt;a href=&quot;http://openaccess.thecvf.com/content_cvpr_2017/papers/Ledig_Photo-Realistic_Single_Image_CVPR_2017_paper.pdf&quot;&gt;adversarial training&lt;/a&gt;, which is an example of likelihood-free inference, but this approach has its own issues.&lt;/p&gt;

&lt;p&gt;Gaussians are also used, and often prove too simple, as the pdf for latent variables in Variational Autoencoders (VAEs), which I describe in my &lt;a href=&quot;http://akosiorek.github.io/ml/2018/03/14/what_is_wrong_with_vaes.html&quot;&gt;previous post&lt;/a&gt;.
Fortunately, we can often take a simple probability distribution, take a sample from it and then transform the sample.
This is equivalent to change of variables in probability distributions and, if the transformation meets some mild conditions, can result in a very complex pdf of the transformed variable.
&lt;a href=&quot;https://danilorezende.com/&quot;&gt;Danilo Rezende&lt;/a&gt; formalised this in his paper on &lt;a href=&quot;https://arxiv.org/abs/1505.05770&quot;&gt;Normalizing Flows (NF)&lt;/a&gt;, which I describe below.
NFs are usually used to parametrise the approximate posterior \(q\) in VAEs but can also be applied for the likelihood function.&lt;/p&gt;

&lt;h1 id=&quot;change-of-variables-in-probability-distributions&quot;&gt;Change of Variables in Probability Distributions&lt;/h1&gt;
&lt;p&gt;We can transform a probability distribution using an invertible mapping (&lt;em&gt;i.e.&lt;/em&gt; bijection).
Let \(\mathbf{z} \in \mathbb{R}^d\) be a random variable and \(f: \mathbb{R}^d \mapsto \mathbb{R}^d\) an invertible smooth mapping.
We can use \(f\) to transform \(\mathbf{z} \sim q(\mathbf{z})\).
The resulting random variable \(\mathbf{y} = f(\mathbf{z})\) has the following probability distribution:&lt;/p&gt;

\[q_y(\mathbf{y}) = q(\mathbf{z}) \left|
    \mathrm{det} \frac{
      \partial f^{-1}
    }{
      \partial \mathbf{z}\
    }
  \right|
  = q(\mathbf{z}) \left|
    \mathrm{det} \frac{
      \partial f
    }{
      \partial \mathbf{z}\
    }
  \right| ^{-1}. \tag{1}\]

&lt;p&gt;We can apply a series of mappings \(f_k\), \(k \in {1, \dots, K}\), with \(K \in \mathbb{N}_+\) and obtain a normalizing flow, first introduced in &lt;a href=&quot;https://arxiv.org/abs/1505.05770&quot;&gt;Variational Inference with Normalizing Flows&lt;/a&gt;,&lt;/p&gt;

\[\mathbf{z}_K = f_K \circ \dots \circ f_1 (\mathbf{z}_0), \quad \mathbf{z}_0 \sim q_0(\mathbf{z}_0), \tag{2}\]

\[\mathbf{z}_K \sim q_K(\mathbf{z}_K) = q_0(\mathbf{z}_0) \prod_{k=1}^K
  \left|
    \mathrm{det} \frac{
      \partial f_k
    }{
      \partial \mathbf{z}_{k-1}\
    }
  \right| ^{-1}. \tag{3}\]

&lt;p&gt;This series of transformations can transform a simple probability distribution (&lt;em&gt;e.g.&lt;/em&gt; Gaussian) into a complicated multi-modal one.
To be of practical use, however, we can consider only transformations whose determinants of Jacobians are easy to compute.
The original paper considered two simple family of transformations, named planar and radial flows.&lt;/p&gt;

&lt;h1 id=&quot;simple-flows&quot;&gt;Simple Flows&lt;/h1&gt;
&lt;h2 id=&quot;planar-flow&quot;&gt;Planar Flow&lt;/h2&gt;
&lt;p&gt;\(f(\mathbf{z}) = \mathbf{z} + \mathbf{u} h(\mathbf{w}^T \mathbf{z} + b), \tag{4}\)&lt;/p&gt;

&lt;p&gt;with \(\mathbf{u}, \mathbf{w} \in \mathbb{R}^d\) and \(b \in \mathbb{R}\) and \(h\) an element-wise non-linearity.
Let \(\psi (\mathbf{z}) = h&apos; (\mathbf{w}^T \mathbf{z} + b) \mathbf{w}\). The determinant can be easily computed as&lt;/p&gt;

\[\left| \mathrm{det} \frac{\partial f}{\partial \mathbf{z}} \right| =
  \left| 1 + \mathbf{u}^T \psi( \mathbf{z} ) \right|. \tag{5}\]

&lt;p&gt;We can think of it as slicing the \(\mathbf{z}\)-space with straight lines (or hyperplanes), where each line contracts or expands the space around it, see &lt;a href=&quot;#simple_flows&quot;&gt;figure 1&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;radial-flow&quot;&gt;Radial Flow&lt;/h2&gt;

\[f(\mathbf{z}) = \mathbf{z} + \beta h(\alpha, r)(\mathbf{z} - \mathbf{z}_0), \tag{6}\]

&lt;p&gt;with \(r = \Vert\mathbf{z} - \mathbf{z}_0\Vert_2\), \(h(\alpha, r) = \frac{1}{\alpha + r}\)
and parameters \(\mathbf{z}_0 \in \mathbb{R}^d, \alpha \in \mathbb{R}_+\) and \(\beta \in \mathbb{R}\).&lt;/p&gt;

&lt;p&gt;Similarly to planar flows, radial flows introduce spheres in the \(\mathbf{z}\)-space, which either contract or expand the space inside the sphere, see &lt;a href=&quot;#simple_flows&quot;&gt;figure 1&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;discussion&quot;&gt;Discussion&lt;/h2&gt;
&lt;p&gt;These simple flows are useful only for low dimensional spaces, since each transformation affects only a small volume in the original space. As the volume of the space grows exponentially with the number of dimensions \(d\), we need a lot of layers in a high-dimensional space.&lt;/p&gt;

&lt;p&gt;Another way to understand the need for many layers is to look at the form of the mappings. Each mapping behaves as a hidden layer of a neural network with one hidden unit and a skip connection. Since a single hidden unit is not very expressive, we need a lot of transformations. Recently introduced &lt;a href=&quot;https://arxiv.org/abs/1803.05649&quot;&gt;Sylvester Normalising Flows&lt;/a&gt; overcome the single-hidden-unit issue of these simple flows; for more details please read the paper.&lt;/p&gt;

&lt;p&gt;Simple flows are useful for sampling, &lt;em&gt;e.g.&lt;/em&gt; as parametrisation of \(q(\mathbf{z})\) in VAEs, but it is very difficult to evaluate probability of a data point that was not sampled from it.
This is because the functions \(h\) in planar and radial flow are invertible only in some regions of the \(\mathbf{z}\)-space, and the functional form of their inverse is generally unknown. Please drop a comment if you have an idea how to fix that.&lt;/p&gt;

&lt;figure&gt;
  &lt;a name=&quot;simple_flows&quot;&gt;&lt;/a&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/simple_flows.png&quot; alt=&quot;Planar and Radial Flows&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
    &lt;b&gt;Fig 1.&lt;/b&gt; The effect of planar and radial flows on the Gaussian and uniform distributions. The figure comes from the &lt;a href=&quot;https://arxiv.org/abs/1505.05770&quot;&gt;original paper&lt;/a&gt;.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h1 id=&quot;autoregressive-flows&quot;&gt;Autoregressive Flows&lt;/h1&gt;
&lt;p&gt;Enhancing expressivity of normalising flows is not easy, since we are constrained by functions, whose Jacobians are easy to compute.
It turns out, though, that we can introduce dependencies between different dimensions of the latent variable, and still end up with a tractable Jacobian.
Namely, if after a transformation, the dimension \(i\) of the resulting variable depends only on dimensions \(1:i\) of the input variable, then the Jacobian of this transformation is triangular.
As we know, a determinant of a triangular matrix is equal to the product of the terms on the diagonal.
More formally, let \(J \in \mathcal{R}^{d \times d}\) be the Jacobian of the mapping \(f\), then&lt;/p&gt;

\[y_i = f(\mathbf{z}_{1:i}),
  \qquad J = \frac{\partial \mathbf{y}}{\partial \mathbf{z}}, \tag{7}\]

\[\det{J} = \prod_{i=1}^d J_{ii}. \tag{8}\]

&lt;p&gt;I would like to draw your attention to three interesting flows that use the above observation, albeit in different ways, and arrive at mappings with very different properties.&lt;/p&gt;

&lt;h2 id=&quot;real-non-volume-preserving-flows-r-nvp&quot;&gt;&lt;a href=&quot;https://arxiv.org/abs/1605.08803&quot;&gt;Real Non-Volume Preserving Flows (R-NVP)&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;R-NVPs are arguably the least expressive but the most generally applicable of the three.
Let \(1 &amp;lt; k &amp;lt; d\), \(\circ\) element-wise multiplication and \(\mu\) and \(\sigma\) two mappings \(\mathcal{R}^k \mapsto \mathcal{R}^{d-k}\) (Note that \(\sigma\) is &lt;strong&gt;not&lt;/strong&gt; the sigmoid function). R-NVPs are defined as:&lt;/p&gt;

\[\mathbf{y}_{1:k} = \mathbf{z}_{1:k},\\
  \mathbf{y}_{k+1:d} = \mathbf{z}_{k+1:d} \circ \sigma(\mathbf{z}_{1:k}) + \mu(\mathbf{z}_{1:k}). \tag{9}\]

&lt;p&gt;It is an autoregressive transformation, although not as general as equation (7) allows.
It copies the first \(k\) dimensions, while shifting and scaling all the remaining ones.
The first part of the Jacobian (up to dimension \(k\)) is just an identity matrix, while the second part is lower-triangular with \(\sigma(\mathbf{z}_{1:k})\) on the diagonal.
Hence, the determinant of the Jacobian is&lt;/p&gt;

\[\frac{\partial \mathbf{y}}{\partial \mathbf{z}} = \prod_{i=1}^{d-k} \sigma_i(\mathbf{z}_{1:k}). \tag{10}\]

&lt;p&gt;R-NVPs are particularly attractive, because both sampling and evaluating probability of some external sample are very efficient.
Computational complexity of both operations is, in fact, exactly the same.
This allows to use R-NVPs as a parametrisation of an approximate posterior \(q\) in VAEs, but also as the output likelihood (in VAEs or general regression models).
To see this, first note that we can compute all elements of \(\mu\) and \(\sigma\) in parallel, since all inputs (\(\mathbf{z}\)) are available.
We can therefore compute \(\mathbf{y}\) in a single forward pass.
Next, note that the inverse transformation has the following form, with all divisions done element-wise,&lt;/p&gt;

\[\mathbf{z}_{1:k} = \mathbf{y}_{1:k},\\
\mathbf{z}_{k+1:d} = (\mathbf{y}_{k+1:d} - \mu(\mathbf{y}_{1:k}))~/~\sigma(\mathbf{y}_{1:k}). \tag{11}\]

&lt;p&gt;Note that \(\mu\) and \(\sigma\) are usually implemented as neural networks, which are generally not invertible. Thanks to equation (11), however, they do not have to be invertible for the whole R-NVP transformation to be invertible.
The original paper applies several layers of this mapping.
The authors also reverse the ordering of dimensions after every step.
This way, variables that are just copied in one step, are transformed in the following step.&lt;/p&gt;

&lt;h2 id=&quot;autoregressive-transformation&quot;&gt;Autoregressive Transformation&lt;/h2&gt;
&lt;p&gt;We can be even more expressive than R-NVPs, but we pay a price.
Here’s why.&lt;/p&gt;

&lt;p&gt;Now, let \(\mathbf{\mu} \in \mathbb{R}^d\) and \(\mathbf{\sigma} \in \mathbb{R}^d_+\).
We can introduce complex dependencies between dimensions of the random variable \(\mathbf{y} \in \mathbb{R}^d\) by specifying it in the following way.&lt;/p&gt;

\[y_1 = \mu_1 + \sigma_1 z_1 \tag{12}\]

\[y_i = \mu (\mathbf{y}_{1:i-1}) + \sigma (\mathbf{y}_{1:i-1}) z_i \tag{13}\]

&lt;p&gt;Since each dimension depends only on the previous dimensions, the Jacobian of this transformation is a lower-triangular matrix with \(\sigma (\mathbf{z}_{1:i-1})\) on the diagonal;
the determinant is just a product of the terms on the diagonal.
We might be able to sample \(\mathbf{z} \sim q(\mathbf{z})\) in parallel (if different dimensions are &lt;em&gt;i.i.d.&lt;/em&gt;), but the transformation is inherently sequential.
We need to compute all \(\mathbf{y}_{1:i-1}\) before computing \(\mathbf{y}_i\), which can be time consuming, and is therefore expensive to use as a parametrisation for the approximate posterior in VAEs.&lt;/p&gt;

&lt;p&gt;This is an invertible transformation, and the inverse has the following form.&lt;/p&gt;

\[z_i = \frac{
    y_i - \mu (\mathbf{y}_{1:i-1})
  }{
    \sigma (\mathbf{y}_{1:i-1})
  } \tag{14}\]

&lt;p&gt;Given vectors \(\mathbf{\mu}\) and \(\mathbf{\sigma}\), we can vectorise the inverse transformation, similar to equation (11), as&lt;/p&gt;

\[\mathbf{z} = \frac{
    \mathbf{y} - \mathbf{\mu} (\mathbf{y})
  }{
    \mathbf{\sigma} (\mathbf{y})
}. \tag{15}\]

&lt;p&gt;The Jacobian is again lower-triangular, with \(\frac{1}{\mathbf{\sigma}}\) on the diagonal and
we can compute probability in a single pass.&lt;/p&gt;

&lt;p&gt;The difference between the forward and the inverse transofrmations is that in the forward transformation, statistics used to transform every dimension depend on all the previously transformed dimensions. In the inverse transformation, the statistics used to invert \(\mathbf{y}\) (which is the input), depend only on that input, and not on any result of the inversion.&lt;/p&gt;

&lt;h2 id=&quot;masked-autoregressive-flow-maf&quot;&gt;&lt;a href=&quot;https://arxiv.org/abs/1705.07057&quot;&gt;Masked Autoregressive Flow (MAF)&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;MAF directly uses equations (12) and (13) to transform as random variable.
Since this transformation is inherently sequential, MAF is terribly slow when it comes to sampling.
To evaluate the probability of a sample, however, we need the inverse mapping.
MAF, which was designed for density estimation, can do that efficiently by using equation (15).&lt;/p&gt;

&lt;p&gt;In principle, we could use it to parametrise the likelihood function (&lt;em&gt;a.k.a.&lt;/em&gt; the decoder) in VAEs. Training would be fast, but, if the data dimensionality is high (&lt;em&gt;e.g.&lt;/em&gt; images), generating new data would take very long.
For a colour image of size \(300 \times 200\), we would need to perform \(300 \cdot 200 \cdot 3 = 1.8 \cdot 10^5\) sequential iterations of equation (13).
This &lt;strong&gt;cannot&lt;/strong&gt; be parallelised, and hence, we abandon the all powerful GPUs we otherwise use.&lt;/p&gt;

&lt;p&gt;We could also use MAF as a prior \(p(\mathbf{z})\) in VAEs.
Training requires only evaluation of a sample \(\mathbf{z} \sim q(\mathbf{z})\) under the prior \(p(\mathbf{z})\).
The dimensionality \(d\) of the latent variable \(\mathbf{z}\) is typically much smaller than that of the output; often below \(1000\).
Sampling can still be expensive, but at least doable.&lt;/p&gt;

&lt;p&gt;What other applications would you use MAF in? Please write a comment if anything comes to mind.&lt;/p&gt;

&lt;h2 id=&quot;inverse-autoregressive-flow-iaf&quot;&gt;&lt;a href=&quot;https://arxiv.org/abs/1606.04934&quot;&gt;Inverse Autoregressive Flow (IAF)&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;IAF defines a pdf by using a reparametrised version of equations (14) and (15), which we derive later.
In this case, the transformed variable is defined as an inverse autoregressive mapping of the following form.&lt;/p&gt;

\[y_i = z_i \sigma (\mathbf{z}_{1:i-1}) + \mu (\mathbf{z}_{1:i-1}) \tag{16}\]

&lt;p&gt;Since all \(\mu\) and \(\sigma\) depend only on \(\mathbf{z}\) but not on \(\mathbf{y}\), they can be all computed in parallel, in a single forward pass.&lt;/p&gt;

\[\mathbf{y} = \mathbf{z} \circ \sigma (\mathbf{z}) + \mu (\mathbf{z}). \tag{17}\]

&lt;p&gt;To understand how IAF affects the pdf of \(\mathbf{z}\), we can compute the resulting probability density function. Other types of flows admit similar derivations. Here, we assume that \(\mathbf{z}\) follows a unit Gaussian,&lt;/p&gt;

\[\log q( \mathbf{z} )
  = \log \mathcal{N} (\mathbf{z} \mid \mathbf{0}, \mathbf{I})
  = - \sum_{i=1}^d \left(
    \log z_i + \frac{1}{2} \log 2 \pi
  \right)
  = - \frac{d}{2} \log 2 \pi - \sum_{i=1}^d \log z_i. \tag{18}\]

&lt;p&gt;The final pdf can be comprised of \(K \in \mathcal{N}_+\) IAFs.
To take this into account, we now set \(\mathbf{z}_k = \mathbf{z}\) and \(\mathbf{z}_{k+1} = \mathbf{y}\);
&lt;em&gt;i.e.&lt;/em&gt; \(\mathbf{z}_{k+1}\) is the result of transforming \(\mathbf{z}_k\).
To factor in subsequent transformations, we need to compute all the Jacobians:&lt;/p&gt;

\[\frac{\partial \mathbf{z}_k}{\partial \mathbf{z}_{k-1}}
  = \underbrace{
    \frac{\partial \mu_k}{\partial \mathbf{z}_{k-1}}
  + \frac{\partial \sigma_k}{\partial \mathbf{z}_{k-1}} \mathrm{diag} ( \mathbf{z}_{k-1} )
}_\text{lower triangular with zeros on the diagonal}
  + \mathrm{diag}( \sigma_k )
  \underbrace{
    \frac{\partial \mathbf{z}_{k-1}}{\partial \mathbf{z}_{k-1}}
  }_{= \mathbf{I}} \tag{19}\]

&lt;p&gt;If \(\mu_k = \mu_k ( \mathbf{z}_{k-1})\) and \(\sigma_k = \sigma_k ( \mathbf{z}_{k-1})\) are implemented as autoregressive transformations (with respect to \(\mathbf{z}_{k-1}\)), then the first two terms in the Jacobian above are lower triangular matrices with zeros on the diagonal.
The last term is a diagonal matrix, with \(\sigma_k\) on the diagonal.
Thus, the determinant of the Jacobian is just&lt;/p&gt;

\[\mathrm{det} \left( \frac{\partial \mathbf{z}_k}{\partial \mathbf{z}_{k-1}} \right) = \prod_{i=1}^d \sigma_{k, i}. \tag{20}\]

&lt;p&gt;Therefore, the final log-probability can be written as&lt;/p&gt;

\[\log q_K (\mathbf{z}_K) = \log q(\mathbf{z}) - \sum_{k=0}^K \sum_{i=1}^d \log \sigma_{k, i}. \tag{21}\]

&lt;p&gt;Sampling from an IAF is easy, since we just sample \(\mathbf{z} \sim q(\mathbf{z})\) and then forward-transform it into \(\mathbf{z}_K\).
Each of the transformations gives us the vector \(\sigma_k\), so that we can readily evaluate the probability of the sample \(q_K(\mathbf{z}_K)\).&lt;/p&gt;

&lt;p&gt;To evaluate the density of a sample not taken from \(q_K\), we need to compute the chain of inverse transformations \(f^{-1}_k\), \(k = K, \dots, 0\). To do so, we have to sequentially compute&lt;/p&gt;

\[\mathbf{z}_{k-1, 1} = \frac{\mathbf{z}_{k, 1} - \mu_{k, 1}}{\sigma_{k, 1}},\\
  \mathbf{z}_{k-1, i} = \frac{\mathbf{z}_{k, i} - \mu_{k, i} (\mathbf{z}_{k-1, 1:i-1})}{\sigma_{k, i} (\mathbf{z}_{k-1, 1:i-1})}. \tag{22}\]

&lt;p&gt;This can be expensive, but as long as \(\mu\) and \(\sigma\) are implemented as autoregressive transformations, it is possible.&lt;/p&gt;

&lt;h2 id=&quot;maf-vs-iaf&quot;&gt;MAF vs IAF&lt;/h2&gt;
&lt;p&gt;Both MAF and IAF use autoregressive transformations, but in a different way.
To see that IAF really is the inverse of MAF and that the equation (16) is in fact a reparametrised version of equation (14), set \(\tilde{z}_i = y_i\), \(\tilde{y}_i = z_i\), \(\tilde{\mu} = -\frac{\mu}{\sigma}\) and \(\tilde{\sigma} = \frac{1}{\sigma}\).&lt;/p&gt;

\[(16) \implies
  \tilde{z}_i = -\frac{\tilde{\mu} (\tilde{\mathbf{y}}_{1:i-1})}{ \tilde{\sigma} (\tilde{\mathbf{y}}_{1:i-1})} + \frac{1}{\tilde{\sigma} (\tilde{\mathbf{y}}_{1:i-1})}\tilde{y}_i =
  \frac{\tilde{y}_i - \tilde{\mu} (\tilde{\mathbf{y}}_{1:i-1})}{ \tilde{\sigma}(\tilde{\mathbf{y}}_{1:i-1})}
  = (14).\]

&lt;p&gt;This reparametrisation is useful, because it avoids divisions, which can be numerically unstable.
To allow the vectorised form of equations (15) and (17), \(\mu\) and \(\sigma\) have to be implemented as autoregressive functions; and one efficient way to do so is to use &lt;a href=&quot;https://arxiv.org/abs/1502.03509&quot;&gt;MADE&lt;/a&gt;-type neural networks (nicely explained in &lt;a href=&quot;http://www.inference.vc/masked-autoencoders-icml-paper-highlight/&quot;&gt;this blog post by Ferenc&lt;/a&gt;).
In fact, both original papers use MADE as a building block.&lt;/p&gt;

&lt;p&gt;To understand the trade-offs between MAF and IAF, it is instructive to study equations (15) and (17) in detail.
You will notice, that although the equations look very similar, the position of inputs \(\mathbf{z}\) and outputs \(\mathbf{y}\) is swapped.
This is why for IAF, sampling is efficient but density estimation is not, while for MAF, sampling is inefficient while density estimation is very fast.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1711.10433&quot;&gt;Parallel WaveNet&lt;/a&gt; introduced the notion of Distribution Distillation, which combines the advantages of both types of flows.
It trains one model, which closely resembles MAF, for density estimation.
Its role is just to evaluate probability of a data point, given that data point.
Once this model is trained, the authors instantiate a second model parametrised by IAF.
Now, we can draw samples from IAF and evaluate their probability under the MAF.
This allows us to compute Monte-Carlo approximation of the &lt;a href=&quot;https://www.countbayesie.com/blog/2017/5/9/kullback-leibler-divergence-explained&quot;&gt;&lt;em&gt;KL-divergence&lt;/em&gt;&lt;/a&gt; between the two probability distributions, which we can use as a training objective for IAF.
This way, MAF acts as a teacher and IAF as a student.
This clever application of both types of flows allowed to improve efficiency of the &lt;a href=&quot;https://arxiv.org/abs/1609.03499&quot;&gt;original WaveNet&lt;/a&gt; by the factor of 300.&lt;/p&gt;

&lt;h1 id=&quot;further-reading&quot;&gt;Further reading&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://blog.evjang.com/2018/01/nf1.html&quot;&gt;Two-part practical tutorial on normalising flows by Eric Jang&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1705.07057&quot;&gt;MAF paper&lt;/a&gt; explores theoretical links between R-NVP, MAF and IAF in great detail,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1711.10433&quot;&gt;Parallel WaveNet&lt;/a&gt; combines MAF and IAF in a very clever trick the authors call Distribution Distillation,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1709.01179&quot;&gt;Continuous-Time Flows&lt;/a&gt;, as an example of even more expressive transformation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h4&gt;
&lt;p&gt;I would like to thank &lt;a href=&quot;http://adamgol.me/&quot;&gt;Adam Goliński&lt;/a&gt; for fruitful discussions as well as his detailed feedback and numerous remarks on how to improve this post.&lt;/p&gt;
</description>
        <pubDate>Tue, 03 Apr 2018 09:43:00 +0000</pubDate>
        <link>http://akosiorek.github.io/norm_flows/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/norm_flows/</guid>
        
        
        <category>ml</category>
        
      </item>
    
      <item>
        <title>What is wrong with VAEs?</title>
        <description>&lt;h1 id=&quot;latent-variable-models&quot;&gt;Latent Variable Models&lt;/h1&gt;
&lt;p&gt;Suppose you would like to model the world in terms of the probability distribution over its possible states \(p(\mathbf{x})\) with \(\mathbf{x} \in \mathcal{R}^D\).
The world may be complicated and we do not know what form \(p(\mathbf{x})\) should have.
To account for it, we introduce another variable \(\mathbf{z} \in \mathcal{R}^d\), which describes, or explains the content of \(\mathbf{x}\).
If \(\mathbf{x}\) is an image, \(\mathbf{z}\) can contain information about the number, type and appearance of objects visible in the scene as well as the background and lighting conditions.
This new variable allows us to express \(p(\mathbf{x})\) as an infinite mixture model,&lt;/p&gt;

\[p(\mathbf{x}) = \int p(\mathbf{x} \mid \mathbf{z}) p(\mathbf{z})~d \mathbf{z}. \tag{1}\]

&lt;p&gt;It is a mixture model, because for every possible value of \(\mathbf{z}\), we add another conditional distribution to \(p(\mathbf{x})\), weighted by its probability.&lt;/p&gt;

&lt;p&gt;Having a setup like that, it is interesting to ask what the latent variables \(\mathbf{z}\) are, given an observation \(\mathbf{x}\).
Namely, we would like to know the posterior distribution \(p(\mathbf{z} \mid \mathbf{x})\).
However, the relationship between \(\mathbf{z}\) and \(\mathbf{x}\) can be highly non-linear (&lt;em&gt;e.g.&lt;/em&gt; implemented by a multi-layer neural network) and both \(D\), the dimensionality of our observations, and \(d\), the dimensionality of the latent variable, can be quite large.
Since both marginal and posterior probability distributions require evaluation of the integral in eq. (1), they are intractable.&lt;/p&gt;

&lt;p&gt;We could try to approximate eq. (1) by Monte-Carlo sampling as \(p(\mathbf{x}) \approx \frac{1}{M} \sum_{m=1}^M p(\mathbf{x} \mid \mathbf{z}^{(m)})\), \(\mathbf{z}^{(m)} \sim p(\mathbf{z})\), but since the volume of \(\mathbf{z}\)-space is potentially large, we would need millions of samples of \(\mathbf{z}\) to get a reliable estimate.&lt;/p&gt;

&lt;p&gt;To train a probabilistic model, we can use a parametric distribution - parametrised by a neural network with parameters \(\theta \in \Theta\).
We can now learn the parameters by maximum likelihood estimation,&lt;/p&gt;

\[\theta^\star = \arg \max_{\theta \in \Theta} p_\theta(\mathbf{x}). \tag{2}\]

&lt;p&gt;The problem is, we cannot maximise an expression (eq. (1)), which we can’t even evaluate.
To improve things, we can resort to &lt;a href=&quot;https://en.wikipedia.org/wiki/Importance_sampling&quot;&gt;importance sampling (IS)&lt;/a&gt;.
When we need to evaluate an expectation with respect to the original (&lt;em&gt;nominal&lt;/em&gt;) probability density function (&lt;em&gt;pdf&lt;/em&gt;), IS allows us to sample from a different probability distribution (&lt;em&gt;proposal&lt;/em&gt;) and then weigh those samples with respect to the nominal pdf.
Let \(q_\phi ( \mathbf{z} \mid \mathbf{x})\) be our proposal - a probability distribution parametrised by a neural network with parameters \(\phi \in \Phi\).
We can write&lt;/p&gt;

\[p_\theta(\mathbf{x}) = \int p(\mathbf{z}) p_\theta (\mathbf{x} \mid \mathbf{z})~d \mathbf{z} =\\
  \mathbb{E}_{p(\mathbf{z})} \left[ p_\theta (\mathbf{x} \mid \mathbf{z} )\right] =
  \mathbb{E}_{p(\mathbf{z})} \left[ \frac{q_\phi ( \mathbf{z} \mid \mathbf{x})}{q_\phi ( \mathbf{z} \mid \mathbf{x})} p_\theta (\mathbf{x} \mid \mathbf{z} )\right] =
  \mathbb{E}_{q_\phi ( \mathbf{z} \mid \mathbf{x})} \left[ \frac{p_\theta (\mathbf{x} \mid \mathbf{z} ) p(\mathbf{z})}{q_\phi ( \mathbf{z} \mid \mathbf{x})} )\right]. \tag{3}\]

&lt;p&gt;From &lt;a href=&quot;http://statweb.stanford.edu/~owen/mc/Ch-var-is.pdf&quot;&gt;importance sampling literature&lt;/a&gt; we know that the optimal proposal is proportional to the nominal pdf times the function, whose expectation we are trying to approximate.
In our setting, that function is just \(p_\theta (\mathbf{x} \mid \mathbf{z} )\).
From Bayes’ theorem, \(p(z \mid x) = \frac{p(x \mid z) p (z)}{p(x)}\), we see that the optimal proposal is proportional to the posterior distribution, which is of course intractable.&lt;/p&gt;

&lt;h1 id=&quot;rise-of-a-variational-autoencoder&quot;&gt;Rise of a Variational Autoencoder&lt;/h1&gt;
&lt;p&gt;Fortunately, it turns out, we can kill two birds with one stone:
by trying to approximate the posterior with a learned proposal, we can efficiently approximate the marginal probability \(p_\theta(\mathbf{x})\).
A bit by accident, we have just arrived at an autoencoding setup. To learn our model, we need&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(p_\theta ( \mathbf{x}, \mathbf{z})\) - the generative model, which consists of
    &lt;ul&gt;
      &lt;li&gt;\(p_\theta ( \mathbf{x} \mid \mathbf{z})\) - a probabilistic decoder, and&lt;/li&gt;
      &lt;li&gt;\(p ( \mathbf{z})\)                        - a prior over the latent variables,&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;\(q_\phi ( \mathbf{z} \mid \mathbf{x})\)   - a probabilistic encoder.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To approximate the posterior, we can use the &lt;a href=&quot;https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence&quot;&gt;KL-divergence&lt;/a&gt; (think of it as a distance between probability distributions) between the proposal and the posterior itself; and we can minimise it.&lt;/p&gt;

\[KL \left( q_\phi (\mathbf{z} \mid \mathbf{x}) || p_\theta(\mathbf{z} \mid \mathbf{x}) \right) = \mathbb{E}_{q_\phi (\mathbf{z} \mid \mathbf{x})} \left[ \log \frac{q_\phi (\mathbf{z} \mid \mathbf{x})}{p_\theta(\mathbf{z} \mid \mathbf{x})} \right] \tag{4}\]

&lt;p&gt;Our new problem is, of course, that to evaluate the &lt;em&gt;KL&lt;/em&gt; we need to know the posterior distribution.
Not all is lost, for doing a little algebra can give us an objective function that is possible to compute.&lt;/p&gt;

\[\begin{align}
  KL &amp;amp;\left( q_\phi (\mathbf{z} \mid \mathbf{x}) || p_\theta(\mathbf{z} \mid \mathbf{x}) \right)\\
  &amp;amp;=\mathbb{E}_{q_\phi (\mathbf{z} \mid \mathbf{x})} \left[ \log q_\phi (\mathbf{z} \mid \mathbf{x}) - \log p_\theta(\mathbf{z} \mid \mathbf{x}) \right]\\
  &amp;amp;=\mathbb{E}_{q_\phi (\mathbf{z} \mid \mathbf{x})} \left[ \log q_\phi (\mathbf{z} \mid \mathbf{x}) - \log p_\theta(\mathbf{z}, \mathbf{x}) \right] + \log p_\theta(\mathbf{x})\\
  &amp;amp;= -\mathcal{L} (\mathbf{x}; \theta, \phi) + \log p_\theta(\mathbf{x})
  \tag{5}
\end{align}\]

&lt;p&gt;Where on the second line I expanded the logarithm, on the third line I used the Bayes’ theorem and the fact that \(p_\theta (\mathbf{x})\) is independent of \(\mathbf{z}\). \(\mathcal{L} (\mathbf{x}; \theta, \phi)\) in the last line is a lower bound on the log probability of data \(p_\theta (\mathbf{x})\) - the so-called evidence-lower bound (&lt;em&gt;ELBO&lt;/em&gt;). We can rewrite it as&lt;/p&gt;

\[\log p_\theta(\mathbf{x}) = \mathcal{L} (\mathbf{x}; \theta, \phi) + KL \left( q_\phi (\mathbf{z} \mid \mathbf{x}) || p_\theta(\mathbf{z} \mid \mathbf{x}) \right), \tag{6}\]

\[\mathcal{L} (\mathbf{x}; \theta, \phi) = \mathbb{E}_{q_\phi (\mathbf{z} \mid \mathbf{x})}
    \left[
      \log \frac{
        p_\theta (\mathbf{x}, \mathbf{z})
      }{
        q_\phi (\mathbf{z} \mid \mathbf{x})
      }
    \right]. \tag{7}\]

&lt;p&gt;We can approximate it using a single sample from the proposal distribution as&lt;/p&gt;

\[\mathcal{L} (\mathbf{x}; \theta, \phi) \approx  \log \frac{
      p_\theta (\mathbf{x}, \mathbf{z})
    }{
      q_\phi (\mathbf{z} \mid \mathbf{x})
    }, \qquad \mathbf{z} \sim q_\phi (\mathbf{z} \mid \mathbf{x}). \tag{8}\]

&lt;p&gt;We train the model by finding \(\phi\) and \(\theta\) (usually by stochastic gradient descent) that maximise the &lt;em&gt;ELBO&lt;/em&gt;:&lt;/p&gt;

\[\phi^\star,~\theta^\star = \arg \max_{\phi \in \Phi,~\theta \in \Theta}
  \mathcal{L} (\mathbf{x}; \theta, \phi). \tag{9}\]

&lt;p&gt;By maximising the &lt;em&gt;ELBO&lt;/em&gt;, we (1) maximise the marginal probability or (2) minimise the KL-divergence, or both.
It is worth noting that the approximation of &lt;em&gt;ELBO&lt;/em&gt; has the form of the log of importance-sampled expectation of \(f(\mathbf{x}) = 1\), with importance weights \(w(\mathbf{x}) = \frac{ p_\theta (\mathbf{x}, \mathbf{z}) }{ q_\phi (\mathbf{z} \mid \mathbf{x})}\).&lt;/p&gt;

&lt;h1 id=&quot;what-is-wrong-with-this-estimate&quot;&gt;What is wrong with this estimate?&lt;/h1&gt;
&lt;p&gt;If you look long enough at importance sampling, it becomes apparent that the support of the proposal distribution should be wider than that of the nominal pdf - both to avoid infinite variance of the estimator and numerical instabilities.
In this case, it would be better to optimise the reverse \(KL(p \mid\mid q)\), which has mode-averaging behaviour, as opposed to  \(KL(q \mid\mid p)\), which tries to match the mode of \(q\) to one of the modes of \(p\).
This would typically require taking samples from the true posterior, which is hard.
Instead, we can use IS estimate of the &lt;em&gt;ELBO&lt;/em&gt;, introduced as &lt;a href=&quot;https://arxiv.org/abs/1509.00519&quot;&gt;Importance Weighted Autoencoder&lt;/a&gt; (&lt;em&gt;IWAE&lt;/em&gt;). The idea is simple: we take \(K\) samples from the proposal and we use an average of probability ratios evaluated at those samples. We call each of the samples a &lt;em&gt;particle&lt;/em&gt;.&lt;/p&gt;

\[\mathcal{L}_K (\mathbf{x}; \theta, \phi) \approx
    \log \frac{1}{K} \sum_{k=1}^{K}
      \frac{
        p_\theta (\mathbf{x},~\mathbf{z^{(k)}})
      }{
        q_\phi (\mathbf{z^{(k)}} \mid \mathbf{x})
      },
      \qquad \mathbf{z}^{(k)} \sim q_\phi (\mathbf{z} \mid \mathbf{x}). \tag{10}\]

&lt;p&gt;This estimator &lt;a href=&quot;https://arxiv.org/abs/1705.10306&quot;&gt;has been shown&lt;/a&gt; to optimise the modified KL-divergence \(KL(q^{IS} \mid \mid p^{IS})\), with \(q^{IS}\) and \(p^{IS}\) defined as
\(q^{IS} = q^{IS}_\phi (\mathbf{z} \mid \mathbf{x}) = \frac{1}{K} \prod_{k=1}^K q_\phi ( \mathbf{z}^{(k)} \mid \mathbf{x} ), \tag{11}\)&lt;/p&gt;

\[p^{IS} = p^{IS}_\theta (\mathbf{z} \mid \mathbf{x}) = \frac{1}{K} \sum_{k=1}^K
  \frac{
      q^{IS}_\phi (\mathbf{z} \mid \mathbf{x})
    }{
      q_\phi (\mathbf{z^{(k)}} \mid \mathbf{x})
    }
    p_\theta (\mathbf{z}^{(k)} \mid \mathbf{x}).
  \tag{12}\]

&lt;p&gt;While similar to the original distributions, \(q^{IS}\) and \(p^{IS}\) allow small variations in \(q\) and \(p\) that we would not have expected.
Optimising this lower bound leads to better generative models, as shown in the original paper.
It also leads to higher-entropy (wider, more scattered) estimates of the approximate posterior \(q\), effectively breaking the mode-matching behaviour of the original KL-divergence.
As a curious consequence, if we increase the number of particles \(K\) to infinity, we no longer need the inference model \(q\).&lt;/p&gt;

&lt;figure&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/iwae_vs_vae.png&quot; alt=&quot;IWAE vs VAE&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;
    Posterior distribution of &lt;b&gt;z&lt;/b&gt; for the IWAE (top row) and VAE (bottom row). Figure reproduced from the &lt;a href=&quot;https://arxiv.org/abs/1509.00519&quot;&gt;IWAE paper&lt;/a&gt;.
  &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h1 id=&quot;what-is-wrong-with-iwae&quot;&gt;What is wrong with IWAE?&lt;/h1&gt;
&lt;p&gt;The importance-weighted &lt;em&gt;ELBO&lt;/em&gt;, or the &lt;em&gt;IWAE&lt;/em&gt;, generalises the original &lt;em&gt;ELBO&lt;/em&gt;: for \(K=1\), we have \(\mathcal{L}_K = \mathcal{L}_1 = \mathcal{L}\).
It is also true that \(\log p(\mathbf{x}) \geq \mathcal{L}_{n+1} \geq \mathcal{L}_n \geq \mathcal{L}_1\).
In other words, the more particles we use to estimate \(\mathcal{L}_K\), the closer it gets in value to the true log probability of data - we say that the bound becomes tighter.
This means that the gradient estimator, derived by differentiating the &lt;em&gt;IWAE&lt;/em&gt;, points us in a better direction than the gradient of the original &lt;em&gt;ELBO&lt;/em&gt; would.
Additionally, as we increase \(K\), the variance of that gradient estimator shrinks.&lt;/p&gt;

&lt;p&gt;It is great for the generative model, but, as we shown in our recent paper &lt;a href=&quot;https://arxiv.org/abs/1802.04537&quot;&gt;&lt;em&gt;Tighter Variational Bounds are Not Necessarily Better&lt;/em&gt;&lt;/a&gt;, it turns out to be problematic for the proposal.
The magnitude of the gradient with respect to proposal parameters goes to zero with increasing \(K\), and it does so much faster than its variance.&lt;/p&gt;

&lt;p&gt;Let \(\Delta (\phi)\) be a minibatch estimate of the gradient of an objective function we’re optimising (&lt;em&gt;e.g.&lt;/em&gt; &lt;em&gt;ELBO&lt;/em&gt;) with respect to \(\phi\). If we define signal-to-noise ratio (SNR) of the parameter update as&lt;/p&gt;

\[SNR(\phi) = \frac{
      \left| \mathbb{E} \left[ \Delta (\phi ) \right] \right|
    }{
      \mathbb{V} \left[ \Delta (\phi ) \right]^{\frac{1}{2}}
      }, \tag{13}\]

&lt;p&gt;where \(\mathbb{E}\) and \(\mathbb{V}\) are expectation and variance, respectively, it turns out that SNR increases with \(K\) for \(p_\theta\), but it decreases for \(q_\phi\).
The conclusion here is simple: the more particles we use, the worse the inference model becomes.
If we care about representation learning, we have a problem.&lt;/p&gt;

&lt;h1 id=&quot;better-estimators&quot;&gt;Better estimators&lt;/h1&gt;
&lt;p&gt;We can do better than the IWAE, as we’ve shown in &lt;a href=&quot;https://arxiv.org/abs/1802.04537&quot;&gt;our paper&lt;/a&gt;.
The idea is to use separate objectives for the inference and the generative models.
By doing so, we can ensure that both get non-zero low-variance gradients, which lead to better models.&lt;/p&gt;

&lt;figure&gt;
  &lt;img style=&quot;display: box; margin: auto&quot; src=&quot;http://akosiorek.github.io/resources/snr_encoder.png&quot; alt=&quot;Signal-to-Noise ratio for the encoder across training epochs&quot; /&gt;
  &lt;figcaption align=&quot;center&quot;&gt;Signal-to-Noise ratio for the proposal across training epochs for different training objectives.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the above plot, we compare &lt;em&gt;SNR&lt;/em&gt; of the updates of parameters \(\phi\) of the proposal \(q_\phi\) acorss training epochs. &lt;em&gt;VAE&lt;/em&gt;, which shows the highest &lt;em&gt;SNR&lt;/em&gt;, is trained by optimising \(\mathcal{L}_1\). &lt;em&gt;IWAE&lt;/em&gt;, trained with \(\mathcal{L}_{64}\), has the lowest &lt;em&gt;SNR&lt;/em&gt;. The three curves in between use different combinations of \(\mathcal{L}_{64}\) for the generative model and \(\mathcal{L}_8\) or \(\mathcal{L}_1\) for the inference model. While not as good as the &lt;em&gt;VAE&lt;/em&gt; under this metric, they all lead to training better proposals and generative models than either &lt;em&gt;VAE&lt;/em&gt; or &lt;em&gt;IWAE&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;As a, perhaps surprising, side effect, models trained with our new estimators achieve higher \(\mathcal{L}_{64}\) bounds than the IWAE itself trained with this objective.
Why?
By looking at the &lt;a href=&quot;https://en.wikipedia.org/wiki/Effective_sample_size&quot;&gt;effective sample-size (ESS)&lt;/a&gt; and the marginal log probability of data, it looks like optimising \(\mathcal{L}_1\) leads to producing the best quality proposals, but the worst generative models.
If we combine a good proposal with an objective that leads to good generative models, we should be able to provide lower-variance estimate of this objective and thus learn even better models.
Please see &lt;a href=&quot;https://arxiv.org/abs/1802.04537&quot;&gt;our paper&lt;/a&gt; for details.&lt;/p&gt;

&lt;h1 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;More flexible proposals: Normalizing Flows tutorial by Eric Jang &lt;a href=&quot;https://blog.evjang.com/2018/01/nf1.html&quot;&gt;part 1&lt;/a&gt; and &lt;a href=&quot;https://blog.evjang.com/2018/01/nf2.html&quot;&gt;part 2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;More flexible likelihood function: A post on &lt;a href=&quot;http://sergeiturukin.com/2017/02/22/pixelcnn.html&quot;&gt;Pixel CNN by Sergei Turukin&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Extension of IWAE to sequences: &lt;a href=&quot;https://arxiv.org/abs/1705.09279&quot;&gt;Chris Maddison &lt;em&gt;et. al.&lt;/em&gt;, “FIVO”&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/1705.10306&quot;&gt;Tuan Anh Le &lt;em&gt;et. al.&lt;/em&gt;, “AESMC”&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h4&gt;
&lt;p&gt;I would like to thank &lt;a href=&quot;http://www.robots.ox.ac.uk/~neild/&quot;&gt;Neil Dhir&lt;/a&gt; and &lt;a href=&quot;http://troynikov.io/&quot;&gt;Anton Troynikov&lt;/a&gt; for proofreading this post and suggestions on how to make it better.&lt;/p&gt;
</description>
        <pubDate>Wed, 14 Mar 2018 15:15:00 +0000</pubDate>
        <link>http://akosiorek.github.io/what_is_wrong_with_vaes/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/what_is_wrong_with_vaes/</guid>
        
        
        <category>ML</category>
        
      </item>
    
      <item>
        <title>Attention in Neural Networks and How to Use It</title>
        <description>&lt;p&gt;Attention mechanisms in neural networks, otherwise known as &lt;em&gt;neural attention&lt;/em&gt; or just &lt;em&gt;attention&lt;/em&gt;, have recently attracted a lot of attention (pun intended). In this post, I will try to find a common denominator for different mechanisms and use-cases and I will describe (and implement!) two mechanisms of soft visual attention.&lt;/p&gt;

&lt;h1 id=&quot;what-is-attention&quot;&gt;What is Attention?&lt;/h1&gt;
&lt;p&gt;Informally, a neural attention mechanism equips a neural network with the ability to focus on a subset of its inputs (or features): it selects specific inputs. Let \(\mathbf{x} \in \mathcal{R}^d\) be an input vector, \(\mathbf{z} \in \mathcal{R}^k\) a feature vector, \(\mathbf{a} \in [0, 1]^k\) an attention vector, \(\mathbf{g} \in \mathcal{R}^k\) an attention glimpse and \(f_\mathbb{\phi}(\mathbf{x})\) an attention network with parameters \(\mathbb{\phi}\). Typically, attention is implemented as&lt;/p&gt;

\[\begin{align}
  \mathbf{a} &amp;amp;= f_\phi(\mathbf{x}), \tag{1} \label{att}\\
  \mathbf{g} &amp;amp;= \mathbf{a} \odot \mathbf{z},
  \end{align}\]

&lt;p&gt;where \(\odot\) is element-wise multiplication, while \(\mathbf{z}\) is an output of another neural network \(f_\mathbf{\theta} (\mathbf{x})\) with parameters \(\mathbf{\theta}\).
We can talk about &lt;em&gt;soft attention&lt;/em&gt;, which multiplies features with a (soft) mask of values between zero and one, or &lt;em&gt;hard attention&lt;/em&gt;, when those values are constrained to be exactly zero or one, namely \(\mathbf{a} \in \{0, 1\}^k\). In the latter case, we can use the hard attention mask to directly index the feature vector: \(\tilde{\mathbf{g}} = \mathbf{z}[\mathbf{a}]\) (in Matlab notation), which changes its dimensionality and now \(\tilde{\mathbf{g}} \in \mathcal{R}^m\) with \(m \leq k\).&lt;/p&gt;

&lt;p&gt;To understand why attention is important, we have to think about what a neural network really is: a function approximator. Its ability to approximate different classes of functions depends on its architecture. A typical neural net is implemented as a chain of matrix multiplications and element-wise non-linearities, where elements of the input or feature vectors interact with each other only by addition.&lt;/p&gt;

&lt;p&gt;Attention mechanisms compute a mask which is used to multiply features. This seemingly innocent extension has profound implications: suddenly, the space of functions that can be well approximated by a neural net is vastly expanded, making entirely new use-cases possible. Why? While I have no proof, the intuition is following: the theory says that &lt;a href=&quot;http://www.sciencedirect.com/science/article/pii/0893608089900208&quot;&gt;neural networks are universal function approximators and can approximate an arbitrary function to arbitrary precision, but only in the limit of an infinite number of hidden units&lt;/a&gt;. In any practical setting, that is not the case: we are limited by the number of hidden units we can use. Consider the following example: we would like to approximate the product of \(N &amp;gt;&amp;gt; 0\) inputs. A feed-forward neural network can do it only by simulating multiplications with (many) additions (plus non-linearities), and thus it requires a lot of neural-network real estate. If we introduce multiplicative interactions, it becomes simple and compact.&lt;/p&gt;

&lt;p&gt;The above definition of attention as multiplicative interactions allow us to consider a broader class of models if we relax the constrains on the values of the attention mask and let \(\mathbf{a} \in \mathcal{R}^k\). For example,  &lt;a href=&quot;https://arxiv.org/abs/1605.09673&quot;&gt;Dynamic Filter Networks (DFN)&lt;/a&gt; use a filter-generating network, which computes filters (or weights of arbitrary magnitudes) based on inputs, and applies them to features, which effectively is a multiplicative interaction. The only difference with soft-attention mechanisms is that the attention weights are not constrained to lie between zero and one. Going further in that direction, it would be very interesting to learn which interactions should be additive and which multiplicative, a concept explored in &lt;a href=&quot;https://arxiv.org/abs/1604.03736&quot;&gt;A Differentiable Transition Between Additive and Multiplicative Neurons&lt;/a&gt;. The excellent &lt;a href=&quot;https://distill.pub/2016/augmented-rnns/&quot;&gt;distill blog&lt;/a&gt; provides a great overview of soft-attention mechanisms.&lt;/p&gt;

&lt;h1 id=&quot;visual-attention&quot;&gt;Visual Attention&lt;/h1&gt;
&lt;p&gt;Attention can be applied to any kind of inputs, regardless of their shape. In the case of matrix-valued inputs, such as images, we can talk about &lt;em&gt;visual attention&lt;/em&gt;. Let \(\mathbf{I} \in \mathcal{R}^{H \times W}\) be an image and \(\mathbf{g} \in \mathcal{R}^{h \times w}\) an attention glimpse &lt;em&gt;i.e.&lt;/em&gt; the result of applying an attention mechanism to the image \(\mathbf{I}\).&lt;/p&gt;

&lt;h3 id=&quot;hard-attention&quot;&gt;Hard Attention&lt;/h3&gt;
&lt;p&gt;Hard attention for images has been known for a very long time: image cropping. It is very easy conceptually, as it only requires indexing. Let \(y \in [0, H - h]\) and \(x \in [0, W - w]\) be coordinates in the image space; hard-attention can be implemented in Python (or Tensorflow) as&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The only problem with the above is that it is non-differentiable; to learn the parameters of the model, one must resort to &lt;em&gt;e.g.&lt;/em&gt; the score-function estimator (REINFORCE), briefly mentioned in my &lt;a href=&quot;http://akosiorek.github.io/ml/2017/09/03/implementing-air.html#estimating-gradients-for-discrete-variables&quot;&gt;previous post&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;soft-attention&quot;&gt;Soft Attention&lt;/h3&gt;
&lt;p&gt;Soft attention, in its simplest variant, is no different for images than for vector-valued features and is implemented exactly as in equation \ref{att}. One of the early uses of this types of attention comes from the paper called &lt;a href=&quot;https://arxiv.org/abs/1502.03044&quot;&gt;Show, Attend and Tell&lt;/a&gt;: &lt;img src=&quot;https://distill.pub/2016/augmented-rnns/assets/show-attend-tell.png&quot; alt=&quot;aa&quot; /&gt;
The model learns to &lt;em&gt;attend&lt;/em&gt; to specific parts of the image while generating the word describing that part.&lt;/p&gt;

&lt;p&gt;This type of soft attention is computationally wasteful, however. The blacked-out parts of the input do not contribute to the results but still need to be processed. It is also over-parametrised: sigmoid activations that implement the attention are independent of each other. It can select multiple objects at once, but in practice we often want to be selective and focus only on a single element of the scene. The two following mechanisms, introduced by &lt;a href=&quot;https://arxiv.org/abs/1502.04623&quot;&gt;DRAW&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/1506.02025&quot;&gt;Spatial Transformer Networks&lt;/a&gt;, respectively, solve this issue. They can also resize the input, leading to further potential gains in performance.&lt;/p&gt;

&lt;h3 id=&quot;gaussian-attention&quot;&gt;Gaussian Attention&lt;/h3&gt;
&lt;p&gt;Gaussian attention works by exploiting parametrised one-dimensional Gaussian filters to create an image-sized attention map. Let \(\mathbf{a}_y \in \mathcal{R}^H\) and \(\mathbf{a}_x \in \mathcal{R}^W\) be attention vectors, which specify which part of the image should be attended to in \(y\) and \(x\) axis, respectively. The attention masks can be created as \(\mathbf{a} = \mathbf{a}_y \mathbf{a}_x^T\).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;hard_gauss.jpeg&quot; alt=&quot;hard_gauss&quot; style=&quot;max-width: 400px; display: block; margin: auto;&quot; /&gt;
In the above figure, the top row shows \(\mathbf{a}_x\), the column on the right shows \(\mathbf{a}_y\) and the middle rectangle shows the resulting \(\mathbf{a}\). Here, for the visualisation purposes, the vectors contain only zeros and ones. In practice, they can be implemented as vectors of one-dimensional Gaussians. Typically, the number of Gaussians is equal to the spatial dimension and each vector is parametrised by three parameters: centre of the first Gaussian \(\mu\), distance between centres of consecutive Gaussians \(d\) and the standard deviation of the Gaussians \(\sigma\). With this parametrisation, both attention and the glimpse are differentiable with respect to attention parameters, and thus easily learnable.&lt;/p&gt;

&lt;p&gt;Attention in the above form is still wasteful, as it selects only a part of the image while blacking-out all the remaining parts. Instead of using the vectors directly, we can cast them into matrices \(A_y \in \mathcal{R}^{h \times H}\) and \(A_x \in \mathcal{R}^{w \times W}\), respectively. Now, each matrix has one Gaussian per row and the parameter \(d\) specifies distance (in column units) between centres of Gaussians in consecutive rows. The glimpse is now implemented as&lt;/p&gt;

\[\mathbf{g} = A_y \mathbf{I} A_x^T.\]

&lt;p&gt;I used this mechanism in &lt;a href=&quot;https://arxiv.org/abs/1706.09262&quot;&gt;HART, my recent paper on biologically-inspired object tracking with RNNs with attention&lt;/a&gt;. Here is an example with the input image on the left hand side and the attention glimpse on the right hand side; the glimpse shows the box marked in the main image in green:&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
  &lt;img src=&quot;full_fig.png&quot; style=&quot;width: 500px&quot; /&gt;
  &lt;img src=&quot;att_fig.png&quot; style=&quot;width: 125px&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The code below lets you create one of the above matrix-valued masks for a mini-batch of samples in Tensorflow. If you want to create \(A_y\), you would call it as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ay = gaussian_mask(u, s, d, h, H)&lt;/code&gt;, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u, s, d&lt;/code&gt; are \(\mu, \sigma\) and \(d\), in that order and specified in pixels.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gaussian_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    :param u: tf.Tensor, centre of the first Gaussian.
    :param s: tf.Tensor, standard deviation of Gaussians.
    :param d: tf.Tensor, shift between Gaussian centres.
    :param R: int, number of rows in the mask, there is one Gaussian per row.
    :param C: int, number of columns in the mask.
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# indices to create centres
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;centres&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newaxis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newaxis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;column_centres&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;centres&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column_centres&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# we add eps for numerical stability
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;normalised_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_dims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;normalised_mask&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can also write a function to directly extract a glimpse from the image:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gaussian_glimpse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transform_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crop_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    :param img_tensor: tf.Tensor of size (batch_size, Height, Width, channels)
    :param transform_params: tf.Tensor of size (batch_size, 6), where params are  (mean_y, std_y, d_y, mean_x, std_x, d_x) specified in pixels.
    :param crop_size): tuple of 2 ints, size of the resulting crop
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# parse arguments
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crop_size&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;W&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;split_ax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transform_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;uy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ux&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;split_ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# create Gaussian masks, one for each axis
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;Ay&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gaussian_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Ax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gaussian_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ux&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;W&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# extract glimpse
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;glimpse&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Ay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adjoint_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glimpse&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;spatial-transformer&quot;&gt;Spatial Transformer&lt;/h3&gt;
&lt;p&gt;Spatial Transformer (STN) allows for much more general transformation that just differentiable image-cropping, but image cropping is one of the possible use cases. It is made of two components: a grid generator and a sampler. The grid generator specifies a grid of points to be sampled from, while the sampler, well, samples. The Tensorflow implementation is particularly easy in &lt;a href=&quot;https://github.com/deepmind/sonnet&quot;&gt;Sonnet&lt;/a&gt;, a recent neural network library from &lt;a href=&quot;https://deepmind.com/&quot;&gt;DeepMind&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;spatial_transformer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transform_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crop_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    :param img_tensor: tf.Tensor of size (batch_size, Height, Width, channels)
    :param transform_params: tf.Tensor of size (batch_size, 4), where params are  (scale_y, shift_y, scale_x, shift_x)
    :param crop_size): tuple of 2 ints, size of the resulting crop
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;constraints&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AffineWarpConstraints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;no_shear_2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;img_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;warper&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AffineGridWarper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crop_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constraints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;grid_coords&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;glimpse&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resampler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[...,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newaxis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grid_coords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glimpse&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;gaussian-attention-vs-spatial-transformer&quot;&gt;Gaussian Attention vs. Spatial Transformer&lt;/h3&gt;
&lt;p&gt;Both Gaussian attention and Spatial Transformer can implement a very similar behaviour. How do we choose which to use? There are several nuances:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Gaussian attention is an over-parametrised cropping mechanism: it requires six parameters, but there are only four degrees of freedom (y, x, height width). STN needs only four parameters.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;I haven’t run any tests yet, but STN &lt;em&gt;should be&lt;/em&gt; faster. It relies on linear interpolation at sampling points, while the Gaussian attention has to perform two huge matrix multiplications. STN &lt;em&gt;could be&lt;/em&gt; an order of magnitude faster (in terms of pixels in the input image).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Gaussian attention &lt;em&gt;should be&lt;/em&gt; (no tests run) easier to train. This is because every pixel in the resulting glimpse can be a convex combination of a relatively big patch of pixels of the source image, which (informally) makes it easier to find the cause of any errors. STN, on the other hand, relies on linear interpolation, which means that gradient at every sampling point is non-zero only with respect to the two nearest pixels in each axis.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;a-minimum-working-example&quot;&gt;A Minimum Working Example&lt;/h3&gt;
&lt;p&gt;Let’s create a minimum working example of Gaussian Attention and STN. First, we need to import a few libraries, define sizes and create an input image and a crop.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sonnet&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;img_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;glimpse_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Create a random image with a square
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;crop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# contains the square
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, we need placeholders for Tensorflow variables.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset_default_graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# placeholders
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;image&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;u&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;s&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;d&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stn_params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;stn_params&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can now define the Tensorflow expression for Gaussian Attention and STN glimpses.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Gaussian Attention
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gaussian_att_params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;gaussian_glimpse_expr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gaussian_glimpse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gaussian_att_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glimpse_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Spatial Transformer
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stn_glimpse_expr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spatial_transformer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stn_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;glimpse_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s run those expressions and plot them:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# extract a Gaussian glimpse
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;gaussian_crop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gaussian_glimpse_expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# extract STN glimpse
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stn_crop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stn_glimpse_expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stn_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;# plots
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;figsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;titles&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Input Image&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Crop&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;Gaussian Att&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;STN&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;imgs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gaussian_crop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stn_crop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imshow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squeeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;gray&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vmin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vmax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xaxis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_visible&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yaxis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_visible&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;attention_example.png&quot; alt=&quot;Attention Examples&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can find a Jupyter Notebook with the code used to create the above &lt;a href=&quot;https://github.com/akosiorek/akosiorek.github.io/tree/master/notebooks/attention_glimpse.ipynb&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;closing-thoughts&quot;&gt;Closing Thoughts&lt;/h1&gt;
&lt;p&gt;Attention mechanisms expand capabilities of neural networks: they allow approximating more complicated functions, or in more intuitive terms, they enable focusing on specific parts of the input. They have led to performance improvements in natural language benchmarks, as well as to entirely new capabilities such as image captioning, addressing in memory networks and neural programmers.&lt;/p&gt;

&lt;p&gt;I believe that the most important cases in which attention is useful have not been discovered yet. For example, we know that objects in videos are consistent and coherent, &lt;em&gt;e.g.&lt;/em&gt; they do not disappear into thin air between frames. Attention mechanisms can be used to express this consistency prior. How? Stay tuned.&lt;/p&gt;
</description>
        <pubDate>Sat, 14 Oct 2017 11:00:00 +0000</pubDate>
        <link>http://akosiorek.github.io/visual-attention/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/visual-attention/</guid>
        
        
        <category>ML</category>
        
      </item>
    
      <item>
        <title>Conditional KL-divergence in Hierarchical VAEs</title>
        <description>&lt;p&gt;Inference is hard and often computationally expensive. Variational Autoencoders (VAE) lead to an efficient amortised inference scheme, where amortised means that once the model is trained (which can take a long time), the inference has constant computational complexity.
Variational Autoencoders (VAE) learn the approximate posterior distribution \(q(z\mid x)\) over some latent variables \(z\) by maximising a lower bound on the true data likelihood \(p(x)\). This is useful, because the latent variables explain what we see (\(x\)), and often in a concise form.&lt;/p&gt;

&lt;p&gt;One problem with VAEs is that we have to assume some functional form for \(q\).
While the majority of papers take the Gaussian distribution with a diagonal covariance matrix, it has been shown that more complex (&lt;em&gt;e.g.&lt;/em&gt; multi-modal) approximate posterior distributions can improve the quality of the model, with a good example being &lt;a href=&quot;https://arxiv.org/abs/1505.05770&quot;&gt;the normalizing flows paper&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Normalizing flows take a simple probability distribution (here: a Gaussian) and apply a series of invertible transformations to get a more complicated distribution.
While useful, the resulting distribution is limited by the form of the transforming functions, which in this case have to be invertible.
Another way of achieving the same goal is to split the latent variables into two groups \(z = \{u, v\}\), say, and express the joint distribution as \(q(z) = q(u, v) = q(u \mid v) q(v)\) by using the product rule of probability. The conditional distribution \(q(u \mid v)\) can depend on \(v\) in a highly non-linear fashion (it can be implemented as a neural net). Even though both the marginal \(q(v)\) and the conditional \(q(u \mid v)\) can be Gaussians, their joint might be highly non-Gaussian. Consider the below example and the resulting density plot (in the plot \(x=v\) and \(y=u\)).&lt;/p&gt;

\[\begin{align}
q(v) &amp;amp;= \mathcal{N} (v \mid 0, I)\\
q(u \mid v) &amp;amp;= \mathcal{N} (u \mid Fv, Fvv^TF^T + \beta I),
\tag{1}
\label{hierarchical}
\end{align}\]

&lt;p&gt;&lt;img src=&quot;true_distrib.png&quot; style=&quot;width: 500px; display: block; margin: auto;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The above density plot shows a highly non-Gaussian probability distribution. \(x \sim q(v)\) is, in fact, a Gaussian random variable, but \(y \sim q(u \mid v)\) is not, since its variance is not constant and depends on its mean: the variance increases with the increasing distance from the mean, resulting in heavy tails.
In the above plot, \(q(u \mid v)\) is obtained as a simple transformation of \(v \sim q(v)\), which could be implemented by a one-layer neural net; see &lt;a href=&quot;https://arxiv.org/abs/1509.00519&quot;&gt;Importance Weighted Autoencoders&lt;/a&gt; for a more general example.
This simple scheme results in a VAE with a hierarchy of latent variables and can lead to much more complicated posterior distributions, but it also leads to a more complicated (and ambiguous) optimisation procedures. Let me elaborate.&lt;/p&gt;

&lt;p&gt;As part of the variational objective, the learning process is optimising the
Kullback-Leibler divergence \(KL[q \mid p]\) between the approximate posterior \(q\) and a prior over the latent variables \(p\). KL is an asymmetric measure of similarity between two probability distributions \(q\) and \(p\) that is often used in machine learning. It can be interpreted as the information gain from using \(q\) instead of \(p\), or in the context of coding theory, the extra number of bits to code samples from \(q\) by using \(p\). You can read more about information measures in this &lt;a href=&quot;http://threeplusone.com/on_information.pdf&quot;&gt;cheat sheet&lt;/a&gt;. It is defined as&lt;/p&gt;

\[KL[q(z) \mid \mid p(z)] = \int q(z) \log \frac{q(z)}{p(z)} \mathrm{d}z.
   \tag{2}
   \label{kl_def}\]

&lt;p&gt;If we split the random variable \(z\) into two disjoint sets \(z = \{u, v\}\) as  above, the KL factorises as&lt;/p&gt;

\[\begin{align}
   KL[q(u, v) \mid \mid p(u, v)] &amp;amp;= \iint q(u, v) \log \frac{q(u, v)}{p(u, v)} \mathrm{d}u \mathrm{d}v\\
   % sum of integrals
   &amp;amp;= \int q(v) \log \frac{q(v)}{p(v)} \mathrm{d}v
   + \int q(v) \int q(u \mid v) \log \frac{q(u \mid v)}{p(u \mid v))} \mathrm{d}u \mathrm{d}v \tag{3}\\
   % sum of KLs
   &amp;amp;= KL[q(v) \mid \mid p(v)] + KL[q(u \mid v) \mid \mid p(u \mid v)],
   \label{conditional_kl}
   \end{align}\]

&lt;p&gt;where \(KL[q(u \mid v) \mid \mid p(u \mid v)] = \mathbb{E}_{q(v)} \left[ \tilde{KL}[q(u \mid v) \mid \mid p(u \mid v)] \right]\) is known as the conditional KL-divergence, with&lt;/p&gt;

\[\tilde{KL}[q(u \mid v) \mid \mid p(u \mid v) = \int q(u \mid v) \log \frac{q(u \mid v)}{p(u \mid v))} \mathrm{d}u \tag{4}.\]

&lt;p&gt;The conditional KL-divergence amounts to the expected value of the KL-divergence between conditional distributions \(q(u \mid v)\) and \(p(u \mid v)\), where the expectation is taken with respect to \(q(v)\).
Since KL-divergence is non-negative, both terms are non-negative.
KL is equal to zero only when both probability distributions are exactly equal.
The conditional KL is equal to zero when both conditional distributions are exactly equal on the whole support defined by \(q(v)\).
This last bit makes it difficult to optimise with respect to the parameters of both distributions.&lt;/p&gt;

&lt;p&gt;Let \(q(z) = q_\psi(u, v) = q_\phi (u \mid v) q_\theta(v)\), such that the posterior is parametrised by \(\psi = \begin{bmatrix} \phi\\ \theta\end{bmatrix}\). If we look at the gradient of the KL divergence, we have that&lt;/p&gt;

\[\begin{align}
\nabla_\psi &amp;amp;KL[q_\psi(u, v) \mid \mid p(u, v)] = \begin{bmatrix} \nabla_\phi KL[q_\psi(u, v) \mid \mid p(u, v)] \\ \nabla_\theta KL[q_\psi(u, v) \mid \mid p(u, v)] \end{bmatrix}
\tag{5},
\end{align}\]

&lt;p&gt;with&lt;/p&gt;

\[\nabla_\phi KL[q_\psi(u, v) \mid \mid p(u, v)] = \nabla_\phi KL[q_\phi(u \mid v) \mid \mid p(u \mid v)]
\tag{6},\]

\[\nabla_\theta KL[q_\psi(u, v) \mid \mid p(u, v)] = \nabla_\theta KL[q_\theta(v) \mid \mid p(v)] + \nabla_\theta KL[q_\phi(u \mid v) \mid \mid p(u \mid v)],
\tag{7}\]

&lt;p&gt;where the gradient with respect to the parameters of the lower-level distribution \(q_\theta(v)\) is comprised of two components. The second component is problematic. Let’s have a closer look:&lt;/p&gt;

\[\begin{align}
\nabla_\theta &amp;amp;KL[q_\phi(u \mid v) \mid \mid p(u \mid v)] = \nabla_\theta \mathbb{E}_{q_\theta(v)} \left[
\tilde{KL}[q_\phi (u \mid v) \mid \mid p(u \mid v) \right]
\tag{8}\\
&amp;amp;= \mathbb{E}_{q_\theta(v)} \left[
\tilde{KL}[q_\phi (u \mid v) \mid \mid p(u \mid v)] \nabla_\theta \log q_\theta(v) \right],
\end{align}\]

&lt;p&gt;where in the second line we used the &lt;a href=&quot;http://blog.shakirm.com/2015/11/machine-learning-trick-of-the-day-5-log-derivative-trick/&quot;&gt;log-derivative trick&lt;/a&gt; (suggested here by &lt;a href=&quot;https://scholar.google.com/citations?user=MtTyY5IAAAAJ&amp;amp;hl=en&quot;&gt;Max Soelch&lt;/a&gt;, thanks!). This score-function formulation makes it clear that following the (negative, as in SGD) gradient estimate maximises the probability of samples for which the conditional-KL divergence has the lowest values. In particular, it might be easier to change the support \(q_\theta(v)\) to a volume where both conditionals have very small values instead of optimising \(q_\phi(u \mid v)\). From my experience, it happens especially when the value of the conditional KL is much bigger than the value of the first KL term.&lt;/p&gt;

&lt;p&gt;An alternative approach would be to optimise the conditional-KL only with respect to the parameters of the distribution inside the expectation: \(\phi\). That would result in the following gradient equation:&lt;/p&gt;

\[\nabla_\psi KL[q_\psi(u, v) \mid \mid p(u, v)]
=
\begin{bmatrix} 0\\ \nabla_\theta KL[q_\theta(v) \mid \mid p(v)] \end{bmatrix}
+
\begin{bmatrix} \nabla_\phi KL[q_\phi(u \mid v) \mid \mid p(u \mid v)] \\ 0 \end{bmatrix}
\tag{9}\]

&lt;p&gt;This optimisation scheme resembles the &lt;a href=&quot;https://en.wikipedia.org/wiki/Expectation%E2%80%93maximization_algorithm&quot;&gt;Expectation-Maximisation (EM) algorithm&lt;/a&gt;.
In the E step, we compute the expectations, while in the M step we fix the parameters with respect to which the expectations were computed and we maximise with respect to the functions inside the expectation.
In EM we do this, because maximum-likelihood with latent variables often does not have closed-form solutions.
The motivation here is to make the optimisation more stable.&lt;/p&gt;

&lt;p&gt;I wrote this blog post, because I have no idea whether this &lt;em&gt;changed&lt;/em&gt; optimisation procedure is justified in any way. What do you think? I would appreciate any comments.&lt;/p&gt;
</description>
        <pubDate>Sun, 10 Sep 2017 13:57:00 +0000</pubDate>
        <link>http://akosiorek.github.io/kl-hierarchical-vae/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/kl-hierarchical-vae/</guid>
        
        
        <category>ML</category>
        
      </item>
    
      <item>
        <title>Implementing Attend, Infer, Repeat</title>
        <description>&lt;p&gt;Variational Autoencoders (VAEs) are getting more and more popular in the Machine Learning community.
While the formulation is more involved then that of a typical feed-forward neural network, VAEs have a lot of added benefits.
I’ve been recently playing with one of the more complicated VAE models: &lt;a href=&quot;https://papers.nips.cc/paper/6230-attend-infer-repeat-fast-scene-understanding-with-generative-models&quot;&gt;Attend, Infer, Repeat (AIR)&lt;/a&gt; by &lt;a href=&quot;http://arkitus.com/&quot;&gt;Ali Eslami et. al.&lt;/a&gt; from &lt;a href=&quot;https://deepmind.com/&quot;&gt;DeepMind&lt;/a&gt;, and I must say it’s really cool.
In this blog post, I will describe the model and break it down into simple components. We will also cover parts of the implementation and some issues I had while implementing it. The full implementation is available &lt;a href=&quot;https://github.com/akosiorek/attend_infer_repeat&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;what-does-air-do&quot;&gt;What does AIR do?&lt;/h1&gt;
&lt;p&gt;AIR aims to reconstruct an image, but instead of doing it in a single shot, it focuses on interesting image parts one-by-one.
The figure below demonstrates AIR’s inner workings.
It takes a look at the image, figures out how many interesting parts there are and where they are in the image.
It then reconstructs them by painting one-part-at-a-time onto a blank canvas.
AIR takes a look at the image, figures out how many interesting parts there are, and reconstructs it by painting one-part-at-a-time onto a blank canvas.
Sounds easy enough?
Well, it’s not, and for two reasons:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;It’s completely unsupervised,&lt;/li&gt;
  &lt;li&gt;It takes a variable yet discrete number of steps.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first one is tricky, because we don’t really know how to define an object or an interesting part (more on this later).
The second leads to discrete latent variables, which are not-that-easy to deal with when computing gradients.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;air_flow.png&quot; alt=&quot;AIR&quot; style=&quot;display: block; margin: auto; max-width: 400px;&quot; /&gt;
Let’s go back to the figure. AIR is called Attend, Infer, Repeat for a reason:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;It &lt;strong&gt;attends&lt;/strong&gt; to a part of an image using spatial transformers, effectively cropping it,&lt;/li&gt;
  &lt;li&gt;then it &lt;strong&gt;infers&lt;/strong&gt; the latent variables best describing the crop,&lt;/li&gt;
  &lt;li&gt;and finally it &lt;strong&gt;repeats&lt;/strong&gt; the procedure for the rest of the image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Technically, the order is different, because it has to infer presence of an object and its location before attending to it; and the name describes only the inference process, not reconstruction.&lt;/p&gt;

&lt;p&gt;What’s beautiful, is that we get a variable-length representation of the image: the more complicated the image is, the longer the representation will be.
What’s even better, is that we know that each piece of description is tied to a particular location (and hopefully an object), which allows explicit reasoning about objects and relations between them.&lt;/p&gt;

&lt;h1 id=&quot;results&quot;&gt;Results&lt;/h1&gt;
&lt;p&gt;Measuring performance of generative models is always tricky, and I’d recommend &lt;a href=&quot;https://arxiv.org/abs/1511.01844&quot;&gt;this paper&lt;/a&gt; for a discussion. Here are some plots similar to the ones reported by the AIR paper. The first row of the topmost figure shows the input images, rows 2-4 are reconstructions at steps 1, 2 and 3 (with marked location of the attention glimpse in red, if it exists). Rows 5-7 are the reconstructed image crops, and above each crop is the probability of executing 1, 2 or 3 steps. If the reconstructed crop is black and there is “0 with …” written above it, it means that this step was not used (3rd step is never used, hence the last row is black). Click on the image for a higher-resolution view.&lt;/p&gt;

&lt;div style=&quot;margin: auto&quot;&gt;
  &lt;a href=&quot;reconstruction_300k.png&quot;&gt;
    &lt;img src=&quot;reconstruction_300k.png&quot; style=&quot;width: 800px&quot; /&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;At every time-step, AIR chooses where to look in the image. The image on the left hand-side visualises the localisation policy of the spatial transformer, with red corresponding to the first step and green to the second. We see that the scanning policy is spatial with the majority of first steps located on the left hand-side of the image. The plot on the right hand-side is the counting accuracy on the validation set while training for 300k iterations, evaluated every 10k iterations.&lt;/p&gt;

&lt;div style=&quot;margin: auto&quot;&gt;
  &lt;img src=&quot;heatmap.png&quot; style=&quot;width: 200px&quot; /&gt;
  &lt;img src=&quot;acc_plot.png&quot; style=&quot;width: 500px&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;AIR can reach almost 100% accuracy in counting objects, but this outcome does heavily depend on initialisation. Very often (80% of the time) the model converges to either zero or the maximum number of steps and fails to converge to the preferred solution.&lt;/p&gt;

&lt;h1 id=&quot;why-and-how-does-it-work&quot;&gt;Why and how does it work?&lt;/h1&gt;
&lt;p&gt;Like every VAE, AIR is trained by maximising the evidence lower bound (ELBO) \(\mathcal{L}\) on the log probability of the data:&lt;/p&gt;

\[\begin{align*}
        \log p(x) &amp;amp;= \mathcal{L}(\theta, \phi) + KL(q_\phi(z \mid x) \mid\mid p(z \mid x)),\\
        \mathcal{L}(\theta, \phi) &amp;amp;= \mathbb{E}_{q_\phi(z)} [\log p_\theta(x \mid z)]] - KL(q_\phi(z\mid x) \mid\mid p(z)).
    \end{align*}\]

&lt;p&gt;The first term of the ELBO is a probabilistic analog of the reconstruction error and the second term acts as a regulariser.
For AIR, the second term tries to keep the number of steps low, but it’s also forcing the latent encoding of each image part to be as short as possible.&lt;/p&gt;

&lt;p&gt;Short encoding means that the model has to focus on parts of the image that can be explained with relatively few variables.
It turns out that we can define an object as an image patch, where pixel correlations within that patch are strong, but the correlation between pixels inside and outside of that patch is weak.
We can also assume that pixels belonging to two different objects have very low correlation (as long as the two objects appear independently of each other).
That means that explaining even small parts of two different objects at the same time leads to potentially longer encoding than explaining one (potentially big) object at a time.
This leads, at least in case of uncomplicated backgrounds as in the paper, to a  model which learns to take the minimum number of steps possible, where every step explains an internally-consistent part of the image.&lt;/p&gt;

&lt;h1 id=&quot;what-do-we-need&quot;&gt;What do we need?&lt;/h1&gt;
&lt;p&gt;We will start by defining a few core components. AIR is an autoencoder, and we will need an encoder and a decoder, but there’s more than that, namely:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Input encoder: transforms the input image \(x\) into some hidden representation \(v\).&lt;/li&gt;
  &lt;li&gt;RNN: Since we’re taking multiple peeks at the image, we need some hidden state \(h\) to keep track of what has already been explained. It creates the new hidden state as&lt;/li&gt;
&lt;/ul&gt;

\[\begin{align}
    h^{i+1} = RNN(v, h^i, z^i),
\end{align}\]

&lt;p&gt;where \(z^i = \{z^i_{what}, z^i_{where}, z^i_{pres}\}\) are the latent variables describing the appearance, location and presence of an object, respectively.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Presence &amp;amp; Location models: Given the hidden state \(h^i\), they predict \(z^i_{pres}\) and \(z^i_{where}\).&lt;/li&gt;
  &lt;li&gt;Spatial Transformer: Given the location parameters \(z^i_{pres}\), it extracts a crop of the original input image \(x^i_{att}\). It will later place a reconstructed crop \(y^i_{att}\) into the canvas.&lt;/li&gt;
  &lt;li&gt;Glimpse encoder: It encodes \(x^i_{att}\) into a low-dimensional latent representation \(z^i_{what}\).&lt;/li&gt;
  &lt;li&gt;Glimpse decoder: It decodes \(z^i_{what}\) in the reconstructed glimpse \(y^i_{att}\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I defined all these components in a &lt;a href=&quot;https://github.com/akosiorek/attend_infer_repeat/blob/master/attend_infer_repeat/modules.py&quot;&gt;single file&lt;/a&gt; as &lt;a href=&quot;https://github.com/deepmind/sonnet&quot;&gt;Sonnet&lt;/a&gt; modules.
Since we don’t want to dwell on complicated architectures, I used small multi-layer perceptrons (MLPs) with 2 hidden layers of 256 units each and ELU nonlinearities for every component.
My RNN is a 256-dimensional LSTM core from Sonnet with a trainable initial state.
I put together all the modules into a working &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.RNNCell&lt;/code&gt; in the &lt;a href=&quot;https://github.com/akosiorek/attend_infer_repeat/blob/master/attend_infer_repeat/cell.py&quot;&gt;cell.py&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;probability-distributions&quot;&gt;Probability Distributions&lt;/h1&gt;
&lt;p&gt;One reason why VAEs are more complicated than standard neural nets are the probability distributions.
 Each of the latent variables \(z\) is not just predicted by the corresponding model; the model predicts parameters of a probability distribution, and then we randomly sample from it.
\(z_{what}\) and \(z_{where}\) both come from Gaussian distributions with diagonal covariance matrices, whose means and variances are predicted by MLPs (glimpse encoder and location model, respectively).
I used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.NormalWithSoftplusScale&lt;/code&gt; for numerical stability of the scale parameters.
\(z_{pres}\) is much more tricky.
 At inference time, it comes from a Bernoulli distribution parametrised by an output of the presence model.
When the previous sample was equal to 1, we take the current sample as is.
As soon as we draw a sample equal to zero, however, all subsequent samples have to be set to zero, too.
This ancestral-sampling scheme results in a modified geometrical distribution, for which we have to account when we implement the KL-divergence with the prior. For this reason, I implemented a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NumStepsDistribution&lt;/code&gt; in &lt;a href=&quot;https://github.com/akosiorek/attend_infer_repeat/blob/master/attend_infer_repeat/prior.py&quot;&gt;prior.py&lt;/a&gt; that creates the modified geometric distribution given Bernoulli probabilities at consecutive steps.&lt;/p&gt;

&lt;h1 id=&quot;piors&quot;&gt;Piors&lt;/h1&gt;
&lt;p&gt;Every VAE requires a prior on its latent representation.
AIR requires at least three priors for three different latent variables.
I used a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Normal(0, 1)&lt;/code&gt; prior for both \(z_{what}\) and \(z_{where}\) and a modified geometric-like prior for \(z_{pres}\) (number of steps).
Setting its success probability is tricky, though.
The paper mentions only that it is uses a “geometric prior which encourages sparse solutions”, which tells us only that the success probability in the geometric distribution is low.
When I emailed the author, I found out that he annealed the success probability from a value close to 1 to either \(10^{-5}\) or \(10^{-10}\) depending on the dataset over the course of 100k training iterations.&lt;/p&gt;

&lt;p&gt;Intuitively, it makes sense.
At the beginning of training, we would like the model to take a positive number of steps so that it can learn.
The further we go into the training, the more we can constrain it.
Very low values of the success probability are important, because the reconstruction loss is summed across the whole image
(it has to be: in the derivation of the loss, pixels are assumed to be conditionally independent given \(z_{what}\)
and log probability of independent events results in a sum) and KL-divergence has to compete with it during the optimisation.&lt;/p&gt;

&lt;h1 id=&quot;estimating-gradients-for-discrete-variables&quot;&gt;Estimating Gradients for Discrete Variables&lt;/h1&gt;
&lt;p&gt;Discrete variables, or more specifically, samples from a discrete probability distribution, are difficult to back-propagate through.
AIR uses a score-function estimator, otherwise known as REINFORCE.
More about it &lt;a href=&quot;https://www.google.com/search?q=score-function+estimator&amp;amp;rlz=1C5CHFA_enGB715GB715&amp;amp;oq=score-function+estimator&amp;amp;aqs=chrome..69i57j0.3730j0j7&amp;amp;sourceid=chrome&amp;amp;ie=UTF-8&quot;&gt;here&lt;/a&gt;.
This estimator is difficult to work with, because the estimate has a high variance. It expresses the gradient of an expectation of a smooth function (here \(\mathcal{L}\)) as the expectation of the gradient of the log-probability with respect to which the expectation is taken multiplied by that function.&lt;/p&gt;

\[\begin{align}
\nabla_\phi \mathbb{E}_{q_\phi(z)} [ \mathcal{L} (z)] = \mathbb{E}_{q_\phi(z)} [\mathcal{L}(z) \nabla_\phi \log q_\phi(z) ]
\end{align}\]

&lt;p&gt;It turns out that the expectation of this expression is equal to zero, and therefore we can add an arbitrary term with zero expectation without changing the result.
If what we add is negatively correlated with \(\mathcal{L}\), we will reduce variance. AIR uses “neural baselines” and cites &lt;a href=&quot;https://arxiv.org/abs/1402.0030&quot;&gt;Neural Variational Inference and Learning in Belief Networks&lt;/a&gt; by A. Mnih and K. Gregor, but doesn’t give much detail.&lt;/p&gt;

&lt;p&gt;Do we really need to reduce variance? Well, yes. I’ve measured variance on a per-parameter basis for the AIR model. Back-propagation results in variance on the order of \(10^{-2}\). There is some variance, as we’d expect from Stochastic Gradient Decent, but it’s not huge. Due to discrete latent variables, gradient of some of the parameters comes only from the REINFORCE formulation, and its variance is on the order of \(10^3\). It’s five orders of magnitude higher, and I wouldn’t expect it to be very useful for training. The neural baseline reduces the variance to about \(10^{-1}\). It’s still higher than from back-prop, but usable.&lt;/p&gt;

&lt;p&gt;I used an MLP with 2 hidden layers of 256 and 128 neurones, respectively, with a single output unit. As input, I used the original flattened image concatenated with all latent variables produced by the main model. The baseline is trained to minimise the mean-squared error with the current reconstruction error (\(-\mathbb{E}_{q_\phi(z)} [\log p_\theta(x \mid z)]\)) of the main model as the target. The learning rate used for training this auxiliary model was set 10 times higher than the learning rate of the base model.&lt;/p&gt;

&lt;p&gt;To see how REINFORCE with a neural baseline is implemented, have a look at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AIRModel._reinforce&lt;/code&gt; method in &lt;a href=&quot;https://github.com/akosiorek/attend_infer_repeat/blob/master/attend_infer_repeat/model.py&quot;&gt;model.py&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;issues&quot;&gt;Issues&lt;/h1&gt;
&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;My implementation is very fragile. It recovers the performance reported in the paper once for about 5 training runs. I’m not saying it’s an issue with the model, it’s probably just my implementation. If anyone has ideas how to improve it, please let me know.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If I change the multi-MNIST dataset to have smaller digits, the model doesn’t count as well (number of steps is wrong). That’s probably an issue of my implementation, too.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;It is sensitive to initialisation of the output layers that produce the final reconstruction but also of the “where” and “pres” latent variables. If the reconstruction has too big values at the beginning of the training, the number of steps shrinks to zero and the model never recovers. Similar things happen when “where” latent variable has too big a variance at the beginning. This behaviour is obvious in hindsight, but it wasn’t that clear while implementing.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;It’s a really cool if a bit complicated model. I hope this post has brought you closer to understanding of what’s going on in the paper. I’ve implemented it because I have a few ideas on how to use it in my research. Feel free to reach out if you have any questions or comments.&lt;/p&gt;
</description>
        <pubDate>Sun, 03 Sep 2017 14:44:17 +0000</pubDate>
        <link>http://akosiorek.github.io/implementing-air/</link>
        <guid isPermaLink="true">http://akosiorek.github.io/implementing-air/</guid>
        
        
        <category>ML</category>
        
      </item>
    
  </channel>
</rss>
