/
automodel.py
126 lines (93 loc) · 4.52 KB
/
automodel.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
class AutoModel:
def __init__(self, task, experiment_name, metric=None):
'''
Creates an input model for Scan(). Optimized for being used together
with Params(). For example:
p = talos.AutoParams().params
model = talos.AutoModel(task='binary').model
talos.Scan(x, y, p, model)
NOTE: the parameter space from Params() is very large, so use limits
in or reducers in Scan() accordingly.
task : string or None
If 'continuous' then mae is used for metric, if 'binary',
'multiclass', or 'multilabel', f1score is used. Accuracy is always
used.
experiment_name | str | Must be same as in `Scan()`
metric : None or list
You can also input a list with one or more custom metrics or names
of Keras or Talos metrics.
'''
from talos.utils.experiment_log_callback import ExperimentLogCallback
self.task = task
self.experiment_name = experiment_name
self.metric = metric
if self.task is not None:
self.metrics = self._set_metric()
elif self.metric is not None and isinstance(self.metric, list):
self.metrics = self.metric + ['acc']
else:
print("Either pick task or provide list as input for metric.")
# create the model
self.model = self._create_input_model
self.callback = ExperimentLogCallback
def _set_metric(self):
"""Sets the metric for the model based on the experiment type
or a list of metrics from user."""
import talos as ta
if self.task in ['binary', 'multiclass', 'multilabel']:
return [ta.utils.metrics.f1score, 'acc']
elif self.task == 'continuous':
return [ta.utils.metrics.mae, 'acc']
def _create_input_model(self, x_train, y_train, x_val, y_val, params):
import wrangle as wr
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Flatten
from tensorflow.keras.layers import LSTM, Conv1D, SimpleRNN, Dense, Bidirectional
model = Sequential()
if params['network'] != 'dense':
x_train = wr.array_reshape_conv1d(x_train)
x_val = wr.array_reshape_conv1d(x_val)
if params['network'] == 'conv1d':
model.add(Conv1D(params['first_neuron'], x_train.shape[1]))
model.add(Flatten())
elif params['network'] == 'lstm':
model.add(LSTM(params['first_neuron']))
if params['network'] == 'bidirectional_lstm':
model.add(Bidirectional(LSTM(params['first_neuron'])))
elif params['network'] == 'simplernn':
model.add(SimpleRNN(params['first_neuron']))
elif params['network'] == 'dense':
model.add(Dense(params['first_neuron'],
input_dim=x_train.shape[1],
activation='relu',
kernel_initializer=params['kernel_initializer']))
model.add(Dropout(params['dropout']))
# add hidden layers to the model
from talos.model.hidden_layers import hidden_layers
hidden_layers(model, params, 1)
# get the right activation and last_neuron based on task
from talos.model.output_layer import output_layer
activation, last_neuron = output_layer(self.task,
params['last_activation'],
y_train,
y_val)
model.add(Dense(last_neuron,
activation=activation,
kernel_initializer=params['kernel_initializer']))
# bundle the optimizer with learning rate changes
from talos.model.normalizers import lr_normalizer
optimizer = params['optimizer'](lr=lr_normalizer(params['lr'],
params['optimizer']))
# compile the model
model.compile(optimizer=optimizer,
loss=params['losses'],
metrics=self.metrics)
# fit the model
out = model.fit(x_train, y_train,
batch_size=params['batch_size'],
epochs=params['epochs'],
verbose=0,
callbacks=[self.callback(self.experiment_name, params)],
validation_data=(x_val, y_val))
# pass the output to Talos
return out, model