Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
22416ba6
提交
22416ba6
authored
6月 06, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
6月 08, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace Elemwise use with scalar Ops in broadcast_shape_iter
上级
1615f991
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
32 行增加
和
18 行删除
+32
-18
extra_ops.py
aesara/tensor/extra_ops.py
+14
-8
test_basic_opt.py
tests/tensor/test_basic_opt.py
+18
-10
没有找到文件。
aesara/tensor/extra_ops.py
浏览文件 @
22416ba6
from
collections.abc
import
Collection
from
functools
import
reduce
from
typing
import
Iterable
,
Tuple
,
Union
import
numpy
as
np
...
...
@@ -6,6 +7,7 @@ import numpy.core.numeric
from
numpy.core.multiarray
import
normalize_axis_index
import
aesara
import
aesara.scalar.basic
as
aes
from
aesara.gradient
import
(
DisconnectedType
,
_float_zeros_like
,
...
...
@@ -26,9 +28,7 @@ from aesara.tensor import get_vector_length
from
aesara.tensor.exceptions
import
NotScalarConstantError
from
aesara.tensor.math
import
abs
as
at_abs
from
aesara.tensor.math
import
all
as
at_all
from
aesara.tensor.math
import
eq
,
ge
,
lt
from
aesara.tensor.math
import
max
as
at_max
from
aesara.tensor.math
import
maximum
,
minimum
,
or_
,
prod
from
aesara.tensor.math
import
ge
,
lt
,
maximum
,
minimum
,
prod
from
aesara.tensor.math
import
sum
as
at_sum
from
aesara.tensor.subtensor
import
advanced_inc_subtensor1
,
set_subtensor
from
aesara.tensor.type
import
TensorType
,
dvector
,
int_dtypes
,
integer_dtypes
,
vector
...
...
@@ -1534,13 +1534,19 @@ def broadcast_shape_iter(
result_dims
.
append
(
maybe_non_bcast_shapes
[
0
])
continue
non_bcast_vec
=
at
.
as_tensor
(
maybe_non_bcast_shapes
)
non_bcast_vec
=
at
.
switch
(
eq
(
non_bcast_vec
,
1
),
-
one_at
,
non_bcast_vec
)
dim_max
=
at_abs
(
at_max
(
non_bcast_vec
))
non_bcast_vec
=
[
aes
.
switch
(
aes
.
eq
(
nbv
,
1
),
-
one_at
,
nbv
)
for
nbv
in
maybe_non_bcast_shapes
]
dim_max
=
aes
.
abs
(
reduce
(
aes
.
scalar_maximum
,
non_bcast_vec
))
assert_dim
=
Assert
(
"Could not broadcast dimensions"
)
assert_cond
=
at_all
(
or_
(
eq
(
non_bcast_vec
,
-
one_at
),
eq
(
non_bcast_vec
,
dim_max
))
assert_cond
=
reduce
(
aes
.
and_
,
(
aes
.
or_
(
aes
.
eq
(
nbv
,
-
one_at
),
aes
.
eq
(
nbv
,
dim_max
))
for
nbv
in
non_bcast_vec
),
)
bcast_dim
=
assert_dim
(
dim_max
,
assert_cond
)
...
...
tests/tensor/test_basic_opt.py
浏览文件 @
22416ba6
...
...
@@ -2890,10 +2890,10 @@ class TestShapeI(utt.InferShapeTester):
self
.
_compile_and_check
([
admat
],
[
Shape_i
(
1
)(
admat
)],
[
admat_val
],
Shape_i
)
class
TestS
hapeFeatur
e
:
class
TestS
ameShap
e
:
def
test_scalar
(
self
):
x
=
scalar
()
cst
=
at
.
constant
(
1
)
.
clone
()
cst
=
at
.
constant
(
1
)
o
=
x
+
cst
fgraph
=
FunctionGraph
([
x
],
[
o
],
clone
=
False
)
shape_feature
=
ShapeFeature
()
...
...
@@ -2902,34 +2902,42 @@ class TestShapeFeature:
def
test_vector
(
self
):
x
=
vector
()
cst
=
at
.
constant
(
1
)
.
clone
()
cst
=
at
.
constant
(
1
)
o
=
x
+
cst
fgraph
=
FunctionGraph
([
x
],
[
o
],
clone
=
False
)
shape_feature
=
ShapeFeature
()
fgraph
.
attach_feature
(
shape_feature
)
assert
shape_feature
.
same_shape
(
x
,
o
)
def
test_
vector2
(
self
):
def
test_
no_static_shapes
(
self
):
x
=
vector
()
y
=
vector
()
o
=
x
+
y
fgraph
=
FunctionGraph
([
x
,
y
],
[
o
],
clone
=
False
)
shape_feature
=
ShapeFeature
()
fgraph
.
attach_feature
(
shape_feature
)
assert
shape_feature
.
same_shape
(
x
,
o
)
# We no longer assume that `x` has the same shape as `y` simply because
# neither has static shape information. Instead, when there is no
# static shape information is available, we assume that `x` and/or `y`
# could have shapes `(1,)` and/or `(n,)`, where `n != 1`, or any
# combination of the two.
assert
not
shape_feature
.
same_shape
(
x
,
o
)
# The following case isn't implemented
assert
not
shape_feature
.
same_shape
(
y
,
o
)
def
test_vector_dim
(
self
):
x
=
vector
()
y
=
vector
()
@pytest.mark.parametrize
(
"y_dim_0"
,
[
2
,
pytest
.
param
(
None
,
marks
=
pytest
.
mark
.
xfail
(
reason
=
"Not implemented"
))],
)
def
test_vector_dim
(
self
,
y_dim_0
):
x
=
at
.
tensor
(
dtype
=
"floatX"
,
shape
=
(
2
,
None
))
y
=
at
.
tensor
(
dtype
=
"floatX"
,
shape
=
(
y_dim_0
,
None
))
o
=
x
+
y
fgraph
=
FunctionGraph
([
x
,
y
],
[
o
],
clone
=
False
)
shape_feature
=
ShapeFeature
()
fgraph
.
attach_feature
(
shape_feature
)
assert
shape_feature
.
same_shape
(
x
,
o
,
0
,
0
)
# The following case isn't implemented
assert
not
shape_feature
.
same_shape
(
y
,
o
,
0
,
0
)
assert
not
shape_feature
.
same_shape
(
x
,
o
,
1
,
1
)
def
test_vector_dim_err
(
self
):
x
=
vector
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论