# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# https://machinelearningmastery.com/autoencoder-for-classification/

# +
# Synthectic classification dataset
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, LeakyReLU, BatchNormalization
from tensorflow.keras.utils import plot_model

from matplotlib import pyplot
# -

# Define dataset
X, y = make_classification(n_samples=1000, n_features=100, n_informative=10, n_redundant=90, random_state=1)

# Summarize the dataset
print(X.shape, y.shape)

# Split into train test sets
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.33, random_state=1)

# Scale data
t = MinMaxScaler()
t.fit(X_train)
X_train = t.transform(X_train)
X_test = t.transform(X_test)

# Number input columns
n_inputs = X.shape[1]

# +
# Define encoder
visible = Input(shape=(n_inputs,))

# Encoder level 1
e = Dense(n_inputs*2)(visible)
e = BatchNormalization()(e)
e = LeakyReLU()(e)

# Encoder level 2
e = Dense(n_inputs)(e)
e = BatchNormalization()(e)
e = LeakyReLU()(e)
# -

# Bottleneck
# n_bottleneck = n_inputs
n_bottleneck = round(float(n_inputs)/2.0)
bottleneck = Dense(n_bottleneck)(e)

# +
# Define decoder level 1
d = Dense(n_inputs)(bottleneck)
d = BatchNormalization()(d)
d = LeakyReLU()(d)

# Define decoder level 2
d = Dense(n_inputs*2)(d)
d = BatchNormalization()(d)
d = LeakyReLU()(d)
# -

# Output layer
output = Dense(n_inputs, activation='linear')(d)

# Define autoencoder model
model = Model(inputs=visible, outputs=output)

# Compile autoencoder model
model.compile(optimizer='adam', loss='mse')

# Plot the autoencoder
plot_model(model, 'autoencoder_no_compress.png', show_shapes=True)

# Fit the autoencoder model to reconstruct input
history = model.fit(X_train, X_train, epochs=200, batch_size=16, verbose=2, validation_data=(X_test, X_test))

# Plot loss
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()

# Define an encoder model (without the decoder)
encoder = Model(inputs=visible, outputs=bottleneck)

# Plot the encoder
plot_model(model, 'encoder_no_compress.png', show_shapes=True)

# Save the encoder to file
encoder.save('encoder.h5')



StarcoderdataJupyterScriptsDedupFiltered