-
Notifications
You must be signed in to change notification settings - Fork 13
/
d_masked_skip_connection_seq2seq.py
1362 lines (1224 loc) · 75.6 KB
/
d_masked_skip_connection_seq2seq.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pathlib
import sys
import tensorflow as tf
import numpy as np
from tensorflow.python.ops import init_ops
from tensorflow.python.ops.rnn_cell_impl import _Linear, LSTMStateTuple, LSTMCell, GRUCell
from tensorflow.python.ops import variable_scope as vs
from utils import *
from tensorflow.contrib import seq2seq
class RLSTMCell(tf.nn.rnn_cell.BasicLSTMCell):
"""Basic LSTM recurrent network cell.
The implementation is based on: http://arxiv.org/abs/1409.2329.
We add forget_bias (default: 1) to the biases of the forget gate in order to
reduce the scale of forgetting in the beginning of the training.
It does not allow cell clipping, a projection layer, and does not
use peep-hole connections: it is the basic baseline.
For advanced models, please use the full @{tf.nn.rnn_cell.LSTMCell}
that follows.
"""
def __init__(self, num_units, forget_bias=1.0, state_is_tuple=True, activation=None, reuse=None, dense=None):
"""Initialize the basic LSTM cell.
Args:
num_units: int, The number of units in the LSTM cell.
forget_bias: float, The bias added to forget gates (see above).
Must set to `0.0` manually when restoring from CudnnLSTM-trained
checkpoints.
state_is_tuple: If True, accepted and returned states are 2-tuples of
the `c_state` and `m_state`. If False, they are concatenated
along the column axis. The latter behavior will soon be deprecated.
activation: Activation function of the inner states. Default: `tanh`.
reuse: (optional) Python boolean describing whether to reuse variables
in an existing scope. If not `True`, and the existing scope already has
the given variables, an error is raised.
When restoring from CudnnLSTM-trained checkpoints, must use
CudnnCompatibleLSTMCell instead.
"""
super(tf.nn.rnn_cell.BasicLSTMCell, self).__init__(_reuse=reuse)
if not state_is_tuple:
tf.logging.warn("%s: Using a concatenated state is slower and will soon be "
"deprecated. Use state_is_tuple=True.", self)
self._num_units = num_units
self._forget_bias = forget_bias
self._state_is_tuple = state_is_tuple
self._activation = activation or tf.nn.tanh
self._linear = None
self._dense = dense
def call(self, inputs, state):
"""Long short-term memory cell (LSTM).
Args:
inputs: `2-D` tensor with shape `[batch_size x input_size]`.
state: An `LSTMStateTuple` of state tensors, each shaped
`[batch_size x self.state_size]`, if `state_is_tuple` has been set to
`True`. Otherwise, a `Tensor` shaped
`[batch_size x 2 * self.state_size]`.
Returns:
A pair containing the new hidden state, and the new state (either a
`LSTMStateTuple` or a concatenated state, depending on
`state_is_tuple`).
"""
sigmoid = tf.sigmoid
# Parameters of gates are concatenated into one multiply for efficiency.
if self._state_is_tuple:
c, h = state
else:
c, h = tf.split(value=state, num_or_size_splits=2, axis=1)
if self._linear is None:
self._linear = _Linear([inputs, h], 4 * self._num_units, True)
i, j, f, o = tf.split(value=self._linear([inputs, h]), num_or_size_splits=4, axis=1)
new_c = (c * sigmoid(f + self._forget_bias) + sigmoid(i) * self._activation(j))
new_h_1 = self._activation(new_c) * sigmoid(o)
w_h, b_h = self.weight_bias([self._num_units, self._num_units], [self._num_units])
new_h_2 = sigmoid(tf.matmul(h, w_h) + b_h)
new_h = new_h_1 + new_h_2
if self._state_is_tuple:
new_state = LSTMStateTuple(new_c, new_h)
else:
new_state = tf.concat([new_c, new_h], 1)
return new_h, new_state
def weight_bias(self, W_shape, b_shape, bias_init=0.1):
"""Fully connected highway layer adopted from
https://github.com/fomorians/highway-fcn/blob/master/main.py
"""
W = tf.get_variable("weight_h_2", shape=W_shape, initializer=tf.truncated_normal_initializer(stddev=0.1))
b = tf.get_variable("bias_h_2", shape=b_shape)
return W, b
class RSLSTMCell(tf.nn.rnn_cell.BasicLSTMCell):
"""Basic LSTM recurrent network cell.
The implementation is based on: http://arxiv.org/abs/1409.2329.
We add forget_bias (default: 1) to the biases of the forget gate in order to
reduce the scale of forgetting in the beginning of the training.
It does not allow cell clipping, a projection layer, and does not
use peep-hole connections: it is the basic baseline.
For advanced models, please use the full @{tf.nn.rnn_cell.LSTMCell}
that follows.
"""
def __init__(self, num_units, forget_bias=1.0, state_is_tuple=True, activation=None, reuse=None, dense=None,
file_name='tweet', type='enc', component=1, partition=1):
"""Initialize the basic LSTM cell.
Args:
num_units: int, The number of units in the LSTM cell.
forget_bias: float, The bias added to forget gates (see above).
Must set to `0.0` manually when restoring from CudnnLSTM-trained
checkpoints.
state_is_tuple: If True, accepted and returned states are 2-tuples of
the `c_state` and `m_state`. If False, they are concatenated
along the column axis. The latter behavior will soon be deprecated.
activation: Activation function of the inner states. Default: `tanh`.
reuse: (optional) Python boolean describing whether to reuse variables
in an existing scope. If not `True`, and the existing scope already has
the given variables, an error is raised.
When restoring from CudnnLSTM-trained checkpoints, must use
CudnnCompatibleLSTMCell instead.
"""
super(tf.nn.rnn_cell.BasicLSTMCell, self).__init__(_reuse=reuse)
if not state_is_tuple:
tf.logging.warn("%s: Using a concatenated state is slower and will soon be "
"deprecated. Use state_is_tuple=True.", self)
self._num_units = num_units
self._forget_bias = forget_bias
self._state_is_tuple = state_is_tuple
self._activation = activation or tf.nn.tanh
self._linear = None
self._dense = dense
self._step = 0
self._file_name = file_name
self._type = type
self._component = component
self._partition = partition
if not os.path.exists('./weight/' + self._file_name):
os.makedirs('./weight/' + self._file_name)
if not os.path.exists('./weight/' + self._file_name + '/' + str(self._partition)):
os.makedirs('./weight/' + self._file_name + '/' + str(self._partition))
if not os.path.exists('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component)):
os.makedirs('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component))
if not os.path.exists('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component) + '/' + self._type):
os.makedirs('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component) + '/' + self._type)
def call(self, inputs, state):
"""Long short-term memory cell (LSTM).
Args:
inputs: `2-D` tensor with shape `[batch_size x input_size]`.
state: An `LSTMStateTuple` of state tensors, each shaped
`[batch_size x self.state_size]`, if `state_is_tuple` has been set to
`True`. Otherwise, a `Tensor` shaped
`[batch_size x 2 * self.state_size]`.
Returns:
A pair containing the new hidden state, and the new state (either a
`LSTMStateTuple` or a concatenated state, depending on
`state_is_tuple`).
"""
sigmoid = tf.sigmoid
self._step = self._step + 1
# Parameters of gates are concatenated into one multiply for efficiency.
if self._state_is_tuple:
c, h = state
else:
c, h = tf.split(value=state, num_or_size_splits=2, axis=1)
if self._linear is None:
self._linear = _Linear([inputs, h], 4 * self._num_units, True)
i, j, f, o = tf.split(value=self._linear([inputs, h]), num_or_size_splits=4, axis=1)
new_c = (c * sigmoid(f + self._forget_bias) + sigmoid(i) * self._activation(j))
new_h_1 = self._activation(new_c) * sigmoid(o)
w_h, b_h = self.weight_bias([self._num_units, self._num_units], [self._num_units])
new_h_2 = sigmoid(tf.matmul(h, w_h) + b_h)
masked_w1, masked_w2 = self.masked_weight(_load=False)
new_h = new_h_1 * masked_w1 + new_h_2 * masked_w2
if self._state_is_tuple:
new_state = LSTMStateTuple(new_c, new_h)
else:
new_state = tf.concat([new_c, new_h], 1)
return new_h, new_state
def weight_bias(self, W_shape, b_shape, bias_init=0.1):
"""Fully connected highway layer adopted from
https://github.com/fomorians/highway-fcn/blob/master/main.py
"""
W = tf.get_variable("weight_h_2", shape=W_shape, initializer=tf.truncated_normal_initializer(stddev=0.1))
b = tf.get_variable("bias_h_2", shape=b_shape)
return W, b
def masked_weight(self, _load=False):
if _load==False:
masked_W1 = np.random.randint(2, size=1)
if masked_W1 == 0:
masked_W2 = 1
else:
masked_W2 = np.random.randint(2, size=1)
np.save('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component) + '/'
+ self._type + '/W1_' + str(self._step), masked_W1)
np.save('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component) + '/'
+ self._type + '/W2_' + str(self._step), masked_W2)
else:
masked_W1 = np.load('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component)
+ '/' + str(self._type) + '/W1_' + str(self._step) + '.npy')
masked_W2 = np.load('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component)
+ '/' + str(self._type) + '/W2_' + str(self._step) + '.npy')
tf_mask_W1 = tf.constant(masked_W1, dtype=tf.float32)
tf_mask_W2 = tf.constant(masked_W2, dtype=tf.float32)
return tf_mask_W1, tf_mask_W2
class RKLSTMCell(tf.nn.rnn_cell.BasicLSTMCell):
"""Basic LSTM recurrent network cell.
The implementation is based on: http://arxiv.org/abs/1409.2329.
We add forget_bias (default: 1) to the biases of the forget gate in order to
reduce the scale of forgetting in the beginning of the training.
It does not allow cell clipping, a projection layer, and does not
use peep-hole connections: it is the basic baseline.
For advanced models, please use the full @{tf.nn.rnn_cell.LSTMCell}
that follows.
"""
def __init__(self, num_units, forget_bias=1.0, state_is_tuple=True, activation=None, reuse=None, dense=None,
file_name='tweet', type='enc', component=1, partition=1, skip_size=3):
"""Initialize the basic LSTM cell.
Args:
num_units: int, The number of units in the LSTM cell.
forget_bias: float, The bias added to forget gates (see above).
Must set to `0.0` manually when restoring from CudnnLSTM-trained
checkpoints.
state_is_tuple: If True, accepted and returned states are 2-tuples of
the `c_state` and `m_state`. If False, they are concatenated
along the column axis. The latter behavior will soon be deprecated.
activation: Activation function of the inner states. Default: `tanh`.
reuse: (optional) Python boolean describing whether to reuse variables
in an existing scope. If not `True`, and the existing scope already has
the given variables, an error is raised.
When restoring from CudnnLSTM-trained checkpoints, must use
CudnnCompatibleLSTMCell instead.
"""
super(tf.nn.rnn_cell.BasicLSTMCell, self).__init__(_reuse=reuse)
if not state_is_tuple:
tf.logging.warn("%s: Using a concatenated state is slower and will soon be "
"deprecated. Use state_is_tuple=True.", self)
self._num_units = num_units
self._forget_bias = forget_bias
self._state_is_tuple = state_is_tuple
self._activation = activation or tf.nn.tanh
self._linear = None
self._dense = dense
self._step = 0
self._file_name = file_name
self._type = type
self._component = component
self._partition = partition
self._skip_size = skip_size
if not os.path.exists('./weight/' + self._file_name):
os.makedirs('./weight/' + self._file_name)
if not os.path.exists('./weight/' + self._file_name + '/' + str(self._partition)):
os.makedirs('./weight/' + self._file_name + '/' + str(self._partition))
if not os.path.exists('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component)):
os.makedirs('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component))
if not os.path.exists('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component) + '/' + self._type):
os.makedirs('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component) + '/' + self._type)
def call(self, inputs, state):
"""Long short-term memory cell (LSTM).
Args:
inputs: `2-D` tensor with shape `[batch_size x input_size]`.
state: An `LSTMStateTuple` of state tensors, each shaped
`[batch_size x self.state_size]`, if `state_is_tuple` has been set to
`True`. Otherwise, a `Tensor` shaped
`[batch_size x 2 * self.state_size]`.
Returns:
A pair containing the new hidden state, and the new state (either a
`LSTMStateTuple` or a concatenated state, depending on
`state_is_tuple`).
"""
sigmoid = tf.sigmoid
self._step = self._step + 1
# Parameters of gates are concatenated into one multiply for efficiency.
if self._state_is_tuple:
c, h = state
else:
c, h = tf.split(value=state, num_or_size_splits=2, axis=1)
if self._linear is None:
self._linear = _Linear([inputs, h], 4 * self._num_units, True)
i, j, f, o = tf.split(value=self._linear([inputs, h]), num_or_size_splits=4, axis=1)
new_c = (c * sigmoid(f + self._forget_bias) + sigmoid(i) * self._activation(j))
new_h_cnt = self._activation(new_c) * sigmoid(o)
if self._step % self._skip_size == 0:
w_h_skip, b_h_skip = self.weight_bias([self._num_units, self._num_units], [self._num_units])
new_h_skip = sigmoid(tf.matmul(h, w_h_skip) + b_h_skip)
masked_w1, masked_w2 = self.masked_weight(_load=False)
new_h = new_h_cnt * masked_w1 + new_h_skip * masked_w2
else:
new_h = new_h_cnt
if self._state_is_tuple:
new_state = LSTMStateTuple(new_c, new_h)
else:
new_state = tf.concat([new_c, new_h], 1)
return new_h, new_state
def weight_bias(self, W_shape, b_shape, bias_init=0.1):
"""Fully connected highway layer adopted from
https://github.com/fomorians/highway-fcn/blob/master/main.py
"""
W = tf.get_variable("weight_h_2", shape=W_shape, initializer=tf.truncated_normal_initializer(stddev=0.1))
b = tf.get_variable("bias_h_2", shape=b_shape)
return W, b
def masked_weight(self, _load=False):
if _load==False:
masked_W1 = np.random.randint(2, size=1)
if masked_W1 == 0:
masked_W2 = 1
else:
masked_W2 = np.random.randint(2, size=1)
np.save('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component) + '/'
+ self._type + '/W1_' + str(self._step), masked_W1)
np.save('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component) + '/'
+ self._type + '/W2_' + str(self._step), masked_W2)
else:
masked_W1 = np.load('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component)
+ '/' + str(self._type) + '/W1_' + str(self._step) + '.npy')
masked_W2 = np.load('./weight/' + self._file_name + '/' + str(self._partition) + '/' + str(self._component)
+ '/' + str(self._type) + '/W2_' + str(self._step) + '.npy')
tf_mask_W1 = tf.constant(masked_W1, dtype=tf.float32)
tf_mask_W2 = tf.constant(masked_W2, dtype=tf.float32)
return tf_mask_W1, tf_mask_W2
class RGRUCell(tf.nn.rnn_cell.GRUCell):
"""Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078).
Args:
num_units: int, The number of units in the GRU cell.
activation: Nonlinearity to use. Default: `tanh`.
reuse: (optional) Python boolean describing whether to reuse variables
in an existing scope. If not `True`, and the existing scope already has
the given variables, an error is raised.
kernel_initializer: (optional) The initializer to use for the weight and
projection matrices.
bias_initializer: (optional) The initializer to use for the bias.
"""
def __init__(self, num_units, activation=None, reuse=None, kernel_initializer=None, bias_initializer=None):
super(tf.nn.rnn_cell.GRUCell, self).__init__(_reuse=reuse)
self._num_units = num_units
self._activation = activation or tf.nn.tanh
self._kernel_initializer = kernel_initializer
self._bias_initializer = bias_initializer
self._gate_linear = None
self._candidate_linear = None
@property
def state_size(self):
return self._num_units
@property
def output_size(self):
return self._num_units
def call(self, inputs, state):
"""Gated recurrent unit (GRU) with nunits cells."""
sigmoid = tf.sigmoid
if self._gate_linear is None:
bias_ones = self._bias_initializer
if self._bias_initializer is None:
bias_ones = init_ops.constant_initializer(1.0, dtype=inputs.dtype)
with vs.variable_scope("gates"): # Reset gate and update gate.
self._gate_linear = _Linear([inputs, state], 2 * self._num_units, True, bias_initializer=bias_ones, kernel_initializer=self._kernel_initializer)
value = sigmoid(self._gate_linear([inputs, state]))
r, u = tf.split(value=value, num_or_size_splits=2, axis=1)
r_state = r * state
if self._candidate_linear is None:
with vs.variable_scope("candidate"):
self._candidate_linear = _Linear([inputs, r_state], self._num_units, True, bias_initializer=self._bias_initializer, kernel_initializer=self._kernel_initializer)
c = self._activation(self._candidate_linear([inputs, r_state]))
new_h = (u * state + (1 - u) * c)
return new_h, new_h
def weight_bias(self, W_shape, b_shape, bias_init=0.1):
"""Fully connected highway layer adopted from
https://github.com/fomorians/highway-fcn/blob/master/main.py
"""
W = tf.get_variable("weight_h_2", shape=W_shape, initializer=tf.truncated_normal_initializer(stddev=0.1))
b = tf.get_variable("bias_h_2", shape=b_shape)
return W, b
def Model(_j, _abnormal_data, _abnormal_label, _hidden_num, _elem_num, _file_name, _partition):
tf.reset_default_graph()
g = tf.Graph()
with g.as_default():
# placeholder list
p_input = tf.placeholder(tf.float32, shape=(batch_num, _abnormal_data.shape[1], _abnormal_data.shape[2]))
# create RNN cell
if cell_type == 0:
enc_cell = tf.nn.rnn_cell.BasicRNNCell(_hidden_num)
dec_cell = tf.nn.rnn_cell.BasicRNNCell(_hidden_num)
if cell_type == 1:
pure_enc_cell = LSTMCell(_hidden_num)
pure_dec_cell = LSTMCell(_hidden_num)
residual_enc_cell = RLSTMCell(_hidden_num)
residual_dec_cell = RLSTMCell(_hidden_num)
enc_cell = RKLSTMCell(_hidden_num, file_name=_file_name, component=_j, partition=_partition, type='enc')
dec_cell = RKLSTMCell(_hidden_num, file_name=_file_name, component=_j, partition=_partition, type='dec')
if cell_type == 2:
pure_enc_cell = GRUCell(_hidden_num)
pure_dec_cell = GRUCell(_hidden_num)
enc_cell = RGRUCell(_hidden_num)
dec_cell = RGRUCell(_hidden_num)
projection_layer = tf.layers.Dense(units=_elem_num, use_bias=True)
# with tf.device('/device:GPU:0'):
with tf.variable_scope('encoder'):
if _j == 0:
(enc_output, enc_state) = tf.nn.dynamic_rnn(pure_enc_cell, p_input, dtype=tf.float32)
elif _j == 1:
(enc_output, enc_state) = tf.nn.dynamic_rnn(residual_enc_cell, p_input, dtype=tf.float32)
else:
(enc_output, enc_state) = tf.nn.dynamic_rnn(enc_cell, p_input, dtype=tf.float32)
# with tf.device('/device:GPU:1'):
with tf.variable_scope('decoder') as vs:
# dec_weight_ = tf.Variable(tf.truncated_normal([hidden_num, elem_num], dtype=tf.float32), name='dec_weight')
# dec_bias_ = tf.Variable(tf.constant(0.1, shape=[elem_num], dtype=tf.float32), name='dec_bias')
if decode_without_input:
dec_input = tf.zeros([p_input.shape[0], p_input.shape[1], p_input.shape[2]], dtype=tf.float32)
if _j == 0:
(dec_outputs, dec_state_) = tf.nn.dynamic_rnn(pure_dec_cell, dec_input, initial_state=enc_state, dtype=tf.float32)
elif _j == 1:
(dec_outputs, dec_state_) = tf.nn.dynamic_rnn(residual_dec_cell, dec_input, initial_state=enc_state, dtype=tf.float32)
else:
(dec_outputs, dec_state_) = tf.nn.dynamic_rnn(dec_cell, dec_input, initial_state=enc_state, dtype=tf.float32)
if reverse:
dec_outputs = dec_outputs[::-1]
else:
dec_input = tf.zeros([p_input.shape[0], p_input.shape[2]], dtype=tf.float32)
inference_helper = tf.contrib.seq2seq.InferenceHelper(
sample_fn=lambda outputs: outputs,
sample_shape=[_elem_num],
sample_dtype=tf.float32,
start_inputs=dec_input,
end_fn=lambda sample_ids: False)
if _j == 0:
inference_decoder = tf.contrib.seq2seq.BasicDecoder(pure_dec_cell, inference_helper, enc_state, output_layer=projection_layer)
elif _j == 1:
inference_decoder = tf.contrib.seq2seq.BasicDecoder(residual_dec_cell, inference_helper, enc_state, output_layer=projection_layer)
else:
inference_decoder = tf.contrib.seq2seq.BasicDecoder(dec_cell, inference_helper, enc_state, output_layer=projection_layer)
dec_outputs, _, _ = tf.contrib.seq2seq.dynamic_decode(inference_decoder, impute_finished=True, maximum_iterations=p_input.shape[1])
if reverse:
dec_outputs = dec_outputs[::-1]
loss = tf.reduce_mean(tf.square(p_input - dec_outputs))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
return g, p_input, dec_outputs, loss, optimizer, saver
def RunModel(_abnormal_data, _abnormal_label, _hidden_num, _elem_num, _file_name, _partition):
error = []
for j in range(ensemble_space):
graph, p_input, dec_outputs, loss, optimizer, saver = Model(j, _abnormal_data, _abnormal_label, _hidden_num, _elem_num, _file_name, _partition)
config = tf.ConfigProto()
# config.gpu_options.allow_growth = True
# config.allow_soft_placement=True
# Add ops to save and restore all the variables.
with tf.Session(graph=graph, config=config) as sess:
sess.run(tf.global_variables_initializer())
for i in range(iteration):
"""Random sequences.
Every sequence has size batch_num * step_num * elem_num
Each step number increases 1 by 1.
An initial number of each sequence is in the range from 0 to 19.
(ex. [8. 9. 10. 11. 12. 13. 14. 15])
"""
(loss_val, _) = sess.run([loss, optimizer], {p_input: _abnormal_data})
# print('iter %d:' % (i + 1), loss_val)
if save_model:
save_path = saver.save(sess, './saved_model/' + pathlib.Path(file_name).parts[0] + '/masked_skip_rnn_seq2seq_' + str(cell_type) + '_' + os.path.basename(file_name) + '.ckpt')
print("Model saved in path: %s" % save_path)
(input_, output_) = sess.run([p_input, dec_outputs], {p_input: _abnormal_data})
error.append(SquareErrorDataPoints(input_, output_[0]))
ensemble_errors = np.asarray(error)
anomaly_score = CalculateFinalAnomalyScore(ensemble_errors)
zscore = Z_Score(anomaly_score)
y_pred = CreateLabelBasedOnZscore(zscore, 3)
if not partition:
score_pred_label = np.c_[ensemble_errors, y_pred, _abnormal_label]
np.savetxt('./saved_result/' + pathlib.Path(file_name).parts[0] + '/masked_skip_rnn_seq2seq_' + os.path.basename(file_name) + '_score.txt', score_pred_label, delimiter=',') # X is an array
precision, recall, f1 = CalculatePrecisionRecallF1Metrics(_abnormal_label, y_pred)
if not partition:
PrintPrecisionRecallF1Metrics(precision, recall, f1)
fpr, tpr, roc_auc = CalculateROCAUCMetrics(_abnormal_label, anomaly_score)
# PlotROCAUC(fpr, tpr, roc_auc)
if not partition:
print('roc_auc=' + str(roc_auc))
precision_curve, recall_curve, average_precision = CalculatePrecisionRecallCurve(_abnormal_label, anomaly_score)
# PlotPrecisionRecallCurve(precision_curve, recall_curve, average_precision)
if not partition:
print('pr_auc=' + str(average_precision))
cks = CalculateCohenKappaMetrics(_abnormal_label, y_pred)
if not partition:
print('cks=' + str(cks))
return anomaly_score, precision, recall, f1, roc_auc, average_precision, cks
if __name__ == '__main__':
batch_num = 1
hidden_num = 8
# step_num = 8
iteration = 30
learning_rate = 1e-3
multivariate = True
reverse = True
decode_without_input = False
partition = True
save_model = False
# cell type 0 => BasicRNN, 1 => LSTM, 2 => GRU
cell_type = 1
try:
sys.argv[1]
sys.argv[2]
except IndexError:
ensemble_space = 20
for n in range(1, 6):
# file name parameter
dataset = n
if dataset == 1:
file_name = './GD/data/Genesis_AnomalyLabels.csv'
print(file_name)
k_partition = 40
abnormal_data, abnormal_label = ReadGDDataset(file_name)
elem_num = 18
if multivariate:
abnormal_data = np.expand_dims(abnormal_data, axis=0)
if partition:
splitted_data, splitted_label = PartitionTimeSeriesKPart(abnormal_data, abnormal_label, _part_number=k_partition)
final_error = []
for i in range(k_partition):
error_partition, precision_partition, recall_partition, f1_partition, roc_auc_partition, pr_auc_partition, cks = RunModel(splitted_data[i], splitted_label[i], hidden_num, elem_num)
final_error.append(error_partition)
# print('-----------------------------------------')
final_error = np.concatenate(final_error).ravel()
final_zscore = Z_Score(final_error)
y_pred = CreateLabelBasedOnZscore(final_zscore, 3)
precision, recall, f1 = CalculatePrecisionRecallF1Metrics(abnormal_label, y_pred)
PrintPrecisionRecallF1Metrics(precision, recall, f1)
_, _, roc_auc = CalculateROCAUCMetrics(abnormal_label, final_error)
print('roc_auc=' + str(roc_auc))
_, _, pr_auc = CalculatePrecisionRecallCurve(abnormal_label, final_error)
print('pr_auc=' + str(pr_auc))
cks = CalculateCohenKappaMetrics(abnormal_label, y_pred)
print('cohen_kappa=' + str(cks))
else:
error, precision, recall, f1, roc_auc, pr_auc, cks = RunModel(abnormal_data, abnormal_label, hidden_num, elem_num)
if dataset == 2:
file_name = './HSS/data/HRSS_anomalous_standard.csv'
print(file_name)
k_partition = 80
abnormal_data, abnormal_label = ReadHSSDataset(file_name)
elem_num = 18
if multivariate:
abnormal_data = np.expand_dims(abnormal_data, axis=0)
if partition:
splitted_data, splitted_label = PartitionTimeSeriesKPart(abnormal_data, abnormal_label, _part_number=k_partition)
final_error = []
for i in range(k_partition):
error_partition, precision_partition, recall_partition, f1_partition, roc_auc_partition, pr_auc_partition, cks = RunModel(splitted_data[i], splitted_label[i], hidden_num, elem_num)
final_error.append(error_partition)
# print('-----------------------------------------')
final_error = np.concatenate(final_error).ravel()
final_zscore = Z_Score(final_error)
y_pred = CreateLabelBasedOnZscore(final_zscore, 3)
precision, recall, f1 = CalculatePrecisionRecallF1Metrics(abnormal_label, y_pred)
PrintPrecisionRecallF1Metrics(precision, recall, f1)
_, _, roc_auc = CalculateROCAUCMetrics(abnormal_label, final_error)
print('roc_auc=' + str(roc_auc))
_, _, pr_auc = CalculatePrecisionRecallCurve(abnormal_label, final_error)
print('pr_auc=' + str(pr_auc))
cks = CalculateCohenKappaMetrics(abnormal_label, y_pred)
print('cohen_kappa=' + str(cks))
else:
error, precision, recall, f1, roc_auc, pr_auc, cks = RunModel(abnormal_data, abnormal_label, hidden_num, elem_num)
if dataset == 3:
for root, dirs, _ in os.walk('./YAHOO/data'):
for dir in dirs:
k_partition = 10
s_precision = []
s_recall = []
s_f1 = []
s_roc_auc = []
s_pr_auc = []
s_cks = []
for _, _, files in os.walk(root + '/' + dir):
for file in files:
file_name = os.path.join(root, dir, file)
print(file_name)
abnormal_data, abnormal_label = ReadS5Dataset(file_name)
elem_num = 1
if multivariate:
abnormal_data = np.expand_dims(abnormal_data, axis=0)
if partition:
splitted_data, splitted_label = PartitionTimeSeriesKPart(abnormal_data,
abnormal_label,
_part_number=k_partition)
final_error = []
for i in range(k_partition):
error_partition, precision_partition, recall_partition, f1_partition, roc_auc_partition, pr_auc_partition, cks = RunModel(
splitted_data[i], splitted_label[i], hidden_num, elem_num, _file_name=file_name, _partition=i)
final_error.append(error_partition)
# print('-----------------------------------------')
final_error = np.concatenate(final_error).ravel()
final_zscore = Z_Score(final_error)
y_pred = CreateLabelBasedOnZscore(final_zscore, 3)
precision, recall, f1 = CalculatePrecisionRecallF1Metrics(abnormal_label, y_pred)
PrintPrecisionRecallF1Metrics(precision, recall, f1)
_, _, roc_auc = CalculateROCAUCMetrics(abnormal_label, final_error)
print('roc_auc=' + str(roc_auc))
_, _, pr_auc = CalculatePrecisionRecallCurve(abnormal_label, final_error)
print('pr_auc=' + str(pr_auc))
cks = CalculateCohenKappaMetrics(abnormal_label, y_pred)
print('cohen_kappa=' + str(cks))
else:
error, precision, recall, f1, roc_auc, pr_auc, cks = RunModel(abnormal_data,
abnormal_label,
hidden_num, elem_num)
s_precision.append(precision)
s_recall.append(recall)
s_f1.append(f1)
s_roc_auc.append(roc_auc)
s_pr_auc.append(pr_auc)
s_cks.append(cks)
print('########################################')
avg_precision = CalculateAverageMetric(s_precision)
print('avg_precision=' + str(avg_precision))
avg_recall = CalculateAverageMetric(s_recall)
print('avg_recall=' + str(avg_recall))
avg_f1 = CalculateAverageMetric(s_f1)
print('avg_f1=' + str(avg_f1))
avg_roc_auc = CalculateAverageMetric(s_roc_auc)
print('avg_roc_auc=' + str(avg_roc_auc))
avg_pr_auc = CalculateAverageMetric(s_pr_auc)
print('avg_pr_auc=' + str(avg_pr_auc))
avg_cks = CalculateAverageMetric(s_cks)
print('avg_cks=' + str(avg_cks))
print('########################################')
if dataset == 4:
for root, dirs, _ in os.walk('./NAB/data'):
for dir in dirs:
k_partition = 10
s_precision = []
s_recall = []
s_f1 = []
s_roc_auc = []
s_pr_auc = []
s_cks = []
for _, _, files in os.walk(root + '/' + dir):
for file in files:
file_name = os.path.join(root, dir, file)
print(file_name)
abnormal_data, abnormal_label = ReadNABDataset(file_name)
elem_num = 1
if multivariate:
abnormal_data = np.expand_dims(abnormal_data, axis=0)
if partition:
splitted_data, splitted_label = PartitionTimeSeriesKPart(abnormal_data,
abnormal_label,
_part_number=k_partition)
final_error = []
for i in range(k_partition):
error_partition, precision_partition, recall_partition, f1_partition, roc_auc_partition, pr_auc_partition, cks = RunModel(
splitted_data[i], splitted_label[i], hidden_num, elem_num, _file_name=os.path.splitext(os.path.basename(file_name))[0], _partition=i)
final_error.append(error_partition)
# print('-----------------------------------------')
final_error = np.concatenate(final_error).ravel()
final_zscore = Z_Score(final_error)
y_pred = CreateLabelBasedOnZscore(final_zscore, 3)
precision, recall, f1 = CalculatePrecisionRecallF1Metrics(abnormal_label, y_pred)
PrintPrecisionRecallF1Metrics(precision, recall, f1)
_, _, roc_auc = CalculateROCAUCMetrics(abnormal_label, final_error)
print('roc_auc=' + str(roc_auc))
_, _, pr_auc = CalculatePrecisionRecallCurve(abnormal_label, final_error)
print('pr_auc=' + str(pr_auc))
cks = CalculateCohenKappaMetrics(abnormal_label, y_pred)
print('cohen_kappa=' + str(cks))
else:
error, precision, recall, f1, roc_auc, pr_auc, cks = RunModel(abnormal_data,
abnormal_label,
hidden_num, elem_num)
s_precision.append(precision)
s_recall.append(recall)
s_f1.append(f1)
s_roc_auc.append(roc_auc)
s_pr_auc.append(pr_auc)
s_cks.append(cks)
print('########################################')
avg_precision = CalculateAverageMetric(s_precision)
print('avg_precision=' + str(avg_precision))
avg_recall = CalculateAverageMetric(s_recall)
print('avg_recall=' + str(avg_recall))
avg_f1 = CalculateAverageMetric(s_f1)
print('avg_f1=' + str(avg_f1))
avg_roc_auc = CalculateAverageMetric(s_roc_auc)
print('avg_roc_auc=' + str(avg_roc_auc))
avg_pr_auc = CalculateAverageMetric(s_pr_auc)
print('avg_pr_auc=' + str(avg_pr_auc))
avg_cks = CalculateAverageMetric(s_cks)
print('avg_cks=' + str(avg_cks))
print('########################################')
if dataset == 5:
k_partition = 3
s_precision = []
s_recall = []
s_f1 = []
s_roc_auc = []
s_pr_auc = []
s_cks = []
for root, dirs, files in os.walk('./2D/test'):
for file in files:
file_name = os.path.join(root, file)
print(file_name)
abnormal_data, abnormal_label = Read2DDataset(file_name)
elem_num = 2
if multivariate:
abnormal_data = np.expand_dims(abnormal_data, axis=0)
if partition:
splitted_data, splitted_label = PartitionTimeSeriesKPart(abnormal_data, abnormal_label,
_part_number=k_partition)
final_error = []
for i in range(k_partition):
error_partition, precision_partition, recall_partition, f1_partition, roc_auc_partition, pr_auc_partition, pr_cks = RunModel(
splitted_data[i], splitted_label[i], _file_name=os.path.splitext(os.path.basename(file_name))[0], _partition=i)
final_error.append(error_partition)
# print('-----------------------------------------')
final_error = np.concatenate(final_error).ravel()
final_zscore = Z_Score(final_error)
y_pred = CreateLabelBasedOnZscore(final_zscore, 3)
precision, recall, f1 = CalculatePrecisionRecallF1Metrics(abnormal_label, y_pred)
PrintPrecisionRecallF1Metrics(precision, recall, f1)
_, _, roc_auc = CalculateROCAUCMetrics(abnormal_label, final_error)
print('roc_auc=' + str(roc_auc))
_, _, pr_auc = CalculatePrecisionRecallCurve(abnormal_label, final_error)
print('pr_auc=' + str(pr_auc))
cks = CalculateCohenKappaMetrics(abnormal_label, y_pred)
print('cohen_kappa=' + str(cks))
else:
error, precision, recall, f1, roc_auc, pr_auc, cks = RunModel(abnormal_data, abnormal_label,
hidden_num, elem_num)
s_precision.append(precision)
s_recall.append(recall)
s_f1.append(f1)
s_roc_auc.append(roc_auc)
s_pr_auc.append(pr_auc)
s_cks.append(cks)
avg_precision = CalculateAverageMetric(s_precision)
print('avg_precision=' + str(avg_precision))
avg_recall = CalculateAverageMetric(s_recall)
print('avg_recall=' + str(avg_recall))
avg_f1 = CalculateAverageMetric(s_f1)
print('avg_f1=' + str(avg_f1))
avg_roc_auc = CalculateAverageMetric(s_roc_auc)
print('avg_roc_auc=' + str(avg_roc_auc))
avg_pr_auc = CalculateAverageMetric(s_pr_auc)
print('avg_pr_auc=' + str(avg_pr_auc))
avg_cks = CalculateAverageMetric(s_cks)
print('avg_cks=' + str(avg_cks))
if dataset == 6:
k_partition = 2
s_precision = []
s_recall = []
s_f1 = []
s_roc_auc = []
s_pr_auc = []
s_cks = []
for root, dirs, _ in os.walk('./UAH/'):
for dir in dirs:
folder_name = os.path.join(root, dir)
print(folder_name)
abnormal_data, abnormal_label = ReadUAHDataset(folder_name)
elem_num = 4
if multivariate:
abnormal_data = np.expand_dims(abnormal_data, axis=0)
if partition:
splitted_data, splitted_label = PartitionTimeSeriesKPart(abnormal_data, abnormal_label,
_part_number=k_partition)
final_error = []
for i in range(k_partition):
error_partition, precision_partition, recall_partition, f1_partition, roc_auc_partition, pr_auc_partition, cks = RunModel(
splitted_data[i], splitted_label[i], hidden_num, elem_num, _file_name=dir, _partition=i)
final_error.append(error_partition)
# print('-----------------------------------------')
final_error = np.concatenate(final_error).ravel()
final_zscore = Z_Score(final_error)
y_pred = CreateLabelBasedOnZscore(final_zscore, 3)
print('########################################')
precision, recall, f1 = CalculatePrecisionRecallF1Metrics(abnormal_label, y_pred)
PrintPrecisionRecallF1Metrics(precision, recall, f1)
_, _, roc_auc = CalculateROCAUCMetrics(abnormal_label, final_error)
print('roc_auc=' + str(roc_auc))
_, _, pr_auc = CalculatePrecisionRecallCurve(abnormal_label, final_error)
print('pr_auc=' + str(pr_auc))
cks = CalculateCohenKappaMetrics(abnormal_label, y_pred)
print('cohen_kappa=' + str(cks))
print('########################################')
else:
error, precision, recall, f1, roc_auc, pr_auc, cks = RunModel(abnormal_data, abnormal_label,
hidden_num, elem_num)
s_precision.append(precision)
s_recall.append(recall)
s_f1.append(f1)
s_roc_auc.append(roc_auc)
s_pr_auc.append(pr_auc)
s_cks.append(cks)
print('########################################')
avg_precision = CalculateAverageMetric(s_precision)
print('avg_precision=' + str(avg_precision))
avg_recall = CalculateAverageMetric(s_recall)
print('avg_recall=' + str(avg_recall))
avg_f1 = CalculateAverageMetric(s_f1)
print('avg_f1=' + str(avg_f1))
avg_roc_auc = CalculateAverageMetric(s_roc_auc)
print('avg_roc_auc=' + str(avg_roc_auc))
avg_pr_auc = CalculateAverageMetric(s_pr_auc)
print('avg_pr_auc=' + str(avg_pr_auc))
avg_cks = CalculateAverageMetric(s_cks)
print('avg_cks=' + str(avg_cks))
print('########################################')
if dataset == 7:
for root, dirs, _ in os.walk('./ECG/'):
for dir in dirs:
k_partition = 3
s_precision = []
s_recall = []
s_f1 = []
s_roc_auc = []
s_pr_auc = []
s_cks = []
for _, _, files in os.walk(root + '/' + dir):
for file in files:
file_name = os.path.join(root, dir, file)
print(file_name)
abnormal_data, abnormal_label = ReadECGDataset(file_name)
elem_num = 3
if multivariate:
abnormal_data = np.expand_dims(abnormal_data, axis=0)
if partition:
splitted_data, splitted_label = PartitionTimeSeriesKPart(abnormal_data,
abnormal_label,
_part_number=k_partition)
final_error = []
for i in range(k_partition):
error_partition, precision_partition, recall_partition, f1_partition, roc_auc_partition, pr_auc_partition, cks = RunModel(
splitted_data[i], splitted_label[i], hidden_num, elem_num,
_file_name=os.path.splitext(os.path.basename(file_name))[0], _partition=i)
final_error.append(error_partition)
# print('-----------------------------------------')
final_error = np.concatenate(final_error).ravel()
final_zscore = Z_Score(final_error)
y_pred = CreateLabelBasedOnZscore(final_zscore, 3)
precision, recall, f1 = CalculatePrecisionRecallF1Metrics(abnormal_label, y_pred)
PrintPrecisionRecallF1Metrics(precision, recall, f1)
_, _, roc_auc = CalculateROCAUCMetrics(abnormal_label, final_error)
print('roc_auc=' + str(roc_auc))
_, _, pr_auc = CalculatePrecisionRecallCurve(abnormal_label, final_error)
print('pr_auc=' + str(pr_auc))
cks = CalculateCohenKappaMetrics(abnormal_label, y_pred)
print('cohen_kappa=' + str(cks))
else:
error, precision, recall, f1, roc_auc, pr_auc, cks = RunModel(abnormal_data,
abnormal_label,
hidden_num, elem_num)
s_precision.append(precision)
s_recall.append(recall)
s_f1.append(f1)
s_roc_auc.append(roc_auc)
s_pr_auc.append(pr_auc)
s_cks.append(cks)
print('########################################')
avg_precision = CalculateAverageMetric(s_precision)
print('avg_precision=' + str(avg_precision))
avg_recall = CalculateAverageMetric(s_recall)
print('avg_recall=' + str(avg_recall))
avg_f1 = CalculateAverageMetric(s_f1)
print('avg_f1=' + str(avg_f1))
avg_roc_auc = CalculateAverageMetric(s_roc_auc)
print('avg_roc_auc=' + str(avg_roc_auc))
avg_pr_auc = CalculateAverageMetric(s_pr_auc)
print('avg_pr_auc=' + str(avg_pr_auc))
avg_cks = CalculateAverageMetric(s_cks)
print('avg_cks=' + str(avg_cks))
print('########################################')
else:
# file name parameter
dataset = int(sys.argv[1])
ensemble_space = int(sys.argv[2])
if dataset == 1:
file_name = './GD/data/Genesis_AnomalyLabels.csv'
print(file_name)
k_partition = 40
abnormal_data, abnormal_label = ReadGDDataset(file_name)
elem_num = 18
if multivariate:
abnormal_data = np.expand_dims(abnormal_data, axis=0)
if partition: