<jupyter_start><jupyter_text>Autoencoder with MNIST FCN<jupyter_code>from __future__ import division, print_function
from keras.callbacks import ModelCheckpoint
from keras.layers import Input
from keras.layers.core import Dense, Lambda
from keras.models import Model
from keras.utils import np_utils
import matplotlib.pyplot as plt
import numpy as np
import os
%matplotlib inline
DATA_DIR = "../../data"
TRAIN_FILE = os.path.join(DATA_DIR, "mnist_train.csv")
TEST_FILE = os.path.join(DATA_DIR, "mnist_test.csv")

BATCH_SIZE = 128
NUM_FEATURES = 784
NUM_CLASSES = 10
NUM_EPOCHS = 10

BEST_MODEL = os.path.join(DATA_DIR, "keras-ae-mnist-fcn-best.h5")
FINAL_MODEL = os.path.join(DATA_DIR, "keras-ae-mnist-fcn-final.h5")<jupyter_output><empty_output><jupyter_text>Prepare Data<jupyter_code>def parse_file(filename):
    xdata, ydata = [], []
    fin = open(filename, "rb")
    i = 0
    for line in fin:
        if i % 10000 == 0:
            print("{:s}: {:d} lines read".format(
                os.path.basename(filename), i))
        cols = line.strip().split(",")
        ydata.append(int(cols[0]))
        xdata.append([float(x) / 255. for x in cols[1:]])
        i += 1
    fin.close()
    print("{:s}: {:d} lines read".format(os.path.basename(filename), i))
    y = np.array(ydata)
    X = np.array(xdata)
    return X, y

Xtrain, ytrain = parse_file(TRAIN_FILE)
Xtest, ytest = parse_file(TEST_FILE)
print(Xtrain.shape, ytrain.shape, Xtest.shape, ytest.shape)<jupyter_output>mnist_train.csv: 0 lines read
mnist_train.csv: 10000 lines read
mnist_train.csv: 20000 lines read
mnist_train.csv: 30000 lines read
mnist_train.csv: 40000 lines read
mnist_train.csv: 50000 lines read
mnist_train.csv: 60000 lines read
mnist_test.csv: 0 lines read
mnist_test.csv: 10000 lines read
(60000, 784) (60000,) (10000, 784) (10000,)<jupyter_text>Define Network<jupyter_code>inputs = Input(batch_shape=(None, NUM_FEATURES))
encoded = Dense(256, activation="relu")(inputs)
hidden = Dense(2, activation="relu")(encoded)
decoded = Dense(256, activation="relu")(hidden)
outputs = Dense(NUM_FEATURES, activation="relu")(decoded)

model = Model(inputs=inputs, outputs=outputs)
model.compile(optimizer="adam", loss="mean_squared_error")<jupyter_output><empty_output><jupyter_text>Train Network<jupyter_code>history = model.fit(Xtrain, Xtrain, batch_size=BATCH_SIZE, 
                    epochs=NUM_EPOCHS,
                    validation_data=(Xtest, Xtest))
plt.plot(np.arange(NUM_EPOCHS), history.history["loss"], 
         color="r", label="train")
plt.plot(np.arange(NUM_EPOCHS), history.history["val_loss"], 
         color="b", label="val")
plt.xlabel("epochs")
plt.ylabel("MSE loss")
plt.legend(loc="best")<jupyter_output><empty_output><jupyter_text>Visualize autoencoder outputs<jupyter_code>Xviz = Xtest[0:5]
Xviz_ = model.predict(Xviz)
j = 1
for i in range(5):
    img_orig = Xviz[i].reshape(28, 28)
    img_ae = Xviz_[i].reshape(28, 28)
    plt.subplot(5, 2, j)
    plt.imshow(img_orig)
    plt.xticks([])
    plt.yticks([])
    j += 1
    plt.subplot(5, 2, j)
    plt.imshow(img_ae)
    plt.xticks([])
    plt.yticks([])
    j += 1
plt.show()<jupyter_output><empty_output><jupyter_text>Visualize encoder embeddings<jupyter_code>encoder = Model(inputs=inputs, outputs=hidden)
ytest_ = encoder.predict(Xtest)
colors = ["r", "g", "b", "c", "m", "y", "orange", "brown", "gray", "k"]
for i in range(NUM_CLASSES):
    yi_indices = np.where(ytest == i)[0]
    xi = ytest_[yi_indices][:, 0].tolist()
    yi = ytest_[yi_indices][:, 1].tolist()
    plt.scatter(xi, yi, color=colors[i], alpha=0.5, label=str(i))
plt.legend(loc="best")
plt.xlabel("Z1")
plt.ylabel("Z2")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15463917525773196, 'fraction_numerical': 0.032555615843733045, 'mean_word_length': 4.725155279503106, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '514237', 'license': 'Apache-2.0', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1418, 'n_words': 273, 'path': 'src/keras/03-mnist-autoencoder-fcn.ipynb', 'repo_name': 'sujitpal/polydlot', 'size': '3686'}