<jupyter_start><jupyter_text>Adapted from Edurekahttps://www.edureka.co/ai-deep-learning-with-tensorflow<jupyter_code>import numpy as np
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>The size of the encoded representations<jupyter_code>encoding_dim = 384 # 384 floats -> compression of factor 8, assuming the input is 3072 floats<jupyter_output><empty_output><jupyter_text>The input placeholder<jupyter_code>input_img = Input(shape=(3072,))<jupyter_output><empty_output><jupyter_text>"encoded" is the encoded representation of the input<jupyter_code>encoded = Dense(encoding_dim, activation='relu')(input_img)<jupyter_output><empty_output><jupyter_text>"decoded" is the lossy reconstruction of the input<jupyter_code>decoded = Dense(3072, activation='sigmoid')(encoded)<jupyter_output><empty_output><jupyter_text>This model maps an input to its reconstruction<jupyter_code>autoencoder = Model(input_img, decoded)<jupyter_output><empty_output><jupyter_text>This model maps an input to its encoded representation<jupyter_code>encoder = Model(input_img, encoded)<jupyter_output><empty_output><jupyter_text>Create a placeholder for an encoded (32 dimensional) input<jupyter_code>encoded_input = Input(shape=(encoding_dim,))<jupyter_output><empty_output><jupyter_text>Retreive the last layer of the autoencoder model<jupyter_code>decoder_layer = autoencoder.layers[-1]<jupyter_output><empty_output><jupyter_text>Create the decoder model<jupyter_code>decoder = Model(encoded_input, decoder_layer(encoded_input))<jupyter_output><empty_output><jupyter_text>Configure our model to use a per-pixel binary crossentropy loss<jupyter_code>autoencoder.compile(optimizer='adam', loss='binary_crossentropy')<jupyter_output><empty_output><jupyter_text>Prepare our input data. We're using CIFAR 10 images and we're discarding the labels<jupyter_code>(x_train, _), (x_test, _) = cifar10.load_data()<jupyter_output><empty_output><jupyter_text>Normalize all values between 0 and 1 and we will flatten the 384x384x3 imagesinto vectors of size 3072<jupyter_code>x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
print(x_train.shape)
print(x_test.shape)<jupyter_output>(10000, 3072)<jupyter_text>Train autoencoder for 50 epochs<jupyter_code>autoencoder.fit(x_train, x_train,
               epochs=50,
               batch_size=256,
               shuffle=True,
               validation_data=(x_test,x_test))<jupyter_output>Train on 50000 samples, validate on 10000 samples
Epoch 1/50
50000/50000 [==============================] - 10s 203us/sample - loss: 0.6309 - val_loss: 0.6137
Epoch 2/50
50000/50000 [==============================] - 10s 194us/sample - loss: 0.6035 - val_loss: 0.5981
Epoch 3/50
50000/50000 [==============================] - 10s 202us/sample - loss: 0.5940 - val_loss: 0.5908
Epoch 4/50
50000/50000 [==============================] - 10s 203us/sample - loss: 0.5890 - val_loss: 0.5990
Epoch 5/50
50000/50000 [==============================] - 11s 210us/sample - loss: 0.5855 - val_loss: 0.5827
Epoch 6/50
50000/50000 [==============================] - 11s 219us/sample - loss: 0.5824 - val_loss: 0.5810
Epoch 7/50
50000/50000 [==============================] - 9s 179us/sample - loss: 0.5795 - val_loss: 0.5814
Epoch 8/50
50000/50000 [==============================] - 8s 159us/sample - loss: 0.5780 - val_loss: 0.5772
Epoch 9/50
50000/50000 [==============================] - 8s 168us/sample - loss[...]<jupyter_text>Encode and decode some images.Note that we take them from the *test* set<jupyter_code>encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)<jupyter_output><empty_output><jupyter_text>Visualize the reconstructed inputs and the encoded representation.<jupyter_code>n = 20 # how many images we will display
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(32, 32, 3))
    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[i].reshape(32, 32, 3))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16922066549912435, 'fraction_numerical': 0.07377408056042031, 'mean_word_length': 6.357487922705314, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 64, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '621943', 'license': 'MIT', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1582, 'n_words': 315, 'path': '3_Image_Related_Neural_Networks/Autoencoders/autoencoder_cifar10_demo.ipynb', 'repo_name': 'vladiant/SoftUniDeepLearning2019', 'size': '4568'}