コード例 #1
0
    def create(self):
        x_train, x_test = get_dataSets()
        x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))

        x_test = np.reshape(x_test, (len(x_test),28, 28, 1))
        input_img = Input(shape=(28, 28, 1))
        x = Convolution2D(16, (3, 3), activation='relu', border_mode='same')(input_img)
        x = MaxPooling2D((2, 2), border_mode='same')(x)
        x = Convolution2D(8, (3, 3), activation='relu', border_mode='same')(x)
        x = MaxPooling2D((2, 2), border_mode='same')(x)
        x = Convolution2D(8, (3, 3), activation='relu', border_mode='same')(x)
        encoded = MaxPooling2D((2, 2), border_mode='same')(x)

        x = Convolution2D(8, (3, 3), activation='relu', border_mode='same')(encoded)
        x = UpSampling2D((2, 2))(x)
        x = Convolution2D(8, (3, 3), activation='relu', border_mode='same')(x)
        x = UpSampling2D((2, 2))(x)
        x = Convolution2D(16, 3, 3, activation='relu')(x)
        x = UpSampling2D((2, 2))(x)
        decoded = Convolution2D(1, (3, 3), activation='sigmoid', border_mode='same')(x)
        autoencoder = Model(input_img, decoded)
        autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
        autoencoder.fit(x_train, x_train, epochs=20, batch_size=256,
                           shuffle=True,
                            validation_data=(x_test, x_test))
        return autoencoder
コード例 #2
0
 def create(self):
     x_train, x_test = get_dataSets()
     encoding_dim = 32
     input_img = Input(shape=(784,))
     encoded = Dense(encoding_dim, activation='relu')(input_img)
     decoded = Dense(784, activation='sigmoid')(encoded)
     autoencoder = Model(input=input_img, output=decoded)
     encoder = Model(input=input_img, output=encoded)
     encoded_input = Input(shape=(encoding_dim,))
     decoder_layer = autoencoder.layers[-1]
     decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))
     autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
     autoencoder.fit(x_train, x_train,
                     epochs=50,
                     batch_size=128,
                     shuffle=True,
                     validation_data=(x_test, x_test))
     return autoencoder, encoder, decoder
コード例 #3
0
    def create(self):
        x_train, x_test = get_dataSets()

        input_img = Input(shape=(784,))
        encoded = Dense(128, activation='relu')(input_img)
        encoded = Dense(64, activation='relu')(encoded)
        encoded = Dense(32, activation='relu')(encoded)

        decoded = Dense(64, activation='relu')(encoded)
        decoded = Dense(128, activation='relu')(decoded)
        decoded = Dense(784, activation='sigmoid')(decoded)

        autoencoder = Model(input=input_img, output=decoded)
        autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
        autoencoder.fit(x_train, x_train,
                        epochs=1,
                        batch_size=128,
                        shuffle=True,
                        validation_data=(x_test, x_test))
        return autoencoder
コード例 #4
0
        x_train, x_test = get_dataSets()
        encoding_dim = 32
        input_img = Input(shape=(784, ))
        encoded = Dense(encoding_dim,
                        activation='relu',
                        activity_regularizer=regularizers.l1(10e-5))(input_img)
        decoded = Dense(784, activation='sigmoid')(encoded)
        autoencoder = Model(input=input_img, output=decoded)
        encoder = Model(input=input_img, output=encoded)
        encoded_input = Input(shape=(encoding_dim, ))
        decoder_layer = autoencoder.layers[-1]
        decoder = Model(input=encoded_input,
                        output=decoder_layer(encoded_input))
        autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
        autoencoder.fit(x_train,
                        x_train,
                        epochs=1,
                        batch_size=256,
                        shuffle=True,
                        validation_data=(x_test, x_test))
        return autoencoder, encoder, decoder

    pass


if __name__ == '__main__':
    x_train, x_test = get_dataSets()
    singel = sparse_layer()
    autoencode, encoder, decoder = singel.create()
    decoded_imgs = autoencode.predict(x_test)
    show(x_test, decoded_imgs)