-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
118 lines (81 loc) · 2.94 KB
/
util.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
# Adversarial Anomaly Detection
# - Utility functions
#
# Author: Seongho Baek
# Contact: [email protected]
#
# ==============================================================================
import math
import numpy as np
import tensorflow as tf
def get_batch(X, X_, size):
# X, X_ must be nd-array
a = np.random.choice(len(X), size, replace=False)
return X[a], X_[a]
def get_sequence_batch(X, seq_length, batch_size):
# print('input dim:', len(X[0]), ', seq len:', seq_length, ', batch_size:', batch_size)
# X must be nd-array
a = np.random.choice(len(X)-seq_length, batch_size, replace=False)
a = a + seq_length
# print('index: ', a)
seq = []
for i in range(batch_size):
if a[i] < seq_length - 1:
s = np.random.normal(0.0, 0.1, [seq_length, len(X[0])])
seq.append(s)
else:
s = np.arange(a[i]-seq_length, a[i])
seq.append(X[s])
seq = np.array(seq)
return X[a], seq
def noise_validator(noise, allowed_noises):
'''Validates the noise provided'''
try:
if noise in allowed_noises:
return True
elif noise.split('-')[0] == 'mask' and float(noise.split('-')[1]):
t = float(noise.split('-')[1])
if t >= 0.0 and t <= 1.0:
return True
else:
return False
except:
return False
pass
def sigmoid_normalize(value_list):
list_max = float(max(value_list))
alist = [i/list_max for i in value_list]
alist = [1/(1+math.exp(-i)) for i in alist]
return alist
def swish(logit, name=None):
with tf.name_scope(name):
l = tf.multiply(logit, tf.nn.sigmoid(logit))
return l
def generate_samples(dim, num_inlier, num_outlier, normalize=True):
inlier = np.random.normal(0.0, 1.0, [num_inlier, dim])
sample_inlier = []
if normalize:
inlier = np.transpose(inlier)
for values in inlier:
values = sigmoid_normalize(values)
sample_inlier.append(values)
inlier = np.array(sample_inlier).transpose()
outlier = np.random.normal(1.0, 1.0, [num_outlier, dim])
sample_outlier = []
if normalize:
outlier = np.transpose(outlier)
for values in outlier:
values = sigmoid_normalize(values)
sample_outlier.append(values)
outlier = np.array(sample_outlier).transpose()
return inlier, outlier
def add_gaussian_noise(input_layer, mean, std, truncate=True):
if std < 0.0:
return input_layer
if truncate:
noise = tf.truncated_normal(shape=tf.shape(input_layer), mean=mean, stddev=std, dtype=tf.float32)
else:
noise = tf.random_normal(shape=tf.shape(input_layer), mean=mean, stddev=std, dtype=tf.float32)
return input_layer + noise
def add_uniform_noise(input_layer, min_val, max_val):
return input_layer + tf.random_uniform(shape=tf.shape(input_layer), minval=min_val, maxval=max_val)