Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ca102983
Unverified
提交
ca102983
authored
7月 07, 2024
作者:
Dhruvanshu-Joshi
提交者:
GitHub
7月 07, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add `nan_to_num` helper (#796)
As well as numpy-like posinf and neginf
上级
05d376f3
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
121 行增加
和
0 行删除
+121
-0
math.py
pytensor/tensor/math.py
+75
-0
test_math.py
tests/tensor/test_math.py
+46
-0
没有找到文件。
pytensor/tensor/math.py
浏览文件 @
ca102983
...
@@ -681,6 +681,22 @@ def largest(*args):
...
@@ -681,6 +681,22 @@ def largest(*args):
return
max
(
stack
(
args
),
axis
=
0
)
return
max
(
stack
(
args
),
axis
=
0
)
def
isposinf
(
x
):
"""
Return if the input variable has positive infinity element
"""
return
eq
(
x
,
np
.
inf
)
def
isneginf
(
x
):
"""
Return if the input variable has negative infinity element
"""
return
eq
(
x
,
-
np
.
inf
)
@scalar_elemwise
@scalar_elemwise
def
lt
(
a
,
b
):
def
lt
(
a
,
b
):
"""a < b"""
"""a < b"""
...
@@ -2913,6 +2929,62 @@ def vectorize_node_dot_to_matmul(op, node, batched_x, batched_y):
...
@@ -2913,6 +2929,62 @@ def vectorize_node_dot_to_matmul(op, node, batched_x, batched_y):
return
vectorize_node_fallback
(
op
,
node
,
batched_x
,
batched_y
)
return
vectorize_node_fallback
(
op
,
node
,
batched_x
,
batched_y
)
def
nan_to_num
(
x
,
nan
=
0.0
,
posinf
=
None
,
neginf
=
None
):
"""
Replace NaN with zero and infinity with large finite numbers (default
behaviour) or with the numbers defined by the user using the `nan`,
`posinf` and/or `neginf` keywords.
NaN is replaced by zero or by the user defined value in
`nan` keyword, infinity is replaced by the largest finite floating point
values representable by ``x.dtype`` or by the user defined value in
`posinf` keyword and -infinity is replaced by the most negative finite
floating point values representable by ``x.dtype`` or by the user defined
value in `neginf` keyword.
Parameters
----------
x : symbolic tensor
Input array.
nan
The value to replace NaN's with in the tensor (default = 0).
posinf
The value to replace +INF with in the tensor (default max
in range representable by ``x.dtype``).
neginf
The value to replace -INF with in the tensor (default min
in range representable by ``x.dtype``).
Returns
-------
out
The tensor with NaN's, +INF, and -INF replaced with the
specified and/or default substitutions.
"""
# Replace NaN's with nan keyword
is_nan
=
isnan
(
x
)
is_pos_inf
=
isposinf
(
x
)
is_neg_inf
=
isneginf
(
x
)
x
=
switch
(
is_nan
,
nan
,
x
)
# Get max and min values representable by x.dtype
maxf
=
posinf
minf
=
neginf
# Specify the value to replace +INF and -INF with
if
maxf
is
None
:
maxf
=
np
.
finfo
(
x
.
real
.
dtype
)
.
max
if
minf
is
None
:
minf
=
np
.
finfo
(
x
.
real
.
dtype
)
.
min
# Replace +INF and -INF values
x
=
switch
(
is_pos_inf
,
maxf
,
x
)
x
=
switch
(
is_neg_inf
,
minf
,
x
)
return
x
# NumPy logical aliases
# NumPy logical aliases
square
=
sqr
square
=
sqr
...
@@ -2951,6 +3023,8 @@ __all__ = [
...
@@ -2951,6 +3023,8 @@ __all__ = [
"not_equal"
,
"not_equal"
,
"isnan"
,
"isnan"
,
"isinf"
,
"isinf"
,
"isposinf"
,
"isneginf"
,
"allclose"
,
"allclose"
,
"isclose"
,
"isclose"
,
"and_"
,
"and_"
,
...
@@ -3069,4 +3143,5 @@ __all__ = [
...
@@ -3069,4 +3143,5 @@ __all__ = [
"logaddexp"
,
"logaddexp"
,
"logsumexp"
,
"logsumexp"
,
"hyp2f1"
,
"hyp2f1"
,
"nan_to_num"
,
]
]
tests/tensor/test_math.py
浏览文件 @
ca102983
...
@@ -80,6 +80,8 @@ from pytensor.tensor.math import (
...
@@ -80,6 +80,8 @@ from pytensor.tensor.math import (
isinf
,
isinf
,
isnan
,
isnan
,
isnan_
,
isnan_
,
isneginf
,
isposinf
,
log
,
log
,
log1mexp
,
log1mexp
,
log1p
,
log1p
,
...
@@ -96,6 +98,7 @@ from pytensor.tensor.math import (
...
@@ -96,6 +98,7 @@ from pytensor.tensor.math import (
minimum
,
minimum
,
mod
,
mod
,
mul
,
mul
,
nan_to_num
,
neg
,
neg
,
neq
,
neq
,
outer
,
outer
,
...
@@ -3689,3 +3692,46 @@ class TestPolyGamma:
...
@@ -3689,3 +3692,46 @@ class TestPolyGamma:
n
=
scalar
(
dtype
=
"int64"
)
n
=
scalar
(
dtype
=
"int64"
)
with
pytest
.
raises
(
NullTypeGradError
):
with
pytest
.
raises
(
NullTypeGradError
):
grad
(
polygamma
(
n
,
0.5
),
wrt
=
n
)
grad
(
polygamma
(
n
,
0.5
),
wrt
=
n
)
def
test_infs
():
x
=
tensor
(
shape
=
(
7
,))
f_pos
=
function
([
x
],
isposinf
(
x
))
f_neg
=
function
([
x
],
isneginf
(
x
))
y
=
np
.
array
([
1
,
np
.
inf
,
2
,
np
.
inf
,
-
np
.
inf
,
-
np
.
inf
,
4
])
.
astype
(
x
.
dtype
)
out_pos
=
f_pos
(
y
)
out_neg
=
f_neg
(
y
)
np
.
testing
.
assert_allclose
(
out_pos
,
[
0
,
1
,
0
,
1
,
0
,
0
,
0
],
)
np
.
testing
.
assert_allclose
(
out_neg
,
[
0
,
0
,
0
,
0
,
1
,
1
,
0
],
)
@pytest.mark.parametrize
(
[
"nan"
,
"posinf"
,
"neginf"
],
[(
0
,
None
,
None
),
(
0
,
0
,
0
),
(
0
,
None
,
1000
),
(
3
,
1
,
-
1
)],
)
def
test_nan_to_num
(
nan
,
posinf
,
neginf
):
x
=
tensor
(
shape
=
(
7
,))
out
=
nan_to_num
(
x
,
nan
,
posinf
,
neginf
)
f
=
function
([
x
],
out
)
y
=
np
.
array
([
1
,
2
,
np
.
nan
,
np
.
inf
,
-
np
.
inf
,
3
,
4
])
.
astype
(
x
.
dtype
)
out
=
f
(
y
)
posinf
=
np
.
finfo
(
x
.
real
.
dtype
)
.
max
if
posinf
is
None
else
posinf
neginf
=
np
.
finfo
(
x
.
real
.
dtype
)
.
min
if
neginf
is
None
else
neginf
np
.
testing
.
assert_allclose
(
out
,
np
.
nan_to_num
(
y
,
nan
=
nan
,
posinf
=
posinf
,
neginf
=
neginf
),
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论