版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
神经网络与深度学习第8章深度生成模型8.1判别模型与生成模型8.2自编码器8.2.1自编码器的结构与原理8.2.2自编码器的应用8.3变分自编码器8.3.1变分自编码器的结构与原理8.3.2变分自编码器的应用8.4生成对抗网络8.4.1生成对抗网络的结构与原理8.4.2CycleGAN8.4.3利用CycleGAN实现图像风格迁移8.1判别模型与生成模型判别模型(discriminativemodel)一般用于对现有样本点进行分类(例如“蜜蜂”和“蚂蚁”的图像分为各自的类别),主要属于监督学习。生成模型(generativemodel)试图理解数据集结构并生成类似的示例(例如创建“蜜蜂”和“蚂蚁”的逼真图像),它大多属于无监督和半监督学习。8.1判别模型与生成模型
是否有翅膀(a)判别模型身体直径
是否有翅膀(b)生成模型身体直径
8.1判别模型与生成模型
是否有翅膀(a)判别模型身体直径
是否有翅膀(b)生成模型身体直径
8.2自编码器自编码器的结构与原理自编码器通过接收数据、压缩和编码(coding)数据,然后再从编码表示中重构数据来实现数据编码和解码(decoding)操作;通过对模型进行无监督训练,使损失最小化,重构结果能精确地再现数据,从而使模型最终可以学习到数据的重要特征。8.2自编码器自编码器的结构与原理一个自编码器由编码器(coder)、编码(code)和解码器(decoder)三部分组成。编码部分宽度最小,所以一般又叫瓶颈(bottleneck),通常也称为潜变量、隐变量或者潜在表示。将编码看作向量,将其张成的空间叫隐空间,或者潜在空间。
编码器encoder编码code解码器decoder输入层输出层8.2自编码器自编码器的结构与原理编码器部分通常是前馈、密集连接的网络,其目的是获取输入数据并将其压缩为隐空间表示,从而生成具有降低维数的数据的新表示。编码处理数据的压缩表示形式,该部分经过精心设计,以确定观察到的是数据中最相关的部分,或者对于对数据重建最重要的特征。其目标是确定需要保留数据的哪些方面,丢弃哪些方面。
编码器encoder编码code解码器decoder输入层输出层8.2自编码器自编码器的结构与原理解码器负责获取压缩数据并将其转换回与原始未更改数据具有相同维度的表示形式,编码器与解码器在结构上对称。整个自动编码器最基本的架构是前馈架构,学习通过使用反向传播进行,模型的损失函数一般通过重构数据和原始数据的差异程度度量,称为重构误差。
编码器encoder编码code解码器decoder输入层输出层8.2自编码器自编码器的应用在MNIST数据集上精心编码和解码重构。编码器共3层,按照784
512
2的顺序降低数据的维度,其中输入节点数为784(=28*28),这些节点在隐空间中编码为2个结点。解码器则按照和编码器正好相反的顺序增加数据维度,直至原始输入大小,具体为2
512
784,其中输入是2结点隐空间表示,输出是28*28的重构输入。8.2自编码器自编码器的应用编码器定义#编码器3层,28*28
512
2classEncoder(nn.Module):
def__init__(self,latent_dims):super(Encoder,self).__init__()
self.linear1=nn.Linear(784,512)
self.linear2=nn.Linear(512,latent_dims)
defforward(self,x):x=torch.flatten(x,start_dim=1)x=F.relu(self.linear1(x))
return
self.linear2(x)8.2自编码器自编码器的应用解码器定义#解码器3层,2
512
28*28classDecoder(nn.Module):
def__init__(self,latent_dims):super(Decoder,self).__init__()
self.linear1=nn.Linear(latent_dims,512)
self.linear2=nn.Linear(512,784)
defforward(self,z):z=F.relu(self.linear1(z))z=torch.sigmoid(self.linear2(z))
returnz.reshape((-1,1,28,28))8.2自编码器自编码器的应用自编码器定义#自编码器由编码器和解码器组成#共5层,28*28
512
2
512
28*28classAutoencoder(nn.Module):
def__init__(self,latent_dims):super(Autoencoder,self).__init__()
self.encoder=Encoder(latent_dims)
self.decoder=Decoder(latent_dims)
defforward(self,x):z=self.encoder(x)
return
self.decoder(z)8.2自编码器自编码器的应用实例化及训练#隐空间维度latent_dims=2#实例化模型autoencoder=Autoencoder(latent_dims).to(device)#GPUdeftrain(autoencoder,data,epochs=20):opt=torch.optim.Adam(autoencoder.parameters())
forepochinrange(epochs):
forx,yindata:x=x.to(device)#GPUopt.zero_grad()x_hat=autoencoder(x)loss=((x-x_hat)**2).sum()loss.backward()opt.step()
returnautoencoder
#训练autoencoder=train(autoencoder,data)8.2自编码器自编码器的应用输入数据的潜空间分布#展现输入数据的潜空间分布defplot_latent(autoencoder,data,num_batches=100):
fori,(x,y)inenumerate(data):z=autoencoder.encoder(x.to(device))z=z.to('cpu').detach().numpy()plt.scatter(z[:,0],z[:,1],c=y,cmap='tab10')
ifi>num_batches:plt.colorbar()
break
plot_latent(autoencoder,data)8.2自编码器自编码器的应用生成(重构图像)defplot_reconstructed(autoencoder,r0=(-5,10),r1=(-10,5),n=12):w=28img=np.zeros((n*w,n*w))
fori,yinenumerate(np.linspace(*r1,n)):
forj,xinenumerate(np.linspace(*r0,n)):z=torch.Tensor([[x,y]]).to(device)x_hat=autoencoder.decoder(z)x_hat=x_hat.reshape(28,28).to('cpu').detach().numpy()img[(n-1-i)*w:(n-1-i+1)*w,j*w:(j+1)*w]=x_hatplt.imshow(img,extent=[*r0,*r1])
plot_reconstructed(autoencoder)隐空间中存在“间隙”,即不连续区域,数据从未映射到这些“间隙”。如果我们从隐空间的某个区域采样一个潜在向量,而解码器在训练过程中从未看到这个区域,那么输出可能根本没有任何意义。8.3变分自编码器变分自编码器的结构与原理变分自编码器的基本思想是隐变量不是确定的向量,而是一个多元随机变量,该随机变量能反映真实数据的分布。变分自编码器工作时,将输入映射到隐空间中的概率分布,然后从该分布中采样隐变量,并生成输出数据。自编码器变分自编码器编码编码8.3变分自编码器
编码
采样8.3变分自编码器
编码
采样8.3变分自编码器
8.3变分自编码器
8.3变分自编码器变分自编码器的应用变分自编码器除了一些特殊构件,即隐变量分布的均值向量、标准差向量、隐变量采样等,需要单独定义外,其他模型结构与自编码器一致。8.3变分自编码器变分自编码器的应用使用变分自编码器来实现MNIST数据集上的图像生成任务。模型定义classVariationalEncoder(nn.Module):
def__init__(self,latent_dims):super(VariationalEncoder,self).__init__()
self.linear1=nn.Linear(784,512)
self.linear2=nn.Linear(512,latent_dims)
self.linear3=nn.Linear(512,latent_dims)
#标准正态分布
self.N=torch.distributions.Normal(0,1)
self.N.loc=self.N.loc.to(device)
self.N.scale=self.N.scale.to(device)
self.kl=0
defforward(self,x):x=torch.flatten(x,start_dim=1)x=F.relu(self.linear1(x))#隐变量分布均值向量mu=self.linear2(x)#隐变量分布方差向量,采用指数激活函数sigma=torch.exp(self.linear3(x))#隐变量采样h=mu+sigma*self.N.sample(mu.shape)#KL散度计算
self.kl=(sigma**2+mu**2-torch.log(sigma)-1/2).sum()
returnh
#解码器与AE的相同classDecoder(nn.Module):
def__init__(self,latent_dims):super(Decoder,self).__init__()
self.linear1=nn.Linear(latent_dims,512)
self.linear2=nn.Linear(512,784)
defforward(self,h):h=F.relu(self.linear1(h))h=torch.sigmoid(self.linear2(h))returnh.reshape((-1,1,28,28))
#定义变分自编码器类classVariationalAutoencoder(nn.Module):
def__init__(self,latent_dims):super(VariationalAutoencoder,self).__init__()
self.encoder=VariationalEncoder(latent_dims)
self.decoder=Decoder(latent_dims)
defforward(self,x):h=self.encoder(x)
return
self.decoder(h)8.3变分自编码器变分自编码器的应用使用变分自编码器来实现MNIST数据集上的图像生成任务。模型定义
defforward(self,x):x=torch.flatten(x,start_dim=1)x=F.relu(self.linear1(x))#隐变量分布均值向量mu=self.linear2(x)#隐变量分布方差向量,采用指数激活函数sigma=torch.exp(self.linear3(x))#隐变量采样h=mu+sigma*self.N.sample(mu.shape)#KL散度计算
self.kl=(sigma**2+mu**2-torch.log(sigma)-1/2).sum()
returnh
#解码器与AE的相同classDecoder(nn.Module):
def__init__(self,latent_dims):super(Decoder,self).__init__()
self.linear1=nn.Linear(latent_dims,512)
self.linear2=nn.Linear(512,784)
defforward(self,h):h=F.relu(self.linear1(h))h=torch.sigmoid(self.linear2(h))returnh.reshape((-1,1,28,28))
#定义变分自编码器类classVariationalAutoencoder(nn.Module):
def__init__(self,latent_dims):super(VariationalAutoencoder,self).__init__()
self.encoder=VariationalEncoder(latent_dims)
self.decoder=Decoder(latent_dims)
defforward(self,x):h=self.encoder(x)
return
self.decoder(h)8.3变分自编码器变分自编码器的应用使用变分自编码器来实现MNIST数据集上的图像生成任务。模型定义#解码器与AE的相同classDecoder(nn.Module):
def__init__(self,latent_dims):super(Decoder,self).__init__()
self.linear1=nn.Linear(latent_dims,512)
self.linear2=nn.Linear(512,784)
defforward(self,h):h=F.relu(self.linear1(h))h=torch.sigmoid(self.linear2(h))returnh.reshape((-1,1,28,28))
#定义变分自编码器类classVariationalAutoencoder(nn.Module):
def__init__(self,latent_dims):super(VariationalAutoencoder,self).__init__()
self.encoder=VariationalEncoder(latent_dims)
self.decoder=Decoder(latent_dims)
defforward(self,x):h=self.encoder(x)
return
self.decoder(h)8.3变分自编码器变分自编码器的应用使用变分自编码器来实现MNIST数据集上的图像生成任务。模型定义#定义变分自编码器类classVariationalAutoencoder(nn.Module):
def__init__(self,latent_dims):super(VariationalAutoencoder,self).__init__()
self.encoder=VariationalEncoder(latent_dims)
self.decoder=Decoder(latent_dims)
defforward(self,x):h=self.encoder(x)
return
self.decoder(h)8.3变分自编码器变分自编码器的应用使用变分自编码器来实现MNIST数据集上的图像生成任务。模型训练#隐空间维度latent_dims=2#实例化模型vae=VariationalAutoencoder(latent_dims).to(device)#GPU8.3变分自编码器变分自编码器的应用使用变分自编码器来实现MNIST数据集上的图像生成任务。模型训练#训练函数deftrain(autoencoder,data,epochs=20):opt=torch.optim.Adam(autoencoder.parameters())
forepochinrange(epochs):
forx,yindata:x=x.to(device)#GPUopt.zero_grad()x_hat=autoencoder(x)loss=((x-x_hat)**2).sum()+autoencoder.encoder.klloss.backward()opt.step()
returnautoencoder
#训练vae=train(vae,data)8.3变分自编码器变分自编码器的应用使用变分自编码器来实现MNIST数据集上的图像生成任务。从输入空间到隐空间的映射关系#输入数据的潜空间分布defplot_latent(autoencoder,data,num_batches=100):
fori,(x,y)inenumerate(data):h=autoencoder.encoder(x.to(device))h=h.to('cpu').detach().numpy()plt.scatter(h[:,0],h[:,1],c=y,cmap='tab10')
ifi>num_batches:plt.colorbar()
break
plot_latent(vae,data)8.3变分自编码器变分自编码器的应用使用变分自编码器来实现MNIST数据集上的图像生成任务。重构图像#从隐空间重构输出defplot_reconstructed(autoencoder,r0=(-5,10),r1=(-10,5),n=12):
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年昆明市粮油购销有限责任公司人员招聘考试题库及答案详解
- 2026年中建新疆建工集团有限公司人员招聘考试参考试题及答案详解
- 2026年重庆园林绿化建设投资集团有限公司人员招聘考试参考试题及答案详解
- 2026年福建联通人员招聘考试题库及答案详解
- 2026年深圳市地铁集团有限公司人员招聘考试参考试题及答案详解
- 2026-2027学年四川省攀枝花市中考猜题数学试卷(含答案解析)
- 2026年涞源县教师招聘笔试备考试题及答案解析
- 2026年浙江电信人员招聘笔试参考题库及答案详解
- 2026年中国石油华北石化分公司人员招聘参考题库及答案详解
- 2026年校招:天津航空面试题及答案
- 服装设计师作品组绩效衡量表
- 河南省部分高中2026-2027学年高二上学期9月联考数学试卷(含答案)
- 2025“外研社国才杯”“理解当代中国”外语能力综合能力赛项(英语组)模拟样卷
- 2026年河北高考历史真题(试卷+解析)
- 人教版2026-2027学年高中物理必修第三册第9章-13章期中测试卷(含解析)
- 2026畜禽种业自主创新与核心种群建设战略研究报告
- 2026年企业合规师初级题库及答案
- 2026年陕西事业编考试综合管理模拟题及答案
- 2026年武汉市中考语文试卷(含答案)
- 陕西科技大学教师专业技术职务评审工作实施办法(试行)
- 高中历史课件-第6课-全球航路的开辟
评论
0/150
提交评论