网站备案空壳,python一般要学多久,重庆刚刚发布,网站维护与推广pytorch 实现git地址 论文地址#xff1a;Neural Discrete Representation Learning
1 论文核心知识点 encoder 将图片通过encoder得到图片点表征 如输入shape [32,3,32,32] 通过encoder后输出 [32,64,8,8] (其中64位输出维度) 量化码本 先随机构建一个码本#xff0c;维度…pytorch 实现git地址 论文地址Neural Discrete Representation Learning
1 论文核心知识点 encoder 将图片通过encoder得到图片点表征 如输入shape [32,3,32,32] 通过encoder后输出 [32,64,8,8] (其中64位输出维度) 量化码本 先随机构建一个码本维度与encoder保持一致 这里定义512个离散特征码本shape 为[512,64] encoder 码本中向量最近查找 encoder输出shape [32,64,8,8], 经过维度变换 shape [32 * 8 * 8,64] 在码本中找到最相近的向量并替换为码本中相似向量 输出shape [3288,64]维度变换后shape 为 [32,64,8,8] decoder 将上述数据喂给decoder还原原始图片 loss loss 包含两部分 a . encoder输出和码本向量接近 b. 重构loss,重构图片与原图片接近 2 论文实现
2.1 encoder
encoder是常用的图片卷积神经网络 输入x shape [32,3,32,32] 输出 shape [32,128,8,8]
def __init__(self, in_dim, h_dim, n_res_layers, res_h_dim):super(Encoder, self).__init__()kernel 4stride 2self.conv_stack nn.Sequential(nn.Conv2d(in_dim, h_dim // 2, kernel_sizekernel,stridestride, padding1),nn.ReLU(),nn.Conv2d(h_dim // 2, h_dim, kernel_sizekernel,stridestride, padding1),nn.ReLU(),nn.Conv2d(h_dim, h_dim, kernel_sizekernel-1,stridestride-1, padding1),ResidualStack(h_dim, h_dim, res_h_dim, n_res_layers))def forward(self, x):return self.conv_stack(x)
2.2 VectorQuantizer 向量量化层
输入 为encoder的输出z,shape : [32,64,8,8]码本维度 encoder维度变换为[2024,64]和码本embeddign shape [512,64]计算相似度相似计算使用 ( x − y ) 2 x 2 y 2 − 2 x y (x-y)^2x^2y^2-2xy (x−y)2x2y2−2xy计算和码本的相似度z_q生成 然后取码本中最相似的向量替换encoder中的向量z_1维度 得到z_q shape [2024,64],经维度变换 shape [32,64,8,8] 维度与输入z一致损失函数 使 z_q和z接近构建损失函数
decoder 层
decoder层比较简单与encoder层相反 输入x shape 【326488】 输出shape [32,3,32,32]
class Decoder(nn.Module):This is the p_phi (x|z) network. Given a latent sample z p_phi maps back to the original space z - x.Inputs:- in_dim : the input dimension- h_dim : the hidden layer dimension- res_h_dim : the hidden dimension of the residual block- n_res_layers : number of layers to stackdef __init__(self, in_dim, h_dim, n_res_layers, res_h_dim):super(Decoder, self).__init__()kernel 4stride 2self.inverse_conv_stack nn.Sequential(nn.ConvTranspose2d(in_dim, h_dim, kernel_sizekernel-1, stridestride-1, padding1),ResidualStack(h_dim, h_dim, res_h_dim, n_res_layers),nn.ConvTranspose2d(h_dim, h_dim // 2,kernel_sizekernel, stridestride, padding1),nn.ReLU(),nn.ConvTranspose2d(h_dim//2, 3, kernel_sizekernel,stridestride, padding1))def forward(self, x):return self.inverse_conv_stack(x)
2.3 损失函数
损失函数为重构损失和embedding损失之和
decoder 输出为图片重构x_hatembedding损失为encoder和码本的embedding近似损失重点decoder计算损失时由于中间有取最小值导致梯度不连续因此decoder loss 不能直接对encocer推荐进行求导采用了复制梯度的方式 z_q z (z_q - z).detach()及 for i in range(args.n_updates):(x, _) next(iter(training_loader))x x.to(device)optimizer.zero_grad()embedding_loss, x_hat, perplexity model(x)recon_loss torch.mean((x_hat - x)**2) / x_train_varloss recon_loss embedding_lossloss.backward()optimizer.step()
文章转载自: http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.pjtw.cn.gov.cn.pjtw.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.bwjws.cn.gov.cn.bwjws.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.bmmhs.cn.gov.cn.bmmhs.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.khfk.cn.gov.cn.khfk.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.nkyqh.cn.gov.cn.nkyqh.cn http://www.morning.zpyh.cn.gov.cn.zpyh.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.qpsxz.cn.gov.cn.qpsxz.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.bflwj.cn.gov.cn.bflwj.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.knjj.cn.gov.cn.knjj.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.jtsdk.cn.gov.cn.jtsdk.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.gtcym.cn.gov.cn.gtcym.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.zwckz.cn.gov.cn.zwckz.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.qywfw.cn.gov.cn.qywfw.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.dwzwm.cn.gov.cn.dwzwm.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.zsrdp.cn.gov.cn.zsrdp.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn