<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://wcshin-git.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://wcshin-git.github.io/" rel="alternate" type="text/html" /><updated>2025-03-10T12:50:47+00:00</updated><id>https://wcshin-git.github.io/feed.xml</id><title type="html">Slow and Steady</title><subtitle>Welcome to my blog!
</subtitle><author><name>Woncheol Shin</name></author><entry><title type="html">Introduction to Cell Annotation with ScType</title><link href="https://wcshin-git.github.io/2025/03/10/Intro_Cell_Annotation_with_ScType.html" rel="alternate" type="text/html" title="Introduction to Cell Annotation with ScType" /><published>2025-03-10T02:57:00+00:00</published><updated>2025-03-10T02:57:00+00:00</updated><id>https://wcshin-git.github.io/2025/03/10/Intro_Cell_Annotation_with_ScType</id><content type="html" xml:base="https://wcshin-git.github.io/2025/03/10/Intro_Cell_Annotation_with_ScType.html"><![CDATA[<p><img src="/images//Intro_ScType/Development_of_scRNA_sequencing_technology.png" alt="cluster_annotated" />
<em>Fig. Development of single-cell RNA sequencing technology. (Source: Jovic et al. 2021)</em></p>

<p>Single-cell RNA sequencing (scRNA-seq) has revolutionized the fields of biology and medicine by enabling exploration of the transcriptomic profiles of individual cells.</p>

<p>This post aims to show how to use <a href="https://www.nature.com/articles/s41467-022-28803-w">ScType</a> to annotate cells in R code.</p>

<p>This post can be divided into two main parts:</p>

<p>1) <strong>Clustering Tutorial</strong>, which covers selecting cells (quality control), normalizing the data, identifying highly variable features, scaling the data, performing PCA, clustering the cells, and visualizing the clusters using UMAP.</p>

<p>2) <strong>Cell Annotation</strong> using ScType.</p>

<h2 id="1-clustering-tutorial">1. Clustering Tutorial</h2>

<h3 id="download-the-example-dataset-and-code">Download the example dataset and code</h3>

<p>Run <code class="language-plaintext highlighter-rouge">git clone</code> or click this <a href="https://github.com/wcshin-git/Cell_Annotation_with_ScType/archive/refs/heads/main.zip">link</a> to download.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone https://github.com/wcshin-git/Cell_Annotation_with_ScType
</code></pre></div></div>

<h3 id="import-the-required-packages">Import the required packages</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">lapply</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s2">"dplyr"</span><span class="p">,</span><span class="s2">"Seurat"</span><span class="p">,</span><span class="s2">"HGNChelper"</span><span class="p">,</span><span class="s2">"openxlsx"</span><span class="p">),</span><span class="w"> </span><span class="n">library</span><span class="p">,</span><span class="w"> </span><span class="n">character.only</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>
<p>If any of the packages are missing, please install them first.</p>

<h3 id="load-the-example-dataset">Load the example dataset</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pbmc.data</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">Read10X</span><span class="p">(</span><span class="n">data.dir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"filtered_gene_bc_matrices/hg19"</span><span class="p">)</span><span class="w"> 
</span><span class="c1"># (32738, 2700)</span><span class="w">
</span></code></pre></div></div>
<p>we will analyze the dataset of Peripheral Blood Mononuclear Cells (PBMC) freely available from 10X Genomics.
There are 2,700 single cells that were sequenced on the Illumina NextSeq 500.</p>

<p>Let’s examine <code class="language-plaintext highlighter-rouge">pbmc.data</code> by looking at a few genes in the first 30 cells.</p>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pbmc.data</span><span class="p">[</span><span class="nf">c</span><span class="p">(</span><span class="s2">"CD3D"</span><span class="p">,</span><span class="w"> </span><span class="s2">"TCL1A"</span><span class="p">,</span><span class="w"> </span><span class="s2">"MS4A1"</span><span class="p">),</span><span class="w"> </span><span class="m">1</span><span class="o">:</span><span class="m">30</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="m">3</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="m">30</span><span class="w"> </span><span class="n">sparse</span><span class="w"> </span><span class="n">Matrix</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">class</span><span class="w"> </span><span class="s2">"dgCMatrix"</span><span class="w">
                                                                   
</span><span class="n">CD3D</span><span class="w">  </span><span class="m">4</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">10</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="m">7</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">2</span><span class="w">  </span><span class="m">3</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="m">4</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">5</span><span class="w">
</span><span class="n">TCL1A</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w">  </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w">  </span><span class="n">.</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w">
</span><span class="n">MS4A1</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">6</span><span class="w">  </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">36</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="m">2</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w"> </span><span class="n">.</span><span class="w">
</span></code></pre></div></div>
<p>The <code class="language-plaintext highlighter-rouge">.</code> values in the matrix represent 0s. Since most values in an scRNA-seq matrix are 0, Seurat uses a sparse-matrix representation whenever possible.</p>

<h3 id="setup-the-seurat-object">Setup the Seurat Object</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pbmc</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">CreateSeuratObject</span><span class="p">(</span><span class="n">counts</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pbmc.data</span><span class="p">,</span><span class="w"> </span><span class="n">project</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"pbmc3k"</span><span class="p">,</span><span class="w"> </span><span class="n">min.cells</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="n">min.features</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">200</span><span class="p">)</span><span class="w">
</span><span class="c1"># Retain genes expressed in at least 3 cells and cells with at least 200 detected genes.</span><span class="w">
</span><span class="c1"># (13714, 2700)</span><span class="w">
</span></code></pre></div></div>
<p>The object serves as a container that contains both data (like the count matrix) and analysis (like PCA, or clustering results) for a single-cell dataset.</p>

<h3 id="quality-control">Quality Control</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># percentage of counts originating from a set of mitochondrial genes</span><span class="w">
</span><span class="n">pbmc</span><span class="p">[[</span><span class="s2">"percent.mt"</span><span class="p">]]</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">PercentageFeatureSet</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">pattern</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"^MT-"</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>
<p>Low-quality / dying cells often exhibit extensive mitochondrial contamination.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Visualize QC metrics as a violin plot</span><span class="w">
</span><span class="n">VlnPlot</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">features</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"nFeature_RNA"</span><span class="p">,</span><span class="w"> </span><span class="s2">"nCount_RNA"</span><span class="p">,</span><span class="w"> </span><span class="s2">"percent.mt"</span><span class="p">),</span><span class="w"> </span><span class="n">ncol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p><img src="/images//Intro_ScType/QC_metrics.png" alt="QC_metrics" /></p>

<p>nFeature_RNA represents the number of unique genes (features) detected in each cell.
nCount_RNA represents the total number of RNA molecules detected in each cell.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Cell filtering</span><span class="w">
</span><span class="n">pbmc</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">subset</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">subset</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nFeature_RNA</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="m">200</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="n">nFeature_RNA</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="m">2500</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="n">percent.mt</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w">
</span><span class="c1"># (13714, 2638)</span><span class="w">
</span></code></pre></div></div>
<p>Low-quality cells or empty droplets will often have very few genes.
Cell doublets or multiplets may exhibit an aberrantly high gene count.</p>

<h3 id="normalize-data">Normalize data</h3>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pbmc</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">NormalizeData</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">normalization.method</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"LogNormalize"</span><span class="p">,</span><span class="w"> </span><span class="n">scale.factor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">10000</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>
<p>“LogNormalize” normalizes the feature expression measurements for each cell by the total expression, multiplies this by a scale factor, and log-transforms the result.</p>

<h3 id="identification-of-highly-variable-features">Identification of highly variable features</h3>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pbmc</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">FindVariableFeatures</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">selection.method</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"vst"</span><span class="p">,</span><span class="w"> </span><span class="n">nfeatures</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">2000</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>
<p>Focusing on these genes in downstream analysis helps to highlight biological signal in single-cell datasets.</p>

<h3 id="scaling-the-data">Scaling the data</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pbmc</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">ScaleData</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">features</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rownames</span><span class="p">(</span><span class="n">pbmc</span><span class="p">))</span><span class="w">
</span></code></pre></div></div>
<p>Shifts the expression of each gene, so that the mean expression across cells is 0.</p>

<p>Scales the expression of each gene, so that the variance across cells is 1.</p>

<h3 id="run-pca">Run PCA</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pbmc</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">RunPCA</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">features</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">VariableFeatures</span><span class="p">(</span><span class="n">object</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pbmc</span><span class="p">))</span><span class="w">
</span></code></pre></div></div>
<p>Perform PCA on the scaled data for dimensional reduction.</p>

