-
Notifications
You must be signed in to change notification settings - Fork 30
/
algorithm.py
194 lines (155 loc) · 6.74 KB
/
algorithm.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
import pandas as pd
import numpy as np
import argparse
import json
import sys
from pathlib import Path
import tarfile
import tensorflow as tf
from omni_anomaly.prediction import Predictor
from omni_anomaly.model import OmniAnomaly
from omni_anomaly.training import Trainer
from tfsnippet.scaffold import VariableSaver
from tfsnippet.utils import get_variables_as_dict
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
MODEL_DIR = Path("./tf-model")
class CustomParameters:
def __init__(self, **params):
self.dictionary = {
"use_connected_z_q": True,
"use_connected_z_p": True,
# model parameters
"x_dim": 1,
"z_dim": 3,
"rnn_cell": 'GRU', # 'GRU', 'LSTM' or 'Basic'
"rnn_num_hidden": 500,
"window_size": 100,
"dense_dim": 500,
"posterior_flow_type": 'nf', # 'nf' or None
"nf_layers": 20, # for nf
"epochs": 10, # max_epoch
"train_start": 0, # not exposed, always from beginning!
"test_start": 0, # not exposed, always from beginning!
"batch_size": 50,
"l2_reg": 0.0001,
"learning_rate": 0.001,
"lr_anneal_factor": 0.5,
"lr_anneal_epoch_freq": 40,
"lr_anneal_step_freq": None,
"std_epsilon": 1e-4,
"valid_step_freq": 100,
"gradient_clip_norm": 10.,
"early_stop": True, # whether to apply early stop method
"level": 0.01,
"test_n_z": 1,
"save_z": False, # whether to save sampled z in hidden space
"get_score_on_dim": False, # whether to get score on dim. If `True`, the score will be a 2-dim ndarray
"save_dir": 'model',
"restore_dir": None, # If not None, restore variables from this dir
"result_dir": '.', # Where to save the result file
"train_score_filename": 'train_score.pkl',
"test_score_filename": 'test_score.pkl',
"random_state": 42,
"split": 0.8
}
def set_renamed_param(params, k_orig, k_new):
if k_new in params:
self.dictionary[k_orig] = params[k_new]
self.dictionary = dict((k, params.get(k, v)) for k, v in self.dictionary.items())
set_renamed_param(params, "z_dim", "latent_size")
set_renamed_param(params, "rnn_num_hidden", "rnn_hidden_size")
set_renamed_param(params, "dense_dim", "linear_hidden_size")
for k, v in self.dictionary.items():
setattr(self, k, v)
class AlgorithmArgs(argparse.Namespace):
@property
def ts(self) -> np.ndarray:
dataset = pd.read_csv(self.dataInput)
return dataset.values[:, 1:-1]
@staticmethod
def from_sys_args() -> 'AlgorithmArgs':
args: dict = json.loads(sys.argv[1])
args["customParameters"] = CustomParameters(**args.get("customParameters", {}))
return AlgorithmArgs(**args)
class OldTFModelSaver:
def __init__(self, model_vs, args: AlgorithmArgs, model_dir: Path = MODEL_DIR):
self.model_vs = model_vs
self.args = args
self.model_dir = model_dir
self.model_dir.mkdir(parents=True, exist_ok=True)
def save(self):
var_dict = get_variables_as_dict(self.model_vs)
saver = VariableSaver(var_dict, self.model_dir)
saver.save()
# write archive with model files
with tarfile.open(self.args.modelOutput, "w:gz") as f:
f.add(self.model_dir)
def load(self):
# decompress archive with model files
with tarfile.open(self.args.modelInput, "r:gz") as f:
f.extractall()
saver = VariableSaver(get_variables_as_dict(self.model_vs), self.model_dir)
saver.restore()
def prepare_data(args: AlgorithmArgs) -> np.ndarray:
ts = args.ts
args.customParameters.x_dim = ts.shape[1]
return ts
def train(args: AlgorithmArgs):
ts = prepare_data(args)
config = args.customParameters
with tf.Session().as_default():
with tf.variable_scope('model') as model_vs:
def save():
return OldTFModelSaver(model_vs, args).save()
model = OmniAnomaly(config=config, name="model")
trainer = Trainer(model=model,
model_vs=model_vs,
max_epoch=config.epochs,
batch_size=config.batch_size,
valid_batch_size=config.batch_size,
initial_lr=config.learning_rate,
lr_anneal_epochs=config.lr_anneal_epoch_freq,
lr_anneal_factor=config.lr_anneal_factor,
grad_clip_norm=config.gradient_clip_norm,
valid_step_freq=config.valid_step_freq)
trainer.fit(ts, valid_portion=1-config.split, with_stats=False, save_model_fn=save)
save()
def execute(args: AlgorithmArgs):
ts = prepare_data(args)
config = args.customParameters
with tf.Session().as_default():
with tf.variable_scope('model') as model_vs:
model = OmniAnomaly(config=config, name="model")
# initializes variables so they can be filled when loading the model from file
Trainer(model=model,
model_vs=model_vs,
max_epoch=config.epochs,
batch_size=config.batch_size,
valid_batch_size=config.batch_size,
initial_lr=config.learning_rate,
lr_anneal_epochs=config.lr_anneal_epoch_freq,
lr_anneal_factor=config.lr_anneal_factor,
grad_clip_norm=config.gradient_clip_norm,
valid_step_freq=config.valid_step_freq)
OldTFModelSaver(model_vs, args).load()
predictor = Predictor(model, batch_size=config.batch_size, n_z=config.test_n_z, last_point_only=True)
# negate as recommended in predictor.get_score(...)
scores = predictor.get_score(ts, with_stats=False) * -1
scores.tofile(args.dataOutput, sep="\n")
def set_random_state(config: AlgorithmArgs) -> None:
seed = config.customParameters.random_state
import random
random.seed(seed)
np.random.seed(seed)
tf.set_random_seed(seed)
if __name__ == "__main__":
args = AlgorithmArgs.from_sys_args()
set_random_state(args)
if args.executionType == "train":
train(args)
elif args.executionType == "execute":
execute(args)
else:
ValueError(f"No executionType '{args.executionType}' available! Choose either 'train' or 'execute'.")