python - keras 编码器和解码器上的分割自动编码器

标签 python machine-learning keras neural-network autoencoder

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

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

如何分割训练并与训练后的权重分割?

最佳答案

制作编码器:

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

关于python - keras 编码器和解码器上的分割自动编码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54928981/

相关文章:

python - 多处理池挂起,无法脱离应用程序

python - 分组并填充缺失的日期时间值

python - ValueError : Expected 2D array, 在拟合模型时得到一维数组

Tensorflow TensorBoard 不显示 acc、loss、acc_val 和 loss_val 仅仅显示 epoch_accuracy 和 epoch_loss

python - 如何计算神经网络模型中的精度、召回率和 F1 分数?

python - Pandas:使用正则表达式清理包含单引号和括号的字符串列?

python - 在 Windows 7 中使用路径扩展\\?\和 python 脚本

python - 为什么我收到 AlreadyExistsError?

python - SCons 问题 - 不理解变量类

python-3.x - 批量读取Cifar10数据集