-
Notifications
You must be signed in to change notification settings - Fork 3
/
replicate_result.py
60 lines (50 loc) · 1.6 KB
/
replicate_result.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
import tensorflow as tf
import pandas as pd
import numpy as np
import glob
import datetime
from sklearn.preprocessing import robust_scale
from tqdm.auto import tqdm
import argparse
from preprocessing import *
from AdvALSTM import *
class TestEvaluation(tf.keras.callbacks.Callback):
def __init__(self, X_test, y_test):
super(TestEvaluation, self).__init__()
self.X_test, self.y_test = X_test, y_test
def on_epoch_end(self, epoch, logs):
X_test, y_test = self.X_test, self.y_test
test_metrics = self.model.evaluate(self.X_test, self.y_test, verbose = 0)
logs["test_loss"] = test_metrics[0]
logs["test_acc"] = test_metrics[1]
def main():
# Load preprocessed data
with open('preprocessed_data.npz', 'rb') as f:
X_train = np.load(f, allow_pickle=True)
y_train = np.load(f, allow_pickle=True)
X_validation = np.load(f, allow_pickle=True)
y_validation = np.load(f, allow_pickle=True)
X_test = np.load(f, allow_pickle=True)
y_test = np.load(f, allow_pickle=True)
# Create model
model = AdvALSTM(
units = 16,
epsilon = 0.1,
beta = 0.05,
learning_rate = 1E-2,
l2 = 0.001,
attention = True,
hinge = True,
dropout = 0.0,
adversarial_training = True,
random_perturbations = False
)
model.fit(
X_train, y_train,
validation_data = (X_validation, y_validation),
epochs = 300,
batch_size = 1024,
callbacks=[TestEvaluation(X_test, y_test)]
)
if __name__ == "__main__":
main()