-
Notifications
You must be signed in to change notification settings - Fork 1
/
deep_autoencoder.py
50 lines (38 loc) · 1.87 KB
/
deep_autoencoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
seed=75
import numpy as np
np.random.seed(seed)
from tensorflow import set_random_seed
set_random_seed(seed)
from sklearn import model_selection
from keras.layers import Input, Dense, Lambda
from keras.models import Model, load_model
from keras import backend as K
from keras import metrics
from keras import callbacks
import os
#return prediction for x_test
def autoencoder_predict(encoder, x_test):
prediction = encoder.predict(x_test)
return prediction.reshape((len(prediction), np.prod(prediction.shape[1:])))
#return a fit deep encoder
def deep_autoencoder_fit(x_train, encoding_dim=2, optimizer="adadelta", loss_function="binary_crossentropy", nb_epoch=4, batch_size=2, path='./saved_models'):
input_img = Input(shape=(x_train.shape[1],), name="x")
#"encoded" is the encoded representation of the input
#x_train, x_valid = model_selection.train_test_split(x_train, test_size=0.10, random_state=seed)
encoded = Dense(1000, name="encoder_h1", activation='relu')(input_img)
encoded = Dense(encoding_dim, name="encoder_mu", activation='relu')(encoded)
#decoded" is the lossy reconstruction of the input
decoded = Dense(1000, name="decoder_h1", activation='relu')(encoded)
decoded = Dense(x_train.shape[1], name="decoder_mu", activation='sigmoid')(decoded)
autoencoder = Model(inputs=input_img, outputs=decoded)
encoder = Model(inputs=input_img, outputs=encoded)
autoencoder.compile(optimizer=optimizer, loss=loss_function)
autoencoder.fit(x_train, x_train,
epochs=nb_epoch,
verbose=1,
batch_size=batch_size,
shuffle=False,
)
#if (save==1):
encoder.save(os.path.join(path, "deep_encoder.h5"))
#return encoder