1. 自动编码器简介
自动编码器是一种特殊类型的神经网络,经过训练后可将其输入复制到输出。例如,给定一张手写数字图像,自动编码器首先将该图像编码为较低维度的潜在表示,然后将潜在表示解码回图像。自动编码器学习压缩数据,同时最小化重构误差。
2. Python 实现
2.1 导入 TensorFlow 和其他库
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, losses
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Model
2.2 加载数据集
首先,导入 Fashion MNIST 数据集训练基本自动编码器。此数据集中的每幅图像均为 28x28 像素。
(x_train, _), (x_test, _) = fashion_mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
print (x_train.shape)
print (x_test.shape)
2.3 基本自动编码器
定义一个具有两个密集层的自动编码器:一个encoder,将图像压缩为 64 维潜在向量;一个decoder,从潜在空间重建原始图像。
class Autoencoder(Model):
def __init__(self, latent_dim, shape):
super(Autoencoder, self).__init__()
self.latent_dim = latent_dim
self.shape = shape
self.encoder = tf.keras.Sequential([
layers.Flatten(),
layers.Dense(latent_dim, activation='relu'),
])
self.decoder = tf.keras.Sequential([
layers.Dense(tf.math.reduce_prod(shape).numpy(), activation='sigmoid'),
layers.Reshape(shape)
])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
shape = x_test.shape[1:]
latent_dim = 64
autoencoder = Autoencoder(latent_dim, shape)
autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())
x_train使用作为输入和目标来训练模型。encoder将学习将数据集从 784 维压缩到潜在空间,并将decoder学习重建原始图像。
autoencoder.fit(x_train, x_train,
epochs=10,
shuffle=True,
validation_data=(x_test, x_test))
现在模型已经训练完毕,让我们通过对测试集的图像进行编码和解码来测试它。
encoded_imgs = autoencoder.encoder(x_test).numpy()
decoded_imgs = autoencoder.decoder(encoded_imgs).numpy()
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
# display original
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i])
plt.title("original")
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display reconstruction
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i])
plt.title("reconstructed")
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
图片为自动编码器的生成结果,第一行为 Fashion MNIST原数据集,第二行为重建的图片。
3. Reference
[1] https://www.tensorflow.org/tutorials/generative/autoencoder
[2] https://www.deeplearningbook.org