[深度学习]半监督学习、⽆监督学习之Autoencoders⾃编码器
(附代码)
⽬录
⾃编码器介绍
⾃编码器的结构简单,由Encoder和Decoder组成,Encoder产⽣的Latent variables是潜在变量,它是Decoder的输⼊。
⾃编码器的⽬标是寻有意义的feature,⽤这些feature来代表输⼊变量,并且可以通过Decoder还原变量。
如果具体到某⼀个数据集MINIST,他的结构如下图所⽰,中间代表了卷积⽹络层或者全连接层。
PCA与⾃编码器对⽐:
⾃编码器更接近真实图像,因为它可以是⾮线性的。
⾃编码器与分类⽹络相⽐:
⾃编码器没有额外的label,属于⽆监督学习,⽽分类⽹络需要label,属于监督学习。
从零开始训练⾃编码器
数据集是MINIST,基于python的pytorch框架。
import os
import torch
as nn
functional as F
# Parameter Settings
latent_dims = 10
num_eopchs = 50
batch_size = 64
capacity = 64
learning_rate = 1e-3
# use_gpu = True
use_gpu = False
# MNIST Data Loading
ansforms as tranforms
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
decoderimg_transform = tranforms.Compose([
tranforms.ToTensor(),
tranforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_dataset = MNIST(root='./data/MINIST', download=True, train=True, transform=img_transform)
train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dataset = MNIST(root='./data/MINIST', download=True, train=False, transform=img_transform)
test_dataloader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True)
# Autoencoder Definition
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
c = capacity
def forward(self, x):
x = F.v1(x))
x = F.v2(x))
x = x.view(x.size(0), -1)  # flatten batch of multi-channel feature maps to a batch of feature vectors
x = x.view(x.size(0), -1)  # flatten batch of multi-channel feature maps to a batch of feature vectors
x = self.fc(x)
return x
class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
c = capacity
self.fc = nn.Linear(in_features=latent_dims, out_features=c * 2 * 7 * 7)
def forward(self, x):
x = self.fc(x)
x = x.view(x.size(0), capacity * 2, 7,
7)  # unflatten batch of feature vectors to a  batch of multi-channel feature maps
x = F.v2(x))
x = torch.v1(x))  # last layer before output is tanh ,since the images are normalized and 0-centered        return x
class Autoencoder(nn.Module):
def __init__(self):
super(Autoencoder, self).__init__()
self.decoder = Decoder()
def forward(self, x):
latent = der(x)
x_recon = self.decoder(latent)
return x_recon
autoencoder = Autoencoder()
device = torch.device("cuda:0" if use_gpu and torch.cuda.is_available() else "cpu")
autoencoder = (device)
num_params = sum(p.numel() for p in autoencoder.parameters() quires_grad)
print('Number of parameters:%d' % num_params)
# Train Autoencoder
optimizer = torch.optim.Adam(params=autoencoder.parameters(), lr=learning_rate, weight_decay=1e-5)
# set to training mode
train_loss_avg = []
print('')
for epoch in range(num_eopchs):
train_loss_avg.append(0)
num_batches = 0
for img_batch, _ in train_dataloader:
img_batch = (device)
# autoencoder reconstruction
img_batch_recon = autoencoder(img_batch)
# reconstrcution error
loss = F.mse_loss(img_batch_recon, img_batch)
# backpropagation
<_grad()
loss.backward()
# one step of the optimizer(using the gradients form backpropagation)
optimizer.step()
train_loss_avg[-1] += loss.item()
num_batches += 1
train_loss_avg[-1] /= num_batches
print("Epoch [%d  / %d] average reconstruction error:%f" % (epoch + 1, num_eopchs, train_loss_avg[-1]))验证模型训练结果
# Evaluate on The Set
# set to evalution mode
autoencoder.eval()
test_loss_avg, num_batches = 0, 0
for img_batch, _ in train_dataloader:
img_batch = (device)
# autoencoder reconstruction
img_batch_recon = autoencoder(img_batch)
# reconstrcution error
loss = F.mse_loss(img_batch_recon, img_batch)
test_loss_avg += loss.item()
num_batches += 1
test_loss_avg /= num_batches
print('average reconstruction error:%f' % (test_loss_avg))
可视化结果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。