keras使用AutoEncoder对mnist数据降维

Posted yytxdy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keras使用AutoEncoder对mnist数据降维相关的知识,希望对你有一定的参考价值。

import keras
import matplotlib.pyplot as plt
from keras.datasets import mnist

(x_train, _), (x_test, y_test) = mnist.load_data()

x_train = x_train.astype(‘float32‘) / 255
x_test = x_test.astype(‘float32‘) / 255
x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)
encoding_dim = 2

encoder = keras.models.Sequential([
    keras.layers.Dense(128, activation=‘relu‘),
    keras.layers.Dense(32, activation=‘relu‘),
    keras.layers.Dense(8, activation=‘relu‘),
    keras.layers.Dense(encoding_dim)
])

decoder = keras.models.Sequential([
    keras.layers.Dense(8, activation=‘relu‘),
    keras.layers.Dense(32, activation=‘relu‘),
    keras.layers.Dense(128, activation=‘relu‘),
    keras.layers.Dense(784, activation=‘tanh‘)
])

AutoEncoder = keras.models.Sequential([
    encoder,
    decoder
])
AutoEncoder.compile(optimizer=‘adam‘, loss=‘mse‘)
AutoEncoder.fit(x_train, x_train, epochs=10, batch_size=256)

predict = encoder.predict(x_test)
plt.scatter(predict[:, 0], predict[:, 1], c=y_test)
plt.show()

  

 

将数据降到两维以后,得到的图像如下:

 

 

技术图片

以上是关于keras使用AutoEncoder对mnist数据降维的主要内容,如果未能解决你的问题,请参考以下文章

[Python人工智能] 三十八.Keras构建无监督学习Autoencoder模型及MNIST聚类可视化详解

[Python人工智能] 三十七.Keras构建无监督学习Autoencoder模型及MNIST聚类可视化详解

python mnist_autoencoder.py

在 TF2/Keras 中正确实现 Autoencoder MSE 损失函数

python mnist_autoencoder_hsparse.py

CNN autoencoder 进行异常检测——TODO,使用keras进行测试