<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Check number of PC components</span><span class="w">
</span><span class="n">ElbowPlot</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">ndims</span><span class="o">=</span><span class="m">20</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p><img src="/images//Intro_ScType/Elbow_plot.png" alt="Elbow_plot" /></p>

<p>We will select 10 PCs for downstream analysis, based on Elbow plot.</p>

<h3 id="cluster-and-visualize">Cluster and visualize</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pbmc &lt;- FindNeighbors(pbmc, dims = 1:10)
pbmc &lt;- FindClusters(pbmc, resolution = 0.5)
pbmc &lt;- RunUMAP(pbmc, dims = 1:10)
DimPlot(pbmc, label = TRUE, reduction = "umap")
</code></pre></div></div>

<p><img src="/images//Intro_ScType/cluster.png" alt="cluster" /></p>

<p>The <code class="language-plaintext highlighter-rouge">resolution</code> parameter in <code class="language-plaintext highlighter-rouge">FindClusters()</code> sets the ‘granularity’ of the downstream clustering, with increased values leading to a greater number of clusters.</p>

<h2 id="2-cell-annotation">2. Cell Annotation</h2>

<p>Now, let’s annotate the clusters with known cell types using ScType.</p>

<h3 id="load-required-functions">Load required functions</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># load gene set preparation function</span><span class="w">
</span><span class="n">source</span><span class="p">(</span><span class="s2">"https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/R/gene_sets_prepare.R"</span><span class="p">)</span><span class="w">
</span><span class="c1"># load cell type annotation function</span><span class="w">
</span><span class="n">source</span><span class="p">(</span><span class="s2">"https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/R/sctype_score_.R"</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<h3 id="prepare-marker-gene-sets">Prepare marker gene sets</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># DB file</span><span class="w">
</span><span class="n">db_</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="s2">"https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/ScTypeDB_full.xlsx"</span><span class="p">;</span><span class="w">
</span><span class="n">tissue</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="s2">"Immune system"</span><span class="w"> </span><span class="c1"># e.g. Immune system,Pancreas,Liver,Eye,Kidney,Brain,Lung,Adrenal,Heart,Intestine,Muscle,Placenta,Spleen,Stomach,Thymus </span><span class="w">

</span><span class="c1"># prepare gene sets</span><span class="w">
</span><span class="n">gs_list</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">gene_sets_prepare</span><span class="p">(</span><span class="n">db_</span><span class="p">,</span><span class="w"> </span><span class="n">tissue</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<h3 id="run-sctype">Run ScType</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># check Seurat object version (scRNA-seq matrix extracted differently in Seurat v4/v5)</span><span class="w">
</span><span class="n">seurat_package_v5</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">isFALSE</span><span class="p">(</span><span class="s1">'counts'</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="nf">names</span><span class="p">(</span><span class="nf">attributes</span><span class="p">(</span><span class="n">pbmc</span><span class="p">[[</span><span class="s2">"RNA"</span><span class="p">]])));</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">sprintf</span><span class="p">(</span><span class="s2">"Seurat object %s is used"</span><span class="p">,</span><span class="w"> </span><span class="n">ifelse</span><span class="p">(</span><span class="n">seurat_package_v5</span><span class="p">,</span><span class="w"> </span><span class="s2">"v5"</span><span class="p">,</span><span class="w"> </span><span class="s2">"v4"</span><span class="p">)))</span><span class="w">

</span><span class="c1"># extract scaled scRNA-seq matrix</span><span class="w">
</span><span class="n">scRNAseqData_scaled</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">seurat_package_v5</span><span class="p">)</span><span class="w"> </span><span class="n">as.matrix</span><span class="p">(</span><span class="n">pbmc</span><span class="p">[[</span><span class="s2">"RNA"</span><span class="p">]]</span><span class="o">$</span><span class="n">scale.data</span><span class="p">)</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">as.matrix</span><span class="p">(</span><span class="n">pbmc</span><span class="p">[[</span><span class="s2">"RNA"</span><span class="p">]]</span><span class="o">@</span><span class="n">scale.data</span><span class="p">)</span><span class="w">

</span><span class="c1"># run ScType</span><span class="w">
</span><span class="n">es.max</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">sctype_score</span><span class="p">(</span><span class="n">scRNAseqData</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scRNAseqData_scaled</span><span class="p">,</span><span class="w"> </span><span class="n">scaled</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">,</span><span class="w"> </span><span class="n">gs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gs_list</span><span class="o">$</span><span class="n">gs_positive</span><span class="p">,</span><span class="w"> </span><span class="n">gs2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gs_list</span><span class="o">$</span><span class="n">gs_negative</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>The score is calculated through the following process:</p>

<p>1) Z-score Standardization</p>

<p>For each gene (row), the expression values across multiple cells (columns) are standardized using the z-score.</p>

<p>2) Calculating and Applying Marker Sensitivity</p>

<p>Genes that appear as markers for multiple cell types are given scores closer to 0, while those that are more specific to a single cell type receive scores closer to 1. By multiplying the z-scored expression matrix by these sensitivity scores, we effectively weight each gene according to how important it is for distinguishing that particular cell type.</p>

<p>3) Cell-Type Scoring via Positive/Negative Markers</p>

<p>For a given cell, the expression values (already multiplied by the gene-specificity scores) of the positive marker genes for a particular cell type are summed. To reduce biases due to varying numbers of marker genes, the total is divided by the square root of the number of marker genes. Negative markers for that cell type are also summed, but with a negative sign, and added in. The final value represents the score of that cell for that specific cell type.</p>

<h3 id="assigning-cell-types-to-clusters">Assigning Cell Types to Clusters</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># merge by cluster</span><span class="w">
</span><span class="n">cL_resutls</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">do.call</span><span class="p">(</span><span class="s2">"rbind"</span><span class="p">,</span><span class="w"> </span><span class="n">lapply</span><span class="p">(</span><span class="n">unique</span><span class="p">(</span><span class="n">pbmc</span><span class="o">@</span><span class="n">meta.data</span><span class="o">$</span><span class="n">seurat_clusters</span><span class="p">),</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">cl</span><span class="p">){</span><span class="w">
    </span><span class="n">es.max.cl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sort</span><span class="p">(</span><span class="n">rowSums</span><span class="p">(</span><span class="n">es.max</span><span class="p">[</span><span class="w"> </span><span class="p">,</span><span class="n">rownames</span><span class="p">(</span><span class="n">pbmc</span><span class="o">@</span><span class="n">meta.data</span><span class="p">[</span><span class="n">pbmc</span><span class="o">@</span><span class="n">meta.data</span><span class="o">$</span><span class="n">seurat_clusters</span><span class="o">==</span><span class="n">cl</span><span class="p">,</span><span class="w"> </span><span class="p">])]),</span><span class="w"> </span><span class="n">decreasing</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">!</span><span class="m">0</span><span class="p">)</span><span class="w">
    </span><span class="n">head</span><span class="p">(</span><span class="n">data.frame</span><span class="p">(</span><span class="n">cluster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cl</span><span class="p">,</span><span class="w"> </span><span class="n">type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">names</span><span class="p">(</span><span class="n">es.max.cl</span><span class="p">),</span><span class="w"> </span><span class="n">scores</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">es.max.cl</span><span class="p">,</span><span class="w"> </span><span class="n">ncells</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="n">pbmc</span><span class="o">@</span><span class="n">meta.data</span><span class="o">$</span><span class="n">seurat_clusters</span><span class="o">==</span><span class="n">cl</span><span class="p">)),</span><span class="w"> </span><span class="m">10</span><span class="p">)</span><span class="w">
</span><span class="p">}))</span><span class="w">
</span><span class="n">sctype_scores</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">cL_resutls</span><span class="w"> </span><span class="o">%&gt;%</span><span class="w"> </span><span class="n">group_by</span><span class="p">(</span><span class="n">cluster</span><span class="p">)</span><span class="w"> </span><span class="o">%&gt;%</span><span class="w"> </span><span class="n">top_n</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">wt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scores</span><span class="p">)</span><span class="w">  

