31、生成模型
一系列用于随机生成课观测数据的模型
两个主要功能:学习一个概率分布、生成数据
(1)自动编码器
最开始作为一种数据的压缩算法
特点:1>与数据的相关程度高,只能压缩与训练数据相似的数据
2>压缩后数据有损,数据降维导致的
应用:1>数据去噪
2>可视化降维
3>生成数据
2、手写数字生成
(1)全连接方法
from google.colab import drive
drive.mount('/content/drive')
import torch
import torch.nn as nn
from torch.autograd import Variable
import matplotlib.pyplot as plt
%matplotlib inline
cudAvl = lambda x : x.cuda() if torch.cuda.is_available() else x
class autoencoder(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(
nn.Linear(28*28, 128),
nn.ReLU(True),
nn.Linear(128, 64),
nn.ReLU(True),
nn.Linear(64, 12),
nn.ReLU(True),
nn.Linear(12, 3)
)
self.decoder = nn.Sequential(
nn.Linear(3, 12),
nn.ReLU(True),
nn.Linear(12, 64),
nn.ReLU(True),
nn.Linear(64, 128),
nn.ReLU(True),
nn.Linear(128, 28*28),
nn.Tanh()
)
def forward(self, x):
code = self.encoder(x)
imgTensor = self.decoder(code)
return code, imgTensor
def encode(self, x):
return self.encoder(x)
def decode(self, x):
return self.decoder(x)
net = autoencoder()
net = cudAvl(net)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)
import csv
with open('/content/drive/MyDrive/Colab Notebooks/train.csv') as f :
lines = csv.reader(f)
label, attr = [], []
for line in lines :
if lines.line_num == 1 :
continue
label.append(int(line[0]))
attr.append([float(j) for j in line[1:]])
print(len(label), len(attr[1]))
digTensor = torch.FloatTensor(attr)
digTensor = digTensor / 255.0
digTensor.shape
epoch = 1500
pltX, pltY = [], []
for e in range(epoch):
pltX.append(e)
Input = cudAvl(Variable(digTensor))
Target = cudAvl(Variable(digTensor))
_, Output = net(Input)
loss = criterion(Output,Target)
print_loss = loss.item()
pltY.append(print_loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (e + 1) % 100 == 0:
print('epoch [%s/%s]: %s' %(e + 1, epoch, print_loss))
plt.title('loss function output curve')
plt.plot(pltX, pltY)
plt.show()
from PIL import Image
import numpy as np
def getImage(matrix):
dig = np.array(matrix.numpy()).reshape((28, 28))
digImg = Image.fromarray(dig * 255)
digImg = digImg.convert('L')
return digImg
def compare(matrix, testNet):
before = getImage(matrix)
plt.subplot(121)
plt.title('before encoding')
plt.imshow(before)
matrix = matrix.unsqueeze(0)
In = cudAvl(Variable(matrix))
code, Out = testNet(In)
if torch.cuda.is_available():
Out = Out.cpu()
after = getImage(Out.data)
plt.subplot(122)
plt.title('after encoding')
plt.imshow(after)
import random
index = random.randint(0, digTensor.shape[0])
print('image index =', index, ', number = ', label[index], '\n')
net.eval()
compare(digTensor[index], net)
(2)卷积神经网络方法
class CNNautoencoder(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(
nn.Conv2d(1, 16, 3, stride=3, padding=1),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(16, 8, 3, stride=2, padding=1),
nn.ReLU(True),
nn.MaxPool2d(2, stride=1)
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(8, 16, 3, stride=2),
nn.ReLU(True),
nn.ConvTranspose2d(16, 8, 5, stride=3, padding=1),
nn.ReLU(True),
nn.ConvTranspose2d(8, 1, 2, stride=2, padding=1),
nn.Tanh()
)
def forward(self, x):
code = self.encoder(x)
transImg = self.decoder(code)
return code, transImg
CNNnet = CNNautoencoder()
CNNnet = cudAvl(CNNnet)
CNNcriterion = nn.MSELoss()
CNNoptimizer = torch.optim.Adam(CNNnet.parameters(), lr=1e-3, weight_decay=1e-5)
CNNdigTensor = digTensor.view(digTensor.shape[0], 1, 28, 28)
epoch = 1000
pltX, pltY = [], []
for e in range(epoch):
pltX.append(e)
Input = cudAvl(Variable(CNNdigTensor))
Target = cudAvl(Variable(CNNdigTensor))
_, Output = CNNnet(Input)
loss = CNNcriterion(Output,Target)
print_loss = loss.item()
pltY.append(print_loss)
CNNoptimizer.zero_grad()
loss.backward()
CNNoptimizer.step()
if (e + 1) % 100 == 0:
print('epoch [%s/%s]: %s' %(e + 1, epoch, print_loss))
plt.title('loss function output curve')
plt.plot(pltX, pltY)
plt.show()
CNNnet.eval()
compare(CNNdigTensor[index], CNNnet)
3、变分自动编码器
自动编码器中需要输入一张照片以解码出生成照片,失去了随机性
变分自动编码器原理:在编码过程中增加限制,使得它生成的隐含向量能够遵循标准正态分布,在生成照片时,仅输入一个标准正态分布则可以生成照片
模型准确率:解码器生成的图片与原图片的相似程度
实际中在模型准确率与隐含向量服从的标准正态分布中做权衡
隐含向量与标准正态分布之间的差异:KL divergence