Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
8ae14c20
提交
8ae14c20
authored
12月 13, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
12月 15, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement vectorize_node for CheckAndRaise Op
上级
31a4df60
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
87 行增加
和
0 行删除
+87
-0
raise_op.py
pytensor/raise_op.py
+19
-0
test_raise_op.py
tests/test_raise_op.py
+68
-0
没有找到文件。
pytensor/raise_op.py
浏览文件 @
8ae14c20
...
...
@@ -6,6 +6,7 @@ import numpy as np
from
pytensor.gradient
import
DisconnectedType
from
pytensor.graph.basic
import
Apply
,
Variable
from
pytensor.graph.replace
import
_vectorize_node
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.link.c.type
import
Generic
...
...
@@ -198,3 +199,21 @@ class Assert(CheckAndRaise):
assert_op
=
Assert
()
@_vectorize_node.register
(
CheckAndRaise
)
def
vectorize_check_and_raise
(
op
,
node
,
batch_x
,
batch_cond
):
from
pytensor.tensor.extra_ops
import
broadcast_arrays
from
pytensor.tensor.shape
import
shape_padright
batch_cond_dims
=
batch_cond
.
type
.
ndim
if
batch_cond_dims
:
out
=
op
(
batch_x
,
batch_cond
.
all
())
# Condition may broadcast batch dims of x
# We broadcast after the Check Op, so it can be removed more easily if not needed
x_core_ndim
=
node
.
inputs
[
0
]
.
type
.
ndim
batch_out
,
_
=
broadcast_arrays
(
out
,
shape_padright
(
batch_cond
,
x_core_ndim
))
return
batch_out
.
owner
else
:
return
op
.
make_node
(
batch_x
,
batch_cond
)
tests/test_raise_op.py
浏览文件 @
8ae14c20
...
...
@@ -5,10 +5,13 @@ import scipy.sparse
import
pytensor
import
pytensor.tensor
as
pt
from
pytensor.compile.mode
import
OPT_FAST_RUN
,
Mode
from
pytensor.graph
import
vectorize_graph
from
pytensor.graph.basic
import
Constant
,
equal_computations
from
pytensor.raise_op
import
Assert
,
CheckAndRaise
,
assert_op
from
pytensor.scalar.basic
import
ScalarType
,
float64
from
pytensor.sparse
import
as_sparse_variable
from
pytensor.tensor.basic
import
second
from
pytensor.tensor.elemwise
import
DimShuffle
from
tests
import
unittest_tools
as
utt
...
...
@@ -184,3 +187,68 @@ def test_CheckAndRaise_sparse_variable():
a2
=
check_and_raise
(
aspe1
,
aspe2
.
sum
()
>
2
)
with
pytest
.
raises
(
ValueError
,
match
=
"sparse_check"
):
a2
.
sum
()
.
eval
()
@pytensor.config.change_flags
(
cxx
=
""
)
# For speed-up
def
test_vectorize
():
floatX
=
pytensor
.
config
.
floatX
x
=
pt
.
vector
(
"x"
)
y
=
pt
.
vector
(
"y"
)
cond
=
pt
.
all
(
y
>=
0
)
out
=
assert_op
(
x
,
cond
)
batch_x
=
pt
.
matrix
(
"batch_x"
,
shape
=
(
2
,
None
))
batch_y
=
pt
.
matrix
(
"batch_y"
,
shape
=
(
2
,
None
))
test_x
=
np
.
arange
(
3
)
.
astype
(
floatX
)
test_y
=
np
.
arange
(
4
)
.
astype
(
floatX
)
test_batch_x
=
np
.
arange
(
6
)
.
reshape
(
2
,
3
)
.
astype
(
floatX
)
test_batch_y
=
np
.
arange
(
8
)
.
reshape
(
2
,
4
)
.
astype
(
floatX
)
# Only x is batched
vect_out
=
vectorize_graph
(
out
,
{
x
:
batch_x
,
y
:
y
})
assert
vect_out
.
type
.
shape
==
(
2
,
None
)
assert
isinstance
(
vect_out
.
owner
.
op
,
CheckAndRaise
)
np
.
testing
.
assert_array_equal
(
vect_out
.
eval
({
batch_x
:
test_batch_x
,
y
:
test_y
}),
test_batch_x
,
)
with
pytest
.
raises
(
AssertionError
):
vect_out
.
eval
({
batch_x
:
test_batch_x
,
y
:
-
test_y
})
# Only y is batched
vect_out
=
vectorize_graph
(
out
,
{
x
:
x
,
y
:
batch_y
})
assert
vect_out
.
type
.
shape
==
(
2
,
None
)
assert
vect_out
.
owner
.
op
==
second
# broadcast
assert
isinstance
(
vect_out
.
owner
.
inputs
[
1
]
.
owner
.
op
,
DimShuffle
)
assert
isinstance
(
vect_out
.
owner
.
inputs
[
1
]
.
owner
.
inputs
[
0
]
.
owner
.
op
,
CheckAndRaise
)
np
.
testing
.
assert_array_equal
(
vect_out
.
eval
({
x
:
test_x
,
batch_y
:
test_batch_y
}),
np
.
broadcast_to
(
test_x
,
(
2
,
*
test_x
.
shape
)),
)
with
pytest
.
raises
(
AssertionError
):
vect_out
.
eval
({
x
:
test_x
,
batch_y
:
-
test_batch_y
})
# Both x, and y are batched
vect_out
=
vectorize_graph
(
out
,
{
x
:
batch_x
,
y
:
batch_y
})
assert
vect_out
.
type
.
shape
==
(
2
,
None
)
assert
vect_out
.
owner
.
op
==
second
assert
isinstance
(
vect_out
.
owner
.
inputs
[
1
]
.
owner
.
op
,
CheckAndRaise
)
np
.
testing
.
assert_array_equal
(
vect_out
.
eval
({
batch_x
:
test_batch_x
,
batch_y
:
test_batch_y
}),
test_batch_x
,
)
with
pytest
.
raises
(
AssertionError
):
vect_out
.
eval
({
batch_x
:
test_batch_x
,
batch_y
:
-
test_batch_y
})
# Both x, and y are batched and broadcast each other
vect_out
=
vectorize_graph
(
out
,
{
x
:
batch_x
[:,
None
,
:],
y
:
batch_y
[
None
,
:,
:]})
assert
vect_out
.
type
.
shape
==
(
2
,
2
,
None
)
assert
vect_out
.
owner
.
op
==
second
assert
isinstance
(
vect_out
.
owner
.
inputs
[
1
]
.
owner
.
op
,
CheckAndRaise
)
np
.
testing
.
assert_array_equal
(
vect_out
.
eval
({
batch_x
:
test_batch_x
,
batch_y
:
test_batch_y
}),
np
.
broadcast_to
(
test_batch_x
[:,
None
,
:],
(
2
,
*
test_batch_x
.
shape
)),
)
with
pytest
.
raises
(
AssertionError
):
vect_out
.
eval
({
batch_x
:
test_batch_x
,
batch_y
:
-
test_batch_y
})
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论