text_plain_output_1.png image_output_1.png
import matplotlib.pyplot as plt

import tensorflow as tf

x_train = x_train / 255
x_test = x_test / 255

i = tf.keras.Input((28, 28, 1))
x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(i)
x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = tf.keras.layers.Conv2D(32, (3, 3), activation='tanh', padding='same')(x)
x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = tf.keras.layers.Conv2DTranspose(32, (3, 3), activation='tanh', padding='same')(x)
x = tf.keras.layers.UpSampling2D((2, 2))(x)
x = tf.keras.layers.Conv2DTranspose(64, (3, 3), activation='relu', padding='same')(x)
x = tf.keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
x = tf.keras.layers.UpSampling2D((2, 2))(x)
autoencoder = tf.keras.models.Model(i, x)

autoencoder.summary()

autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics='accuracy')

r = autoencoder.fit(x=x_train, y=x_train, epochs=50, batch_size=128, shuffle=True, validation_data=(x_test, x_test))

plt.plot(r.history['accuracy'], label='accuracy')
plt.plot(r.history['val_accuracy'], label='val_accuracy')
plt.xlabel('epoch')
plt.legend()