Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
133abe80
提交
133abe80
authored
11月 11, 2024
作者:
ricardoV94
提交者:
Ricardo Vieira
11月 13, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement Kve Op and Kv helper
上级
3523bfa5
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
88 行增加
和
4 行删除
+88
-4
scalar.py
pytensor/link/jax/dispatch/scalar.py
+6
-2
math.py
pytensor/scalar/math.py
+32
-0
math.py
pytensor/tensor/math.py
+12
-0
test_scalar.py
tests/link/jax/test_scalar.py
+2
-0
test_math_scipy.py
tests/tensor/test_math_scipy.py
+36
-2
没有找到文件。
pytensor/link/jax/dispatch/scalar.py
浏览文件 @
133abe80
...
@@ -31,6 +31,7 @@ from pytensor.scalar.math import (
...
@@ -31,6 +31,7 @@ from pytensor.scalar.math import (
GammaIncInv
,
GammaIncInv
,
Iv
,
Iv
,
Ive
,
Ive
,
Kve
,
Log1mexp
,
Log1mexp
,
Psi
,
Psi
,
TriGamma
,
TriGamma
,
...
@@ -288,9 +289,12 @@ def jax_funcify_Iv(op, **kwargs):
...
@@ -288,9 +289,12 @@ def jax_funcify_Iv(op, **kwargs):
@jax_funcify.register
(
Ive
)
@jax_funcify.register
(
Ive
)
def
jax_funcify_Ive
(
op
,
**
kwargs
):
def
jax_funcify_Ive
(
op
,
**
kwargs
):
ive
=
try_import_tfp_jax_op
(
op
,
jax_op_name
=
"bessel_ive"
)
return
try_import_tfp_jax_op
(
op
,
jax_op_name
=
"bessel_ive"
)
return
ive
@jax_funcify.register
(
Kve
)
def
jax_funcify_Kve
(
op
,
**
kwargs
):
return
try_import_tfp_jax_op
(
op
,
jax_op_name
=
"bessel_kve"
)
@jax_funcify.register
(
Log1mexp
)
@jax_funcify.register
(
Log1mexp
)
...
...
pytensor/scalar/math.py
浏览文件 @
133abe80
...
@@ -1281,6 +1281,38 @@ class Ive(BinaryScalarOp):
...
@@ -1281,6 +1281,38 @@ class Ive(BinaryScalarOp):
ive
=
Ive
(
upgrade_to_float
,
name
=
"ive"
)
ive
=
Ive
(
upgrade_to_float
,
name
=
"ive"
)
class
Kve
(
BinaryScalarOp
):
"""Exponentially scaled modified Bessel function of the second kind of real order v."""
nfunc_spec
=
(
"scipy.special.kve"
,
2
,
1
)
@staticmethod
def
st_impl
(
v
,
x
):
return
scipy
.
special
.
kve
(
v
,
x
)
def
impl
(
self
,
v
,
x
):
return
self
.
st_impl
(
v
,
x
)
def
L_op
(
self
,
inputs
,
outputs
,
output_grads
):
v
,
x
=
inputs
[
kve_vx
]
=
outputs
[
g_out
]
=
output_grads
# (1 -v/x) * kve(v, x) - kve(v - 1, x)
kve_vm1x
=
self
(
v
-
1
,
x
)
dx
=
(
1
-
v
/
x
)
*
kve_vx
-
kve_vm1x
return
[
grad_not_implemented
(
self
,
0
,
v
),
g_out
*
dx
,
]
def
c_code
(
self
,
*
args
,
**
kwargs
):
raise
NotImplementedError
()
kve
=
Kve
(
upgrade_to_float
,
name
=
"kve"
)
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/math.py
浏览文件 @
133abe80
...
@@ -1229,6 +1229,16 @@ def ive(v, x):
...
@@ -1229,6 +1229,16 @@ def ive(v, x):
"""Exponentially scaled modified Bessel function of the first kind of order v (real)."""
"""Exponentially scaled modified Bessel function of the first kind of order v (real)."""
@scalar_elemwise
def
kve
(
v
,
x
):
"""Exponentially scaled modified Bessel function of the second kind of real order v."""
def
kv
(
v
,
x
):
"""Modified Bessel function of the second kind of real order v."""
return
kve
(
v
,
x
)
*
exp
(
-
x
)
@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"""
...
@@ -3040,6 +3050,8 @@ __all__ = [
...
@@ -3040,6 +3050,8 @@ __all__ = [
"i1"
,
"i1"
,
"iv"
,
"iv"
,
"ive"
,
"ive"
,
"kv"
,
"kve"
,
"sigmoid"
,
"sigmoid"
,
"expit"
,
"expit"
,
"softplus"
,
"softplus"
,
...
...
tests/link/jax/test_scalar.py
浏览文件 @
133abe80
...
@@ -21,6 +21,7 @@ from pytensor.tensor.math import (
...
@@ -21,6 +21,7 @@ from pytensor.tensor.math import (
gammainccinv
,
gammainccinv
,
gammaincinv
,
gammaincinv
,
iv
,
iv
,
kve
,
log
,
log
,
log1mexp
,
log1mexp
,
polygamma
,
polygamma
,
...
@@ -157,6 +158,7 @@ def test_erfinv():
...
@@ -157,6 +158,7 @@ def test_erfinv():
(
erfcx
,
(
0.7
,)),
(
erfcx
,
(
0.7
,)),
(
erfcinv
,
(
0.7
,)),
(
erfcinv
,
(
0.7
,)),
(
iv
,
(
0.3
,
0.7
)),
(
iv
,
(
0.3
,
0.7
)),
(
kve
,
(
-
2.5
,
2.0
)),
],
],
)
)
@pytest.mark.skipif
(
not
TFP_INSTALLED
,
reason
=
"Test requires tensorflow-probability"
)
@pytest.mark.skipif
(
not
TFP_INSTALLED
,
reason
=
"Test requires tensorflow-probability"
)
...
...
tests/tensor/test_math_scipy.py
浏览文件 @
133abe80
...
@@ -3,7 +3,7 @@ import warnings
...
@@ -3,7 +3,7 @@ import warnings
import
numpy
as
np
import
numpy
as
np
import
pytest
import
pytest
from
pytensor.gradient
import
verify_grad
from
pytensor.gradient
import
NullTypeGradError
,
verify_grad
from
pytensor.scalar
import
ScalarLoop
from
pytensor.scalar
import
ScalarLoop
from
pytensor.tensor.elemwise
import
Elemwise
from
pytensor.tensor.elemwise
import
Elemwise
...
@@ -18,7 +18,7 @@ from pytensor import function, grad
...
@@ -18,7 +18,7 @@ from pytensor import function, grad
from
pytensor
import
tensor
as
pt
from
pytensor
import
tensor
as
pt
from
pytensor.compile.mode
import
get_default_mode
from
pytensor.compile.mode
import
get_default_mode
from
pytensor.configdefaults
import
config
from
pytensor.configdefaults
import
config
from
pytensor.tensor
import
gammaincc
,
inplace
,
vector
from
pytensor.tensor
import
gammaincc
,
inplace
,
kv
,
kve
,
vector
from
tests
import
unittest_tools
as
utt
from
tests
import
unittest_tools
as
utt
from
tests.tensor.utils
import
(
from
tests.tensor.utils
import
(
_good_broadcast_unary_chi2sf
,
_good_broadcast_unary_chi2sf
,
...
@@ -1196,3 +1196,37 @@ class TestHyp2F1Grad:
...
@@ -1196,3 +1196,37 @@ class TestHyp2F1Grad:
[
dd
for
i
,
dd
in
enumerate
(
expected_dds
)
if
i
in
wrt
],
[
dd
for
i
,
dd
in
enumerate
(
expected_dds
)
if
i
in
wrt
],
rtol
=
rtol
,
rtol
=
rtol
,
)
)
def
test_kve
():
rng
=
np
.
random
.
default_rng
(
3772
)
v
=
vector
(
"v"
)
x
=
vector
(
"x"
)
out
=
kve
(
v
[:,
None
],
x
[
None
,
:])
test_v
=
np
.
array
([
-
3.7
,
4
,
4.5
,
5
],
dtype
=
v
.
type
.
dtype
)
test_x
=
np
.
linspace
(
0
,
1005
,
10
,
dtype
=
x
.
type
.
dtype
)
np
.
testing
.
assert_allclose
(
out
.
eval
({
v
:
test_v
,
x
:
test_x
}),
scipy
.
special
.
kve
(
test_v
[:,
None
],
test_x
[
None
,
:]),
)
with
pytest
.
raises
(
NullTypeGradError
):
grad
(
out
.
sum
(),
v
)
verify_grad
(
lambda
x
:
kv
(
4.5
,
x
),
[
test_x
+
0.5
],
rng
=
rng
)
def
test_kv
():
v
=
vector
(
"v"
)
x
=
vector
(
"x"
)
out
=
kv
(
v
[:,
None
],
x
[
None
,
:])
test_v
=
np
.
array
([
-
3.7
,
4
,
4.5
,
5
],
dtype
=
v
.
type
.
dtype
)
test_x
=
np
.
linspace
(
0
,
512
,
10
,
dtype
=
x
.
type
.
dtype
)
np
.
testing
.
assert_allclose
(
out
.
eval
({
v
:
test_v
,
x
:
test_x
}),
scipy
.
special
.
kv
(
test_v
[:,
None
],
test_x
[
None
,
:]),
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论