Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4a1010ef
提交
4a1010ef
authored
11月 24, 2021
作者:
Ricardo
提交者:
Brandon T. Willard
12月 13, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add SoftmaxGrad numba dispatch
上级
f06146ae
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
71 行增加
和
1 行删除
+71
-1
elemwise.py
aesara/link/numba/dispatch/elemwise.py
+26
-1
test_numba.py
tests/link/test_numba.py
+45
-0
没有找到文件。
aesara/link/numba/dispatch/elemwise.py
浏览文件 @
4a1010ef
...
@@ -21,7 +21,7 @@ from aesara.link.utils import (
...
@@ -21,7 +21,7 @@ from aesara.link.utils import (
)
)
from
aesara.tensor.elemwise
import
CAReduce
,
DimShuffle
,
Elemwise
from
aesara.tensor.elemwise
import
CAReduce
,
DimShuffle
,
Elemwise
from
aesara.tensor.math
import
MaxAndArgmax
from
aesara.tensor.math
import
MaxAndArgmax
from
aesara.tensor.nnet.basic
import
LogSoftmax
,
Softmax
from
aesara.tensor.nnet.basic
import
LogSoftmax
,
Softmax
,
SoftmaxGrad
from
aesara.tensor.type
import
tensor
from
aesara.tensor.type
import
tensor
...
@@ -424,6 +424,31 @@ def numba_funcify_Softmax(op, node, **kwargs):
...
@@ -424,6 +424,31 @@ def numba_funcify_Softmax(op, node, **kwargs):
return
softmax
return
softmax
@numba_funcify.register
(
SoftmaxGrad
)
def
numba_funcify_SoftmaxGrad
(
op
,
node
,
**
kwargs
):
sm_at
=
node
.
inputs
[
1
]
sm_dtype
=
sm_at
.
type
.
numpy_dtype
sm_dtype
=
numba
.
np
.
numpy_support
.
from_dtype
(
sm_dtype
)
axis
=
op
.
axis
if
axis
is
not
None
:
reduce_sum
=
create_axis_reducer
(
np
.
add
,
0.0
,
axis
,
sm_at
.
ndim
,
sm_dtype
,
keepdims
=
True
)
else
:
reduce_sum
=
np
.
sum
@numba.njit
def
softmax_grad
(
dy
,
sm
):
dy_times_sm
=
dy
*
sm
sum_dy_times_sm
=
reduce_sum
(
dy_times_sm
)
dx
=
dy_times_sm
-
sum_dy_times_sm
*
sm
return
dx
return
softmax_grad
@numba_funcify.register
(
LogSoftmax
)
@numba_funcify.register
(
LogSoftmax
)
def
numba_funcify_LogSoftmax
(
op
,
node
,
**
kwargs
):
def
numba_funcify_LogSoftmax
(
op
,
node
,
**
kwargs
):
...
...
tests/link/test_numba.py
浏览文件 @
4a1010ef
...
@@ -1893,6 +1893,51 @@ def test_Dot(x, y, exc):
...
@@ -1893,6 +1893,51 @@ def test_Dot(x, y, exc):
)
)
@pytest.mark.parametrize
(
"dy, sm, axis, exc"
,
[
(
set_test_value
(
aet
.
matrix
(),
np
.
array
([[
1
,
1
,
1
],
[
0
,
0
,
0
]],
dtype
=
config
.
floatX
)
),
set_test_value
(
aet
.
matrix
(),
rng
.
random
(
size
=
(
2
,
3
))
.
astype
(
config
.
floatX
)),
None
,
None
,
),
(
set_test_value
(
aet
.
matrix
(),
np
.
array
([[
1
,
1
,
1
],
[
0
,
0
,
0
]],
dtype
=
config
.
floatX
)
),
set_test_value
(
aet
.
matrix
(),
rng
.
random
(
size
=
(
2
,
3
))
.
astype
(
config
.
floatX
)),
0
,
None
,
),
(
set_test_value
(
aet
.
matrix
(),
np
.
array
([[
1
,
1
,
1
],
[
0
,
0
,
0
]],
dtype
=
config
.
floatX
)
),
set_test_value
(
aet
.
matrix
(),
rng
.
random
(
size
=
(
2
,
3
))
.
astype
(
config
.
floatX
)),
1
,
None
,
),
],
)
def
test_SoftmaxGrad
(
dy
,
sm
,
axis
,
exc
):
g
=
nnetb
.
SoftmaxGrad
(
axis
=
axis
)(
dy
,
sm
)
g_fg
=
FunctionGraph
(
outputs
=
[
g
])
cm
=
contextlib
.
suppress
()
if
exc
is
None
else
pytest
.
warns
(
exc
)
with
cm
:
compare_numba_and_py
(
g_fg
,
[
i
.
tag
.
test_value
for
i
in
g_fg
.
inputs
if
not
isinstance
(
i
,
(
SharedVariable
,
Constant
))
],
)
@pytest.mark.parametrize
(
@pytest.mark.parametrize
(
"x, axis, exc"
,
"x, axis, exc"
,
[
[
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论