- Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathfunctions.py
108 lines (83 loc) · 3.12 KB
/
functions.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
# 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.
"""
Mathematical functions used to customize
computation in various places
"""
frommathimportexp, log, sqrt, pi
importnumpyasnp
fromtypingimport*
LOSS_BIAS=0.9# [0..1] where 1 is inf bias
defset_loss_bias(bias: float):
"""
Changes the loss bias
This allows customizing the acceptable tolerance between
false negatives and false positives
Near 1.0 reduces false positives
Near 0.0 reduces false negatives
"""
globalLOSS_BIAS
LOSS_BIAS=bias
defweighted_log_loss(yt, yp) ->Any:
"""
Binary crossentropy with a bias towards false negatives
yt: Target
yp: Prediction
"""
fromkerasimportbackendasK
pos_loss=-(0+yt) *K.log(0+yp+K.epsilon())
neg_loss=-(1-yt) *K.log(1-yp+K.epsilon())
returnLOSS_BIAS*K.mean(neg_loss) + (1.-LOSS_BIAS) *K.mean(pos_loss)
defweighted_mse_loss(yt, yp) ->Any:
"""Standard mse loss with a weighting between false negatives and positives"""
fromkerasimportbackendasK
total=K.sum(K.ones_like(yt))
neg_loss=total*K.sum(K.square(yp* (1-yt))) /K.sum(1-yt)
pos_loss=total*K.sum(K.square(1.- (yp*yt))) /K.sum(yt)
returnLOSS_BIAS*neg_loss+ (1.-LOSS_BIAS) *pos_loss
deffalse_pos(yt, yp) ->Any:
"""
Metric for Keras that *estimates* false positives while training
This will not be completely accurate because it weights batches
equally
"""
fromkerasimportbackendasK
returnK.sum(K.cast(yp* (1-yt) >0.5, 'float')) /K.maximum(1.0, K.sum(1-yt))
deffalse_neg(yt, yp) ->Any:
"""
Metric for Keras that *estimates* false negatives while training
This will not be completely accurate because it weights batches
equally
"""
fromkerasimportbackendasK
returnK.sum(K.cast((1-yp) * (0+yt) >0.5, 'float')) /K.maximum(1.0, K.sum(0+yt))
defload_keras() ->Any:
"""Imports Keras injecting custom functions to prevent exceptions"""
importkeras
keras.losses.weighted_log_loss=weighted_log_loss
keras.metrics.false_pos=false_pos
keras.metrics.false_positives=false_pos
keras.metrics.false_neg=false_neg
returnkeras
defsigmoid(x):
"""Sigmoid squashing function for scalars"""
return1/ (1+exp(-x))
defasigmoid(x):
"""Inverse sigmoid (logit) for scalars"""
return-log(1/x-1)
defpdf(x, mu, std):
"""Probability density function (normal distribution)"""
ifstd==0:
return0
return (1.0/ (std*sqrt(2*pi))) *np.exp(-(x-mu) **2/ (2*std**2))