堆叠式自动编码器+TSNE在Fashion MNIST上进行降维

使用Keras实现堆叠式自动编码器,具体包含一个输入层3个隐藏层和一个输出层,在编码器中对数据集进行编码,解码器中对数据集进行重构,中途实现原始图像和重构后图像的可视化对比。最后实现对数据集的深层次降维,作用是对于一般的降维算法在处理大型数据集时(例如图像),无法进行有效降维,可以借助自动编码器先降低一定维度,再结合普通降维算法进一步降维即可。
结果展示:
堆叠式自动编码器+TSNE在Fashion MNIST上进行降维_第1张图片
堆叠式自动编码器+TSNE在Fashion MNIST上进行降维_第2张图片

堆叠式自动编码器+TSNE在Fashion MNIST上进行降维_第3张图片

import tensorflow as tf
from tensorflow import keras
fashion_mnist = keras.datasets.fashion_mnist
(X_train_full,y_train_full),(X_test,y_test) = fashion_mnist.load_data()
X_valid , X_train = X_train_full[:5000] / 255.0 , X_train_full[5000:] / 255.0
#除以255.0的目的是将像素强度降低到0~1范围
y_valid , y_train = y_train_full[:5000] , y_train_full[5000:]
class_names = ["T-shirt/top","Trouser","Pullover","Dress","Coat","Sandal","Shirt","Sneaker","Bag","Ankle boot"]
#就是标签
#编码器
stacked_encoder = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),
    keras.layers.Dense(100,activation="selu"),
    keras.layers.Dense(30,activation="selu"),
])
#解码器
stacked_decoder = keras.models.Sequential([
    keras.layers.Dense(100,activation="selu",input_shape=[30]),
    keras.layers.Dense(28*28,activation="sigmoid"),
    keras.layers.Reshape([28,28])
])
#构建模型
stacked_ae =  keras.models.Sequential([stacked_encoder,stacked_decoder])
#一个模型可以作为另一个模型的输入
stacked_ae.compile(loss='binary_crossentropy',
                  optimizer=keras.optimizers.SGD(lr=1.5))
# 训练模型
history = stacked_ae.fit(X_train,X_train,epochs=10,
                        validation_data=[X_valid,X_valid])
import matplotlib.pyplot as plt
#可视化重构
def plot_image(image):
    plt.imshow(image,cmap="binary")
    plt.axis("off")

def show_reconstructions(model,n_images=5):
    reconstructions = model.predict(X_valid[:n_images])
    fig = plt.figure(figsize=(n_images * 1.5, 3))
    for image_index in range(n_images):
        plt.subplot(2,n_images,1 + image_index)
        plot_image(X_valid[image_index])
        plt.subplot(2,n_images,1+n_images+image_index)
        plot_image(reconstructions[image_index])
show_reconstructions(stacked_ae)
from sklearn.manifold import TSNE
#先利用编码器对数据集进行降维到30(就是第二层神经网络的神经元数目)
X_valid_compressed = stacked_encoder.predict(X_valid)
#在将数据传给TSNE降维到2
tsne = TSNE()
X_valid_2D = tsne.fit_transform(X_valid_compressed)
plt.scatter(X_valid_2D[:,0],X_valid_2D[:,1],c=y_valid,s=10,cmap="tab10")

你可能感兴趣的:(深度学习,自动编码器,keras,深度学习,机器学习)