0.1用于降维的主成分分析法(PCA)
0.2用于降维的自编码器(Autoencoder)
1.加载数据,并将28×28的图片Reshape成784维的向量
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种方法构建全连接网络。
以其中一种为例。
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开始下降很慢)
4.降维
5 卷积自编码