-
-
Save seyedrezamirkhani/28d43e67e647b41da51d5ee1eb73a20a to your computer and use it in GitHub Desktop.
Training procedure for a vanilla autoencoder model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
autoencoder = Autoencoder(intermediate_dim=64, original_dim=784) | |
opt = tf.optimizers.Adam(learning_rate=learning_rate) | |
(training_features, _), (test_features, _) = tf.keras.datasets.mnist.load_data() | |
training_features = training_features / np.max(training_features) | |
training_features = training_features.reshape(training_features.shape[0], | |
training_features.shape[1] * training_features.shape[2]) | |
training_features = training_features.astype('float32') | |
training_dataset = tf.data.Dataset.from_tensor_slices(training_features) | |
training_dataset = training_dataset.batch(batch_size) | |
training_dataset = training_dataset.shuffle(training_features.shape[0]) | |
training_dataset = training_dataset.prefetch(batch_size * 4) | |
writer = tf.summary.create_file_writer('tmp') | |
with writer.as_default(): | |
with tf.summary.record_if(True): | |
for epoch in range(epochs): | |
for step, batch_features in enumerate(training_dataset): | |
train(loss, autoencoder, opt, batch_features) | |
loss_values = loss(autoencoder, batch_features) | |
original = tf.reshape(batch_features, (batch_features.shape[0], 28, 28, 1)) | |
reconstructed = tf.reshape(autoencoder(tf.constant(batch_features)), (batch_features.shape[0], 28, 28, 1)) | |
tf.summary.scalar('loss', loss_values, step=step) | |
tf.summary.image('original', original, max_outputs=10, step=step) | |
tf.summary.image('reconstructed', reconstructed, max_outputs=10, step=step) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment