Auto-encoding Variational Bayes 阅读笔记

本文探讨了变分自编码器(VAE)的工作原理及其在高效推断和学习中的应用,尤其是在连续隐变量存在不可追踪后验分布以及大规模数据集的情况下的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Notation
  • pθ(z|x)pθ(z|x): intractable posterior
  • pθ(x|z)pθ(x|z): probabilistic decoder
  • qϕ(z|x)qϕ(z|x): recognition model, variational approximation to pθ(z|x)pθ(z|x), also regarded as a probabilistic encoder
  • pθ(z)pθ(x|z)pθ(z)pθ(x|z): generative model
  • ϕϕ: variational parameters
  • θθ: generative parameters
Abbreviation
  • SGVB: Stochastic Gradient Variational Bayes
  • AEVB: auto-encoding VB
  • ML: maximum likelihood
  • MAP: maximum a posteriori

Motivation

  • Problem

    • How to perform efficient inference and learning in directed probabilistic models, in the presence of continuous latent variables with intractable posterior distribution pθ(z|x)pθ(z|x) and large datasets?
  • Existing Solution and Difficulty

    • VB: involves the optimization of an approximation to the intractable posterior
    • mean-field: requires analytical solutions of expectations w.r.t. the approximate posterior, which are also intractable in the general case

Contribution of this paper

  • (1) SGVB estimator: an estimator of the variational lower bound
    • yielded by a reparameterization of the variational lower bound
    • simple & differentiable & unbiased
    • straightforwad to optimize using standard SG ascent techniques
  • (2) AEVB algorithm
    • using SGVB to optimize a recognition model that allows us to perform very efficient approximate posterior inference using simple ancestral sampling, which in turn allows us to efficiently learn the model parameters, without the need of expensive iterative inference schemes (such as MCMC) per datapoint.
    • condition: i.i.d. datasets X={x(i)}Ni=1X={x(i)}i=1N & continuous latent variable zz per datapoint

Methodology

assumption
  • directed graphical models with continuous latent variables
  • i.i.d. dataset with latent variables per datapoint
    • where we like to perform
      • ML or MAP inference on the (global) paramters θ
      • variational inference on the latent variable zz
  • pθ(z) and pθ(x|z)pθ(x|z): both PDFs are differentiable almost everywhere w.r.t. both θθ and zz
target case
  • intractability
    • pθ(x)=pθ(z)pθ(x|z)dz: so we cannot evaluate or differentiate it.
    • pθ(z|x)=pθ(x|z)pθ(z)pθ(x)pθ(z|x)=pθ(x|z)pθ(z)pθ(x): so the EM algorithm cannot be used.
    • the required integrals for any reasonable mean-field VB algorithm: so the VB algorithm cannot be used.
    • in cases of moderately complicated likelihood function, e.g. in a neural network with a nonlinear hidden layer
  • a large dataset
    • batch optimization is too costly => minibatch or single datapoints
    • sampling bases solutions are too slow, e.g. Monte Carlo EM, since it involves a typically expensive sampling loop per datapoint.