</span><span class="c1"># set low-confident (low ScType score) clusters to "unknown"</span><span class="w">
</span><span class="n">sctype_scores</span><span class="o">$</span><span class="n">type</span><span class="p">[</span><span class="nf">as.numeric</span><span class="p">(</span><span class="nf">as.character</span><span class="p">(</span><span class="n">sctype_scores</span><span class="o">$</span><span class="n">scores</span><span class="p">))</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">sctype_scores</span><span class="o">$</span><span class="n">ncells</span><span class="o">/</span><span class="m">4</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="s2">"Unknown"</span><span class="w">
</span></code></pre></div></div>
<p>We sum the ScType scores of the cells within each cluster and select the cell type with the highest score for each cluster.</p>

<h3 id="visualization">Visualization</h3>
<div class="language-R highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">pbmc</span><span class="o">@</span><span class="n">meta.data</span><span class="o">$</span><span class="n">sctype_classification</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="w">
</span><span class="k">for</span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">unique</span><span class="p">(</span><span class="n">sctype_scores</span><span class="o">$</span><span class="n">cluster</span><span class="p">)){</span><span class="w">
  </span><span class="n">cl_type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sctype_scores</span><span class="p">[</span><span class="n">sctype_scores</span><span class="o">$</span><span class="n">cluster</span><span class="o">==</span><span class="n">j</span><span class="p">,];</span><span class="w"> 
  </span><span class="n">pbmc</span><span class="o">@</span><span class="n">meta.data</span><span class="o">$</span><span class="n">sctype_classification</span><span class="p">[</span><span class="n">pbmc</span><span class="o">@</span><span class="n">meta.data</span><span class="o">$</span><span class="n">seurat_clusters</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.character</span><span class="p">(</span><span class="n">cl_type</span><span class="o">$</span><span class="n">type</span><span class="p">[</span><span class="m">1</span><span class="p">])</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">DimPlot</span><span class="p">(</span><span class="n">pbmc</span><span class="p">,</span><span class="w"> </span><span class="n">reduction</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"umap"</span><span class="p">,</span><span class="w"> </span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">,</span><span class="w"> </span><span class="n">repel</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">,</span><span class="w"> </span><span class="n">group.by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'sctype_classification'</span><span class="p">)</span><span class="w">     
</span></code></pre></div></div>

<p><img src="/images//Intro_ScType/cluster_annotated.png" alt="cluster_annotated" /></p>

<h3 id="references">References</h3>

<p>[1] Jovic, Dragomirka, et al. “Single‐cell RNA sequencing technologies and applications: A brief overview.” <em>Clinical and translational medicine</em> 12.3 (2022).</p>

<p>[2] Ianevski, Aleksandr, Anil K. Giri, and Tero Aittokallio. “Fully-automated and ultra-fast cell-type identification using specific marker combinations from single-cell transcriptomic data.” <em>Nature communications</em> 13.1 (2022)</p>

<p>[3] https://github.com/IanevskiAleksandr/sc-type</p>

<p>[4] https://satijalab.org/seurat/articles/pbmc3k_tutorial#determine-the-dimensionality-of-the-dataset</p>

<p>[5] http://youtube.com/watch?v=huxkc2GH4lk</p>]]></content><author><name>Woncheol Shin</name></author><summary type="html"><![CDATA[Fig. Development of single-cell RNA sequencing technology. (Source: Jovic et al. 2021) Single-cell RNA sequencing (scRNA-seq) has revolutionized the fields of biology and medicine by enabling exploration of the transcriptomic profiles of individual cells. This post aims to show how to use ScType to annotate cells in R code. This post can be divided into two main parts: 1) Clustering Tutorial, which covers selecting cells (quality control), normalizing the data, identifying highly variable features, scaling the data, performing PCA, clustering the cells, and visualizing the clusters using UMAP. 2) Cell Annotation using ScType. 1. Clustering Tutorial Download the example dataset and code Run git clone or click this link to download. git clone https://github.com/wcshin-git/Cell_Annotation_with_ScType Import the required packages lapply(c("dplyr","Seurat","HGNChelper","openxlsx"), library, character.only = TRUE) If any of the packages are missing, please install them first. Load the example dataset pbmc.data &lt;- Read10X(data.dir = "filtered_gene_bc_matrices/hg19") # (32738, 2700) we will analyze the dataset of Peripheral Blood Mononuclear Cells (PBMC) freely available from 10X Genomics. There are 2,700 single cells that were sequenced on the Illumina NextSeq 500. Let’s examine pbmc.data by looking at a few genes in the first 30 cells. pbmc.data[c("CD3D", "TCL1A", "MS4A1"), 1:30] 3 x 30 sparse Matrix of class "dgCMatrix" CD3D 4 . 10 . . 1 2 3 1 . . 2 7 1 . . 1 3 . 2 3 . . . . . 3 4 1 5 TCL1A . . . . . . . . 1 . . . . . . . . . . . . 1 . . . . . . . . MS4A1 . 6 . . . . . . 1 1 1 . . . . . . . . . 36 1 2 . . 2 . . . . The . values in the matrix represent 0s. Since most values in an scRNA-seq matrix are 0, Seurat uses a sparse-matrix representation whenever possible. Setup the Seurat Object pbmc &lt;- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200) # Retain genes expressed in at least 3 cells and cells with at least 200 detected genes. # (13714, 2700) The object serves as a container that contains both data (like the count matrix) and analysis (like PCA, or clustering results) for a single-cell dataset. Quality Control # percentage of counts originating from a set of mitochondrial genes pbmc[["percent.mt"]] &lt;- PercentageFeatureSet(pbmc, pattern = "^MT-") Low-quality / dying cells often exhibit extensive mitochondrial contamination. # Visualize QC metrics as a violin plot VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3) nFeature_RNA represents the number of unique genes (features) detected in each cell. nCount_RNA represents the total number of RNA molecules detected in each cell. # Cell filtering pbmc &lt;- subset(pbmc, subset = nFeature_RNA &gt; 200 &amp; nFeature_RNA &lt; 2500 &amp; percent.mt &lt; 5) # (13714, 2638) Low-quality cells or empty droplets will often have very few genes. Cell doublets or multiplets may exhibit an aberrantly high gene count. Normalize data pbmc &lt;- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000) “LogNormalize” normalizes the feature expression measurements for each cell by the total expression, multiplies this by a scale factor, and log-transforms the result. Identification of highly variable features pbmc &lt;- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000) Focusing on these genes in downstream analysis helps to highlight biological signal in single-cell datasets. Scaling the data pbmc &lt;- ScaleData(pbmc, features = rownames(pbmc)) Shifts the expression of each gene, so that the mean expression across cells is 0. Scales the expression of each gene, so that the variance across cells is 1. Run PCA pbmc &lt;- RunPCA(pbmc, features = VariableFeatures(object = pbmc)) Perform PCA on the scaled data for dimensional reduction. # Check number of PC components ElbowPlot(pbmc, ndims=20) We will select 10 PCs for downstream analysis, based on Elbow plot. Cluster and visualize pbmc &lt;- FindNeighbors(pbmc, dims = 1:10) pbmc &lt;- FindClusters(pbmc, resolution = 0.5) pbmc &lt;- RunUMAP(pbmc, dims = 1:10) DimPlot(pbmc, label = TRUE, reduction = "umap") The resolution parameter in FindClusters() sets the ‘granularity’ of the downstream clustering, with increased values leading to a greater number of clusters. 2. Cell Annotation Now, let’s annotate the clusters with known cell types using ScType. Load required functions # load gene set preparation function source("https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/R/gene_sets_prepare.R") # load cell type annotation function source("https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/R/sctype_score_.R") Prepare marker gene sets # DB file db_ &lt;- "https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/ScTypeDB_full.xlsx"; tissue &lt;- "Immune system" # e.g. Immune system,Pancreas,Liver,Eye,Kidney,Brain,Lung,Adrenal,Heart,Intestine,Muscle,Placenta,Spleen,Stomach,Thymus # prepare gene sets gs_list &lt;- gene_sets_prepare(db_, tissue) Run ScType # check Seurat object version (scRNA-seq matrix extracted differently in Seurat v4/v5) seurat_package_v5 &lt;- isFALSE('counts' %in% names(attributes(pbmc[["RNA"]]))); print(sprintf("Seurat object %s is used", ifelse(seurat_package_v5, "v5", "v4"))) # extract scaled scRNA-seq matrix scRNAseqData_scaled &lt;- if (seurat_package_v5) as.matrix(pbmc[["RNA"]]$scale.data) else as.matrix(pbmc[["RNA"]]@scale.data) # run ScType es.max &lt;- sctype_score(scRNAseqData = scRNAseqData_scaled, scaled = TRUE, gs = gs_list$gs_positive, gs2 = gs_list$gs_negative) The score is calculated through the following process: 1) Z-score Standardization For each gene (row), the expression values across multiple cells (columns) are standardized using the z-score. 2) Calculating and Applying Marker Sensitivity Genes that appear as markers for multiple cell types are given scores closer to 0, while those that are more specific to a single cell type receive scores closer to 1. By multiplying the z-scored expression matrix by these sensitivity scores, we effectively weight each gene according to how important it is for distinguishing that particular cell type. 3) Cell-Type Scoring via Positive/Negative Markers For a given cell, the expression values (already multiplied by the gene-specificity scores) of the positive marker genes for a particular cell type are summed. To reduce biases due to varying numbers of marker genes, the total is divided by the square root of the number of marker genes. Negative markers for that cell type are also summed, but with a negative sign, and added in. The final value represents the score of that cell for that specific cell type. Assigning Cell Types to Clusters # merge by cluster cL_resutls &lt;- do.call("rbind", lapply(unique(pbmc@meta.data$seurat_clusters), function(cl){ es.max.cl = sort(rowSums(es.max[ ,rownames(pbmc@meta.data[pbmc@meta.data$seurat_clusters==cl, ])]), decreasing = !0) head(data.frame(cluster = cl, type = names(es.max.cl), scores = es.max.cl, ncells = sum(pbmc@meta.data$seurat_clusters==cl)), 10) })) sctype_scores &lt;- cL_resutls %&gt;% group_by(cluster) %&gt;% top_n(n = 1, wt = scores) # set low-confident (low ScType score) clusters to "unknown" sctype_scores$type[as.numeric(as.character(sctype_scores$scores)) &lt; sctype_scores$ncells/4] &lt;- "Unknown" We sum the ScType scores of the cells within each cluster and select the cell type with the highest score for each cluster. Visualization pbmc@meta.data$sctype_classification = "" for(j in unique(sctype_scores$cluster)){ cl_type = sctype_scores[sctype_scores$cluster==j,]; pbmc@meta.data$sctype_classification[pbmc@meta.data$seurat_clusters == j] = as.character(cl_type$type[1]) } DimPlot(pbmc, reduction = "umap", label = TRUE, repel = TRUE, group.by = 'sctype_classification') References [1] Jovic, Dragomirka, et al. “Single‐cell RNA sequencing technologies and applications: A brief overview.” Clinical and translational medicine 12.3 (2022). [2] Ianevski, Aleksandr, Anil K. Giri, and Tero Aittokallio. “Fully-automated and ultra-fast cell-type identification using specific marker combinations from single-cell transcriptomic data.” Nature communications 13.1 (2022) [3] https://github.com/IanevskiAleksandr/sc-type [4] https://satijalab.org/seurat/articles/pbmc3k_tutorial#determine-the-dimensionality-of-the-dataset [5] http://youtube.com/watch?v=huxkc2GH4lk]]></summary></entry><entry><title type="html">Introduction to Diffusion Models</title><link href="https://wcshin-git.github.io/2022/04/18/Intro_Diffusion.html" rel="alternate" type="text/html" title="Introduction to Diffusion Models" /><published>2022-04-18T02:57:00+00:00</published><updated>2022-04-18T02:57:00+00:00</updated><id>https://wcshin-git.github.io/2022/04/18/Intro_Diffusion</id><content type="html" xml:base="https://wcshin-git.github.io/2022/04/18/Intro_Diffusion.html"><![CDATA[<p>This post aims to illustrate the core mathematical concepts in diffusion models neatly.
You can also find the <a href="#lecture-note-version">lecture notes</a> I made, which are well-organized.
If you use these slides, please cite this post:)</p>

<p><em>Note: This is highly based on <a href="https://lilianweng.github.io/posts/2021-07-11-diffusion-models/#ldm">Lil’Log</a> and <a href="https://arxiv.org/abs/2006.11239">Ho et al. 2020</a>.</em></p>

<h2 id="forward-diffusion-process">Forward Diffusion Process</h2>

<p>In a nutshell, <em>diffusion models generate a sample in the dataset distribution from noise by learning the reverse process of the forward process consisting of injecting small noise into the images of the dataset.</em></p>

<p>We define a Markov chain of diffusion steps to slowly add small amount of Gaussian noise to a sample \(\mathbf{x}_0\) in \(T\) steps, producing a sequence of noisy samples \(\mathbf{x}_{1}, \ldots, \mathbf{x}_T\).</p>

<p><strong>Definition: Forward Diffusion Process</strong></p>

\[q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)=\mathcal{N}\left(\mathbf{x}_{t} ; \sqrt{1-\beta_{t}} \mathbf{x}_{t-1}, \beta_{t} \mathbf{I} \right) \quad q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)=\prod_{t=1}^{T} q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)\]

