Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
db449148
提交
db449148
authored
5月 09, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
5月 09, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix non-symbolic input issues in aesara.tensor.basic helper functions
上级
1704f22a
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
62 行增加
和
32 行删除
+62
-32
basic.py
aesara/tensor/basic.py
+51
-32
test_basic.py
tests/tensor/test_basic.py
+11
-0
没有找到文件。
aesara/tensor/basic.py
浏览文件 @
db449148
...
@@ -926,13 +926,15 @@ def ones_like(model, dtype=None, opt=False):
...
@@ -926,13 +926,15 @@ def ones_like(model, dtype=None, opt=False):
tensor
tensor
tensor the shape of model containing ones of the type of dtype.
tensor the shape of model containing ones of the type of dtype.
"""
"""
_model
=
as_tensor_variable
(
model
)
if
dtype
is
None
:
if
dtype
is
None
:
dtype
=
model
.
type
.
dtype
dtype
=
_
model
.
type
.
dtype
ret
=
constant
(
1.0
,
dtype
=
dtype
)
ret
=
constant
(
1.0
,
dtype
=
dtype
)
# TODO: Remove this weird option
# TODO: Remove this weird option
if
opt
and
ret
.
type
==
model
.
type
:
if
opt
and
ret
.
type
==
_
model
.
type
:
return
ret
return
ret
return
fill
(
model
,
ret
)
return
fill
(
_
model
,
ret
)
def
zeros_like
(
model
,
dtype
=
None
,
opt
=
False
):
def
zeros_like
(
model
,
dtype
=
None
,
opt
=
False
):
...
@@ -951,13 +953,15 @@ def zeros_like(model, dtype=None, opt=False):
...
@@ -951,13 +953,15 @@ def zeros_like(model, dtype=None, opt=False):
tensor the shape of model containing zeros of the type of dtype.
tensor the shape of model containing zeros of the type of dtype.
"""
"""
_model
=
as_tensor_variable
(
model
)
if
dtype
is
None
:
if
dtype
is
None
:
dtype
=
model
.
type
.
dtype
dtype
=
_
model
.
type
.
dtype
ret
=
constant
(
0.0
,
dtype
=
dtype
)
ret
=
constant
(
0.0
,
dtype
=
dtype
)
# TODO: Remove this weird option
# TODO: Remove this weird option
if
opt
and
ret
.
type
==
model
.
type
:
if
opt
and
ret
.
type
==
_
model
.
type
:
return
ret
return
ret
return
fill
(
model
,
ret
)
return
fill
(
_
model
,
ret
)
def
zeros
(
shape
,
dtype
=
None
):
def
zeros
(
shape
,
dtype
=
None
):
...
@@ -1122,7 +1126,8 @@ def nonzero_values(a):
...
@@ -1122,7 +1126,8 @@ def nonzero_values(a):
flattened input array.
flattened input array.
"""
"""
return
a
.
flatten
()[
flatnonzero
(
a
)]
_a
=
as_tensor_variable
(
a
)
return
_a
.
flatten
()[
flatnonzero
(
_a
)]
class
Tri
(
Op
):
class
Tri
(
Op
):
...
@@ -1915,11 +1920,12 @@ def transpose(x, axes=None):
...
@@ -1915,11 +1920,12 @@ def transpose(x, axes=None):
This is a macro around dimshuffle that matches the numpy.transpose function.
This is a macro around dimshuffle that matches the numpy.transpose function.
"""
"""
_x
=
as_tensor_variable
(
x
)
if
axes
is
None
:
if
axes
is
None
:
axes
=
list
(
range
((
x
.
ndim
-
1
),
-
1
,
-
1
))
axes
=
list
(
range
((
_
x
.
ndim
-
1
),
-
1
,
-
1
))
ret
=
DimShuffle
(
x
.
broadcastable
,
axes
)(
x
)
ret
=
DimShuffle
(
_x
.
broadcastable
,
axes
)(
_
x
)
if
x
.
name
and
axes
==
list
(
range
((
x
.
ndim
-
1
),
-
1
,
-
1
)):
if
_x
.
name
and
axes
==
list
(
range
((
_
x
.
ndim
-
1
),
-
1
,
-
1
)):
ret
.
name
=
x
.
name
+
".T"
ret
.
name
=
_
x
.
name
+
".T"
return
ret
return
ret
...
@@ -2802,31 +2808,41 @@ def concatenate(tensor_list, axis=0):
...
@@ -2802,31 +2808,41 @@ def concatenate(tensor_list, axis=0):
def
horizontal_stack
(
*
args
):
def
horizontal_stack
(
*
args
):
"""
r"""Stack arrays in sequence horizontally (column wise)."""
Horizontally stack two L{TensorType}s.
Stack two L{TensorType}s along the second axis (column wise). These
L{TensorType}s must have the same shape along all dimensions but the
second.
"""
# Note: 'horizontal_stack' and 'vertical_stack' do not behave exactly like
# Note: 'horizontal_stack' and 'vertical_stack' do not behave exactly like
# Numpy's hstack and vstack functions. This is intended, because Numpy's
# Numpy's hstack and vstack functions. This is intended, because Numpy's
# functions have potentially confusing/incoherent behavior (try them on 1D
# functions have potentially confusing/incoherent behavior (try them on 1D
# arrays). If this is fixed in a future version of Numpy, it may be worth
# arrays). If this is fixed in a future version of Numpy, it may be worth
# trying to get closer to Numpy's way of doing things. In the meantime,
# trying to get closer to Numpy's way of doing things. In the meantime,
# better keep different names to emphasize the implementation divergences.
# better keep different names to emphasize the implementation divergences.
assert
len
(
args
)
>=
2
if
len
(
args
)
<
2
:
raise
ValueError
(
"Too few arguments"
)
_args
=
[]
for
arg
in
args
:
for
arg
in
args
:
assert
arg
.
type
.
ndim
==
2
_arg
=
as_tensor_variable
(
arg
)
return
concatenate
(
args
,
axis
=
1
)
if
_arg
.
type
.
ndim
!=
2
:
raise
ValueError
(
"All arguments must have two dimensions"
)
_args
.
append
(
_arg
)
return
concatenate
(
_args
,
axis
=
1
)
def
vertical_stack
(
*
args
):
def
vertical_stack
(
*
args
):
assert
len
(
args
)
>=
2
r"""Stack arrays in sequence vertically (row wise)."""
if
len
(
args
)
<
2
:
raise
ValueError
(
"Too few arguments"
)
_args
=
[]
for
arg
in
args
:
for
arg
in
args
:
assert
arg
.
type
.
ndim
==
2
_arg
=
as_tensor_variable
(
arg
)
return
concatenate
(
args
,
axis
=
0
)
if
_arg
.
type
.
ndim
!=
2
:
raise
ValueError
(
"All arguments must have two dimensions"
)
_args
.
append
(
_arg
)
return
concatenate
(
_args
,
axis
=
0
)
class
Flatten
(
COp
):
class
Flatten
(
COp
):
...
@@ -3042,19 +3058,21 @@ def flatten(x, ndim=1):
...
@@ -3042,19 +3058,21 @@ def flatten(x, ndim=1):
if
ndim
is
None
:
if
ndim
is
None
:
ndim
=
1
ndim
=
1
_x
=
as_tensor_variable
(
x
)
# Any input variable can be flattened to have ndim of 1,
# Any input variable can be flattened to have ndim of 1,
# even if it's a scalar. Otherwise, ndim must be positive
# even if it's a scalar. Otherwise, ndim must be positive
# and smaller than x.ndim.
# and smaller than x.ndim.
if
ndim
<
1
or
(
ndim
>
1
and
ndim
>
x
.
ndim
):
if
ndim
<
1
or
(
ndim
>
1
and
ndim
>
_
x
.
ndim
):
raise
ValueError
(
f
"ndim {ndim} out of bound [1, {x.ndim + 1})"
)
raise
ValueError
(
f
"ndim {ndim} out of bound [1, {
_
x.ndim + 1})"
)
if
ndim
>
1
:
if
ndim
>
1
:
dims
=
tuple
(
x
.
shape
[:
ndim
-
1
])
+
(
-
1
,)
dims
=
tuple
(
_
x
.
shape
[:
ndim
-
1
])
+
(
-
1
,)
else
:
else
:
dims
=
(
-
1
,)
dims
=
(
-
1
,)
x_reshaped
=
x
.
reshape
(
dims
)
x_reshaped
=
_
x
.
reshape
(
dims
)
bcast_kept_dims
=
x
.
broadcastable
[:
ndim
-
1
]
bcast_kept_dims
=
_
x
.
broadcastable
[:
ndim
-
1
]
bcast_new_dim
=
builtins
.
all
(
x
.
broadcastable
[
ndim
-
1
:])
bcast_new_dim
=
builtins
.
all
(
_
x
.
broadcastable
[
ndim
-
1
:])
broadcastable
=
bcast_kept_dims
+
(
bcast_new_dim
,)
broadcastable
=
bcast_kept_dims
+
(
bcast_new_dim
,)
x_reshaped
=
addbroadcast
(
x_reshaped
,
*
[
i
for
i
in
range
(
ndim
)
if
broadcastable
[
i
]])
x_reshaped
=
addbroadcast
(
x_reshaped
,
*
[
i
for
i
in
range
(
ndim
)
if
broadcastable
[
i
]])
return
x_reshaped
return
x_reshaped
...
@@ -4260,7 +4278,8 @@ def empty(shape, dtype=None):
...
@@ -4260,7 +4278,8 @@ def empty(shape, dtype=None):
def
empty_like
(
def
empty_like
(
prototype
:
TensorVariable
,
dtype
:
Optional
[
Union
[
str
,
np
.
generic
,
np
.
dtype
]]
=
None
prototype
:
Union
[
np
.
ndarray
,
TensorVariable
],
dtype
:
Optional
[
Union
[
str
,
np
.
generic
,
np
.
dtype
]]
=
None
,
)
->
TensorVariable
:
)
->
TensorVariable
:
"""Return a new array with the same shape and type as a given array.
"""Return a new array with the same shape and type as a given array.
...
...
tests/tensor/test_basic.py
浏览文件 @
db449148
...
@@ -4529,3 +4529,14 @@ def test_full_like(inp, shape):
...
@@ -4529,3 +4529,14 @@ def test_full_like(inp, shape):
y
.
eval
({
x
:
np
.
zeros
(
shape
,
dtype
=
dtype
)}),
y
.
eval
({
x
:
np
.
zeros
(
shape
,
dtype
=
dtype
)}),
np
.
full
(
shape
,
fill_value
,
dtype
=
dtype
),
np
.
full
(
shape
,
fill_value
,
dtype
=
dtype
),
)
)
@pytest.mark.parametrize
(
"func"
,
[
horizontal_stack
,
vertical_stack
])
def
test_oriented_stack_functions
(
func
):
with
pytest
.
raises
(
ValueError
):
func
()
a
=
at
.
tensor
(
np
.
float64
,
shape
=
(
None
,
None
,
None
))
with
pytest
.
raises
(
ValueError
):
func
(
a
,
a
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论