【发布时间】:2020-10-18 08:08:27
【问题描述】:
我已经使用 KERAS 编译了 2 个模型(分类和自动编码器),我能够评估模型并且按照以下方式运行没有问题。
model.compile(loss={'classification': 'categorical_crossentropy',
'autoencoder': 'mean_squared_error'},
optimizer='adam',
metrics={'classification': 'accuracy'})
history = model.fit(x_train,
{'classification': y_train, 'autoencoder': x_train},
batch_size=300,
epochs=1,
validation_data= (x_test, {'classification': y_test}),
verbose=1)
第二部分要求我仅利用自动编码器上的模型部分,并可视化 8 个图像样本。请参考下面的代码,它不能运行,因为代码是针对整个模型的,我如何只提取自动编码器上的模型部分来绘制图像?
# Generate reconstructions
num_reconstructions = 8
samples = x_test[:num_reconstructions]
targets = y_test[:num_reconstructions]
reconstructions = model.autoencoder.predict(samples)
import numpy as np
# Plot reconstructions
for i in np.arange(0, num_reconstructions):
# Get the sample and the recoax = pp.subplot(111)nstruction
sample = samples[i][:, :, 0]
reconstruction = reconstructions[i][:, :, 0]
input_class = targets[i]
# Matplotlib preparations
fig, axes = plt.subplots(1, 2)
# Plot sample and reconstruciton
axes[0].imshow(sample)
axes[0].set_title('Original image')
axes[1].imshow(reconstruction)
axes[1].set_title('Reconstruction with Conv2DTranspose')
fig.suptitle(f'MNIST target = {input_class}')
plt.show()
- 我知道这样做的一种方法是在网络架构之后重新训练仅使用自动编码器的模型,但这将是一个不同的模型,它与之前评估的不同,损失/准确度对应于评估的自动编码器/分类一起在问题的开头。
【问题讨论】:
-
不要忘记投票并接受它作为答案;-)
标签: keras autoencoder