<p>where \(\mathbf{x}_0\) is a data point sampled from a real data distribution \(q(\mathbf{x}_0)\) and \(\left\{\beta_{t} \in(0,1)\right\}_{t=1}^{T}\) is a variance schedule.</p>

<p>Usually, we can afford a larger update step when the sample gets noisier, so \(\beta_{1}&lt;\beta_{2}&lt;\cdots&lt;\beta_{T}\).
<a href="https://arxiv.org/abs/2006.11239">Ho et al. 2020</a> set the forward process variances to constants increasing linearly from \(\beta_{1}=10^{-4}\) to \(\beta_{T}=0.02\).</p>

<p><strong>Property 1</strong></p>

\[q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)=\mathcal{N}\left(\mathbf{x}_{t} ; \sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0},\left(1-\bar{\alpha}_{t}\right) \mathbf{I}\right)\]

<p>where \(\alpha_{t}=1-\beta_{t}\) and \(\bar{\alpha}_{t}=\prod_{i=1}^{t} \alpha_{i}\).</p>

<p>Proof)</p>

\[\begin{array}{rlr}
\mathbf{x}_{t} &amp; =\sqrt{\alpha_{t}} \mathbf{x}_{t-1}+\sqrt{1-\alpha_{t}} \mathbf{z}_{t-1} ; \text { where } \mathbf{z}_{t-1}, \mathbf{z}_{t-2}, \cdots \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) \\
&amp; =\sqrt{\alpha_{t} \alpha_{t-1}} \mathbf{x}_{t-2}+\sqrt{1-\alpha_{t} \alpha_{t-1}} \overline{\mathbf{z}}_{t-2} \\
&amp; \quad \text { where } \overline{\mathbf{z}}_{t-2} \text { merges two Gaussians }\\
&amp; =\cdots \\
&amp; =\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}+\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t} ; \text{ where } \mathbf{\epsilon}_{t} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})\\
q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right) &amp; =\mathcal{N}\left(\mathbf{x}_{t} ; \sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0},\left(1-\bar{\alpha}_{t}\right) \mathbf{I}\right) \quad \square
\end{array}\]

<p>Eventually when \(T \rightarrow \infty, \mathbf{x}_{T}\) is equivalent to an isotropic Gaussian distribution.</p>

<h2 id="reverse-diffusion-process">Reverse Diffusion Process</h2>

<p>The main idea is, <em>“If we can reverse the above process and sample from \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\), we will be able to recreate the true sample from a Gaussian noise input, \({\mathbf{x}_{T} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})}\).”</em></p>

<p>However, we cannot easily estimate \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\) because it needs to use the entire dataset.
Therefore, we need to learn a model \(p_\theta\) to approximate these conditional probabilities!</p>

<p><strong>Definition: Reverse Diffusion Process</strong></p>

<p><em>Reverse Diffusion Process</em> is defined as a Markov chain starting at \(p_{\theta}(\mathbf{x}_{T})=\mathcal{N}\left(\mathbf{x}_{T} ; \mathbf{0}, \mathbf{I}\right)\):</p>

\[\begin{aligned}
&amp;p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \mathbf{\mu}_{\theta}\left(\mathbf{x}_{t}, t\right), \mathbf{\Sigma}_{\theta}\left(\mathbf{x}_{t}, t\right)\right)\\
&amp;p_{\theta}\left(\mathbf{x}_{0: T}\right)=p_{\theta}\left(\mathbf{x}_{T}\right) \prod_{t=1}^{T} p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)
\end{aligned}\]

