<jupyter_start><jupyter_text>Basic autoencoderAll credits to: https://blog.keras.io/building-autoencoders-in-keras.html.The following code is a mere rearrangement of the code from the great tutorial above.<jupyter_code>from keras.layers import Input, Dense
from keras.models import Model
# this is the size of our encoded representations
encoding_dim = 32  # 32 floats -> compression of factor 24.5, assuming the input is 784 floats

# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input given the encoded representation
decoded = Dense(784, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction     (whole thing)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
from keras.datasets import mnist
import numpy as np
(x_train, _), (x_test, _) = mnist.load_data() #y_train and y_test not needed! 

num_pixels = x_train.shape[1] * x_train.shape[2]
x_train = x_train.reshape((len(x_train), num_pixels))
x_test = x_test.reshape((len(x_test), num_pixels))

#Normalize
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test, x_test))
decoded_imgs = autoencoder.predict(x_test)
import matplotlib.pyplot as plt

n = 10  # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
    which = np.random.randint(1, len(x_test[0]))
    
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[which].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    ax = plt.subplot(2, n, i + 1 + n)
    plt.imshow(decoded_imgs[which].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11592178770949721, 'fraction_numerical': 0.024208566108007448, 'mean_word_length': 4.319306930693069, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '283928', 'license': 'MIT', 'n_tokens_mistral': 781, 'n_tokens_neox': 735, 'n_words': 185, 'path': 'Autoencoder/BasicAutoencoder.ipynb', 'repo_name': 'developerator/urban-tribble', 'size': '2148'}