Skip to content

Instantly share code, notes, and snippets.

@airalcorn2
Created May 9, 2017 21:55
Show Gist options
  • Save airalcorn2/feac2f9ecc8e2947ca02ea5bdcb2bc40 to your computer and use it in GitHub Desktop.
Save airalcorn2/feac2f9ecc8e2947ca02ea5bdcb2bc40 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from keras import backend
from keras.layers import Input, Dense
from keras.models import Model
# Autoencoder.
point_2d = Input(shape = (2, ))
h1 = Dense(50, activation = "sigmoid")(point_2d)
point_1d = Dense(1, activation = "sigmoid")(h1)
h2 = Dense(50, activation = "sigmoid")(point_1d)
point_2d_hat = Dense(2, activation = "sigmoid")(h2)
autoencoder = Model(point_2d, point_2d_hat)
autoencoder.compile(optimizer = "adadelta", loss = "binary_crossentropy")
X = np.random.uniform(size = (10000, 2))
epochs = 100
autoencoder.fit(X, X, epochs = epochs, shuffle = True)
get_2d = backend.function([point_1d], [point_2d_hat])
get_1d = backend.function([point_2d], [point_1d])
size = 1000
points_1d = np.arange(0, 1, 1 / size).reshape(size, 1)
points_2d = pd.DataFrame(get_2d([points_1d])[0], columns = ["x", "y"])
sns.regplot(x = "x", y = "y", data = points_2d, fit_reg = False)
plt.show()
points_1d_hat = get_1d([X])
sns.distplot(points_1d_hat)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment