【问题标题】:Split autoencoder on encoder and decoder keras在编码器和解码器 keras 上拆分自动编码器
【发布时间】:2019-07-22 13:30:11
【问题描述】:

我正在尝试创建一个自动编码器:

  1. 训练模型
  2. 拆分编码器和解码器
  3. 可视化压缩数据(编码器)
  4. 使用任意压缩数据获取输出(解码器)
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
from keras.datasets import mnist
import 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_train
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))  # adapt this if using `channels_first` image data format
x_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 format

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)

# 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)]
               )

如何拆分训练它并与训练的权重拆分?

【问题讨论】:

  • 好。你的问题是什么?
  • @desertnaut 我很抱歉!现在我编辑了这个问题。这里的主要难点在这里 autoencoder = Model(input_img, decoded(encoded(input_img)))
  • 我认为 OP 希望将外部输入提供给解码器并观察输出。我不确定如何使用 keras 执行此操作,但使用 tensorflow 您可以使用 tf.placeholder_with_default() 在未输入占位符时通过输入。

标签: python machine-learning keras neural-network autoencoder


【解决方案1】:

制作编码器:

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. 使用编码器和解码器

【讨论】:

  • 你太棒了。非常感谢穆勒。请问您从事神经网络工作多长时间了?
猜你喜欢
  • 2019-09-13
  • 2019-01-05
  • 2018-05-11
  • 2019-06-29
  • 2017-07-19
  • 2021-08-22
  • 1970-01-01
  • 2020-06-11
  • 1970-01-01
相关资源
最近更新 更多