Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
87e28b28
提交
87e28b28
authored
12月 04, 2020
作者:
Brandon T. Willard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove and rename deprecated outdim parameter in theano.tensor.basic.flatten
上级
e2dd22c4
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
46 行增加
和
80 行删除
+46
-80
test_basic.py
tests/tensor/test_basic.py
+8
-11
basic.py
theano/tensor/basic.py
+38
-69
没有找到文件。
tests/tensor/test_basic.py
浏览文件 @
87e28b28
...
...
@@ -4114,7 +4114,7 @@ def test_make_column_matrix_broadcastable():
assert
(
f
(
np
.
zeros
((
3
,
1
)))
+
np
.
ones
(
2
)
==
np
.
ones
((
3
,
2
)))
.
all
()
def
test_flatten_
outdimNone
():
def
test_flatten_
ndim_default
():
a
=
dmatrix
()
c
=
flatten
(
a
)
f
=
inplace_func
([
a
],
c
)
...
...
@@ -4178,9 +4178,6 @@ def test_flatten_ndim2_of_3():
flatten_2
=
partial
(
flatten
,
ndim
=
2
)
utt
.
verify_grad
(
flatten_2
,
[
a_val
])
# test outdim parameter name
flatten_2
=
partial
(
flatten
,
outdim
=
2
)
utt
.
verify_grad
(
flatten_2
,
[
a_val
])
def
test_flatten_broadcastable
():
...
...
@@ -4219,7 +4216,7 @@ def test_flatten_ndim_invalid():
def
test_is_flat
():
# tests is_flat method for constant and symbolic variables,
# as well as reshaped constant and symbolic variables on the
# given
outdim
# given
`ndim`
# Constant variable
assert
tt
.
is_flat
(
tt
.
as_tensor_variable
(
np
.
zeros
(
10
)))
...
...
@@ -6251,10 +6248,10 @@ class TestInferShape(utt.InferShapeTester):
# Flatten
atens3
=
tensor3
()
atens3_val
=
rand
(
4
,
5
,
3
)
for
out
dim
in
(
3
,
2
,
1
):
for
n
dim
in
(
3
,
2
,
1
):
self
.
_compile_and_check
(
[
atens3
],
[
flatten
(
atens3
,
out
dim
)],
[
flatten
(
atens3
,
n
dim
)],
[
atens3_val
],
Reshape
,
excluding
=
[
"local_useless_reshape"
],
...
...
@@ -6262,10 +6259,10 @@ class TestInferShape(utt.InferShapeTester):
amat
=
matrix
()
amat_val
=
rand
(
4
,
5
)
for
out
dim
in
(
2
,
1
):
for
n
dim
in
(
2
,
1
):
self
.
_compile_and_check
(
[
amat
],
[
flatten
(
amat
,
out
dim
)],
[
flatten
(
amat
,
n
dim
)],
[
amat_val
],
Reshape
,
excluding
=
[
"local_useless_reshape"
],
...
...
@@ -6273,10 +6270,10 @@ class TestInferShape(utt.InferShapeTester):
avec
=
vector
()
avec_val
=
rand
(
4
)
out
dim
=
1
n
dim
=
1
self
.
_compile_and_check
(
[
avec
],
[
flatten
(
avec
,
out
dim
)],
[
flatten
(
avec
,
n
dim
)],
[
avec_val
],
Reshape
,
excluding
=
[
"local_useless_reshape"
],
...
...
theano/tensor/basic.py
浏览文件 @
87e28b28
...
...
@@ -5244,8 +5244,8 @@ class Flatten(Op):
"""
Flatten a tensor.
Flattens a tensor to `
out
dim` dimensions by preserving the leading
out
dim - 1 shape components.
Flattens a tensor to `
n
dim` dimensions by preserving the leading
n
dim - 1 shape components.
.. note:: The interface Flatten(Op) is deprecated, you should use flatten.
"""
...
...
@@ -5253,24 +5253,24 @@ class Flatten(Op):
view_map
=
{
0
:
[
0
]}
check_input
=
False
__props__
=
(
"
out
dim"
,)
__props__
=
(
"
n
dim"
,)
def
__init__
(
self
,
out
dim
=
1
):
def
__init__
(
self
,
n
dim
=
1
):
warnings
.
warn
(
"Flatten class is deprecated, "
"please use flatten method instead."
,
DeprecationWarning
,
stacklevel
=
4
,
)
self
.
outdim
=
int
(
out
dim
)
self
.
ndim
=
int
(
n
dim
)
def
__str__
(
self
):
return
f
"{self.__class__.__name__}{{{self.
out
dim}}}"
return
f
"{self.__class__.__name__}{{{self.
n
dim}}}"
def
make_node
(
self
,
x
):
t_x
=
as_tensor_variable
(
x
)
if
self
.
outdim
<
1
or
(
x
.
ndim
and
self
.
out
dim
>
x
.
ndim
):
if
self
.
ndim
<
1
or
(
x
.
ndim
and
self
.
n
dim
>
x
.
ndim
):
raise
ValueError
(
f
"invalid output ndimensions ({self.
out
dim}) for tensor of "
f
"invalid output ndimensions ({self.
n
dim}) for tensor of "
f
"rank {t_x.ndim}"
)
...
...
@@ -5279,8 +5279,8 @@ class Flatten(Op):
# For the dimension resulting from the collapse of other dimensions,
# it should be broadcastable iff all the collapsed dimensions were
# broadcastable.
bcast_kept_dims
=
x
.
broadcastable
[:
self
.
out
dim
-
1
]
bcast_new_dim
=
builtins
.
all
(
x
.
broadcastable
[
self
.
out
dim
-
1
:])
bcast_kept_dims
=
x
.
broadcastable
[:
self
.
n
dim
-
1
]
bcast_new_dim
=
builtins
.
all
(
x
.
broadcastable
[
self
.
n
dim
-
1
:])
broadcastable
=
bcast_kept_dims
+
(
bcast_new_dim
,)
return
gof
.
Apply
(
self
,
[
t_x
],
[
tensor
(
x
.
type
.
dtype
,
broadcastable
)])
...
...
@@ -5288,22 +5288,22 @@ class Flatten(Op):
def
perform
(
self
,
node
,
inp
,
out_
):
(
x
,)
=
inp
(
out
,)
=
out_
outdim
=
self
.
out
dim
if
out
dim
==
1
:
ndim
=
self
.
n
dim
if
n
dim
==
1
:
try
:
out
[
0
]
=
x
.
reshape
(
x
.
size
)
except
AttributeError
:
out
[
0
]
=
x
.
reshape
((
np
.
prod
(
x
.
shape
),))
elif
out
dim
==
len
(
x
.
shape
):
elif
n
dim
==
len
(
x
.
shape
):
out
[
0
]
=
x
else
:
newshape
=
x
.
shape
[:
outdim
-
1
]
+
(
np
.
prod
(
x
.
shape
[
out
dim
-
1
:]),)
newshape
=
x
.
shape
[:
ndim
-
1
]
+
(
np
.
prod
(
x
.
shape
[
n
dim
-
1
:]),)
out
[
0
]
=
x
.
reshape
(
newshape
)
def
infer_shape
(
self
,
fgraph
,
node
,
in_shapes
):
(
in_shp
,)
=
in_shapes
part1
=
in_shp
[:
self
.
out
dim
-
1
]
part2
=
in_shp
[
self
.
out
dim
-
1
:]
part1
=
in_shp
[:
self
.
n
dim
-
1
]
part2
=
in_shp
[
self
.
n
dim
-
1
:]
if
len
(
part2
)
>
1
:
part2
=
(
prod
(
part2
,
dtype
=
"int64"
),)
...
...
@@ -5311,11 +5311,11 @@ class Flatten(Op):
# We do not want to force an upcast of part2 if its length is 1
pass
else
:
if
len
(
in_shp
)
==
0
and
self
.
out
dim
==
1
:
if
len
(
in_shp
)
==
0
and
self
.
n
dim
==
1
:
part2
=
(
1
,)
else
:
raise
ValueError
(
f
"invalid output ndimensions ({self.
out
dim}) for tensor "
f
"invalid output ndimensions ({self.
n
dim}) for tensor "
f
"of rank {len(in_shp)}"
)
...
...
@@ -5338,11 +5338,11 @@ class Flatten(Op):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
(
out
,)
=
outputs
outdim
=
self
.
out
dim
ndim
=
self
.
n
dim
fail
=
sub
[
"fail"
]
return
(
"""
if (
%(
out
dim)
s == PyArray_NDIM(
%(x)
s))
if (
%(
n
dim)
s == PyArray_NDIM(
%(x)
s))
{
Py_XDECREF(
%(out)
s);
Py_XINCREF(
%(x)
s);
...
...
@@ -5352,7 +5352,7 @@ class Flatten(Op):
{
Py_XDECREF(
%(out)
s);
if (
%(
out
dim)
s == 1)
if (
%(
n
dim)
s == 1)
{
npy_intp size = PyArray_SIZE(
%(x)
s);
PyArray_Dims newshape;
...
...
@@ -5365,20 +5365,20 @@ class Flatten(Op):
else
{
npy_intp *oldshape = PyArray_DIMS(
%(x)
s);
npy_intp newshape_dims[
%(
out
dim)
s];
npy_intp newshape_dims[
%(
n
dim)
s];
int i;
for (i = 0; i <
%(
out
dim)
s - 1; ++i)
for (i = 0; i <
%(
n
dim)
s - 1; ++i)
newshape_dims[i] = oldshape[i];
newshape_dims[i] = 1;
for (int j =
%(
out
dim)
s - 1; j < PyArray_NDIM(
%(x)
s); ++j)
for (int j =
%(
n
dim)
s - 1; j < PyArray_NDIM(
%(x)
s); ++j)
newshape_dims[i] *= oldshape[j];
PyArray_Dims newshape;
newshape.ptr = newshape_dims;
newshape.len =
%(
out
dim)
s;
newshape.len =
%(
n
dim)
s;
%(out)
s = (PyArrayObject*)PyArray_Newshape(
%(x)
s,
&newshape,
NPY_CORDER);
...
...
@@ -5428,39 +5428,29 @@ def is_flat(var, ndim=None, outdim=None):
return
var
.
ndim
==
ndim
def
flatten
(
x
,
ndim
=
None
,
outdim
=
None
):
"""
Reshapes the variable x by keeping
the first outdim-1 dimension size(s) of x the same,
and making the last dimension size of x equal to
the
multiplication of its remaining dimension size(s).
def
flatten
(
x
,
ndim
=
1
):
"""
Return a copy of the array collapsed into one dimension.
Reshapes the variable `x` by keeping the first outdim-1 dimension size(s)
of `x` the same, and making the last dimension size of `x` equal to the
multiplication of its remaining dimension size(s).
Parameters
----------
x : theano.tensor.var.TensorVariable
the variable that should be reshaped.
x : theano.tensor.var.TensorVariable
The variable to be reshaped.
ndim : int
The number of dimensions of the returned variable
The default value is ``1``.
ndim : int
the number of dimensions of the returned variable
Default 1.
outdim : int
DEPRECATED synonym for ndim
Returns
-------
theano.tensor.var.TensorVariable
the flattend variable with dimensionality of outdim
"""
if
outdim
is
None
and
ndim
is
None
:
if
ndim
is
None
:
ndim
=
1
elif
outdim
is
not
None
and
ndim
is
not
None
:
raise
ValueError
(
"You should only specify ndim"
)
elif
outdim
is
not
None
:
warnings
.
warn
(
"flatten outdim parameter is deprecated, use ndim instead."
,
category
=
DeprecationWarning
,
)
ndim
=
outdim
# Any input variable can be flattened to have ndim of 1,
# even if it's a scalar. Otherwise, ndim must be positive
# and smaller than x.ndim.
...
...
@@ -5476,32 +5466,11 @@ def flatten(x, ndim=None, outdim=None):
bcast_new_dim
=
builtins
.
all
(
x
.
broadcastable
[
ndim
-
1
:])
broadcastable
=
bcast_kept_dims
+
(
bcast_new_dim
,)
x_reshaped
=
theano
.
tensor
.
addbroadcast
(
x_reshaped
,
*
filter
(
lambda
i
:
broadcastable
[
i
],
range
(
ndim
))
x_reshaped
,
*
[
i
for
i
in
range
(
ndim
)
if
broadcastable
[
i
]]
)
return
x_reshaped
# class TileGrad(Op):
# """
# Calculates the gradient of the Tile Op.
# """
# # this is so weird, I can't think of how to make this a general thing.
# def make_node(self, x, reps, g_out):
# return gof.Apply(self, [x, reps, g_out], [x.type()])
#
# def perform(self, node, inp, out):
# x, reps, g_out = inp
# gx, = out
# xsh = x.shape
# if len(reps) == 2 and reps[1] == 1 and len(x.shape) == 1:
# gx[0] = numpy.sum(g_out, axis=0)
# else:
# raise NotImplementedError('x.shape, reps combination not '
# 'supported', (x.shape, reps))
#
# tilegrad = TileGrad()
class
Tile
(
Op
):
"""
Construct an array by repeating the input x according to reps pattern.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论