Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

statduck

Variational Inference 본문

Machine Learning

Variational Inference

statduck 2023. 8. 17. 09:01

목적 VI 그간 추상적으로 알고있었는데, 실제 파라미터 업데이트 및 사용하는 이유 및 맥락을 살펴보기 위함

정의

q=argminqQDKL(q(z)||pθ(z|x))

ψ=argminψDKL(qψ(z)||pθ(z|x))=argminψEqψ(z)[logqψ(z)log(pθ(x|z)pθ(z)pθ(x))]=argminψEqψ(z)[logqψ(z)logpθ(x|z)logpθ(z)]L(θ,ψ|x)+logpθ(x)

  • known variables x
  • unknown(latent) variables z
  • fixed parameter θ
  • pθ(z|x): original posterior
  • q(z): surrogate distribution

기존에는 q를 찾는건데, paremetric family Q를 하나 고른다고 가정하고, 해당 패밀리에 속하는 모수 ψ (known as variational parameters)를 찾는 식으로 바꾼다. (분포 가정해야 모수개수가 제한된 곳에서 파라미터 찾을 수 있음)

 

이 때 위 식에서 loss인 L을 최소화 하는게 목표가 될 것이다. 그리고 

L(θ,ψ|x)=Eqψ(z)[logpθ(x,z)+logqψ(z)]=negativeELBO(L)

(L for ELBO, L for loss)

 

DKL0 -> 여기서 ELBO가 expected 

 

결국 다음의 DGM(Directed Graphical Model)을 알고 있다는 가정하에 시작하는데 아래 그래프 구조를 알고있음은 다음 수식을 알고있음과 동일하다. p(X,Z)=p(X)×p(Z|X)

 

Parameter Estimation: Variational EM

θ를 모르는 경우 MLE로 이를 추정하려고 한다.(하지만 계산 불가능: incompuatble, intractable)

logp(D|θ)=Nn=1logp(xn|θ)L 

 

우리가 가정하는 latent variable model에서는 θMLE를 어떻게 구할까?

p(D,z1:N|θ)=ΠNn=1p(zn|θ)p(xn|zn,θ)

위 구조에서 local latent variables zn가 hidden이므로 우리가 p(θ|D를 구할 때는 marginze out(적분으로 해당변수 없애주기) 해주어야 한다. 즉 다음과 같이 local log marginal likelihood를 구해야한다.

logp(xnθ)=log[p(xn|zn,θ)p(zn|θ)dzn]

 

하지만 이 적분계산도 마찬가지로 intractable(상식적으로 z1,...,zn 적분 다 해야하는데 계산가능할리가 없음) 하다(Exact posterior normalization과 동일함)

 

L(θ,ψ1:N|D)=≡Nn=1L(θ,ψn|xn)logp(D|θ)

그래서 이러한 optimization 문제로 풀어보자는 것이 variational EM algorithm이다. 

N개의 latent vector : N개의 prior params ψ1:N

  • E step maximizing the ELBO wrt ψn (the variational params)
  • M step maximizing the ELBO wrt θ (model params)

속도 향상을 위해 1)Stochastic VI 2) Amortized VI를 활용할 수 있다.

 

EM 알고리즘은 실제 Z값을 얻을 수 없는 경우 Z에 대한 기댓값, 즉 Ez[lnp(X,Z|θ)]를 이용해 다음을 iterative하게 푸는 것이다. Ez[lnp(X,Z|θ)]=zp(Z|X,θ)posterior×lnp(X,Z|θ)likelihood

  • E step Q(θ,θold)=zp(Z|X,θold)lnp(X,Z|θ)
  • M step θnew=argmaxθQ(θ,θold)

1) Stochastic VI

- 데이터 크기(N)가 큰 경우 학습 속도가 느리다. 따라서 랜덤한 미니배치 B=|B| 를 데이터에서 뽑는다

L(θ,ψ1:N|D)=Nn=1L(θ,ψn|xn)

 

2) Amortized VI

https://ricoshin.tistory.com/3

 

Amortized Inference란?

https://www.quora.com/What-is-amortized-variational-inference What is amortized variational inference? Answer: Let me briefly describe the setting first, as the best way to understand amortized variational inference (in my opinion) is in the context of reg

ricoshin.tistory.com

 

 결국 Inference Network의 파라미터로 학습시킬 파라미터를 바꾼 것이라고 볼 수 있다. zn을 조절하는 파라미터 ψn을 찾아야하는 문제에서 데이터 xn에서 zn을 추론하는 ϕ를 찾는 문제로 바뀌었다. 이를 통해 매번 새로운 x가 들어올때(z가 생길 때) 일일이 파라미터를 학습해줄 필요없이 미리 업데이트 한 ϕ로 추정할 수 있는 장점 획득

 

