Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e8d9bed5
提交
e8d9bed5
authored
3月 23, 2017
作者:
Jeff Donahue
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add sigmoid_binary_crossentropy function and tests
上级
a4126bcc
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
58 行增加
和
1 行删除
+58
-1
__init__.py
theano/tensor/nnet/__init__.py
+2
-1
nnet.py
theano/tensor/nnet/nnet.py
+25
-0
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+31
-0
没有找到文件。
theano/tensor/nnet/__init__.py
浏览文件 @
e8d9bed5
...
@@ -4,7 +4,8 @@ from .nnet import (
...
@@ -4,7 +4,8 @@ from .nnet import (
CrossentropySoftmax1HotWithBiasDx
,
CrossentropySoftmaxArgmax1HotWithBias
,
CrossentropySoftmax1HotWithBiasDx
,
CrossentropySoftmaxArgmax1HotWithBias
,
LogSoftmax
,
Prepend_scalar_constant_to_each_row
,
LogSoftmax
,
Prepend_scalar_constant_to_each_row
,
Prepend_scalar_to_each_row
,
Softmax
,
Prepend_scalar_to_each_row
,
Softmax
,
SoftmaxGrad
,
SoftmaxWithBias
,
binary_crossentropy
,
SoftmaxGrad
,
SoftmaxWithBias
,
binary_crossentropy
,
sigmoid_binary_crossentropy
,
categorical_crossentropy
,
crossentropy_categorical_1hot
,
categorical_crossentropy
,
crossentropy_categorical_1hot
,
crossentropy_categorical_1hot_grad
,
crossentropy_softmax_1hot
,
crossentropy_categorical_1hot_grad
,
crossentropy_softmax_1hot
,
crossentropy_softmax_1hot_with_bias
,
crossentropy_softmax_1hot_with_bias
,
...
...
theano/tensor/nnet/nnet.py
浏览文件 @
e8d9bed5
...
@@ -2017,6 +2017,31 @@ def binary_crossentropy(output, target):
...
@@ -2017,6 +2017,31 @@ def binary_crossentropy(output, target):
return
-
(
target
*
tensor
.
log
(
output
)
+
(
1.0
-
target
)
*
tensor
.
log
(
1.0
-
output
))
return
-
(
target
*
tensor
.
log
(
output
)
+
(
1.0
-
target
)
*
tensor
.
log
(
1.0
-
output
))
def
sigmoid_binary_crossentropy
(
output
,
target
):
"""
Compute the cross-entropy of binary random variables.
`output` should be real-valued (range (-inf, +inf)); `sigmoid` will be
applied to produce a (0, 1) valued input.
`target` is assumed to be probabilities in [0, 1].
Notes
-----
Mathematically equivalent to `binary_crossentropy(sigmoid(output), target)`,
but with more efficient and numerically stable computation.
"""
def
grad
(
inputs
,
out_grads
):
(
output
,
target
),
(
out_grad
,)
=
inputs
,
out_grads
g_output
=
out_grad
*
(
sigmoid
(
output
)
-
target
)
g_target
=
out_grad
*
(
-
output
)
return
[
g_output
,
g_target
]
inp
=
[
output
,
target
]
outp
=
softplus
(
-
abs
(
output
))
+
output
*
((
output
>
0
)
-
target
)
return
theano
.
OpFromGraph
(
inp
,
[
outp
],
grad_overrides
=
grad
,
inline
=
True
,
name
=
'sigmoid_binary_crossentropy'
)(
*
inp
)
def
categorical_crossentropy
(
coding_dist
,
true_dist
):
def
categorical_crossentropy
(
coding_dist
,
true_dist
):
"""
"""
Return the cross-entropy between an approximating distribution and a true
Return the cross-entropy between an approximating distribution and a true
...
...
theano/tensor/nnet/tests/test_nnet.py
浏览文件 @
e8d9bed5
...
@@ -33,6 +33,7 @@ from theano.tensor.nnet import (categorical_crossentropy,
...
@@ -33,6 +33,7 @@ from theano.tensor.nnet import (categorical_crossentropy,
h_softmax
,
h_softmax
,
elu
,
elu
,
binary_crossentropy
,
binary_crossentropy
,
sigmoid_binary_crossentropy
,
confusion_matrix
)
confusion_matrix
)
from
theano.tensor
import
matrix
,
vector
,
lvector
,
scalar
from
theano.tensor
import
matrix
,
vector
,
lvector
,
scalar
from
theano.tensor.nnet.nnet
import
softsign
from
theano.tensor.nnet.nnet
import
softsign
...
@@ -1771,6 +1772,36 @@ SoftsignTester = makeBroadcastTester(
...
@@ -1771,6 +1772,36 @@ SoftsignTester = makeBroadcastTester(
)
)
class
T_sigmoid_binary_crossentropy
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
def
_get_test_inputs
(
self
,
n
=
50
):
pred
,
target
=
numpy
.
random
.
randn
(
2
,
n
)
.
astype
(
config
.
floatX
)
# apply sigmoid to target, but not pred
return
[
pred
,
1
/
(
1
+
numpy
.
exp
(
-
target
))]
def
test_matches_binary_crossentropy
(
self
):
"""
Test sigmoid_binary_crossentropy(p, t) ==
binary_crossentropy(sigmoid(p), t).
"""
pred
,
target
=
inputs
=
tensor
.
vectors
(
'pt'
)
reference_val
=
binary_crossentropy
(
sigmoid
(
pred
),
target
)
f_reference
=
theano
.
function
(
inputs
,
reference_val
)
test_val
=
sigmoid_binary_crossentropy
(
pred
,
target
)
f_test
=
theano
.
function
(
inputs
,
test_val
)
test_inputs
=
self
.
_get_test_inputs
()
utt
.
assert_allclose
(
f_reference
(
*
test_inputs
),
f_test
(
*
test_inputs
))
def
test_grad
(
self
):
utt
.
verify_grad
(
sigmoid_binary_crossentropy
,
self
.
_get_test_inputs
())
def
test_confusion_matrix
():
def
test_confusion_matrix
():
# Defining numpy implementation of confusion matrix
# Defining numpy implementation of confusion matrix
def
numpy_conf_mat
(
actual
,
pred
):
def
numpy_conf_mat
(
actual
,
pred
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论