- Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathmodel.py
91 lines (78 loc) · 3.2 KB
/
model.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
# Copyright 2019 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Loads model
"""
importattr
fromos.pathimportisfile
fromtypingimport*
fromprecise.functionsimportload_keras, false_pos, false_neg, weighted_log_loss, set_loss_bias
fromprecise.paramsimportinject_params, pr
ifTYPE_CHECKING:
fromkeras.modelsimportSequential
@attr.s()
classModelParams:
"""
Attributes:
recurrent_units: Number of GRU units. Higher values increase computation
but allow more complex learning. Too high of a value causes overfitting
dropout: Reduces overfitting but can potentially decrease accuracy if too high
extra_metrics: Whether to include false positive and false negative metrics while training
skip_acc: Whether to skip accuracy calculation while training
loss_bias: Near 1.0 reduces false positives. See <set_loss_bias>
freeze_till: Layer number from start to freeze after loading (allows for partial training)
"""
recurrent_units=attr.ib(20) # type: int
dropout=attr.ib(0.2) # type: float
extra_metrics=attr.ib(False) # type: bool
skip_acc=attr.ib(False) # type: bool
loss_bias=attr.ib(0.7) # type: float
freeze_till=attr.ib(0) # type: int
defload_precise_model(model_name: str) ->Any:
"""Loads a Keras model from file, handling custom loss function"""
ifnotmodel_name.endswith('.net'):
print('Warning: Unknown model type, ', model_name)
inject_params(model_name)
returnload_keras().models.load_model(model_name)
defcreate_model(model_name: Optional[str], params: ModelParams) ->'Sequential':
"""
Load or create a precise model
Args:
model_name: Name of model
params: Parameters used to create the model
Returns:
model: Loaded Keras model
"""
ifmodel_nameandisfile(model_name):
print('Loading from '+model_name+'...')
model=load_precise_model(model_name)
else:
fromkeras.layers.coreimportDense
fromkeras.layers.recurrentimportGRU
fromkeras.modelsimportSequential
model=Sequential()
model.add(GRU(
params.recurrent_units, activation='linear',
input_shape=(
pr.n_features, pr.feature_size), dropout=params.dropout, name='net'
))
model.add(Dense(1, activation='sigmoid'))
load_keras()
metrics= ['accuracy'] +params.extra_metrics* [false_pos, false_neg]
set_loss_bias(params.loss_bias)
foriinmodel.layers[:params.freeze_till]:
i.trainable=False
model.compile('rmsprop', weighted_log_loss,
metrics=(notparams.skip_acc) *metrics)
returnmodel