q(zn|ψn)=q(zn|finfπ(xn))=qϕ(zn|xn)

L(θ,ϕ|D)=Nn=1[Eqϕ(zn|xn)[logpθ(xn,zn)logqϕ(z|xn)]]

 

만약 이를 Stochastic VI와 합치면(미니배치 사이즈 1) 다음과 같다.

L(θ,ϕ|xn)N[Eqϕ(zn|xn)[logpθ(xn,zn)logqϕ(z|xn)]]

 

Algorithm 10.1: Amortized stochastic variational EM

  1. Initialize θ,ϕ
  2. repeat
    1. Sample xnpD
    2. E step ϕ=argmaxϕL(θ,ϕ|xn)
    3. M step θ=argmaxθL(θ,ϕ|xn)
  3. until converged

이 때 M step에서 θ를 업데이트 하는건 gradient update로 가능하지만 E step에서 ϕ를 업데이트 하는게 문제인데, 위의 로스 L에서 이미 ϕ를 이용하고 있기 때문에 Expectation, gradient 자리 이동이 불가하다.(Gradient-Based VI에서 살펴보자)

 

 

Gradient-Based VI

 1) choose convenient form of qπ(z) 2) optimize the ELBO using gradient based method

 

θL(θ,ϕ|x)=θEqϕ(z|x)[logpθ(x,z)logqϕ(z|x)]=Eqϕ(z|x)[θ{logpθ(x,z)logqϕ(z|x)}]θlogpθ(x,zs))

 

where zsqϕ(z|x)

 

θL(θ,ϕ|x)=ϕEqϕ(z|x)[logpθ(x,z)logqϕ(z|x)]Eqϕ(z|x)[ϕ{logpθ(x,z)logqϕ(z|x)}]

 

이 문제를 해결하기 위해 1) reparmetrization trick 혹은 2) blackbox VI 를 이용한다.

 

Reparameterized VI

목적: latent variable zqθ(z|X) 분포에 대해 gradient 계산을 하기 위해 기존 파라미터에 대해 변수변환을 한다.

변수변환을 통해 궁극적으로 얻고자 하는건 Exchange the position between 'Gradient' and 'Expectation' 이다.

왜냐하면 결국 Expectation이 밖으로 빠져나와야지 몬테카를로 estimation으로 우리가 근사할 수 있기 때문이다. 

 

Change Of Variable 과정(zϵ)

zqϕ(z|x)N(μ,σ2)

zlet=g(ϕ,ϵ)=μ+σϵ(s.t.ϵN(0,1))

pϵ(ϵ)=fz(g(ϕ,ϵ))zϵ=12πσexp(ϵ2/2)σ=12πexp(ϵ2/2)

 

So ϵ does not depend on ϕ

Finally, This let the exchange the position between and E is able.

Eqϕ(z|x)[f(z)]=Ep(ϵ)[f(z)]

This let us propagate graidents back through the f function.

 

Gaussian Example (Cov 모양에 따라 3가지 경우로 나뉨) 결론은 reparameterized trick을 적용하기 위해 변수변환을 어떻게 할 것이냐의 문제이다.

Diagonal Covariance

ϵN(0,I) 

z=μ+σϵ

 

Full Covariance

ϵN(0,I)

z=μ+Lϵ

=LLT, where L is a lower triangular matrix with non-zero entries on the diagonal.

 

low-rank plus diagonal Covariance

=BBT+C2

(Cholesky decomposition)

 

Automatic differentiation VI

https://arxiv.org/pdf/1603.00788.pdf

  • Transform the support of latent variable into real coordinate space
  • Compute the ELBO for any model using MC integration
  • Stochastic Gradient Ascent to maximize the ELBO and use automatic differentiation

T:ΘRD : maps from the constrained space Θ to the unconstrained space RD

 예를 들어 Normal의 분산의 경우에는 0이상이어야하는데, 이러한 파라미터 스페이스를 실수 공간으로 확장시켜주는게 T이다.

latent variable에 대해 u=T(z)라고 하고 GVI(가우시안 VI)을 적용하면 qψ(u)=N(u|μd,Σ),ψ=(μ,Σ)

변수변환에 의해 다음과 같이 바뀐다.

p(u)=p(T1(u))|det(JT1(u))|

이제부터 variational parameter는 u이다. 그래서 ELBO도 u에 대해 정의가 되어야 한다.

L(ψ)=Euqψ(u)[logp(D|T1(u))+logp(T1(u))+log|det(JT1(u))|]+H(ψ)

 

Blackbox Variational Inference

˜L(ψ,z)=logp(z,x)logqψ(z)

 

L(θ)=Eqθ(z)[˜L(θ,z)]

 

 

Coordinate ascent VI

qψ(z)=ΠJj=1qj(zj)

This is called the mean field approximation