<p>Note that if \(\beta_t\) is small enough, \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\) is also Gaussian.
Therefore, we define \(p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\) as a Gaussian distribution.</p>

<p>The overview of the forward and reverse process is illustrated in Fig. 1.</p>

<p><img src="/images//Intro_Diffusion/reverse_process.png" alt="Forward and Reverse Diffusion Process" />
<em>Fig. 1. Forward and reverse diffusion process. (Source: <a href="https://lilianweng.github.io/posts/2021-07-11-diffusion-models/#ldm">Lil’Log</a> which is based on <a href="https://arxiv.org/abs/2006.11239">Ho et al. 2020</a>)</em></p>

<p><strong>Property 2</strong></p>

<p>The reverse conditional probability \(q(\mathbf{x}_{t-1}\vert\mathbf{x}_t)\) is tractable when conditioned on \(\mathbf{x}_0\).</p>

\[q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right), \tilde{\beta}_{t} \mathbf{I}\right)\]

<p>where 
\(\tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right)=\frac{\sqrt{\alpha_{t}}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_{t}} \mathbf{x}_{t}+\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_{t}}{1-\bar{\alpha}_{t}} \mathbf{x}_{0} = \frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{t}\right)\) 
and 
\(\tilde{\beta}_{t}=\frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_{t}} \cdot \beta_{t}\).</p>

<p>Proof)</p>

<p>\(* \text{ Gaussian PDF}: f(\mathbf{x}|\mathbf{\mu}, \mathbf{\Sigma}) = \operatorname{det}(2 \pi \mathbf{\Sigma})^{-\frac{1}{2}} \exp \left(-\frac{1}{2}(\mathbf{x}-\mathbf{\mu})^{\top} \mathbf{\Sigma}^{-1}(\mathbf{x}-\mathbf{\mu})\right)\)
\(\begin{aligned}
&amp;q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) =q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}, \mathbf{x}_{0}\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)}=q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)} \because \text{Markov}
\\
&amp; \propto \exp \left(-\frac{1}{2}\left(\frac{\left(\mathbf{x}_{t}-\sqrt{\alpha_{t}} \mathbf{x}_{t-1}\right)^{2}}{\beta_{t}}+\frac{\left(\mathbf{x}_{t-1}-\sqrt{\bar{\alpha}_{t-1}} \mathbf{x}_{0}\right)^{2}}{1-\bar{\alpha}_{t-1}}-\frac{\left(\mathbf{x}_{t}-\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}\right)^{2}}{1-\bar{\alpha}_{t}}\right)\right) \\
&amp;=\exp \left(-\frac{1}{2}\left(\left(\frac{\alpha_{t}}{\beta_{t}}+\frac{1}{1-\bar{\alpha}_{t-1}}\right) \mathbf{x}_{t-1}^{2}-\left(\frac{2 \sqrt{\alpha_{t}}}{\beta_{t}} \mathbf{x}_{t}+\frac{2 \sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_{t-1}} \mathbf{x}_{0}\right) \mathbf{x}_{t-1}+C\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right)\right)\right)
\end{aligned}\)
where \(C\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right)\) is a function not involving \(\mathbf{x}_{t-1}\).</p>

\[\begin{flalign*}
\tilde{\beta}_{t} &amp;=1 /\left(\frac{\alpha_{t}}{\beta_{t}}+\frac{1}{1-\bar{\alpha}_{t-1}}\right)=\frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_{t}} \cdot \beta_{t} \\
\tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right) &amp;=\left(\frac{\sqrt{\alpha_{t}}}{\beta_{t}} \mathbf{x}_{t}+\frac{\sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_{t-1}} \mathbf{x}_{0}\right) /\left(\frac{\alpha_{t}}{\beta_{t}}+\frac{1}{1-\bar{\alpha}_{t-1}}\right)\\
&amp;=\frac{\sqrt{\alpha_{t}}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_{t}} \mathbf{x}_{t}+\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_{t}}{1-\bar{\alpha}_{t}} \mathbf{x}_{0} \\
&amp;=\frac{\sqrt{\alpha_{t}}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_{t}} \mathbf{x}_{t}+\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_{t}}{1-\bar{\alpha}_{t}} \frac{1}{\sqrt{\bar{\alpha}_{t}}}\left(\mathbf{x}_{t}-\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t}\right)\\
&amp; \qquad \qquad \qquad \qquad \qquad \qquad \because \mathbf{x}_{0}=\frac{1}{\sqrt{\bar{\alpha}_{t}}}\left(\mathbf{x}_{t}-\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t}\right) \text{from Prop.1}\\
&amp;=\frac{1}{\sqrt{\alpha_{t}}}\left(\mathrm{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{t}\right)  \quad \square &amp;&amp;
\end{flalign*}\]

<h2 id="learning-objective">Learning Objective</h2>

<p>Goal: We want to minimize the negative log-likelihood.</p>

\[\begin{aligned}
&amp;\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}\left[-\log p_{\theta}\left(\mathbf{x}_{0}\right)\right] \\ &amp;\leq\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}\left[-\log p_{\theta}\left(\mathbf{x}_{0}\right)+D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)\right)\right]\\
&amp;=\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}\left[-\log p_{\theta}\left(\mathbf{x}_{0}\right)+\mathbb{E}_{\mathbf{x}_{1: T} \sim q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0: T}\right) / p_{\theta}\left(\mathbf{x}_{0}\right)}\right]\right] \\
&amp;=\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}\left[-\log p_{\theta}\left(\mathbf{x}_{0}\right)+\mathbb{E}_{\mathbf{x}_{1: T} \sim q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0: T}\right)}+\log p_{\theta}\left(\mathbf{x}_{0}\right)\right]\right] \\
&amp;=\mathbb{E}_{\mathbf{x}_{0:T} \sim q(\mathbf{x}_{0: T})}\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0: T}\right)}\right] := L_{\mathrm{VLB}}
\end{aligned}\]

<p>In other words, we can achieve the goal by minimizing \(L_{\mathrm{VLB}}\)!</p>

<p><strong>Remark 1: \(L_{\mathrm{VLB}}\)</strong></p>

<p>We can convert \(L_{\mathrm{VLB}}\) to be analytically computable.</p>

\[\begin{aligned}
L_{\mathrm{VLB}}=&amp;\mathbb{E}_{q(\mathbf{x}_0)}[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{T}\right)\right)}_{L_{T}}]\\
&amp;+\sum_{t=2}^{T} \mathbb{E}_{q(\mathbf{x}_0, \mathbf{x}_t)}[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\right)}_{L_{t-1}}] \\
&amp;+\mathbb{E}_{q(\mathbf{x}_0, \mathbf{x}_1)}[\underbrace{-\log p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}_{L_0}]
\end{aligned}\]

<p>proof)</p>

\[\begin{flalign*}
&amp;L_{\mathrm{VLB}}=\mathbb{E}_{q\left(\mathbf{x}_{0: T}\right)}\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0: T}\right)}\right] \\
&amp;=\mathbb{E}_{q}\left[\log \frac{\prod_{t=1}^{T} q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)}{p_{\theta}\left(\mathbf{x}_{T}\right) \prod_{t=1}^{T} p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}\right]\\
&amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=1}^{T} \log \frac{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}\right]\\
&amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}+\log \frac{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}\right]\\
&amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=2}^{T} \log \left(\frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)} \cdot \frac{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right)}\right)+\log \frac{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}\right] \\
&amp; \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \because \text{Markov property and Bayes' rule} \\
&amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right)}+\log \frac{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}\right]\\
&amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}+\log \frac{q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}+\log \frac{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}\right]\\
&amp;=\mathbb{E}_{q}\left[\log \frac{q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{T}\right)}+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}-\log p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)\right]\\
&amp;=\mathbb{E}_{q(\mathbf{x}_0)}[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{T}\right)\right)}_{L_{T}}] +\sum_{t=2}^{T} \mathbb{E}_{q(\mathbf{x}_0, \mathbf{x}_t)}[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\right)}_{L_{t-1}}] \\
&amp; \quad +\mathbb{E}_{q(\mathbf{x}_0, \mathbf{x}_1)}[\underbrace{-\log p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}_{L_0}] &amp;&amp;
\end{flalign*}\]

<p><strong>Definition: \(L_T\), \(L_{t-1}\), and \(L_0\)</strong></p>

