美文网首页
keras_Autoencoder 自编码

keras_Autoencoder 自编码

作者: Ledestin | 来源:发表于2017-05-19 12:18 被阅读264次

    自编码,简单来说就是把输入数据进行一个压缩和解压缩的过程。 原来有很多 Feature,压缩成几个来代表原来的数据,解压之后恢复成原来的维度,再和原数据进行比较。
    它是一种非监督算法,只需要输入数据,解压缩之后的结果与原数据本身进行比较。
    今天要做的事情是把 datasets.mnist 数据的 28×28=784 维的数据,压缩成 2 维的数据,然后在一个二维空间中可视化出分类的效果。
    用keras构建自编码器的步骤:
    1.导入模块并创建数据
    2.建立模型
    3.激活模型
    4.训练模型
    5.可视化结果


    Demo.py

    import numpy as np
    np.random.seed(1337)  # for reproducibility
    from keras.datasets import mnist
    from keras.models import Model
    from keras.layers import Dense, Input
    import matplotlib.pyplot as plt
    from keras.optimizers import Adam
    #创建数据
    # download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
    # X shape (60,000 28x28), y shape (10,000, )
    (x_train, _), (x_test, y_test) = mnist.load_data()
    
    # data pre-processing
    x_train = x_train.astype('float32') / 255. - 0.5       # minmax_normalized
    x_test = x_test.astype('float32') / 255. - 0.5         # minmax_normalized
    x_train = x_train.reshape((x_train.shape[0], -1))
    x_test = x_test.reshape((x_test.shape[0], -1))
    print(x_train.shape)
    print(x_test.shape)
    
    #建立模型
    # in order to plot in a 2D figure
    encoding_dim = 2   #encoding_dim,要压缩成的维度。
    
    # this is our input placeholder
    input_img = Input(shape=(784,))
    
    # encoder layers
    encoded = Dense(128, activation='relu')(input_img)
    encoded = Dense(64, activation='relu')(encoded)
    encoded = Dense(10, activation='relu')(encoded)
    encoder_output = Dense(encoding_dim)(encoded)
    
    # decoder layers
    decoded = Dense(10, activation='relu')(encoder_output)
    decoded = Dense(64, activation='relu')(decoded)
    decoded = Dense(128, activation='relu')(decoded)
    decoded = Dense(784, activation='tanh')(decoded)
    
    #接下来直接用 Model 这个模块来组建模型,输入就是图片,输出是解压的最后的结果。
    # construct the autoencoder model
    autoencoder = Model(input=input_img, output=decoded)
    #当我们想要看由 784 压缩到 2维后,这个结果是什么样的时候,也可以只单独组建压缩的板块,
    #此时它的输入是图片,输出是压缩环节的最后结果。
    # construct the encoder model for plotting
    encoder = Model(input=input_img, output=encoder_output)
    
    #激活模型
    #接下来是编译自编码这个模型,优化器用的是 adam,损失函数用的是 mse。
    # compile autoencoder
    adam = Adam(lr = 1e-4)
    autoencoder.compile(optimizer='adam', loss='mse')
    #训练模型
    # training
    autoencoder.fit(x_train, x_train,
                    nb_epoch=20,
                    batch_size=256,
                    shuffle=True)
    #可视化结果
    #最后看到可视化的结果,自编码模型可以把这几个数字给区分开来,
    #我们可以用自编码这个过程来作为一个特征压缩的方法,
    #和PCA的功能一样,效果要比它好一些,因为它是非线性的结构
    # plotting
    encoded_imgs = encoder.predict(x_test)
    plt.scatter(encoded_imgs[:, 0], encoded_imgs[:, 1], c=y_test)
    plt.colorbar()
    plt.show()
    
    

    结果:

    ....
    Epoch 18/20
    60000/60000 [==============================] - 6s - loss: 0.0411     
    Epoch 19/20
    60000/60000 [==============================] - 6s - loss: 0.0407     
    Epoch 20/20
    60000/60000 [==============================] - 6s - loss: 0.0405      
    
    Paste_Image.png

    相关文章

      网友评论

          本文标题:keras_Autoencoder 自编码

          本文链接:https://www.haomeiwen.com/subject/aesexxtx.html