将自动编码器拆分为编码器和解码器的Keras实现

我正在尝试创建一个自动编码器,用于以下目的:

  1. 训练模型
  2. 拆分编码器和解码器
  3. 可视化压缩数据(编码器)
  4. 使用任意压缩数据获取输出(解码器)
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2Dfrom keras.models import Modelfrom keras import backend as Kfrom keras.datasets import mnistimport numpy as np(x_train, _), (x_test, _) = mnist.load_data()x_train = x_train.astype('float32') / 255.x_train = x_train[:100,:,:,]x_test = x_test.astype('float32') / 255.x_test = x_trainx_train = np.reshape(x_train, (len(x_train), 28, 28, 1))  # adapt this if using `channels_first` image data formatx_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  # adapt this if using `channels_first` image data format input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data formatx = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)x = MaxPooling2D((2, 2), padding='same')(x)x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)encoded = MaxPooling2D((2, 2), padding='same')(x)# at this point the representation is (7, 7, 32)decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)x = UpSampling2D((2, 2))(decoder)x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)x = UpSampling2D((2, 2))(x)decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)autoencoder = Model(input_img, decoded(encoded(input_img)))autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')autoencoder.fit(x_train, x_train,                epochs=10,                batch_size=128,                shuffle=True,                validation_data=(x_test, x_test),                #callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)]               )

如何训练并拆分它以使用训练好的权重?


回答:

创建编码器:

input_img = Input(shape=(28, 28, 1))x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)x = MaxPooling2D((2, 2), padding='same')(x)x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)encoded = MaxPooling2D((2, 2), padding='same')(x)encoder = Model(input_img, encoded)

创建解码器:

decoder_input= Input(shape_equal_to_encoder_output_shape)decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)x = UpSampling2D((2, 2))(decoder)x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)x = UpSampling2D((2, 2))(x)decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)decoder = Model(decoder_input, decoded)

创建自动编码器:

auto_input = Input(shape=(28,28,1))encoded = encoder(auto_input)decoded = decoder(encoded)auto_encoder = Model(auto_input, decoded)

现在您可以根据需要使用这些模型:

  1. 训练自动编码器
  2. 使用编码器和解码器

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注