\[\begin{aligned}
&amp;(1) \, L_{T} =D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{T}\right)\right) \\
&amp;(2) \, L_{t-1} =D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\right) \text { for } 2 \leq t \leq T \\
&amp;(3) \, L_{0} =-\log p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)
\end{aligned}\]

<p>1) \(L_T\) <br />
\(\quad \bullet\) From Prop.1, \(q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right) \rightarrow \mathcal{N}\left(\mathbf{x}_{T} ; \mathbf{0}, \mathbf{I}\right)\) when \(T \rightarrow \infty\).<br />
\(\quad \bullet\) We assume that \(p_{\theta}(\mathbf{x}_{T})=\mathcal{N}\left(\mathbf{x}_{T} ; \mathbf{0}, \mathbf{I}\right)\).<br />
\(\quad \bullet\) \(L_T\) is constant and can be ignored during training.</p>

<p>2) \(L_{t-1}\) <br />
\(\quad \bullet\) This term measures the difference between \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)\) and \(p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\).<br />
\(\quad \bullet\) How do we optimize this term? (Explained below)</p>

<p>3) \(L_0\) <br />
\(\quad \bullet\) This term reconstruct the original image from the slightly noised image.<br />
\(\quad \bullet\) This term is optimized by MSE loss: \(\left\|\mathbf{x}_{0}-\mathbf{\mu}_{\theta}\left(\mathbf{x}_{1}, 1\right)\right\|^{2}\)</p>

<p><strong>How to optimize \(L_{t-1}\)</strong></p>

<p>\(L_{t-1} =D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\right)\)<br />
\(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right), \tilde{\beta}_{t} \mathbf{I}\right) = \mathcal{N}\left(\mathbf{x}_{t-1} ; \frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{t}\right), \frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_{t}} \beta_{t}\mathbf{I}\right)\)<br />
\(p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \mathbf{\mu}_{\theta}\left(\mathbf{x}_{t}, t\right), \mathbf{\Sigma}_{\theta}\left(\mathbf{x}_{t}, t\right)\right)\)</p>

<p>Let us set \(\mathbf{\mu}_{\theta}\left(\mathbf{x}_{t}, t\right)=\frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{\theta}(\mathbf{x}_{t}, t)\right)\) and \(\mathbf{\Sigma}_{\theta}\left(\mathbf{x}_{t}, t\right) = \sigma_{t}^{2}\mathbf{I}\).<br />
We have two options for \(\sigma_{t}^{2}\): \(\sigma_{t}^{2}=\beta_{t}\) and \(\sigma_{t}^{2}=\frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_{t}} \beta_{t}\). <br />
According to <a href="https://arxiv.org/abs/2006.11239">Ho et al. 2020</a>, both had similar results experimentally.</p>

<p>\(* D_{K L}(p \| q)=\frac{1}{2}\left[\log \frac{\left|\Sigma_{q}\right|}{\left|\Sigma_{p}\right|}-k+\left(\mathbf{\mu}_{p}-\mathbf{\mu}_{q}\right)^{T} \Sigma_{q}^{-1}\left(\mathbf{\mu}_{p}-\mathbf{\mu}_{q}\right)+\operatorname{tr}\left\{\Sigma_{q}^{-1} \Sigma_{p}\right\}\right]\)
\(\begin{aligned}
L_{t-1} &amp;\propto \frac{1}{2\sigma_{t}^{2}}\left\|\tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right)-\mathbf{\mu}_{\theta}\left(\mathbf{x}_{t}, t\right)\right\|^{2}\\
&amp;=\frac{1}{2\sigma_{t}^{2}}\left\|\frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{t}\right)-\frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{\theta}(\mathbf{x}_{t}, t)\right)\right\|^{2}\\
&amp;=\frac{\beta_{t}^{2}}{2\sigma_{t}^{2}\alpha_{t}\left(1-\bar{\alpha}_{t}\right)}\left\|\mathbf{\epsilon}_{t}-\mathbf{\epsilon}_{\theta}\left(\mathbf{x}_{t}, t\right)\right\|^{2}\\
&amp;=\frac{\beta_{t}^{2}}{2\sigma_{t}^{2}\alpha_{t}\left(1-\bar{\alpha}_{t}\right)}\left\|\mathbf{\epsilon}_{t}-\mathbf{\epsilon}_{\theta}\left(\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}+\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t}, t\right)\right\|^{2}
\end{aligned}\)</p>

<p>Empirically, <a href="https://arxiv.org/abs/2006.11239">Ho et al. 2020</a> found that training the diffusion model works better with a simplified objective that ignores the weighting term:</p>

\[L_{t-1}^{\text {simple }}=\left\|\mathbf{\epsilon}_{t}-\mathbf{\epsilon}_{\theta}\left(\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}+\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t}, t\right)\right\|^{2}\]

<h2 id="training-and-sampling-algorithm">Training and Sampling Algorithm</h2>

<p><img src="/images//Intro_Diffusion/DDPM_algorithm.png" alt="Training and Sampling Algorithm" />
<em>Fig. 2. Training and sampling algorithm. (Source: <a href="https://arxiv.org/abs/2006.11239">Ho et al. 2020</a>)</em></p>

<h2 id="generated-samples">Generated Samples</h2>

<p><img src="/images//Intro_Diffusion/progressive_generation.png" alt="Generated Samples" />
<em>Fig. 3. Unconditional CIFAR10 progressive generation. (Source: <a href="https://arxiv.org/abs/2006.11239">Ho et al. 2020</a>)</em></p>

<h2 id="lecture-note-version">Lecture Note Version</h2>

<p>You can download the <a href="/images//Intro_Diffusion/220418_Intro_Diffusion_Shin.pdf">lecture note version</a>!</p>

<p><img src="/images//Intro_Diffusion/slide1.png" alt="Example Slide 1" />
<em>Fig. 4. Example slide 1</em></p>

<p><img src="/images//Intro_Diffusion/slide2.png" alt="Example Slide 2" />
<em>Fig. 5. Example slide 2</em></p>

<h2 id="references">References</h2>

<p>[1] Weng, Lilian. (Jul 2021). What are diffusion models? Lil’Log. https://lilianweng.github.io/posts/2021-07-11-diffusion-models/.</p>

<p>[2] Jonathan Ho et al. “Denoising diffusion probabilistic models.” arxiv Preprint arxiv:2006.11239 (2020).</p>

<h2 id="citation">Citation</h2>
<p><code class="language-plaintext highlighter-rouge">
Shin, Woncheol. (April 2022). Introduction to Diffusion Models Shin’s Blog. https://wcshin-git.github.io/2022/04/18/Intro_Diffusion.html.
</code></p>

