Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2cef9c0e
提交
2cef9c0e
authored
12月 11, 2023
作者:
David Horsley
提交者:
Ricardo Vieira
12月 11, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add Exponentially scaled modified Bessel function
Fixes #542
上级
68b41a48
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
80 行增加
和
0 行删除
+80
-0
scalar.py
pytensor/link/jax/dispatch/scalar.py
+8
-0
math.py
pytensor/scalar/math.py
+31
-0
inplace.py
pytensor/tensor/inplace.py
+5
-0
math.py
pytensor/tensor/math.py
+6
-0
test_math_scipy.py
tests/tensor/test_math_scipy.py
+30
-0
没有找到文件。
pytensor/link/jax/dispatch/scalar.py
浏览文件 @
2cef9c0e
...
@@ -27,6 +27,7 @@ from pytensor.scalar.math import (
...
@@ -27,6 +27,7 @@ from pytensor.scalar.math import (
Erfcx
,
Erfcx
,
Erfinv
,
Erfinv
,
Iv
,
Iv
,
Ive
,
Log1mexp
,
Log1mexp
,
Psi
,
Psi
,
TriGamma
,
TriGamma
,
...
@@ -267,6 +268,13 @@ def jax_funcify_Iv(op, **kwargs):
...
@@ -267,6 +268,13 @@ def jax_funcify_Iv(op, **kwargs):
return
iv
return
iv
@jax_funcify.register
(
Ive
)
def
jax_funcify_Ive
(
op
,
**
kwargs
):
ive
=
try_import_tfp_jax_op
(
op
,
jax_op_name
=
"bessel_ive"
)
return
ive
@jax_funcify.register
(
Log1mexp
)
@jax_funcify.register
(
Log1mexp
)
def
jax_funcify_Log1mexp
(
op
,
node
,
**
kwargs
):
def
jax_funcify_Log1mexp
(
op
,
node
,
**
kwargs
):
def
log1mexp
(
x
):
def
log1mexp
(
x
):
...
...
pytensor/scalar/math.py
浏览文件 @
2cef9c0e
...
@@ -1197,6 +1197,37 @@ class I0(UnaryScalarOp):
...
@@ -1197,6 +1197,37 @@ class I0(UnaryScalarOp):
i0
=
I0
(
upgrade_to_float
,
name
=
"i0"
)
i0
=
I0
(
upgrade_to_float
,
name
=
"i0"
)
class
Ive
(
BinaryScalarOp
):
"""
Exponentially scaled modified Bessel function of the first kind of order v (real).
"""
nfunc_spec
=
(
"scipy.special.ive"
,
2
,
1
)
@staticmethod
def
st_impl
(
v
,
x
):
return
scipy
.
special
.
ive
(
v
,
x
)
def
impl
(
self
,
v
,
x
):
return
self
.
st_impl
(
v
,
x
)
def
grad
(
self
,
inputs
,
grads
):
v
,
x
=
inputs
(
gz
,)
=
grads
return
[
grad_not_implemented
(
self
,
0
,
v
),
gz
*
(
ive
(
v
-
1
,
x
)
-
2.0
*
_unsafe_sign
(
x
)
*
ive
(
v
,
x
)
+
ive
(
v
+
1
,
x
))
/
2.0
,
]
def
c_code
(
self
,
*
args
,
**
kwargs
):
raise
NotImplementedError
()
ive
=
Ive
(
upgrade_to_float
,
name
=
"ive"
)
class
Sigmoid
(
UnaryScalarOp
):
class
Sigmoid
(
UnaryScalarOp
):
"""
"""
Logistic sigmoid function (1 / (1 + exp(-x)), also known as expit or inverse logit
Logistic sigmoid function (1 / (1 + exp(-x)), also known as expit or inverse logit
...
...
pytensor/tensor/inplace.py
浏览文件 @
2cef9c0e
...
@@ -313,6 +313,11 @@ def iv_inplace(v, x):
...
@@ -313,6 +313,11 @@ def iv_inplace(v, x):
"""Modified Bessel function of the first kind of order v (real)."""
"""Modified Bessel function of the first kind of order v (real)."""
@scalar_elemwise
def
ive_inplace
(
v
,
x
):
"""Exponentially scaled modified Bessel function of the first kind of order v (real)."""
@scalar_elemwise
@scalar_elemwise
def
sigmoid_inplace
(
x
):
def
sigmoid_inplace
(
x
):
"""Logistic sigmoid function (1 / (1 + exp(-x)), also known as expit or inverse logit"""
"""Logistic sigmoid function (1 / (1 + exp(-x)), also known as expit or inverse logit"""
...
...
pytensor/tensor/math.py
浏览文件 @
2cef9c0e
...
@@ -1435,6 +1435,11 @@ def iv(v, x):
...
@@ -1435,6 +1435,11 @@ def iv(v, x):
"""Modified Bessel function of the first kind of order v (real)."""
"""Modified Bessel function of the first kind of order v (real)."""
@scalar_elemwise
def
ive
(
v
,
x
):
"""Exponentially scaled modified Bessel function of the first kind of order v (real)."""
@scalar_elemwise
@scalar_elemwise
def
sigmoid
(
x
):
def
sigmoid
(
x
):
"""Logistic sigmoid function (1 / (1 + exp(-x)), also known as expit or inverse logit"""
"""Logistic sigmoid function (1 / (1 + exp(-x)), also known as expit or inverse logit"""
...
@@ -3039,6 +3044,7 @@ __all__ = [
...
@@ -3039,6 +3044,7 @@ __all__ = [
"i0"
,
"i0"
,
"i1"
,
"i1"
,
"iv"
,
"iv"
,
"ive"
,
"sigmoid"
,
"sigmoid"
,
"expit"
,
"expit"
,
"softplus"
,
"softplus"
,
...
...
tests/tensor/test_math_scipy.py
浏览文件 @
2cef9c0e
...
@@ -75,6 +75,7 @@ expected_jv = scipy.special.jv
...
@@ -75,6 +75,7 @@ expected_jv = scipy.special.jv
expected_i0
=
scipy
.
special
.
i0
expected_i0
=
scipy
.
special
.
i0
expected_i1
=
scipy
.
special
.
i1
expected_i1
=
scipy
.
special
.
i1
expected_iv
=
scipy
.
special
.
iv
expected_iv
=
scipy
.
special
.
iv
expected_ive
=
scipy
.
special
.
ive
expected_erfcx
=
scipy
.
special
.
erfcx
expected_erfcx
=
scipy
.
special
.
erfcx
expected_sigmoid
=
scipy
.
special
.
expit
expected_sigmoid
=
scipy
.
special
.
expit
expected_hyp2f1
=
scipy
.
special
.
hyp2f1
expected_hyp2f1
=
scipy
.
special
.
hyp2f1
...
@@ -639,6 +640,23 @@ TestIvInplaceBroadcast = makeBroadcastTester(
...
@@ -639,6 +640,23 @@ TestIvInplaceBroadcast = makeBroadcastTester(
inplace
=
True
,
inplace
=
True
,
)
)
TestIveBroadcast
=
makeBroadcastTester
(
op
=
at
.
ive
,
expected
=
expected_ive
,
good
=
_good_broadcast_binary_bessel
,
eps
=
2e-10
,
mode
=
mode_no_scipy
,
)
TestIveInplaceBroadcast
=
makeBroadcastTester
(
op
=
inplace
.
ive_inplace
,
expected
=
expected_ive
,
good
=
_good_broadcast_binary_bessel
,
eps
=
2e-10
,
mode
=
mode_no_scipy
,
inplace
=
True
,
)
def
test_verify_iv_grad
():
def
test_verify_iv_grad
():
# Verify Iv gradient.
# Verify Iv gradient.
...
@@ -652,6 +670,18 @@ def test_verify_iv_grad():
...
@@ -652,6 +670,18 @@ def test_verify_iv_grad():
utt
.
verify_grad
(
fixed_first_input_iv
,
[
x_val
])
utt
.
verify_grad
(
fixed_first_input_iv
,
[
x_val
])
def
test_verify_ive_grad
():
# Verify Ive gradient.
# Implemented separately due to need to fix first input for which grad is
# not defined.
v_val
,
x_val
=
_grad_broadcast_binary_bessel
[
"normal"
]
def
fixed_first_input_ive
(
x
):
return
at
.
ive
(
v_val
,
x
)
utt
.
verify_grad
(
fixed_first_input_ive
,
[
x_val
])
TestSigmoidBroadcast
=
makeBroadcastTester
(
TestSigmoidBroadcast
=
makeBroadcastTester
(
op
=
at
.
sigmoid
,
op
=
at
.
sigmoid
,
expected
=
expected_sigmoid
,
expected
=
expected_sigmoid
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论