solution and application
  • efficient approximate ML or MAP estimation for θθ (Full): Appendix F
    • allow us to mimic the hidden random process and generate artificial data that resemble the real data
  • efficient approximate posterior inference pθ(z|x)pθ(z|x) for a choice of θθ
    • useful for coding or data representation tasks
  • efficient approximate marginal inference of xx: Appendix D
    • allow us to perform all kinds of inference tasks where p(x) is required, such as image denoising, inpainting, and super-resolution.
    1. derivation of the variational bound

    logpθ(x(1),x(N))=i=1Nlogpθ(x(i))log⁡pθ(x(1),⋯,x(N))=∑i=1Nlog⁡pθ(x(i))

    Here we use xixi to represent x(i)x(i)

    logp(xi)=zq(z|xi)logp(xi)dz  (q can be any distribution)=zq(z|xi)logp(z,xi)p(z|xi)dz=zq(z|xi)log[q(z|xi)p(z|xi)p(z,xi)q(z|xi)]dz=zq(z|xi)logq(z|xi)p(z|xi)dz+zq(z|xi)logp(z,xi)q(z|xi)dz=DKL[qϕ(z|xi)pθ(z|xi)]+LB(θ,ϕ;xi)log⁡p(xi)=∫zq(z|xi)log⁡p(xi)dz  (q can be any distribution)=∫zq(z|xi)log⁡p(z,xi)p(z|xi)dz=∫zq(z|xi)log⁡[q(z|xi)p(z|xi)⋅p(z,xi)q(z|xi)]dz=∫zq(z|xi)log⁡q(z|xi)p(z|xi)dz+∫zq(z|xi)log⁡p(z,xi)q(z|xi)dz=DKL[qϕ(z|xi)∥pθ(z|xi)]+LB(θ,ϕ;xi)

    Becuase DKL0DKL≥0, so LBLB is called the (variational) lower bound, then we have

    logpθ(xi)LB(θ,ϕ;xi)log⁡pθ(xi)≥LB(θ,ϕ;xi)

    LB(θ,ϕ;xi)=Eqϕ(z|x)[logpθ(x,z)logqϕ(z|x)]=zq(z|xi)logp(z,xi)q(z|xi)dz=zq(z|xi)logp(xi|z)p(z)q(z|xi)dz=DKL[q(z|xi)p(z)]+zq(z|xi)logp(xi|z)dz=DKL[qϕ(z|xi)pθ(z)]+Eqϕ(z|xi)[logpθ(xi|z)]LB(θ,ϕ;xi)=Eqϕ(z|x)[log⁡pθ(x,z)−log⁡qϕ(z|x)]=∫zq(z|xi)log⁡p(z,xi)q(z|xi)dz=∫zq(z|xi)log⁡p(xi|z)p(z)q(z|xi)dz=−DKL[q(z|xi)∥p(z)]+∫zq(z|xi)log⁡p(xi|z)dz=−DKL[qϕ(z|xi)∥pθ(z)]+Eqϕ(z|xi)[log⁡pθ(xi|z)]
    • DKL[qϕ(z|xi)pθ(z)]−DKL[qϕ(z|xi)∥pθ(z)]: act as a regularizer
    • Eqϕ(z|xi)[logpθ(xi|z)]Eqϕ(z|xi)[log⁡pθ(xi|z)]: a an expected negative reconstruction error
    • TARGET: defferentiate and optimize LBLB w.r.t. both ϕϕ and θθ. However, ϕLB∇ϕLB is problematic.
    2. Solution-1 Naive Monte Carlo gradient estimator
    • disadvantages: high variance & impractical for this purpose, 原文中出现的公式如下:
      ϕEqϕ(z)[f(z)]=Eqϕ(z)[f(z)qϕ(z)logqϕ(z)]1Ll=1Lf(z)qϕ(zl)logqϕ(zl)where zlqϕ(z|xi)∇ϕEqϕ(z)[f(z)]=Eqϕ(z)[f(z)∇qϕ(z)log⁡qϕ(z)]≃1L∑l=1Lf(z)∇qϕ(zl)log⁡qϕ(zl)where zl∼qϕ(z|xi)

    但由于我还没来得及学Monte Carlo Gradient Estimator的理论,根据VAE这边论文后面的公式,个人觉得…上面的公式应该是(数学功底不够深厚,不确定二者是否等价):

    ϕEqϕ(z)[f(z)]=Eqϕ(z)[f(z)qϕ(z)logqϕ(z)]1Ll=1Lf(zl)where zlqϕ(z|xi)∇ϕEqϕ(z)[f(z)]=Eqϕ(z)[f(z)∇qϕ(z)log⁡qϕ(z)]≃1L∑l=1Lf(zl)where zl∼qϕ(z|xi)
    3. Solution-2 SGVB estimator
    • reparamterize z̃ qϕ(z|x)z~∼qϕ(z|x) using a differentiable transformation gϕ(ϵ,x)gϕ(ϵ,x) of an (auxiliary) noise variable ϵϵ
      • under certain mild conditions for a chosen approximate posterior qϕ(z|x)qϕ(z|x)
        z̃ =gϕ(ϵ,x) with ϵp(ϵ)z~=gϕ(ϵ,x) with ϵ∼p(ϵ)
    • form Monte Carlo estimates as follows:
      qϕ(z|x)idzi=p(ϵ)idϵiqϕ(z|x)f(z)dz=p(ϵ)f(z)dϵ=p(ϵ)f(gϕ(ϵ,x))dϵEqϕ(z|xi)[f(z)]=Ep(ϵ)[f(gϕ(ϵ,xi))]1Ll=1Lf(gϕ(ϵl,xi))where ϵlp(ϵ)qϕ(z|x)∏idzi=p(ϵ)∏idϵi∫qϕ(z|x)f(z)dz=∫p(ϵ)f(z)dϵ=∫p(ϵ)f(gϕ(ϵ,x))dϵEqϕ(z|xi)[f(z)]=Ep(ϵ)[f(gϕ(ϵ,xi))]≃1L∑l=1Lf(gϕ(ϵl,xi))where ϵl∼p(ϵ)
    • apply the MC estimator technique to LB(θ,ϕ;xi)LB(θ,ϕ;xi), yielding 2 SGVB estimator L̃ AL~A and L̃ BL~B:
      L̃ A(θ,ϕ;xi)=1Ll=1Llogpθ(xi,zi,l)logqϕ(zi,l|xi)L̃ B(θ,ϕ;xi)=DKL(qϕ(z|xi)pθ(z))+1Ll=1Llogpθ(xi|zi,l)where zi,l=gϕ(ϵi,l,xi) and ϵlp(ϵ)L~A(θ,ϕ;xi)=1L∑l=1Llog⁡pθ(xi,zi,l)−log⁡qϕ(zi,l|xi)L~B(θ,ϕ;xi)=−DKL(qϕ(z|xi)∥pθ(z))+1L∑l=1Llog⁡pθ(xi|zi,l)where zi,l=gϕ(ϵi,l,xi) and ϵl∼p(ϵ)
    • minibatch with size MM
      LB(θ,ϕ;X)L~M(θ,ϕ;XM)=NMi=1ML~(θ,ϕ;xi)

      • the number of samples L per datapoint can be set to 1 as long as the minibatch size MM was large enough, e.g. M=100
    3.1 Minibatch version of AEVB algorithm
    • θ,ϕθ,ϕ← Initialize parameters
    • repeate
      • XMXM← Random minibatch of MM datapoints
      • ϵ Random samples from noise distribution p(ϵ)p(ϵ)
      • gθ,ϕL̃ M(θ,ϕ;XM,ϵ)g←∇θ,ϕL~M(θ,ϕ;XM,ϵ)
      • θ,ϕθ,ϕ← Update parameters using gradients gg (e.g. SGD or Adagrad)
    • until convergence of parameters θ,ϕ
    • return θ,ϕθ,ϕ
    chosen of qϕ(z|x)qϕ(z|x), p(ϵ)p(ϵ), and gϕ(ϵ,x)gϕ(ϵ,x)

    There are three basic approaches:

    • Tractable inverse CDF. Let ϵU(0,I)ϵ∼U(0,I), gϕ(ϵ,x)gϕ(ϵ,x) be the inverse CDF of qϕ(z|x)qϕ(z|x).
      • Examples: Exponential, Cauchy, Logistic, Rayleigh, Pareto, Weibull, Reciprocal, Gompertz, Gumbel and Erlang distributions.
    • ”location-scale” family of distributions: choose the standard distribution (with location = 0, scale = 1) as the auxiliary variable ϵϵ, and let g()=location+scaleϵg(⋅)=location+scale⋅ϵ
      • Examples: Laplace, Elliptical, Student’s t, Logistic, Uniform, Triangular and Gaussian distributions.
      • 下文介绍的VAE即适用于此种情况
    • Composition: It is often possible to express random variables as different transformations of auxiliary variables.
      • Examples: Log-Normal (exponentiation of normally distributed variable), Gamma (a sum over exponentially distributed variables), Dirichlet (weighted sum of Gamma variates), Beta, Chi-Squared, and F distributions.

    Variational Auto-Encoder

    • let pθ(z)=N(z;0,I)pθ(z)=N(z;0,I)
    • pθ(z|x)pθ(z|x) is intractable
    • use a neural network for qϕ(z|x)qϕ(z|x)
    • ϕϕ and θθ are optimized jointly with the AEVB algorithm
    • params of pθ(x|z)pθ(x|z) are computed from zz with a MLP (multi-layered perceptrons, a fully-connected neural network with a hidden layer)
      • multivariae Gaussian: in case of real-valued data
        logp(x|z)=logN(x;μ,σ2I)where μ=Wμh+bμlogσ2=Wσh+bσh=tanh(Whz+bh)
      • Bernouli: incase of binary data
        logp(x|z)=i=1Dxilogyi+(1xi)log(1yi)where y=fσ(Wytanh(Wh+bh)+by)fσ():elementwise sigmoid activation functionlog⁡p(x|z)=∑i=1Dxilog⁡yi+(1−xi)⋅log⁡(1−yi)where y=fσ(Wytanh⁡(Wh+bh)+by)fσ(⋅):elementwise sigmoid activation function

        VAE
        VAE-NN

    From what mentioned above, we have:

    logqϕ(z|xi)=logN(z;μi,σ2,iI)log⁡qϕ(z|xi)=log⁡N(z;μi,σ2,iI)

    where μiμi and σiσi are outputs of the encoding MLP.

    We sample form zi,lqϕ(z|xi)zi,l∼qϕ(z|xi) using zi,l=gϕ(xi,ϵl)=μi+σiϵlzi,l=gϕ(xi,ϵl)=μi+σi⊙ϵl, where ϵlN(0,I)ϵl∼N(0,I) and denotes element-wize product.

    Here both pθ(z)pθ(z) and qϕ(z|x)qϕ(z|x) are Gaussian, so we can use the estimator L̃ BL~B, since the KL divergence item is analytical. Then we have:

    L(θ,ϕ;xi)DKL(qϕ(z|xi)pθ(z))+1Ll=1Llogpθ(xi|zi,l)12j=1L(1+log(σi2j)μi2jσi2j)+1Ll=1Llogpθ(xi|zi,l)where zil=μi+σiϵl and ϵlN(0,I)L(θ,ϕ;xi)≃−DKL(qϕ(z|xi)∥pθ(z))+1L∑l=1Llog⁡pθ(xi|zi,l)≃12∑j=1L(1+log⁡(σji2)−μji2−σji2)+1L∑l=1Llog⁡pθ(xi|zi,l)where zil=μi+σi⊙ϵl and ϵl∼N(0,I)
    Solution of DKL(qϕ(z)pθ(z))−DKL(qϕ(z)∥pθ(z)), Gaussian case

    Let JJ be the dimensionality of z, then we have:

    qθ(z)logpθ(z)dz=N(z;μ,σ2)logN(z;0,I)dz=J2log(2π)12j=1J(μ2j+σ2j)∫qθ(z)log⁡pθ(z)dz=∫N(z;μ,σ2)log⁡N(z;0,I)dz=−J2log⁡(2π)−12∑j=1J(μj2+σj2)

    And:

    qθ(z)logqθ(z)dz=N(z;μ,σ2)logN(z;μ,σ2)dz=J2log(2π)12j=1J(1+logσ2j)∫qθ(z)log⁡qθ(z)dz=∫N(z;μ,σ2)log⁡N(z;μ,σ2)dz=−J2log⁡(2π)−12∑j=1J(1+log⁡σj2)

    Therefore:

    DKL(qϕ(z)pθ(z))=qθ(z)(logpθ(z)logqθ(z))dz=12j=1L(1+log(σi2j)μi2jσi2j)−DKL(qϕ(z)∥pθ(z))=∫qθ(z)(log⁡pθ(z)−log⁡qθ(z))dz=12∑j=1L(1+log⁡(σji2)−μji2−σji2)

    此处附上相关证明:
    VAE证明

    Visulization

    Since the prior of the latent space is Gaussian, linearly spaced coordinates on the unit square were transformed through the inverse CDF of the Gaussian to produce values of the latent variables zz.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值