Created
February 23, 2023 22:16
-
-
Save arm2arm/790e29578cda7d70b7320832bf19bb80 to your computer and use it in GitHub Desktop.
This file contains 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
# Import required libraries | |
import tensorflow as tf | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# Load MNIST dataset | |
mnist = tf.keras.datasets.mnist | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
# Preprocess data | |
x_train = x_train.astype('float32') / 255. | |
x_test = x_test.astype('float32') / 255. | |
# Define autoencoder model | |
autoencoder = tf.keras.models.Sequential([ | |
tf.keras.layers.Flatten(input_shape=(28, 28)), | |
tf.keras.layers.Dense(64, activation='relu'), | |
tf.keras.layers.Dense(32, activation='relu'), | |
tf.keras.layers.Dense(64, activation='relu'), | |
tf.keras.layers.Dense(784, activation='sigmoid'), | |
tf.keras.layers.Reshape((28, 28)) | |
]) | |
# Compile autoencoder model | |
autoencoder.compile(optimizer='adam', loss='binary_crossentropy') | |
# Train autoencoder model | |
autoencoder.fit(x_train, x_train, epochs=20, batch_size=128, validation_data=(x_test, x_test)) | |
# Evaluate autoencoder model on test set | |
loss = autoencoder.evaluate(x_test, x_test) | |
print(f"Autoencoder Loss: {loss}") | |
# Extract features from encoded layer | |
encoder = tf.keras.models.Sequential([ | |
autoencoder.layers[0], | |
autoencoder.layers[1], | |
autoencoder.layers[2] | |
]) | |
encoded_train = encoder.predict(x_train) | |
encoded_test = encoder.predict(x_test) | |
# Define classifier model | |
classifier = tf.keras.models.Sequential([ | |
tf.keras.layers.Dense(256, activation='relu', input_shape=(32,)), | |
tf.keras.layers.Dropout(0.5), | |
tf.keras.layers.Dense(10, activation='softmax') | |
]) | |
# Compile classifier model | |
classifier.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) | |
# Train classifier model | |
classifier.fit(encoded_train, y_train, epochs=20, batch_size=128, validation_data=(encoded_test, y_test)) | |
# Evaluate classifier model on test set | |
loss, accuracy = classifier.evaluate(encoded_test, y_test) | |
print(f"Classifier Loss: {loss}, Accuracy: {accuracy}") | |
# Predict labels for test set | |
predicted_labels = classifier.predict_classes(encoded_test) | |
# Plot some test images and their predicted labels | |
fig, axes = plt.subplots(nrows=3, ncols=3) | |
for i, ax in enumerate(axes.flat): | |
ax.imshow(x_test[i], cmap='binary') | |
ax.set_title(f"Predicted: {predicted_labels[i]}") | |
ax.set_xticks([]) | |
ax.set_yticks([]) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment