<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fanghao Han on Medium]]></title>
        <description><![CDATA[Stories by Fanghao Han on Medium]]></description>
        <link>https://medium.com/@patrickhan9?source=rss-f75b238df9ec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*uFB8MsGDI3F_1Rr5wLC9kg.jpeg</url>
            <title>Stories by Fanghao Han on Medium</title>
            <link>https://medium.com/@patrickhan9?source=rss-f75b238df9ec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:24:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@patrickhan9/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Tutorial on Variational Graph Auto-Encoders]]></title>
            <link>https://medium.com/data-science/tutorial-on-variational-graph-auto-encoders-da9333281129?source=rss-f75b238df9ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/da9333281129</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[graph-neural-networks]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Fanghao Han]]></dc:creator>
            <pubDate>Mon, 09 Sep 2019 22:07:39 GMT</pubDate>
            <atom:updated>2019-09-09T22:07:39.878Z</atom:updated>
            <content:encoded><![CDATA[<p>Graphs are applicable to many real-world datasets such as social networks, citation networks, chemical graphs, etc. The growing interest in graph-structured data increases the number of researches in graph neural networks.</p><p>Variational autoencoders (VAEs) embodied the success of variational Bayesian methods in deep learning and have inspired a wide range of ongoing researches. Variational graph autoencoder (VGAE) applies the idea of VAE on graph-structured data, which significantly improves predictive performance on a number of citation network datasets such as Cora and Citesser.</p><p>I searched on the internet and have yet to see a detailed tutorial on VGAE. In this article, I will briefly talk about traditional autoencoders and variational autoencoders. Furthermore, I will discuss the idea of applying VAE to graph-structured data (VGAE).</p><h3>Traditional Auto-Encoders</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*ExKVqAGmTNerUNfdabPmew.jpeg" /><figcaption>Figure 1: The architecture of the traditional autoencoder</figcaption></figure><p>The traditional autoencoder is a neural network that contains an encoder and a decoder. The encoder takes a data point <em>X</em> as input and converts it to a lower-dimensional representation (embedding) <em>Z</em>. The decoder takes the lower-dimensional representation <em>Z</em> and returns a reconstruction of the original input <em>X-hat</em> that looks like the input <em>X</em>. The quality of the embedding determines the quality of the output <em>X-hat</em>. However, it might not be possible for the encoder to encode all information because the embedding has a lower dimensionality than the input. Therefore, if the embedding captures more information from the input, the output will have a better performance.</p><h4>The architecture of the Encoder and Decoder</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*TKROzv_vWHNNLJW9vnkGBw.jpeg" /><figcaption>Figure 2: Traditional autoencoder for MNIST dataset</figcaption></figure><p>Let’s look at an example. Figure 2 shows an example of an autoencoder that takes an MNIST image as input. The MNIST dataset contains grayscale images and each image has a shape of 28 by 28 pixels. The encoder takes this image and converts it to a lower-dimensional embedding <em>Z</em>. The decoder takes the embedding <em>Z</em> and returns a reconstructed input image, which is the output of the autoencoder.</p><h4>Loss Function</h4><p>The loss function of an autoencoder measures the information lost during the reconstruction. We want to minimize the reconstruction loss to make<em> X-hat</em> closer to <em>X</em>. We often use mean square error as our loss function,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*7cGLo3OHiTCySEWM4KF98g@2x.png" /></figure><p>which measures how close <em>X-hat</em> is to <em>X</em>.</p><h4>Why do we want to convert the input to a lower-dimensional embedding?</h4><p>Let’s use images as an example. There are several ways that we can use low-dimensional embeddings of images. Storing images’ low-dimensional embeddings could save storage space compared with storing their pixel intensities. Storing images’ low-dimensional embeddings could also save computational power because the input dimensions are lower.</p><p>Points that are near image embeddings are also image embeddings in the embedding space, and this is another reason that makes autoencoder embedding useful. For example, if you modify the pixel intensities of an MNIST image, you’ll get a noisy image that does not look like an image in the MNIST dataset. However, if you modify the embedding of an MNIST image by a small amount, then decode the modified embedding, you could still get something that looks like an MNIST image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*DNAUNRTfTl2WLmKa_hiP6Q.png" /><figcaption>Figure 3: Modify the pixel intensities of an MNIST image</figcaption></figure><p>Figure 3 shows an example of modifying the <em>pixel intensities</em> of an MNIST image. After adding some random noise to each pixel’s intensity, the output is a noisy image that doesn’t look like an image in the MNIST dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*_hZTlZsX17VmwzDidOquPg.jpeg" /><figcaption>Figure 4: Modify the embedding of MNIST image by a small amount</figcaption></figure><p>Figure 4 shows an example of modifying the <em>embedding</em> of an MNIST image. Modifying the embedding by a small amount is the same as a small shift in the embedding space. From figure 4 we can see that after adding 4 to each element in embedding <em>Z</em>, the output image <em>X2-hat</em> is still very similar to the original output <em>X1-hat</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*UytpUTHacVVDhrnBrGy0UQ.jpeg" /><figcaption>Figure 5: An example of plotting MNIST data on embedding space</figcaption></figure><p>We could also plot these low-dimensional embeddings to the n-d coordinate. Figure 5 shows an example of plotting MNIST data on the embedding space. Each point on the embedding space represents one input data. This visualization is convenient because it could systematically organize all the input data in the coordinates.</p><p>Low-dimensional embeddings could also be used on downstream machine learning tasks. We could use the precalculated embeddings to solve another machine learning problem, i.e., classification problems. We could do classification on these embeddings rather than the original inputs. We could also use embeddings on transfer learning or semi-supervised learning, which may give us a better result compared to original inputs.</p><h3>Variational Autoencoders</h3><h4>Why do we need the variational autoencoders?</h4><p>One of the biggest advantages of the variational autoencoder is that VAE could generate new data from the original source dataset. In contrast, traditional autoencoder could only generate images that are similar to the original inputs. Let’s say you want to build a garden that is filled with bushes. Every single bush needs to be different so your garden would look real. You definitely can’t draw every single bush by yourself, a smarter way is using variational autoencoder to automatically generate new bushes for your garden.</p><h4>Main idea</h4><p>The main idea of a variational autoencoder is that it embeds the input <em>X</em> to a distribution rather than a point. And then a random sample <em>Z</em> is taken from the distribution rather than generated from encoder directly.</p><h4>The architecture of the Encoder and Decoder</h4><p>The encoder for a VAE is often written as <em>qφ(z|x)</em>, which takes a data point <em>X</em> and produces a distribution. The distribution is usually parameterized as a multivariate Gaussian. Therefore, the encoder predicts the means and standard deviation of the Gaussian distribution. The lower-dimensional embedding <em>Z</em> is sampled from this distribution. The decoder is a variational approximation, <em>pθ(x|z)</em>, which takes an embedding <em>Z</em> and produces the output <em>X-hat</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/355/1*MTQxyzVDc0SBn5aWwKlFKw.jpeg" /><figcaption>Figure 6: An example of a variational autoencoder</figcaption></figure><h4>Loss Function</h4><p>The loss function for VAE has two parts. The first part of the loss function is called the variational lower bound, which measures how well the network reconstructs the data. If the reconstructed data <em>X</em> is very different than the original data, then the reconstruction loss will be high. The second part of the loss function works as a regularizer. It is the KL-divergence of the approximate from the true posterior (<em>p(z)</em>), which measures how closely the output distribution (<em>qφ(z|x)</em>) match to <em>p(z)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZDCMACuxeFDapxdFLgmGCQ.png" /></figure><h4>Summary</h4><p>The idea of VAE can be generalized by the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*n3Aikem65wlIsjc-U5QbOA.jpeg" /><figcaption>Figure 7: The architecture of the variational autoencoder</figcaption></figure><p>The encoder takes a datapoint <em>X</em> as input and generates μ and logσ² as outputs. The reason that we use logσ² instead of σ² is that σ² is non-negative, so we will need an extra activation function. But logσ² could be positive or negative. After we get μ and logσ², we try to make both μ and logσ² close to 0, which means μ is close to 0 and σ is close to 1. Therefore the final distribution will be close to <em>N(0,1)</em>. Finally, we want to generate the embedding <em>Z</em> from μ and σ by<em> z = μ + σ * ε</em>, where ε ~ <em>N(0,1)</em>. This is called the <em>reparameterization trick</em>. Now with the latent variable <em>Z</em>, we can generate our output <em>X-hat</em> through the decoder.</p><h3>Variational Graph Autoencoders</h3><h4>Introduction</h4><p>We want to build a variational graph autoencoder that applies the idea of VAE to graph-structured data. We want our variational graph autoencoder to be able to generate new graphs or reason about graphs. However, we can’t just straightforwardly apply the idea of VAE because graph-structured data are irregular. Each graph has a variable size of unordered nodes and each node in a graph has a different number of neighbours, so we can’t just use convolution directly anymore. Let’s figure out how to represent a graph in a way that a neural network can understand.</p><h4>Adjacency Matrix</h4><p>We use an adjacency matrix <em>A</em> to represent the input graph. Normally we assume the adjacency matrix is binary. The value 1 at row <em>i</em> and column <em>j</em> means that there is an edge between vertex <em>i</em> and vertex <em>j</em>. The value 0 at row <em>m</em> and column<em> n</em> means that there is no edge between vertex <em>m</em> and vertex <em>n</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*Cbk54LfvgeTIAAB6meRW8Q.jpeg" /><figcaption>Figure 8: An example of the adjacency matrix</figcaption></figure><p>Figure 8 shows an example of the adjacency matrix when the input graph is directed and undirected. On the left-hand side when the graph is undirected, the adjacency matrix is symmetric. Vertex 0 and vertex 1 are connected to each other, so <em>A[0,1] = 1</em> and <em>A[1,0] = 1</em>. On the right-hand side when the graph is directed, the adjacency matrix is unsymmetric. Vertex 0 points to vertex 1, which means vertex 1 aggregates information from vertex 0, so <em>A[1,0] = 1</em>.</p><h4>Feature Matrix</h4><p>We use the feature matrix <em>X</em> to represent the features of each node from the input graph. Row<em> i</em> of the feature matrix <em>X</em> represents the feature embeddings for vertex <em>i</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*qYHYoEMaSrQDpZZuFcpRww.jpeg" /><figcaption>Figure 9: An example of the feature matrix</figcaption></figure><p>Figure 9 shows an example of the feature matrix. Each node <em>i</em> has its own feature matrix <em>X_i</em> and we can combine them together to get the feature matrix <em>X</em> for the whole graph, where each row <em>i</em> represents the feature matrix of node <em>i</em>.</p><h4>The architecture of the Encoder and Decoder</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*CijfkQ_NMDKsYbsN6FqCRA.jpeg" /><figcaption>Figure 10: The architecture of the variational graph autoencoder</figcaption></figure><p>The encoder (inference model) of VGAE consists of <a href="https://tkipf.github.io/graph-convolutional-networks/">graph convolutional networks</a> (<em>GCNs</em>). It takes an adjacency matrix <em>A</em> and a feature matrix <em>X</em> as inputs and generates the latent variable <em>Z</em> as output. The first GCN layer generates a lower-dimensional feature matrix. It is defined as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*rWj6q2iBnkFj8d88DeH8HA@2x.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*vS6LTrwVtUdVtOjJTabzHg@2x.png" /></figure><p><em>A-tilde </em>is the symmetrically normalized adjacency matrix.</p><p>The second GCN layer generates μ and logσ², where</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*PLPO5H-SveNhOdFTMoofDg@2x.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*QgrtH54dF_T-WSi3It2bcA@2x.png" /></figure><p>Now if we combine the math of two-layer GCN together, we get</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*yEg0vudn7KMlBdnvSkGP0w@2x.png" /></figure><p>which generates μ and logσ².</p><p>Then we can calculate <em>Z</em> using parameterization trick</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/241/1*RdPMY0tWXaJ7WfkkV5ej2A@2x.png" /></figure><p>where ε ~ <em>N(0,1).</em></p><p>The decoder (generative model) is defined by an inner product between latent variable <em>Z</em>. The output of our decoder is a reconstructed adjacency matrix <em>A-hat</em>, which is defined as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*Jc6W3f9g4PAJM8rkFzXAKQ@2x.png" /></figure><p>where σ(•) is the logistic sigmoid function.</p><p>In summary, the encoder is represented as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*51jxQJHoQ-njSFzoLvvWDw@2x.png" /></figure><p>and the decoder is represented as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*j07CkXS8fWncypbDZ8IE8Q@2x.png" /></figure><h4>Loss Function</h4><p>The loss function for variational graph autoencoder is pretty much the same as before. The first part is the reconstruction loss between the input adjacency matrix and the reconstructed adjacency matrix. More specifically, it is the binary cross-entropy between the target (A) and output (A’) logits. The second part is the KL-divergence between <em>q​​(Z | X, A)</em> and<em> p(Z)</em>, where <em>p(Z) = N(0,1)</em>. It measures how closely our <em>q​​(Z | X, A)</em> matches to<em> p(Z)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*fSmD3RmZ_nmCZWuBXTowyQ@2x.png" /></figure><h4>The advantage of using inner product decoder</h4><p>After we get the latent variable <em>Z</em>, we want to find a way to learn the similarity of each row in the latent variable (because one row represents one vertex) to generate the output adjacency matrix. Inner product could calculate the cosine similarity of two vectors, which is useful when we want a distance measure that is invariant to the magnitude of the vectors. Therefore, by applying the inner product on the latent variable <em>Z</em> and <em>Z^T</em>, we can learn the similarity of each node inside <em>Z</em> to predict our adjacency matrix.</p><h4>Experiments on link prediction</h4><p>In the <a href="https://arxiv.org/pdf/1611.07308.pdf">variational graph auto-encoders paper</a>, the authors also create a traditional graph autoencoder model (GAE) as a contrast to the VGAE model. The GAE model has a single-GCN layer as the encoder, which generates a latent variable <em>Z</em> directly, and an inner product decoder, which is the same as the VGAE model. The authors test both models on three datasets (Cora, Citesser and Pubmed), and the VGAE model achieves higher predictive performance on both the Cora and the Citeseer dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*s2se4BF-BzluXQXr-9PxPw.png" /></figure><h3>Conclusion</h3><p>In this post, you have learned the basic idea of the traditional autoencoder, the variational autoencoder and how to apply the idea of VAE to graph-structured data.</p><p>Graph-structured data plays a more important role in various fields nowadays. And I believe graph-structured data analysis will become one of the most popular topics in machine learning in the near future.</p><h3>References</h3><p>[1] <a href="https://arxiv.org/pdf/1312.6114.pdf">Auto-Encoding Variational Bayes</a></p><p>[2] <a href="https://arxiv.org/pdf/1611.07308.pdf">Variational Graph Auto-Encoders</a></p><p>[3] <a href="https://github.com/tkipf/gae">Graph Auto-Encoders TensorFlow implementation</a></p><p>[4] <a href="https://arxiv.org/pdf/1606.05908.pdf">Tutorial on Variational Autoencoders</a></p><p>[5] <a href="https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence">Kullback–Leibler divergence</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da9333281129" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/tutorial-on-variational-graph-auto-encoders-da9333281129">Tutorial on Variational Graph Auto-Encoders</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>