intractable棘手的,难处理的  posterior distributions后验分布 directed probabilistic有向概率

approximate inference近似推理  multivariate Gaussian多元高斯  diagonal对角 maximum likelihood极大似然

参考:https://blog.csdn.net/yao52119471/article/details/84893634

VAE论文所在讲的问题是:

我们现在就是想要训练一个模型P(x),并求出其参数Θ:

通过极大似然估计求其参数

Variational Inference

在论文中P(x)模型会被拆分成两部分,一部分由数据x生成潜在向量z,即pθ(z|X);一部分从z重新在重构数据x,即pθ(X|z)

实现过程则是希望能够使用一个qΦ(z|X)模型去近似pθ(z|X),然后作为模型的Encoder;后半部分pθ(X|z)则作为Decoder,Φ/θ表示参数,实现一种同时学习识别模型参数φ和参数θ的生成模型的方法,推导过程为:

现在问题就在于怎么进行求导,因为现在模型已经不是一个完整的P(x) = pθ(z|X) + pθ(X|z),现在变成了P(x) = qΦ(z|X) + pθ(X|z),那么如果对Φ求导就会变成一个问题,因此论文中就提出了一个reparameterization trick方法:

取样于一个标准正态分布来采样z,以此将qΦ(z|X) 和pθ(X|z)两个子模型通过z连接在了一起

最终的目标函数为:

因此目标函数 = 输入和输出x求MSELoss - KL(qΦ(z|X) || pθ(z))

在论文上对式子最后的KL散度 -KL(qΦ(z|X) || pθ(z))的计算有简化为:

多维KL散度的推导可见:KL散度

假设pθ(z)服从标准正态分布,采样ε服从标准正态分布满足该假设

简单代码实现:

import torch
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F
import torchvision
from torchvision import transforms
import torch.optim as optim
from torch import nn
import matplotlib.pyplot as plt class Encoder(torch.nn.Module):
def __init__(self, D_in, H, D_out):
super(Encoder, self).__init__()
self.linear1 = torch.nn.Linear(D_in, H)
self.linear2 = torch.nn.Linear(H, D_out) def forward(self, x):
x = F.relu(self.linear1(x))
return F.relu(self.linear2(x)) class Decoder(torch.nn.Module):
def __init__(self, D_in, H, D_out):
super(Decoder, self).__init__()
self.linear1 = torch.nn.Linear(D_in, H)
self.linear2 = torch.nn.Linear(H, D_out) def forward(self, x):
x = F.relu(self.linear1(x))
return F.relu(self.linear2(x)) class VAE(torch.nn.Module):
latent_dim = def __init__(self, encoder, decoder):
super(VAE, self).__init__()
self.encoder = encoder
self.decoder = decoder
self._enc_mu = torch.nn.Linear(, )
self._enc_log_sigma = torch.nn.Linear(, ) def _sample_latent(self, h_enc):
"""
Return the latent normal sample z ~ N(mu, sigma^)
"""
mu = self._enc_mu(h_enc)
log_sigma = self._enc_log_sigma(h_enc) #得到的值是loge(sigma)
sigma = torch.exp(log_sigma) # = e^loge(sigma) = sigma
#从均匀分布中取样
std_z = torch.from_numpy(np.random.normal(, , size=sigma.size())).float() self.z_mean = mu
self.z_sigma = sigma return mu + sigma * Variable(std_z, requires_grad=False) # Reparameterization trick def forward(self, state):
h_enc = self.encoder(state)
z = self._sample_latent(h_enc)
return self.decoder(z) # 计算KL散度的公式
def latent_loss(z_mean, z_stddev):
mean_sq = z_mean * z_mean
stddev_sq = z_stddev * z_stddev
return 0.5 * torch.mean(mean_sq + stddev_sq - torch.log(stddev_sq) - ) if __name__ == '__main__': input_dim = *
batch_size = transform = transforms.Compose(
[transforms.ToTensor()])
mnist = torchvision.datasets.MNIST('./', download=True, transform=transform) dataloader = torch.utils.data.DataLoader(mnist, batch_size=batch_size,
shuffle=True, num_workers=) print('Number of samples: ', len(mnist)) encoder = Encoder(input_dim, , )
decoder = Decoder(, , input_dim)
vae = VAE(encoder, decoder) criterion = nn.MSELoss() optimizer = optim.Adam(vae.parameters(), lr=0.0001)
l = None
for epoch in range():
for i, data in enumerate(dataloader, ):
inputs, classes = data
inputs, classes = Variable(inputs.resize_(batch_size, input_dim)), Variable(classes)
optimizer.zero_grad()
dec = vae(inputs)
ll = latent_loss(vae.z_mean, vae.z_sigma)
loss = criterion(dec, inputs) + ll
loss.backward()
optimizer.step()
l = loss.data[]
print(epoch, l) plt.imshow(vae(inputs).data[].numpy().reshape(, ), cmap='gray')
plt.show(block=True)

