-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAutoencoder.py
More file actions
109 lines (77 loc) · 3.08 KB
/
Autoencoder.py
File metadata and controls
109 lines (77 loc) · 3.08 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from keras.models import Model
from keras.layers import Dense, Input
from keras.optimizers import Adam
from sklearn import preprocessing
from sklearn import metrics
from sklearn.metrics import accuracy_score
from keras import backend as K
import matplotlib as mpl
import networkx as nx
import numpy as np
class Autoencoder:
def __init__(self,nets):
self.nb_epoch = 15
self.batch_size = 128
self.h1_dim = 800
self.lr = 0.001
self.noise = 0.05
self.X = self.triangular_adjacency_matrix(nets,3)
self.embs = 0
self.sim_mat = 0
def similarity_matrix(self):
dist = metrics.pairwise.euclidean_distances(self.embs)
dist_sc = (dist - dist.min())/(dist.max()-dist.min())
emb_sim = 1 - dist_sc
return emb_sim
def triangular_adjacency_matrix(self, nets, power=1):
X = []
for g in nets.keys():
A = nx.adjacency_matrix(nets[g]['network'])
A = np.linalg.matrix_power(A.toarray(),power)
indices = np.triu_indices_from(A)
X.append(A[indices])
#X.append(A.toarray().flatten())
X = np.array(X)
return X
def get_noise(self,data,p):
res = []
for i in range(data.shape[0]):
res.append(np.random.binomial(1,p, data.shape[1])*-1)
return np.array(res)
def get_activations(self,model, layer, X_batch):
get_activations = K.function([model.layers[0].input, K.learning_phase()], [model.get_layer(layer).output,])
activations = get_activations([X_batch,0])
return activations
def train(self):
train_index = range(0,self.X.shape[0])
nb_visible = self.X.shape[1]
x_train = self.X[train_index]
noise_matrix = self.get_noise(x_train,self.noise)
x_train_noisy = x_train + noise_matrix
x_train_noisy[x_train_noisy<0] = 0
print("Training shape: ", x_train.shape)
print("Hidden dimension: ", self.h1_dim)
print("Learning rate: ", self.lr)
print("Batch size: ", self.batch_size)
sc = preprocessing.MinMaxScaler().fit(x_train)
x_train = sc.transform(x_train)
x_train_noisy = sc.transform(x_train_noisy)
# Build autoencoder model
input_net = Input(shape=(nb_visible,))
encoded = Dense(self.h1_dim, activation='tanh', name='h1_layer')(input_net)
#encoded = BatchNormalization(axis=-1)(encoded)
decoded = Dense(nb_visible, activation='sigmoid', name='output')(encoded)
autoencoder = Model(input_net, decoded)
adam = Adam(lr=self.lr)
#autoencoder.compile(optimizer=adam, loss='binary_crossentropy'); print("*** Cross-Entropy loss... ****");
autoencoder.compile(optimizer=adam, loss='mean_squared_error'); print("*** Mean Square Error loss... ****");
# Train
autoencoder.fit(x_train_noisy, x_train, epochs=self.nb_epoch, batch_size=self.batch_size, shuffle=True, verbose=1)
# Evaluate
evaluation = autoencoder.evaluate(x_train, x_train, batch_size=self.batch_size, verbose=1)
print('\nSummary: Loss over the dataset: %.2f' % evaluation)
################ Getting activations ##############################
layer = 'h1_layer'
self.embs = self.get_activations(autoencoder, layer, x_train)[0]
self.sim_mat = self.similarity_matrix()
print("\n")