-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathNNPredictor_Multihead.py
91 lines (63 loc) · 2.83 KB
/
NNPredictor_Multihead.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
# Neural Network Binary Classifier: this subclass uses a Transformer model
import numpy as np
from pandas import DataFrame, Series
import pandas as pd
pd.options.mode.chained_assignment = None # default='warn'
# Strategy specific imports, files must reside in same folder as strategy
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent))
sys.path.append(str(Path(__file__).parent.parent))
import logging
import warnings
log = logging.getLogger(__name__)
# log.setLevel(logging.DEBUG)
warnings.simplefilter(action='ignore', category=pd.errors.PerformanceWarning)
import random
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
os.environ['TF_DETERMINISTIC_OPS'] = '1'
import tensorflow as tf
seed = 42
os.environ['PYTHONHASHSEED'] = str(seed)
random.seed(seed)
tf.random.set_seed(seed)
np.random.seed(seed)
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.WARN)
#import keras
# from tensorflow.python.keras import layers
from utils.ClassifierKerasLinear import ClassifierKerasLinear
import h5py
class NNPredictor_Multihead(ClassifierKerasLinear):
is_trained = False
clean_data_required = False # training data can contain anomalies
model_per_pair = False # separate model per pair
# override the build_model function in subclasses
def create_model(self, seq_len, num_features):
dropout = 0.1
input_shape = (seq_len, num_features)
inputs = tf.keras.Input(shape=input_shape)
x = inputs
x = tf.keras.layers.LSTM(64, return_sequences=True, activation='tanh', input_shape=input_shape)(x)
x = tf.keras.layers.Dropout(dropout)(x)
x = tf.keras.layers.Dense(num_features)(x)
x = tf.keras.layers.LayerNormalization(epsilon=1e-6)(x)
# "ATTENTION LAYER"
# x = tf.keras.layers.MultiHeadAttention(key_dim=num_features, num_heads=3, dropout=dropout)(x, x, x)
x = tf.keras.layers.MultiHeadAttention(key_dim=num_features, num_heads=3, dropout=dropout)(x, x)
x = tf.keras.layers.Dropout(0.1)(x)
res = x + inputs
# FEED FORWARD Part - you can stick anything here or just delete the whole section - it will still work.
x = tf.keras.layers.LayerNormalization(epsilon=1e-6)(res)
x = tf.keras.layers.Conv1D(filters=seq_len, kernel_size=1, activation="relu")(x)
x = tf.keras.layers.Dropout(dropout)(x)
x = tf.keras.layers.Conv1D(filters=num_features, kernel_size=1)(x)
x = x + res
# remplace sequence column with the average value
x = tf.keras.layers.GlobalAveragePooling1D()(x)
x = tf.keras.layers.Dense(16)(x)
x = tf.keras.layers.Dropout(0.1)(x)
# last layer is a linear decision - do not change
outputs = tf.keras.layers.Dense(1, activation="linear")(x)
model = tf.keras.Model(inputs, outputs, name=self.name)
return model