VAE论文学习的更多相关文章

  1. Faster RCNN论文学习

    Faster R-CNN在Fast R-CNN的基础上的改进就是不再使用选择性搜索方法来提取框,效率慢,而是使用RPN网络来取代选择性搜索方法,不仅提高了速度,精确度也更高了 Faster R-CNN ...

  2. 《Explaining and harnessing adversarial examples》 论文学习报告

    <Explaining and harnessing adversarial examples> 论文学习报告 组员:裴建新   赖妍菱    周子玉 2020-03-27 1 背景 Sz ...

  3. 论文学习笔记 - 高光谱 和 LiDAR 融合分类合集

    A³CLNN: Spatial, Spectral and Multiscale Attention ConvLSTM Neural Network for Multisource Remote Se ...

  4. Apache Calcite 论文学习笔记

    特别声明:本文来源于掘金,"预留"发表的[Apache Calcite 论文学习笔记](https://juejin.im/post/5d2ed6a96fb9a07eea32a6f ...

  5. FactorVAE论文学习-1

    Disentangling by Factorising 我们定义和解决了从变量的独立因素生成的数据的解耦表征的无监督学习问题.我们提出了FactorVAE方法,通过鼓励表征的分布因素化且在维度上独立 ...

  6. GoogleNet:inceptionV3论文学习

    Rethinking the Inception Architecture for Computer Vision 论文地址:https://arxiv.org/abs/1512.00567 Abst ...

  7. IEEE Trans 2008 Gradient Pursuits论文学习

    之前所学习的论文中求解稀疏解的时候一般采用的都是最小二乘方法进行计算,为了降低计算复杂度和减少内存,这篇论文梯度追踪,属于贪婪算法中一种.主要为三种:梯度(gradient).共轭梯度(conjuga ...

  8. Raft论文学习笔记

    先附上论文链接  https://pdos.csail.mit.edu/6.824/papers/raft-extended.pdf 最近在自学MIT的6.824分布式课程,找到两个比较好的githu ...

  9. 论文学习-系统评估卷积神经网络各项超参数设计的影响-Systematic evaluation of CNN advances on the ImageNet

    博客:blog.shinelee.me | 博客园 | CSDN 写在前面 论文状态:Published in CVIU Volume 161 Issue C, August 2017 论文地址:ht ...

随机推荐

  1. 数据分析常用shell命令

    目录 0.vim编辑器 1.awk命令(重要) 1.1 基本语法 1.2 基本用法 1.3 运算符 1.4 内建变量 1.5 其他 1.6 awk是一门变成语言,支持条件判断.数组.循环等功能.所以我 ...

  2. 零基础如何学好python之变量

    想要自学python,变量(variable)是必经之路,它是学习python初始时,就会接触到的一个新的知识点,也是一个需要熟知的概念.python是一种动态类型语言,在赋值的执行中可以绑定不同类型 ...

  3. python_面向对象——动态创建类和isinstance和issubclass方法

    # 给动态生产的类定义一个方法 def __init__(self,name): self.name = name print(self.name) def take(self,obj): print ...

  4. 评估预测函数(3)---Model selection(选择多项式的次数) and Train/validation/test sets

    假设我们现在想要知道what degree of polynomial to fit to a data set 或者 应该选择什么features 或者 如何选择regularization par ...

  5. springboot框架笔记

    01.spring data是一个开源的框架,在这个开源的框架中spring  data  api只是其中的一个模块,只需要编写一个接口继承一个类就行了. 02.spring boot框架底层好像将所 ...

  6. 织梦cms 后台查看会员性别为空

    织梦cms会员注册选择性别为保密时,在后台会员中心查看性别为空的修改方法 第一步:找到dede/templets/member_view.htm,在引号中添加保密就ok 再次修改后的结果

  7. BZOJ 2333: [SCOI2011]棘手的操作

    题目描述 真的是个很棘手的操作.. 注意每删除一个点,就需要clear一次. #include<complex> #include<cstdio> using namespac ...

  8. codevs6003一次做对算我输

    6003 一次做对算我输 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 大师 Master       题目描述 Description 更新数据了!!!!!!!更新数据了!!!!!! ...

  9. 洛谷 P2010 回文日期 题解

    P2010 回文日期 题目描述 在日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用88位数字表示一个日期,其中,前44位代表年份,接下来22位代表月 份,最后22位代表日 ...

  10. javascript练习题

    function Vertex(city, x) { this.name = city; this.num = x; } var node0 = new Vertex("邯郸", ...