上海网站建设seo公司哪家好,野花韩国视频在线观看免费高清,网站定制开发合同,温州手机网站制作哪家好前言#xff1a; 本篇重点介绍AE#xff08;Auto-Encoder#xff09; 自编码器。这是深度学习的一个核心模型. 自编码网络是一种基于无监督学习方法的生成类模型,自编码最大特征输出等于输入 Yann LeCunBengio, Hinton 对无监督学习的看法. 目录#xff1a; AE 模型原…前言 本篇重点介绍AEAuto-Encoder 自编码器。这是深度学习的一个核心模型. 自编码网络是一种基于无监督学习方法的生成类模型,自编码最大特征输出等于输入 Yann LeCunBengio, Hinton 对无监督学习的看法. 目录 AE 模型原理 De-noising auto-encoder 文字检索应用例子Text Retrieval 影像相似度比较例子 Similar Image Search CNN-AE Pytorch 例子 堆叠自编码网络Stacked Autoencoders 一 AE模型原理 1.1 模型简介 AE 编码集主要由两个部分组成 编码器Encoder: 输入 向量 , 输出向量 O 解码器Decoder): 输入向量 O, 输出向量 模型跟DNN 基本一致 1.2 损失函数 使用均方差作为损失函数 loss mse_loss(, ) 1.3 手写数字识别效果图 在手写数字识别中,相对t-SNE,PCA等模型,AE 有更好的效果. 二 De-noising auto-encoder Vincent在2008年的论文中提出了AutoEncoder的改良版——DAE Extracting and Composing Robust Features提取,编码出具有鲁棒性的特征 人类具有认知被阻挡的破损图像能力此源于我们高等的联想记忆感受机能。
我们能以多种形式去记忆比如图像、声音甚至如上图的词根记忆法所以即便是数据破损丢失我们也能回想起来,所以在输入中即使加入了噪声也不会影响模型的性能,只会使得鲁棒性更强. 2.1 流程 输入 向量 step1: 增加噪声 方案1 以一定概率分布通常使用二项分布去擦除原始矩阵即每个值都随机置0. 方案2 以一定的概率高斯分布)产生噪声n针对输入向量x,得到 step2: 经过AE 编码器,重构出 2.2 损失函数 loss mse_loss(, ) 三 文字检索应用例子Text Retrieval 3.1 传统方案 最初的Bag-of-words 也叫做“词袋”在信息检索中Bag-of-words model假定对于一个文本忽略其词序和语法句法将其仅仅看做是一个词集合或者说是词的一个组合文本中每个词的出现都是独立的不依赖于其他词是否出现。 例子 应用于文本的BoW简单实例 文章1 John likes to watch movies. Mary likes too. 文章2 John also likes to watch football games. step1 建立词典 1 2 3 4 5 6 7 8 9 10 John likes to watch movies also football games mary too
step2 文章词向量表示
文章1向量a [1, 2, 1, 1, 1, 0, 0, 0, 1, 1] 文章2文章b [1, 1, 1, 1, 0, 1, 1, 1, 0, 0] step3 文章相似度 3.2 Auto-Encoder 方案 文章1 先通过AE 编码器进行降维得到向量a 文章2 先通过AE 编码器进行降维得到向量b 然后通过 a,b 计算向量之间的余弦相似度 四 影像相似度比较例子 Similar Image Search 4.1 传统方案 传统的图像算法 一般用 感知哈希算法Perceptual HashPHash 深度学习里面人脸识别算法提取特征后然后算相似度 4.2 Auto-Encoder step1: 通过AE 编码器对输入向量x 进行降维,得到code1,code2 step2: 计算code1,code2 之间的余弦相似度 五 CNN-AE AE 编码器除了使用DNN模型外,还可以使用CNN 模型.如下图所示。
相对CNN 主要区别是两个模块 Deconvolution 反卷积 Unpooling 反池化 5.1 Unpooling 反池化 是MaxPooling 的反过程 MaxPooling: Unpooling 5.2 Deconvolution 反卷积 卷积操作: 图像A 经过卷积核,得到特征图B 原图像尺寸为h 卷积核 k, paddingp , stride s 输出特征图尺寸 反卷积: 已知特征图尺寸o, 使用相同的卷积核: k,p,s 输出原图像尺寸 # -*- coding: utf-8 -*-Created on Tue Feb 20 14:18:59 2024author: chengxf2
import torchdef conv2d():·· 输入batch_size一个batch中样本的个数 3channels通道数RGB 图为3灰度图为1height 图片的高 5width_1 图片的宽 5卷积核:channels通道数和上面保持一致也就是当前层的深度 1output 输出的深度 4【需要4个filter】kernel_size 卷积核大小stride 步长padding填充系数x torch.randn(3,1,24,24)conv torch.nn.Conv2d(in_channels1, out_channels4, kernel_size2, stride2,padding1) res conv(x)print(\n 卷积输出,res.shape)return resdef deConv(x):in_channels(int)输入张量的通道数out_channels(int)输出张量的通道数kernel_size(int or tuple)卷积核大小stride(int or tuple,optional)卷积步长决定上采样的倍数padding(int or tuple, optional)对输入图像进行padding输入图像尺寸增加2*paddingoutput_padding(int or tuple, optional)对输出图像进行padding输出图像尺寸增加paddinggroups分组卷积必须能够整除in_channels和out_channelsbias是否加上偏置dilation卷积核之间的采样距离即空洞卷积padding_mode(str)padding的类型另外对于可以传入tuple的参数tuple[0]是在height维度上tuple[1]是在width维度上conv torch.nn.ConvTranspose2d(in_channels4, out_channels1, kernel_size2,stride2,padding1)out conv(x)print(\n 反卷积 输出,out.shape)if __name__ __main__:res conv2d()deConv(res) 六 AE PyTorch 例子 两个模块 main.py autoEncoder.py
5.1 autoEncoder.py
# -*- coding: utf-8 -*-Created on Tue Feb 20 14:44:21 2024author: chengxf2
import torch
import torch.nn as nnclass AE(nn.Module):def __init__(self):super(AE,self).__init__()#编码器self.encoder nn.Sequential(nn.Linear(in_features784, out_features256),nn.ReLU(),nn.Linear(in_features256, out_features128),nn.ReLU(),nn.Linear(in_features128, out_features64),nn.ReLU(),nn.Linear(in_features64, out_features16),nn.ReLU())#解码器self.decoder nn.Sequential(nn.Linear(in_features16, out_features64),nn.ReLU(),nn.Linear(in_features64, out_features128),nn.ReLU(),nn.Linear(in_features128, out_features256),nn.ReLU(),nn.Linear(in_features256, out_features784),nn.Sigmoid())def forward(self, x):batch, channel,width,height x.shapex x.view(-1, 28*28)#low dimensional vectora self.encoder(x)#print(\n a.shape ,a.shape)hatX self.decoder(a)hatX hatX.view(batch,channel,width,height)return hatX 5.2 main.py
# -*- coding: utf-8 -*-Created on Tue Feb 20 15:01:54 2024author: chengxf2
import torch
from torch.utils.data import DataLoader
from torchvision import transforms, datasets
import time
from torch import optim,nn
from autoEncoder import AE
import visdomdef main():batchNum 64lr 1e-3epochs 20device torch.device(cuda:0 if torch.cuda.is_available() else cpu)torch.manual_seed(1234)viz visdom.Visdom()viz.line([0],[-1],wintrain_loss,opts dict(titletrain acc))tf transforms.Compose([ transforms.ToTensor()])mnist_train datasets.MNIST(mnist,True,transform tf,downloadTrue)train_data DataLoader(mnist_train, batch_sizebatchNum, shuffleTrue)mnist_test datasets.MNIST(mnist,False,transform tf,downloadTrue)test_data DataLoader(mnist_test, batch_sizebatchNum, shuffleTrue)global_step 0model AE().to(device)criteon nn.MSELoss().to(device) #损失函数optimizer optim.Adam(model.parameters(),lrlr) #梯度更新规则print(\n ----main-----)for epoch in range(epochs):start time.perf_counter()for step ,(x,y) in enumerate(train_data):#[b,1,28,28]x x.to(device)#print(\n x shape,x.shape)x_hat model(x)#print(\n xHat,x_hat.shape)loss criteon(x_hat, x)#backpropoptimizer.zero_grad()loss.backward()optimizer.step()viz.line(Y[loss.item()],X[global_step],wintrain_loss,updateappend)global_step 1end time.perf_counter() interval end - startprint(\n 每轮训练时间 :,int(interval))print(epoch, loss:,loss.item())x,target iter(test_data).next()x x.to(device)with torch.no_grad():x_hat model(x)tip hatstr(epoch)#print(x[0])print(\n ----)#print(x_hat[0])#viz.images(x,nrow8, winx,optsdict(titlex))viz.images(x_hat,nrow8, winx_hat,optsdict(titletip))if __name__ __main__:main()六 Stacked Autoencoders Bengio等人在2007年的 Greedy Layer-Wise Training of Deep Networks 中
仿照stacked RBM构成的DBN提出Stacked AutoEncoder. 堆叠式自编码器通过将多个自编码器连接在一起来构建一个深层的神经网络结构。每个自编码器的隐藏层都作为下一个自编码器的输入层逐层堆叠在一起。这种堆叠的方式使得每个自编码器都可以学习到更高级别的数据特征表示。 堆叠式自编码器的训练过程分为两个阶段。 1每个自编码器都被独立地训练以学习到数据的低维表示。 2 使用已训练好的自编码器来初始化下一个自编码器的编码器部分然后再次进行训练以学习到更高级别的特征表示。 这个过程可以重复多次以构建更深层次的堆叠式自编码器. 建议看一下这个代码AutoEncoder: 堆栈自动编码器 Stacked_AutoEncoder - 知乎
简答的说先训练出一个AE, 把该AE 的隐藏层作为下个AE 的输入
反复迭代训练 参考
自编码器AE、VAE的原理与代码实现 - 知乎
16: Unsupervised Learning - Auto-encoder_哔哩哔哩_bilibili
神经网络-AEVAE基础 - 知乎
自编码网络附代码实现-CSDN博客
浅析Bag-of-words及Bag-of-features原理_bag of words-CSDN博客
https://www.cnblogs.com/neopenx/p/4378635.html
AutoEncoder: 堆栈自动编码器 Stacked_AutoEncoder - 知乎