I have compiled 2 models , (Classification and Autoencoder) using KERAS ,I am able to evaluate the model and there is no issue running as per below.
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)
The second part requires me to exploit only the portion of the model on autoencoder , and visualize 8 samples of the images. Please refer to the code below , it can't run as the code is meant for the whole model , how do i extract only the portion of the model on autoencoder to plot the image?
# 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()
See below for my network architect fyi:
- I know that one way to do it is to retrain a model with only autoencoder after the network architecture , but that would be a different model which is not the same evaluated earlier , the loss/accuracy corresponds to the autoencoder/classification evaluated together at the start of the question.