【Keras入门】1.4用于降维的自编码器Autoencoder与PCA

0.1用于降维的主成分分析法(PCA)

【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第1张图片

0.2用于降维的自编码器(Autoencoder)

【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第2张图片

1.加载数据,并将28×28的图片Reshape成784维的向量【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第3张图片

from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

print('shape of x_train:' + str(x_train.shape))
print('shape of x_test:' + str(x_test.shape))
print('shape of y_train:' + str(y_train.shape))
print('shape of y_test:' + str(y_test.shape))

x_train_vec = x_train.reshape(60000,784)
x_test_vec = x_test.reshape(10000,784)

print('shape of x_train_vec is' + str(x_train_vec.shape))
print('shape of x_test_vec is' + str(x_test_vec.shape))

shape of x_train:(60000, 28, 28)
shape of x_test:(10000, 28, 28)
shape of y_train:(60000,)
shape of y_test:(10000,)
shape of x_train_vec is(60000, 784)
shape of x_test_vec is(10000, 784)

2.构建全连接层网络

这里提供2种方法构建全连接网络。
【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第4张图片
以其中一种为例。

from keras.layers import Dense
from keras import models

model = models.Sequential()
model.add(Dense(100,activation='relu',input_shape=(784,)))
model.add(Dense(20,activation='relu'))
model.add(Dense(100,activation='relu'))
model.add(Dense(784,activation='relu'))

model.summary()

3.训练网络(这一步有点问题,loss从900开始下降很慢)

【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第5张图片

4.降维

【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第6张图片

5 卷积自编码

【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第7张图片【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第8张图片
【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第9张图片
【Keras入门】1.4用于降维的自编码器Autoencoder与PCA_第10张图片

你可能感兴趣的:(Keras入门神经网络,深度学习,神经网络,python,pca降维,自编码器)