-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
61 lines (50 loc) · 1.7 KB
/
model.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
import tensorflow as tf
class Encoder(tf.keras.layers.Layer):
"""
Encoder component that learns structured data representation
"""
def __init__(self, hidden_layer_dim, bottleneck_layer_dim):
super(Encoder, self).__init__()
self.hidden_layer = tf.keras.layers.Dense(
units=hidden_layer_dim,
activation=tf.nn.relu,
)
self.bottleneck_layer = tf.keras.layers.Dense(
units=bottleneck_layer_dim,
activation=tf.nn.sigmoid
)
def call(self, x):
activation = self.hidden_layer(x)
return self.bottleneck_layer(activation)
class Decoder(tf.keras.layers.Layer):
def __init__(self, hidden_layer_dim, original_dim):
super(Decoder, self).__init__()
self.hidden_layer = tf.keras.layers.Dense(
units=hidden_layer_dim,
activation=tf.nn.relu,
)
self.output_layer = tf.keras.layers.Dense(
units=original_dim,
activation=tf.nn.sigmoid
)
def call(self, x):
activation = self.hidden_layer(x)
return self.output_layer(activation)
class Autoencoder(tf.keras.Model):
"""
Custom model combining the Encoder and Decoder sub-models
"""
def __init__(self, hidden_layer_dim, bottleneck_layer_dim, original_dim):
super(Autoencoder, self).__init__()
self.encoder = Encoder(
hidden_layer_dim=hidden_layer_dim,
bottleneck_layer_dim=bottleneck_layer_dim
)
self.decoder = Decoder(
hidden_layer_dim=hidden_layer_dim,
original_dim=original_dim
)
def call(self, input_features):
encoded = self.encoder(input_features)
reconstructed = self.decoder(encoded)
return reconstructed