-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple_vae_lstm_model.py
303 lines (236 loc) · 10.1 KB
/
simple_vae_lstm_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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
################################## Import Libraries ########################
import numpy as np
np.random.seed(0)
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import tensorflow as tf
tf.random.set_seed(0)
from tensorflow import keras, data
import tensorflow_probability as tfp
from tensorflow.keras import layers, regularizers, activations
from tensorflow.keras.layers import Bidirectional
from tensorflow.keras import backend as K
import seaborn as sns
import matplotlib.pyplot as plt
row_mark = 225
batch_size = 1
time_step = 1
lstm_h_dim = 8
z_dim = 4
epoch_num = 59
threshold = 1
mode = 'train'
model_dir = "./lstm_vae_model/"
image_dir = "./lstm_vae_images/"
#################
# Split dataset
#################
def split_normalize_data(all_df):
train_df = all_df[:row_mark]
test_df = all_df[row_mark:]
scaler = MinMaxScaler()
scaler.fit(np.array(all_df)[:, 1:])
train_scaled = scaler.transform(np.array(train_df)[:, 1:])
test_scaled = scaler.transform(np.array(test_df)[:, 1:])
return train_scaled, test_scaled
#################
# Reshape Function
#################
def reshape(da):
return da.reshape(da.shape[0], time_step, da.shape[1]).astype("float32")
#################
# Reparametrization
#################
class Sampling(layers.Layer):
def __init__(self, name='sampling_z'):
super(Sampling, self).__init__(name=name)
def call(self, inputs,**kwargs):
mu, logvar = inputs
print('mu: ', mu)
sigma = K.exp(logvar * 0.5)
epsilon = K.random_normal(shape=(mu.shape[0], z_dim), mean=0.0, stddev=1.0)
return mu + epsilon * sigma
def get_config(self):
config = super(Sampling, self).get_config()
config.update({'name': self.name})
return config
#################
# Encoder
#################
class Encoder(layers.Layer):
def __init__(self, time_step, x_dim, lstm_h_dim, z_dim, name='encoder', **kwargs):
super(Encoder, self).__init__(name=name, **kwargs)
self.encoder_inputs = keras.Input(shape=(time_step, x_dim))
self.encoder_lstm = Bidirectional(layers.LSTM(lstm_h_dim, activation='softplus', name='encoder_lstm', stateful=True))
self.z_mean = layers.Dense(z_dim, name='z_mean')
self.z_logvar = layers.Dense(z_dim, name='z_log_var')
self.z_sample = Sampling()
def call(self, inputs,**kwargs):
self.encoder_inputs = inputs
hidden = self.encoder_lstm(self.encoder_inputs)
mu_z = self.z_mean(hidden)
logvar_z = self.z_logvar(hidden)
z = self.z_sample((mu_z, logvar_z))
return mu_z, logvar_z, z
def get_config(self):
config = super(Encoder, self).get_config()
config.update({
'name': self.name,
'z_sample': self.z_sample.get_config()
})
return config
#################
# Decoder
#################
class Decoder(layers.Layer):
def __init__(self, time_step, x_dim, lstm_h_dim, z_dim, name='decoder', **kwargs):
super(Decoder, self).__init__(name=name, **kwargs)
self.z_inputs = layers.RepeatVector(time_step, name='repeat_vector')
self.decoder_lstm_hidden = Bidirectional(layers.LSTM(lstm_h_dim, activation='softplus', return_sequences=True,
name='decoder_lstm'))
self.x_mean = layers.Dense(x_dim, name='x_mean')
self.x_sigma = layers.Dense(x_dim, name='x_sigma', activation='tanh')
def call(self, inputs,**kwargs):
z = self.z_inputs(inputs)
hidden = self.decoder_lstm_hidden(z)
mu_x = self.x_mean(hidden)
sigma_x = self.x_sigma(hidden)
return mu_x, sigma_x
def get_config(self):
config = super(Decoder, self).get_config()
config.update({
'name': self.name
})
return config
#################
# Simple LSTM VAE
#################
loss_metric = keras.metrics.Mean(name='loss')
likelihood_metric = keras.metrics.Mean(name='log likelihood')
class LSTM_VAE(keras.Model):
def __init__(self, time_step, x_dim, lstm_h_dim, z_dim, name='lstm_vae', **kwargs):
super(LSTM_VAE, self).__init__(name=name, **kwargs)
self.encoder = Encoder(time_step, x_dim, lstm_h_dim, z_dim, **kwargs)
self.decoder = Decoder(time_step, x_dim, lstm_h_dim, z_dim, **kwargs)
def call(self, inputs,**kwargs):
mu_z, logvar_z, z = self.encoder(inputs)
mu_x, sigma_x = self.decoder(z)
var_z = K.exp(logvar_z)
kl_loss = K.mean(-0.5 * K.sum(var_z - logvar_z + tf.square(1 - mu_z), axis=1), axis=0)
self.add_loss(kl_loss)
dist = tfp.distributions.Normal(loc=mu_x, scale=tf.abs(sigma_x))
log_px = -dist.log_prob(inputs)
return mu_x, sigma_x, log_px
def get_config(self):
config = {
'encoder': self.encoder.get_config(),
'decoder': self.decoder.get_config(),
'name': self.name
}
return config
def reconstruct_loss(self, x, mu_x, sigma_x):
var_x = K.square(sigma_x)
reconst_loss = -0.5 * K.sum(K.log(var_x), axis=2) + K.sum(K.square(x - mu_x) / var_x, axis=2)
reconst_loss = K.reshape(reconst_loss, shape=(x.shape[0], 1))
return K.mean(reconst_loss, axis=0)
def mean_log_likelihood(self, log_px):
log_px = K.reshape(log_px, shape=(log_px.shape[0], log_px.shape[2]))
mean_log_px = K.mean(log_px, axis=1)
return K.mean(mean_log_px, axis=0)
def train_step(self, data):
if isinstance(data, tuple):
x = data[0]
else:
x = data
with tf.GradientTape() as tape:
mu_x, sigma_x, log_px = self(x, training=True)
loss = self.reconstruct_loss(x, mu_x, sigma_x)
loss += sum(self.losses)
mean_log_px = self.mean_log_likelihood(log_px)
grads = tape.gradient(loss, self.trainable_variables)
self.optimizer.apply_gradients(zip(grads, self.trainable_variables))
loss_metric.update_state(loss)
likelihood_metric.update_state(mean_log_px)
return {'loss': loss_metric.result(), 'log_likelihood': likelihood_metric.result()}
def plot_loss_moment(history):
_, ax = plt.subplots(figsize=(14, 6), dpi=80)
ax.plot(history['loss'], 'blue', label='Loss', linewidth=1)
ax.plot(history['log_likelihood'], 'red', label='Log likelihood', linewidth=1)
ax.set_title('Loss and log likelihood over epochs')
ax.set_ylabel('Loss and log likelihood')
ax.set_xlabel('Epoch')
ax.legend(loc='upper right')
plt.savefig(image_dir + 'loss_lstm_vae_' + mode + '.png')
def plot_log_likelihood_train(df_log_px):
plt.figure(figsize=(14, 6), dpi=80)
plt.title("Log likelihood")
sns.set_color_codes()
sns.distplot(df_log_px, bins=40, kde=True, rug=True, color='blue')
plt.savefig(image_dir + 'log_likelihood_train' + '.png')
def plot_log_likelihood_test(df_log_px):
plt.figure(figsize=(14, 6), dpi=80)
plt.title("Log likelihood")
sns.set_color_codes()
sns.distplot(df_log_px, bins=40, kde=True, rug=True, color='blue')
plt.savefig(image_dir + 'log_likelihood_test' + '.png')
def save_model(model):
with open(model_dir + 'lstm_vae.json', 'w') as f:
f.write(model.to_json())
model.save_weights(model_dir + 'lstm_vae_ckpt')
def load_model():
lstm_vae_obj = {'Encoder': Encoder, 'Decoder': Decoder, 'Sampling': Sampling}
with keras.utils.custom_object_scope(lstm_vae_obj):
with open(model_dir + 'lstm_vae.json', 'r'):
model = keras.models.model_from_json(model_dir + 'lstm_vae.json')
model.load_weights(model_dir + 'lstem_vae_ckpt')
return model
def main():
try:
dataset = pd.read_csv("./dataset/Bearing_dataset.csv")
print("Dataset shape: ", dataset.shape)
except Exception:
print("Dataset not found")
all_df = pd.DataFrame(dataset)
train_scaled, test_scaled = split_normalize_data(all_df)
x_dim = train_scaled.shape[1]
print("train and test data shape after scaling: ", train_scaled.shape, test_scaled.shape)
train_X = reshape(train_scaled)
test_X = reshape(test_scaled)
opt = keras.optimizers.Adam(learning_rate=0.001, epsilon=1e-6, amsgrad=True)
if mode == "train":
model = LSTM_VAE(time_step, x_dim, lstm_h_dim, z_dim, dtype='float32')
model.compile(optimizer=opt)
train_dataset = data.Dataset.from_tensor_slices(train_X)
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size, drop_remainder=True)
history = model.fit(train_dataset, epochs=epoch_num, shuffle=False).history
model.summary()
plot_loss_moment(history)
save_model(model)
elif mode == "infer":
model = load_model()
model.compile(optimizer=opt)
else:
print("Unknown mode: ", mode)
exit(1)
_, _, train_log_px = model.predict(train_X, batch_size=1)
train_log_px = train_log_px.reshape(train_log_px.shape[0], train_log_px.shape[2])
df_train_log_px = pd.DataFrame()
df_train_log_px['log_px'] = np.mean(train_log_px, axis=1)
plot_log_likelihood_train(df_train_log_px)
_, _, test_log_px = model.predict(test_X, batch_size=1)
test_log_px = test_log_px.reshape(test_log_px.shape[0], test_log_px.shape[2])
df_test_log_px = pd.DataFrame()
df_test_log_px['log_px'] = np.mean(test_log_px, axis=1)
plot_log_likelihood_test(df_test_log_px)
df_log_px = pd.DataFrame()
df_log_px['log_px'] = np.mean(test_log_px, axis=1)
df_log_px = pd.concat([df_train_log_px, df_log_px])
df_log_px['threshold'] = threshold
df_log_px['anomaly'] = df_log_px['log_px'] > df_log_px['threshold']
df_log_px.index = np.array(all_df)[:, 0]
df_log_px.plot(logy=True, figsize=(16, 9), color=['blue', 'red'])
plt.savefig(image_dir + 'anomaly_lstm_vae_train_and_test' + '.png')
if __name__ == "__main__":
main()