CVAE (条件 变分 自动编码器)

本文介绍了CVAE(条件变分自动编码器)的基本原理,包括模型结构、变分推断的SGVB框架以及如何优化ELBO。在实验中观察到,合适的超参数能加速收敛至优质模型,而过大或过小的参数可能导致不佳的模型性能。神经网络的学习过程呈现出先学数字轮廓后学细节的特点,且在训练过程中存在过拟合的风险。

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

notations

  • xxx image
  • zzz latent
  • yyy label (omitted to lighten notation)
  • p(x∣z)p(x|z)p(xz) decoder Encoder
  • q(z∣x)q(z|x)q(zx) encoder Decoder
  • p^(h)\hat{p}(h)p^(h) prior encoder (by variational inference) PriorEncoder

model structure

class CVAE(nn.Module):
    def __init__(self, config):
        super(CVAE, self).__init__()
        self.encoder = Encoder(...)
        self.decoder = Decoder(...)
        self.priorEncoder = PriorEncoder(...)
    def forward(self, x, y):
        x = x.reshape((-1, 784)) # MNIST
        mu, sigma = self.encoder(x, y)
        prior_mu, prior_sigma = self.priorEncoder(y)
        z = torch.randn_like(mu)
        z = z * sigma + mu
        reconstructed_x = self.decoder(z, y)
        reconstructed_x = reconstructed_x.reshape((-1, 28, 28))
        return reconstructed_x, mu, sigma, prior_mu, prior_sigma
    def infer(self, y):
        prior_mu, prior_sigma = self.priorEncoder(y)
        z = torch.randn_like(prior_mu)
        z = z * prior_sigma + prior_mu
        reconstructed_x = self.decoder(z, y)
        return reconstructed_x
#
class Loss(nn.Module):
    def __init__(self):
        super(Loss,self).__init__()
        self.loss_fn = nn.MSELoss(reduction='mean')
        self.kld_loss_weight = 1e-5
    def forward(self, x, reconstructed_x, mu, sigma, prior_mu, prior_sigma):
        mse_loss = self.loss_fn(x, reconstructed_x)
        kld_loss = torch.log(prior_sigma / sigma) + (sigma**2 + (mu - prior_mu)**2) / (2 * prior_sigma**2) - 0.5
        kld_loss 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值