from keras.layers import Input, Dense, Reshape
from keras.models import Model
import matplotlib.pyplot as plt
# this is the size of our encoded representations
encoding_dim = 14 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats
# this is our input placeholder
input_img = Input(shape=(14,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(14, activation='sigmoid')(encoded)
# this model maps an input to its reconstruction
autoencoder = Model(input=input_img, output=decoded)
# this model maps an input to its encoded representation
encoder = Model(input=input_img, output=encoded)
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
#from keras.datasets import mnist
import numpy as np
#(x_train, _), (x_test, _) = mnist.load_data()
import pandas as pd
#split into train and test sets
data = np.genfromtxt('<file directory>/boston_house_prices.csv',delimiter = ',')
train_size = int(len(data)*0.60)
test_size = len(data)-train_size
x_train, x_test = data[0:train_size,:], data[train_size:len(data),:]
#normalize the value:
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
## we ignore the flatten value needed only when pixel data
###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:])))
autoencoder.fit(x_train, x_train,
nb_epoch=50,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))
# encode and decode some digits
# note that we take them from the *test* set
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
# display reconstruction
plt.plot(decoded_imgs[:,:])
plt.show()
# display original
plt.plot(x_test[:,:])
plt.show()