[深度学习]半监督学习、⽆监督学习之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
# u_gpu = True
u_gpu = Fal
# MNIST Data Loading
ansforms as tranforms
from torch.utils.data import DataLoader
from torchvision.datats import MNIST
img_transform = tranforms.Compo([
tranforms.ToTensor(),
tranforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
小鸟嘴面相
])
train_datat = MNIST(root='./data/MINIST', download=True, train=True, transform=img_transform)
train_dataloader = DataLoader(train_datat, batch_size=batch_size, shuffle=True)
test_datat = MNIST(root='./data/MINIST', download=True, train=Fal, transform=img_transform)
test_dataloader = DataLoader(test_datat, batch_size=batch_size, shuffle=True)
# Autoencoder Definition
class Encoder(nn.Module):
def __init__(lf):
super(Encoder, lf).__init__()
c = capacity
def forward(lf, 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 = lf.fc(x)
return x
class Decoder(nn.Module):
def __init__(lf):
super(Decoder, lf).__init__()斤打一成语
c = capacity
lf.fc = nn.Linear(in_features=latent_dims, out_features=c * 2 * 7 * 7)
广角镜
def forward(lf, x):
x = lf.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__(lf):
super(Autoencoder, lf).__init__()
lf.decoder = Decoder()
def forward(lf, x):
latent = lf.encoder(x)
x_recon = lf.decoder(latent)
return x_recon
autoencoder = Autoencoder()
device = torch.device("cuda:0" if u_gpu and torch.cuda.is_available() el "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)
# t 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.m_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
# t 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.m_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))
可视化结果