Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bafd9638
提交
bafd9638
authored
11月 25, 2021
作者:
Ricardo
提交者:
Brandon T. Willard
12月 13, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make local_softmax_grad rewrite work with arbitrary axis
上级
57c388a7
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
9 行增加
和
10 行删除
+9
-10
basic.py
aesara/tensor/nnet/basic.py
+6
-8
test_basic.py
tests/tensor/nnet/test_basic.py
+3
-2
没有找到文件。
aesara/tensor/nnet/basic.py
浏览文件 @
bafd9638
...
@@ -1098,8 +1098,7 @@ def local_logsoftmax_grad(fgraph, node):
...
@@ -1098,8 +1098,7 @@ def local_logsoftmax_grad(fgraph, node):
and
node
.
inputs
[
0
]
.
owner
.
op
==
true_div
and
node
.
inputs
[
0
]
.
owner
.
op
==
true_div
and
len
(
node
.
inputs
[
0
]
.
owner
.
inputs
)
>=
2
and
len
(
node
.
inputs
[
0
]
.
owner
.
inputs
)
>=
2
and
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
.
owner
is
not
None
and
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
.
owner
is
not
None
and
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
.
owner
.
op
==
softmax_legacy
and
isinstance
(
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
.
owner
.
op
,
Softmax
)
and
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
.
ndim
==
2
and
node
.
inputs
[
1
]
==
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
and
node
.
inputs
[
1
]
==
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
and
not
(
and
not
(
# skip if it will be optimized by
# skip if it will be optimized by
...
@@ -1109,15 +1108,14 @@ def local_logsoftmax_grad(fgraph, node):
...
@@ -1109,15 +1108,14 @@ def local_logsoftmax_grad(fgraph, node):
and
isinstance
(
and
isinstance
(
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
.
owner
.
op
,
AdvancedIncSubtensor
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
.
owner
.
op
,
AdvancedIncSubtensor
)
)
# the rewrite only applies to legacy SoftmaxGrad
and
node
.
op
==
softmax_grad_legacy
and
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
.
ndim
==
2
)
)
):
):
# get parameters from unoptimized op
# get parameters from unoptimized op
sm
=
node
.
inputs
[
0
]
.
owner
.
inputs
[
1
]
grads
,
sm
=
node
.
inputs
[
0
]
.
owner
.
inputs
# sm_input = node.inputs[1].owner.inputs[0]
ret
=
grads
-
aet_sum
(
grads
,
axis
=
sm
.
owner
.
op
.
axis
,
keepdims
=
True
)
*
sm
grads
=
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
if
grads
.
broadcastable
[
1
]
and
not
sm
.
broadcastable
[
1
]:
grads
=
aet
.
alloc
(
grads
,
grads
.
shape
[
0
],
sm
.
shape
[
1
])
ret
=
grads
-
aet_sum
(
grads
,
axis
=
1
,
keepdims
=
True
)
*
sm
ret
.
tag
.
values_eq_approx
=
values_eq_approx_remove_nan
ret
.
tag
.
values_eq_approx
=
values_eq_approx_remove_nan
copy_stack_trace
(
node
.
outputs
[
0
],
ret
)
copy_stack_trace
(
node
.
outputs
[
0
],
ret
)
return
[
ret
]
return
[
ret
]
...
...
tests/tensor/nnet/test_basic.py
浏览文件 @
bafd9638
...
@@ -278,7 +278,8 @@ class TestLogSoftmax(utt.InferShapeTester):
...
@@ -278,7 +278,8 @@ class TestLogSoftmax(utt.InferShapeTester):
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
LogSoftmax
)
assert
isinstance
(
f
.
maker
.
fgraph
.
outputs
[
0
]
.
owner
.
op
,
LogSoftmax
)
assert
check_stack_trace
(
f
,
ops_to_check
=
LogSoftmax
)
assert
check_stack_trace
(
f
,
ops_to_check
=
LogSoftmax
)
def
test_local_softmax_grad_optimization_and_big_input
(
self
):
@pytest.mark.parametrize
(
"axis"
,
[
None
,
0
,
-
1
])
def
test_local_logsoftmax_grad_opt
(
self
,
axis
):
# Test the Logsoftmax's grad substitution.
# Test the Logsoftmax's grad substitution.
#
#
# Check that Log(Softmax(x))'s grad is substituted with Logsoftmax(x)'s
# Check that Log(Softmax(x))'s grad is substituted with Logsoftmax(x)'s
...
@@ -294,7 +295,7 @@ class TestLogSoftmax(utt.InferShapeTester):
...
@@ -294,7 +295,7 @@ class TestLogSoftmax(utt.InferShapeTester):
a
=
np
.
exp
(
10
*
rng
.
random
((
5
,
10
))
.
astype
(
config
.
floatX
))
a
=
np
.
exp
(
10
*
rng
.
random
((
5
,
10
))
.
astype
(
config
.
floatX
))
def
myfunc
(
x
):
def
myfunc
(
x
):
sm
=
softmax
(
x
)
sm
=
softmax
(
x
,
axis
=
axis
)
logsm
=
log
(
sm
)
logsm
=
log
(
sm
)
return
logsm
return
logsm
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论