Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e48ff560
提交
e48ff560
authored
4月 13, 2024
作者:
Dhruvanshu-Joshi
提交者:
Ricardo Vieira
5月 01, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support squeezing of unit dimension broadcastable axis
上级
044910bf
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
29 行增加
和
8 行删除
+29
-8
extra_ops.py
pytensor/tensor/extra_ops.py
+10
-0
test_extra_ops.py
tests/tensor/test_extra_ops.py
+19
-8
没有找到文件。
pytensor/tensor/extra_ops.py
浏览文件 @
e48ff560
...
...
@@ -30,6 +30,7 @@ from pytensor.tensor.math import eq as pt_eq
from
pytensor.tensor.math
import
ge
,
lt
,
maximum
,
minimum
,
prod
,
switch
from
pytensor.tensor.math
import
max
as
pt_max
from
pytensor.tensor.math
import
sum
as
pt_sum
from
pytensor.tensor.shape
import
specify_broadcastable
from
pytensor.tensor.subtensor
import
advanced_inc_subtensor1
,
set_subtensor
from
pytensor.tensor.type
import
TensorType
,
dvector
,
int_dtypes
,
integer_dtypes
,
vector
from
pytensor.tensor.variable
import
TensorVariable
...
...
@@ -592,6 +593,15 @@ def squeeze(x, axis=None):
# Nothing to do
return
_x
if
_x
.
ndim
==
0
:
# Nothing could be squeezed
return
_x
# `Dimshuffle` raises when we try to drop an axis that is not statically broadcastable.
# We add a `specify_broadcastable` instead of raising.
non_broadcastable_axis
=
[
i
for
i
in
axis
if
not
_x
.
broadcastable
[
i
]]
_x
=
specify_broadcastable
(
_x
,
*
non_broadcastable_axis
)
return
_x
.
dimshuffle
([
i
for
i
in
range
(
_x
.
ndim
)
if
i
not
in
axis
])
...
...
tests/tensor/test_extra_ops.py
浏览文件 @
e48ff560
...
...
@@ -463,14 +463,6 @@ class TestSqueeze(utt.InferShapeTester):
assert
res
.
broadcastable
==
(
False
,
True
,
False
)
def
test_invalid_axis
(
self
):
# Test that trying to squeeze a non broadcastable dimension raises error
variable
=
TensorType
(
config
.
floatX
,
shape
=
(
1
,
None
))()
with
pytest
.
raises
(
ValueError
,
match
=
"Cannot drop a non-broadcastable dimension"
):
squeeze
(
variable
,
axis
=
1
)
def
test_scalar_input
(
self
):
x
=
pt
.
scalar
(
"x"
)
...
...
@@ -482,6 +474,25 @@ class TestSqueeze(utt.InferShapeTester):
):
squeeze
(
x
,
axis
=
1
)
def
test_invalid_input
(
self
):
x
=
pt
.
vector
(
"x"
)
axis
=
0
f
=
pytensor
.
function
([
x
],
pt
.
squeeze
(
x
,
axis
))
# Test that we allow squeezing of valid non-broadcastable dimension
assert
f
([
0
])
==
0
# Test that we cannot squeeze dimensions whose length is greater than 1
error_txt_1
=
re
.
escape
(
"SpecifyShape: Got shape (3,), expected (1,)."
)
error_txt_2
=
re
.
escape
(
"SpecifyShape: dim 0 of input has shape 3, expected 1"
)
match
=
error_txt_1
if
pytensor
.
config
.
mode
==
"FAST_COMPILE"
else
error_txt_2
with
pytest
.
raises
(
AssertionError
,
match
=
match
,
):
f
([
0
,
1
,
2
])
class
TestCompress
(
utt
.
InferShapeTester
):
def
setup_method
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论