Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3a264877
提交
3a264877
authored
10月 19, 2015
作者:
Sina Honari
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improving is_flat, adding a test and docstring
上级
072b031f
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
59 行增加
和
18 行删除
+59
-18
basic_ops.py
theano/sandbox/cuda/basic_ops.py
+8
-0
basic.py
theano/tensor/basic.py
+19
-17
test_basic.py
theano/tensor/tests/test_basic.py
+31
-0
test_opt.py
theano/tensor/tests/test_opt.py
+1
-1
没有找到文件。
theano/sandbox/cuda/basic_ops.py
浏览文件 @
3a264877
...
...
@@ -3341,6 +3341,14 @@ class GpuFlatten(gof.HideC, tensor.Reshape, GpuOp):
def
gpu_flatten
(
x
,
outdim
=
1
):
"""
Implement flatten on the gpu.
:param x: the variable that should be reshaped.
:type x: theano.tensor.var.TensorVariable
:param outdim: the number of dimensions of the returned variable
:type outdim: int
:returns: the flattend variable with dimensionality of outdim
"""
x
=
as_cuda_ndarray_variable
(
x
)
if
outdim
>
1
:
...
...
theano/tensor/basic.py
浏览文件 @
3a264877
...
...
@@ -4659,32 +4659,34 @@ class Flatten(Op):
def
is_flat
(
node
,
outdim
=
1
):
"""
Checks whether node's op is an instance of Reshape
and verifies the dimensionality of variable is correct.
"""
if
not
isinstance
(
node
.
op
,
theano
.
tensor
.
Reshape
):
return
False
verifies the dimensionality of variable is correct.
new_shape
=
node
.
inputs
[
1
]
:param node: the theano node on which the dimensionality is checked.
:type node: theano.tensor.var.TensorVariable
# If new shape is defined by `MakeVector`, then number of inputs to
# `MakeVector` must be equal to `outdim`.
if
new_shape
.
owner
and
\
isinstance
(
new_shape
.
owner
.
op
,
theano
.
tensor
.
opt
.
MakeVector
):
return
new_shape
.
ndim
==
1
and
len
(
new_shape
.
owner
.
inputs
)
==
outdim
# `TensorConstant`
elif
isinstance
(
new_shape
,
theano
.
tensor
.
TensorConstant
):
return
new_shape
.
ndim
==
1
and
new_shape
.
data
.
shape
[
0
]
==
outdim
else
:
raise
NotImplemented
()
:param outdim: the expected dimensionality of node.
:type outdim: int
:returns: the comparison result of node's dim
and the expected outdim.
"""
return
node
.
ndim
==
outdim
def
flatten
(
x
,
outdim
=
1
):
"""
Reshapes the variable x by keeping
the first outdim-1 dimension(s) of x the same
the first outdim-1 dimension(s) of x the same
,
and making the last dimension of x equal to
the multiplication of its remaining dimensions.
:param x: the theano variable that should be reshaped.
:type x: theano.tensor.var.TensorVariable
:param outdim: the number of dimensions of the returned variable
:type outdim: int
:returns: the flattend variable with dimensionality of outdim
"""
outdim
=
int
(
outdim
)
# Any input variable can be flattened to have outdim of 1,
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
3a264877
...
...
@@ -5254,6 +5254,37 @@ def test_flatten_outdim_invalid():
pass
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
"""
# Constant variable
assert
tensor
.
is_flat
(
tensor
.
as_tensor_variable
(
numpy
.
zeros
((
10
))))
assert
tensor
.
is_flat
(
tensor
.
as_tensor_variable
(
numpy
.
zeros
((
10
,
10
,
10
))),
outdim
=
3
)
assert
not
tensor
.
is_flat
(
tensor
.
as_tensor_variable
(
numpy
.
zeros
((
10
,
10
,
10
))))
# Symbolic variable
assert
tensor
.
is_flat
(
tensor
.
vector
())
assert
tensor
.
is_flat
(
tensor
.
tensor3
(),
outdim
=
3
)
assert
not
tensor
.
is_flat
(
tensor
.
tensor3
())
# Reshape with constant shape
X
=
tensor
.
tensor4
()
assert
tensor
.
is_flat
(
X
.
reshape
((
-
1
,
)))
assert
tensor
.
is_flat
(
X
.
reshape
((
10
,
10
,
-
1
)),
outdim
=
3
)
assert
not
tensor
.
is_flat
(
X
.
reshape
((
10
,
10
,
-
1
)))
# Reshape with symbolic shape
X
=
tensor
.
tensor4
()
assert
tensor
.
is_flat
(
X
.
reshape
((
tensor
.
iscalar
(),
)))
assert
tensor
.
is_flat
(
X
.
reshape
((
tensor
.
iscalar
(),
)
*
3
),
outdim
=
3
)
assert
not
tensor
.
is_flat
(
X
.
reshape
((
tensor
.
iscalar
(),
)
*
3
))
def
test_tile
():
def
run_tile
(
x
,
x_
,
reps
,
use_symbolic_reps
):
if
use_symbolic_reps
:
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
3a264877
...
...
@@ -5890,7 +5890,7 @@ def test_local_flatten_lift():
topo
=
f
.
maker
.
fgraph
.
toposort
()
shape_out_np
=
tuple
(
x_np
.
shape
[:
i
-
1
])
+
(
numpy
.
prod
(
x_np
.
shape
[
i
-
1
:]),)
assert
shape_out_np
==
out_np
.
shape
assert
tensor
.
is_flat
(
topo
[
-
2
],
outdim
=
i
)
assert
tensor
.
is_flat
(
topo
[
-
2
]
.
outputs
[
0
]
,
outdim
=
i
)
assert
isinstance
(
topo
[
-
1
]
.
op
,
tensor
.
Elemwise
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论