-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoEncoders.py
122 lines (94 loc) · 4.26 KB
/
AutoEncoders.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
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
110
111
112
113
114
115
116
117
118
119
120
121
#
# AutoEncoders.py
# Amir Farbin
from ModelWrapper import *
from keras.layers import Input, Dense, LSTM, RepeatVector
from keras.models import Model
class DenseAutoEncoder(ModelWrapper):
def __init__(self, Name,
InputShape= (100,),
Widths=[10],
EncodeActivation="relu",
DecodeActivation="sigmoid",
Loss="adadelta",
Optimizer="binary_crossentropy"):
super(LSTMAutoEncoder,self).__init__(Name,Loss,Optimizer)
self.InputShape=self.MetaData["InputShape"]=InputShape
self.Widths=self.MetaData["Widths"]=Widths
self.EncodeActivation=self.MetaData["EncodeActivation"]=EncodeActivation
self.DecodeActivation=self.MetaData["DecodeActivation"]=DecodeActivation
def Build():
# Input
myInput = Input(shape=self.InputShape)
myModel = myInput
# Encode
for i in range(0,len(Widths)):
myModel = Dense(Widths[i], activation=self.EncodeActivation)(myModel)
# Decode
for i in range(len(Widths)-1,-1, 0):
myModel = Dense(Widths[i], activation=self.EncodeActivation)(myModel)
myModel = Dense(Widths[0], activation=self.DecodeActivation)(myModel)
self.Model = Model(input=myInput, output=myModel)
class LSTMAutoEncoder(ModelWrapper):
def __init__(self, Name,
InputShape= (10,100),
Widths=[10],
EncodeActivation="tanh",
DecodeActivation="tanh",
Loss="mse",
Optimizer="binary_crossentropy"):
super(LSTMAutoEncoder,self).__init__(Name,Loss,Optimizer)
self.InputShape=self.MetaData["InputShape"]=InputShape
self.Widths=self.MetaData["Widths"]=Widths
self.EncodeActivation=self.MetaData["EncodeActivation"]=EncodeActivation
self.DecodeActivation=self.MetaData["DecodeActivation"]=DecodeActivation
def Build(self):
# Input
print self.InputShape
myInput = Input(shape=self.InputShape)
myModel = myInput
# Encode
for i in range(0,len(self.Widths)):
print "Adding Encoder",i,self.Widths[i]
myModel = LSTM(self.Widths[i],consume_less="gpu",
activation=self.EncodeActivation,
return_sequences=True)(myModel)
# myModel = RepeatVector(self.InputShape[0])(myModel)
# Decode
for i in range(len(self.Widths)-1,-1, -1):
myModel = LSTM(self.Widths[i],consume_less="gpu",
activation=self.DecodeActivation,
return_sequences=True)(myModel)
myModel = LSTM(self.InputShape[1],consume_less="gpu",return_sequences=True)(myModel)
self.Model = Model(input=myInput, output=myModel)
class LSTMAutoEncoder2(ModelWrapper):
def __init__(self, Name,
InputShape= (10,100),
Widths=[10],
EncodeActivation="relu",
DecodeActivation="sigmoid",
Loss="adadelta",
Optimizer="binary_crossentropy"):
super(LSTMAutoEncoder2,self).__init__(Name,Loss,Optimizer)
self.InputShape=self.MetaData["InputShape"]=InputShape
self.Widths=self.MetaData["Widths"]=Widths
self.EncodeActivation=self.MetaData["EncodeActivation"]=EncodeActivation
self.DecodeActivation=self.MetaData["DecodeActivation"]=DecodeActivation
def Build(self):
# Input
print self.InputShape
myInput = Input(shape=self.InputShape)
encoder = myInput
# Encode
for i in range(0,len(self.Widths)):
print "Adding Encoder",i,self.Widths[i]
encoder = LSTM(self.Widths[i],return_sequences=True)(encoder)
## myModel = RepeatVector(self.InputShape[0])(myModel)
# Decode
decoder = encoder
for i in range(len(self.Widths)-1,0, -1):
decoder = LSTM(self.Widths[i],return_sequences=True)(decoder)
# Reconstruct output
for i in range(len(self.Widths)-1,-1, -1):
myModel = LSTM(self.InputShape[1],return_sequences=True)(myModel)
self.Model = Model(input=myInput, output=myModel)