<p>Or</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>@article{shin2022intro,
  title   = "Introduction to Diffusion Models",
  author  = "Shin, Woncheol",
  journal = "wcshin-git.github.io",
  year    = "2022",
  month   = "April",
  url     = "https://wcshin-git.github.io/2022/04/18/Intro_Diffusion.html"
}
</code></pre></div></div>]]></content><author><name>Woncheol Shin</name></author><summary type="html"><![CDATA[This post aims to illustrate the core mathematical concepts in diffusion models neatly. You can also find the lecture notes I made, which are well-organized. If you use these slides, please cite this post:) Note: This is highly based on Lil’Log and Ho et al. 2020. Forward Diffusion Process In a nutshell, diffusion models generate a sample in the dataset distribution from noise by learning the reverse process of the forward process consisting of injecting small noise into the images of the dataset. We define a Markov chain of diffusion steps to slowly add small amount of Gaussian noise to a sample \(\mathbf{x}_0\) in \(T\) steps, producing a sequence of noisy samples \(\mathbf{x}_{1}, \ldots, \mathbf{x}_T\). Definition: Forward Diffusion Process \[q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)=\mathcal{N}\left(\mathbf{x}_{t} ; \sqrt{1-\beta_{t}} \mathbf{x}_{t-1}, \beta_{t} \mathbf{I} \right) \quad q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)=\prod_{t=1}^{T} q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)\] where \(\mathbf{x}_0\) is a data point sampled from a real data distribution \(q(\mathbf{x}_0)\) and \(\left\{\beta_{t} \in(0,1)\right\}_{t=1}^{T}\) is a variance schedule. Usually, we can afford a larger update step when the sample gets noisier, so \(\beta_{1}&lt;\beta_{2}&lt;\cdots&lt;\beta_{T}\). Ho et al. 2020 set the forward process variances to constants increasing linearly from \(\beta_{1}=10^{-4}\) to \(\beta_{T}=0.02\). Property 1 \[q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)=\mathcal{N}\left(\mathbf{x}_{t} ; \sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0},\left(1-\bar{\alpha}_{t}\right) \mathbf{I}\right)\] where \(\alpha_{t}=1-\beta_{t}\) and \(\bar{\alpha}_{t}=\prod_{i=1}^{t} \alpha_{i}\). Proof) \[\begin{array}{rlr} \mathbf{x}_{t} &amp; =\sqrt{\alpha_{t}} \mathbf{x}_{t-1}+\sqrt{1-\alpha_{t}} \mathbf{z}_{t-1} ; \text { where } \mathbf{z}_{t-1}, \mathbf{z}_{t-2}, \cdots \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) \\ &amp; =\sqrt{\alpha_{t} \alpha_{t-1}} \mathbf{x}_{t-2}+\sqrt{1-\alpha_{t} \alpha_{t-1}} \overline{\mathbf{z}}_{t-2} \\ &amp; \quad \text { where } \overline{\mathbf{z}}_{t-2} \text { merges two Gaussians }\\ &amp; =\cdots \\ &amp; =\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}+\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t} ; \text{ where } \mathbf{\epsilon}_{t} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})\\ q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right) &amp; =\mathcal{N}\left(\mathbf{x}_{t} ; \sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0},\left(1-\bar{\alpha}_{t}\right) \mathbf{I}\right) \quad \square \end{array}\] Eventually when \(T \rightarrow \infty, \mathbf{x}_{T}\) is equivalent to an isotropic Gaussian distribution. Reverse Diffusion Process The main idea is, “If we can reverse the above process and sample from \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\), we will be able to recreate the true sample from a Gaussian noise input, \({\mathbf{x}_{T} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})}\).” However, we cannot easily estimate \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\) because it needs to use the entire dataset. Therefore, we need to learn a model \(p_\theta\) to approximate these conditional probabilities! Definition: Reverse Diffusion Process Reverse Diffusion Process is defined as a Markov chain starting at \(p_{\theta}(\mathbf{x}_{T})=\mathcal{N}\left(\mathbf{x}_{T} ; \mathbf{0}, \mathbf{I}\right)\): \[\begin{aligned} &amp;p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \mathbf{\mu}_{\theta}\left(\mathbf{x}_{t}, t\right), \mathbf{\Sigma}_{\theta}\left(\mathbf{x}_{t}, t\right)\right)\\ &amp;p_{\theta}\left(\mathbf{x}_{0: T}\right)=p_{\theta}\left(\mathbf{x}_{T}\right) \prod_{t=1}^{T} p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right) \end{aligned}\] Note that if \(\beta_t\) is small enough, \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\) is also Gaussian. Therefore, we define \(p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\) as a Gaussian distribution. The overview of the forward and reverse process is illustrated in Fig. 1. Fig. 1. Forward and reverse diffusion process. (Source: Lil’Log which is based on Ho et al. 2020) Property 2 The reverse conditional probability \(q(\mathbf{x}_{t-1}\vert\mathbf{x}_t)\) is tractable when conditioned on \(\mathbf{x}_0\). \[q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right), \tilde{\beta}_{t} \mathbf{I}\right)\] where \(\tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right)=\frac{\sqrt{\alpha_{t}}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_{t}} \mathbf{x}_{t}+\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_{t}}{1-\bar{\alpha}_{t}} \mathbf{x}_{0} = \frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{t}\right)\) and \(\tilde{\beta}_{t}=\frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_{t}} \cdot \beta_{t}\). Proof) \(* \text{ Gaussian PDF}: f(\mathbf{x}|\mathbf{\mu}, \mathbf{\Sigma}) = \operatorname{det}(2 \pi \mathbf{\Sigma})^{-\frac{1}{2}} \exp \left(-\frac{1}{2}(\mathbf{x}-\mathbf{\mu})^{\top} \mathbf{\Sigma}^{-1}(\mathbf{x}-\mathbf{\mu})\right)\) \(\begin{aligned} &amp;q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) =q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}, \mathbf{x}_{0}\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)}=q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)} \because \text{Markov} \\ &amp; \propto \exp \left(-\frac{1}{2}\left(\frac{\left(\mathbf{x}_{t}-\sqrt{\alpha_{t}} \mathbf{x}_{t-1}\right)^{2}}{\beta_{t}}+\frac{\left(\mathbf{x}_{t-1}-\sqrt{\bar{\alpha}_{t-1}} \mathbf{x}_{0}\right)^{2}}{1-\bar{\alpha}_{t-1}}-\frac{\left(\mathbf{x}_{t}-\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}\right)^{2}}{1-\bar{\alpha}_{t}}\right)\right) \\ &amp;=\exp \left(-\frac{1}{2}\left(\left(\frac{\alpha_{t}}{\beta_{t}}+\frac{1}{1-\bar{\alpha}_{t-1}}\right) \mathbf{x}_{t-1}^{2}-\left(\frac{2 \sqrt{\alpha_{t}}}{\beta_{t}} \mathbf{x}_{t}+\frac{2 \sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_{t-1}} \mathbf{x}_{0}\right) \mathbf{x}_{t-1}+C\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right)\right)\right) \end{aligned}\) where \(C\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right)\) is a function not involving \(\mathbf{x}_{t-1}\). \[\begin{flalign*} \tilde{\beta}_{t} &amp;=1 /\left(\frac{\alpha_{t}}{\beta_{t}}+\frac{1}{1-\bar{\alpha}_{t-1}}\right)=\frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_{t}} \cdot \beta_{t} \\ \tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right) &amp;=\left(\frac{\sqrt{\alpha_{t}}}{\beta_{t}} \mathbf{x}_{t}+\frac{\sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_{t-1}} \mathbf{x}_{0}\right) /\left(\frac{\alpha_{t}}{\beta_{t}}+\frac{1}{1-\bar{\alpha}_{t-1}}\right)\\ &amp;=\frac{\sqrt{\alpha_{t}}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_{t}} \mathbf{x}_{t}+\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_{t}}{1-\bar{\alpha}_{t}} \mathbf{x}_{0} \\ &amp;=\frac{\sqrt{\alpha_{t}}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_{t}} \mathbf{x}_{t}+\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_{t}}{1-\bar{\alpha}_{t}} \frac{1}{\sqrt{\bar{\alpha}_{t}}}\left(\mathbf{x}_{t}-\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t}\right)\\ &amp; \qquad \qquad \qquad \qquad \qquad \qquad \because \mathbf{x}_{0}=\frac{1}{\sqrt{\bar{\alpha}_{t}}}\left(\mathbf{x}_{t}-\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t}\right) \text{from Prop.1}\\ &amp;=\frac{1}{\sqrt{\alpha_{t}}}\left(\mathrm{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{t}\right) \quad \square &amp;&amp; \end{flalign*}\] Learning Objective Goal: We want to minimize the negative log-likelihood. \[\begin{aligned} &amp;\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}\left[-\log p_{\theta}\left(\mathbf{x}_{0}\right)\right] \\ &amp;\leq\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}\left[-\log p_{\theta}\left(\mathbf{x}_{0}\right)+D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)\right)\right]\\ &amp;=\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}\left[-\log p_{\theta}\left(\mathbf{x}_{0}\right)+\mathbb{E}_{\mathbf{x}_{1: T} \sim q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0: T}\right) / p_{\theta}\left(\mathbf{x}_{0}\right)}\right]\right] \\ &amp;=\mathbb{E}_{\mathbf{x}_0 \sim q(\mathbf{x}_0)}\left[-\log p_{\theta}\left(\mathbf{x}_{0}\right)+\mathbb{E}_{\mathbf{x}_{1: T} \sim q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0: T}\right)}+\log p_{\theta}\left(\mathbf{x}_{0}\right)\right]\right] \\ &amp;=\mathbb{E}_{\mathbf{x}_{0:T} \sim q(\mathbf{x}_{0: T})}\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0: T}\right)}\right] := L_{\mathrm{VLB}} \end{aligned}\] In other words, we can achieve the goal by minimizing \(L_{\mathrm{VLB}}\)! Remark 1: \(L_{\mathrm{VLB}}\) We can convert \(L_{\mathrm{VLB}}\) to be analytically computable. \[\begin{aligned} L_{\mathrm{VLB}}=&amp;\mathbb{E}_{q(\mathbf{x}_0)}[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{T}\right)\right)}_{L_{T}}]\\ &amp;+\sum_{t=2}^{T} \mathbb{E}_{q(\mathbf{x}_0, \mathbf{x}_t)}[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\right)}_{L_{t-1}}] \\ &amp;+\mathbb{E}_{q(\mathbf{x}_0, \mathbf{x}_1)}[\underbrace{-\log p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}_{L_0}] \end{aligned}\] proof) \[\begin{flalign*} &amp;L_{\mathrm{VLB}}=\mathbb{E}_{q\left(\mathbf{x}_{0: T}\right)}\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0: T}\right)}\right] \\ &amp;=\mathbb{E}_{q}\left[\log \frac{\prod_{t=1}^{T} q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)}{p_{\theta}\left(\mathbf{x}_{T}\right) \prod_{t=1}^{T} p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}\right]\\ &amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=1}^{T} \log \frac{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}\right]\\ &amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}+\log \frac{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}\right]\\ &amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=2}^{T} \log \left(\frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)} \cdot \frac{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right)}\right)+\log \frac{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}\right] \\ &amp; \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \qquad \because \text{Markov property and Bayes' rule} \\ &amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right)}+\log \frac{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}\right]\\ &amp;=\mathbb{E}_{q}\left[-\log p_{\theta}\left(\mathbf{x}_{T}\right)+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}+\log \frac{q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right)}{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}+\log \frac{q\left(\mathbf{x}_{1} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}\right]\\ &amp;=\mathbb{E}_{q}\left[\log \frac{q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{T}\right)}+\sum_{t=2}^{T} \log \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)}{p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)}-\log p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)\right]\\ &amp;=\mathbb{E}_{q(\mathbf{x}_0)}[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{T}\right)\right)}_{L_{T}}] +\sum_{t=2}^{T} \mathbb{E}_{q(\mathbf{x}_0, \mathbf{x}_t)}[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\right)}_{L_{t-1}}] \\ &amp; \quad +\mathbb{E}_{q(\mathbf{x}_0, \mathbf{x}_1)}[\underbrace{-\log p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right)}_{L_0}] &amp;&amp; \end{flalign*}\] Definition: \(L_T\), \(L_{t-1}\), and \(L_0\) \[\begin{aligned} &amp;(1) \, L_{T} =D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{T}\right)\right) \\ &amp;(2) \, L_{t-1} =D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\right) \text { for } 2 \leq t \leq T \\ &amp;(3) \, L_{0} =-\log p_{\theta}\left(\mathbf{x}_{0} \mid \mathbf{x}_{1}\right) \end{aligned}\] 1) \(L_T\) \(\quad \bullet\) From Prop.1, \(q\left(\mathbf{x}_{T} \mid \mathbf{x}_{0}\right) \rightarrow \mathcal{N}\left(\mathbf{x}_{T} ; \mathbf{0}, \mathbf{I}\right)\) when \(T \rightarrow \infty\). \(\quad \bullet\) We assume that \(p_{\theta}(\mathbf{x}_{T})=\mathcal{N}\left(\mathbf{x}_{T} ; \mathbf{0}, \mathbf{I}\right)\). \(\quad \bullet\) \(L_T\) is constant and can be ignored during training. 2) \(L_{t-1}\) \(\quad \bullet\) This term measures the difference between \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)\) and \(p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\). \(\quad \bullet\) How do we optimize this term? (Explained below) 3) \(L_0\) \(\quad \bullet\) This term reconstruct the original image from the slightly noised image. \(\quad \bullet\) This term is optimized by MSE loss: \(\left\|\mathbf{x}_{0}-\mathbf{\mu}_{\theta}\left(\mathbf{x}_{1}, 1\right)\right\|^{2}\) How to optimize \(L_{t-1}\) \(L_{t-1} =D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right) \| p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)\right)\) \(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}, \mathbf{x}_{0}\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right), \tilde{\beta}_{t} \mathbf{I}\right) = \mathcal{N}\left(\mathbf{x}_{t-1} ; \frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{t}\right), \frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_{t}} \beta_{t}\mathbf{I}\right)\) \(p_{\theta}\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{t}\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \mathbf{\mu}_{\theta}\left(\mathbf{x}_{t}, t\right), \mathbf{\Sigma}_{\theta}\left(\mathbf{x}_{t}, t\right)\right)\) Let us set \(\mathbf{\mu}_{\theta}\left(\mathbf{x}_{t}, t\right)=\frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{\theta}(\mathbf{x}_{t}, t)\right)\) and \(\mathbf{\Sigma}_{\theta}\left(\mathbf{x}_{t}, t\right) = \sigma_{t}^{2}\mathbf{I}\). We have two options for \(\sigma_{t}^{2}\): \(\sigma_{t}^{2}=\beta_{t}\) and \(\sigma_{t}^{2}=\frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_{t}} \beta_{t}\). According to Ho et al. 2020, both had similar results experimentally. \(* D_{K L}(p \| q)=\frac{1}{2}\left[\log \frac{\left|\Sigma_{q}\right|}{\left|\Sigma_{p}\right|}-k+\left(\mathbf{\mu}_{p}-\mathbf{\mu}_{q}\right)^{T} \Sigma_{q}^{-1}\left(\mathbf{\mu}_{p}-\mathbf{\mu}_{q}\right)+\operatorname{tr}\left\{\Sigma_{q}^{-1} \Sigma_{p}\right\}\right]\) \(\begin{aligned} L_{t-1} &amp;\propto \frac{1}{2\sigma_{t}^{2}}\left\|\tilde{\mathbf{\mu}}_{t}\left(\mathbf{x}_{t}, \mathbf{x}_{0}\right)-\mathbf{\mu}_{\theta}\left(\mathbf{x}_{t}, t\right)\right\|^{2}\\ &amp;=\frac{1}{2\sigma_{t}^{2}}\left\|\frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{t}\right)-\frac{1}{\sqrt{\alpha_{t}}}\left(\mathbf{x}_{t}-\frac{\beta_{t}}{\sqrt{1-\bar{\alpha}_{t}}} \mathbf{\epsilon}_{\theta}(\mathbf{x}_{t}, t)\right)\right\|^{2}\\ &amp;=\frac{\beta_{t}^{2}}{2\sigma_{t}^{2}\alpha_{t}\left(1-\bar{\alpha}_{t}\right)}\left\|\mathbf{\epsilon}_{t}-\mathbf{\epsilon}_{\theta}\left(\mathbf{x}_{t}, t\right)\right\|^{2}\\ &amp;=\frac{\beta_{t}^{2}}{2\sigma_{t}^{2}\alpha_{t}\left(1-\bar{\alpha}_{t}\right)}\left\|\mathbf{\epsilon}_{t}-\mathbf{\epsilon}_{\theta}\left(\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}+\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t}, t\right)\right\|^{2} \end{aligned}\) Empirically, Ho et al. 2020 found that training the diffusion model works better with a simplified objective that ignores the weighting term: \[L_{t-1}^{\text {simple }}=\left\|\mathbf{\epsilon}_{t}-\mathbf{\epsilon}_{\theta}\left(\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}+\sqrt{1-\bar{\alpha}_{t}} \mathbf{\epsilon}_{t}, t\right)\right\|^{2}\] Training and Sampling Algorithm Fig. 2. Training and sampling algorithm. (Source: Ho et al. 2020) Generated Samples Fig. 3. Unconditional CIFAR10 progressive generation. (Source: Ho et al. 2020) Lecture Note Version You can download the lecture note version! Fig. 4. Example slide 1 Fig. 5. Example slide 2 References [1] Weng, Lilian. (Jul 2021). What are diffusion models? Lil’Log. https://lilianweng.github.io/posts/2021-07-11-diffusion-models/. [2] Jonathan Ho et al. “Denoising diffusion probabilistic models.” arxiv Preprint arxiv:2006.11239 (2020). Citation Shin, Woncheol. (April 2022). Introduction to Diffusion Models Shin’s Blog. https://wcshin-git.github.io/2022/04/18/Intro_Diffusion.html. Or @article{shin2022intro, title = "Introduction to Diffusion Models", author = "Shin, Woncheol", journal = "wcshin-git.github.io", year = "2022", month = "April", url = "https://wcshin-git.github.io/2022/04/18/Intro_Diffusion.html" }]]></summary></entry></feed>