Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3df1e8a3
提交
3df1e8a3
authored
4月 21, 2017
作者:
Pascal Lamblin
提交者:
GitHub
4月 21, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5873 from nouiz/gh-5836
[Interface change] be more consistent, use ndim instead of outdim
上级
ac04eccd
7c9a605b
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
37 行增加
和
23 行删除
+37
-23
basic.py
theano/tensor/basic.py
+24
-13
test_basic.py
theano/tensor/tests/test_basic.py
+13
-10
没有找到文件。
theano/tensor/basic.py
浏览文件 @
3df1e8a3
...
...
@@ -5099,7 +5099,7 @@ def is_flat(var, outdim=1):
return
var
.
ndim
==
outdim
def
flatten
(
x
,
outdim
=
1
):
def
flatten
(
x
,
ndim
=
None
,
outdim
=
None
):
"""
Reshapes the variable x by keeping
the first outdim-1 dimension size(s) of x the same,
...
...
@@ -5111,31 +5111,42 @@ def flatten(x, outdim=1):
x : theano.tensor.var.TensorVariable
the variable that should be reshaped.
out
dim : int
n
dim : 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
"""
# Any input variable can be flattened to have outdim of 1,
# even if it's a scalar. Otherwise, outdim must be positive
if
outdim
is
None
and
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."
)
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.
if
outdim
<
1
or
(
outdim
>
1
and
out
dim
>
x
.
ndim
):
raise
ValueError
(
'
out
dim
%
s out of bound [1,
%
d)'
%
(
out
dim
,
x
.
ndim
+
1
))
if
ndim
<
1
or
(
ndim
>
1
and
n
dim
>
x
.
ndim
):
raise
ValueError
(
'
n
dim
%
s out of bound [1,
%
d)'
%
(
n
dim
,
x
.
ndim
+
1
))
if
out
dim
>
1
:
dims
=
tuple
(
x
.
shape
[:
out
dim
-
1
])
+
(
-
1
,)
if
n
dim
>
1
:
dims
=
tuple
(
x
.
shape
[:
n
dim
-
1
])
+
(
-
1
,)
else
:
dims
=
(
-
1
,)
x_reshaped
=
x
.
reshape
(
dims
)
bcast_kept_dims
=
x
.
broadcastable
[:
out
dim
-
1
]
bcast_new_dim
=
python_all
(
x
.
broadcastable
[
out
dim
-
1
:])
bcast_kept_dims
=
x
.
broadcastable
[:
n
dim
-
1
]
bcast_new_dim
=
python_all
(
x
.
broadcastable
[
n
dim
-
1
:])
broadcastable
=
bcast_kept_dims
+
(
bcast_new_dim
,)
x_reshaped
=
theano
.
tensor
.
addbroadcast
(
x_reshaped
,
*
filter
(
lambda
i
:
broadcastable
[
i
],
range
(
out
dim
)))
x_reshaped
,
*
filter
(
lambda
i
:
broadcastable
[
i
],
range
(
n
dim
)))
return
x_reshaped
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
3df1e8a3
...
...
@@ -5530,7 +5530,7 @@ def test_flatten_scalar():
# utt.verify_grad(flatten, [a_val]) #TODO: fix verify_grd to work on scalars
def
test_flatten_
out
dim1
():
def
test_flatten_
n
dim1
():
a
=
dmatrix
()
c
=
flatten
(
a
,
1
)
f
=
inplace_func
([
a
],
c
)
...
...
@@ -5543,7 +5543,7 @@ def test_flatten_outdim1():
utt
.
verify_grad
(
flatten
,
[
a_val
])
def
test_flatten_
out
dim2
():
def
test_flatten_
n
dim2
():
a
=
dmatrix
()
c
=
flatten
(
a
,
2
)
f
=
inplace_func
([
a
],
c
)
...
...
@@ -5552,11 +5552,11 @@ def test_flatten_outdim2():
f
=
inplace_func
([
a
],
c
)
assert
np
.
all
(
f
(
a_val
)
==
a_val
)
flatten_2
=
partial
(
flatten
,
out
dim
=
2
)
flatten_2
=
partial
(
flatten
,
n
dim
=
2
)
utt
.
verify_grad
(
flatten_2
,
[
a_val
])
def
test_flatten_
out
dim2_of_3
():
def
test_flatten_
n
dim2_of_3
():
a
=
TensorType
(
'float64'
,
(
False
,
False
,
False
))()
c
=
flatten
(
a
,
2
)
f
=
inplace_func
([
a
],
c
)
...
...
@@ -5567,6 +5567,9 @@ def test_flatten_outdim2_of_3():
f
=
inplace_func
([
a
],
c
)
assert
np
.
all
(
f
(
a_val
)
==
c_val
)
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
])
...
...
@@ -5576,27 +5579,27 @@ def test_flatten_broadcastable():
# that of the input
inp
=
TensorType
(
'float64'
,
(
False
,
False
,
False
,
False
))()
out
=
flatten
(
inp
,
out
dim
=
2
)
out
=
flatten
(
inp
,
n
dim
=
2
)
assert
out
.
broadcastable
==
(
False
,
False
)
inp
=
TensorType
(
'float64'
,
(
False
,
False
,
False
,
True
))()
out
=
flatten
(
inp
,
out
dim
=
2
)
out
=
flatten
(
inp
,
n
dim
=
2
)
assert
out
.
broadcastable
==
(
False
,
False
)
inp
=
TensorType
(
'float64'
,
(
False
,
True
,
False
,
True
))()
out
=
flatten
(
inp
,
out
dim
=
2
)
out
=
flatten
(
inp
,
n
dim
=
2
)
assert
out
.
broadcastable
==
(
False
,
False
)
inp
=
TensorType
(
'float64'
,
(
False
,
True
,
True
,
True
))()
out
=
flatten
(
inp
,
out
dim
=
2
)
out
=
flatten
(
inp
,
n
dim
=
2
)
assert
out
.
broadcastable
==
(
False
,
True
)
inp
=
TensorType
(
'float64'
,
(
True
,
False
,
True
,
True
))()
out
=
flatten
(
inp
,
out
dim
=
3
)
out
=
flatten
(
inp
,
n
dim
=
3
)
assert
out
.
broadcastable
==
(
True
,
False
,
True
)
def
test_flatten_
out
dim_invalid
():
def
test_flatten_
n
dim_invalid
():
a
=
dmatrix
()
assert_raises
(
ValueError
,
flatten
,
a
,
3
)
assert_raises
(
ValueError
,
flatten
,
a
,
0
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论