Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d9a66a7d
提交
d9a66a7d
authored
9月 20, 2016
作者:
Gijs van Tulder
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pad_dims examples, and keep as many dimensions as possible.
上级
d39be759
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
74 行增加
和
18 行删除
+74
-18
opt_util.py
theano/gpuarray/opt_util.py
+37
-9
opt_util.py
theano/sandbox/cuda/opt_util.py
+37
-9
没有找到文件。
theano/gpuarray/opt_util.py
浏览文件 @
d9a66a7d
...
...
@@ -341,6 +341,24 @@ def pad_dims(input, leftdims, rightdims):
This reduces or expands the number of dimensions of the input to
exactly `leftdims`, by adding extra dimensions on the left or by
combining some existing dimensions on the left of the input.
Use `unpad_dims` to reshape back to the original dimensions.
Examples
--------
Given input of shape (3, 5, 7), ``pad_dims(input, 2, 2)``
adds a singleton dimension and reshapes to (3, 1, 5, 7).
Given that output from pad_dims, ``unpad_dims(output, input, 2, 2)``
reshapes back to (3, 5, 7).
Given input of shape (3, 5, 7, 9), ``pad_dims(input, 2, 2)``
does not reshape and returns output with shape (3, 5, 7, 9).
Given input of shape (3, 5, 7, 9, 11), ``pad_dims(input, 2, 2)``
combines the first two dimensions and reshapes to (8, 7, 9, 11).
Given input of shape (3, 5, 7, 9), ``pad_dims(input, 2, 3)``
adds a singleton dimension and reshapes to (3, 1, 5, 7, 9).
"""
assert
input
.
ndim
>=
rightdims
...
...
@@ -350,15 +368,25 @@ def pad_dims(input, leftdims, rightdims):
# extract image dimensions
img_shape
=
input
.
shape
[
-
rightdims
:]
# count the number of "leading" dimensions, store as dmatrix
batch_size
=
tensor
.
prod
(
input
.
shape
[:
-
rightdims
])
batch_size
=
tensor
.
shape_padright
(
batch_size
,
1
)
# store in the required shape, for example as a 4D tensor
# with shape: (batch_size,1,height,width)
new_shape
=
tensor
.
cast
(
tensor
.
join
(
0
,
batch_size
,
tensor
.
as_tensor
([
1
]
*
(
leftdims
-
1
)),
img_shape
),
'int64'
)
non_pool_ndim
=
input
.
ndim
-
rightdims
if
non_pool_ndim
<
leftdims
:
# too few dimensions, pad on the left
dummy_dims
=
tensor
.
as_tensor
([
1
]
*
(
leftdims
-
non_pool_ndim
))
new_shape
=
tensor
.
join
(
0
,
dummy_dims
,
input
.
shape
[:
non_pool_ndim
],
img_shape
)
else
:
# too many dimensions, combine the leading dimensions
batched_ndim
=
non_pool_ndim
-
leftdims
+
1
batch_size
=
tensor
.
prod
(
input
.
shape
[:
batched_ndim
])
# convert to a vector for tensor.join
batch_size
=
tensor
.
shape_padright
(
batch_size
,
1
)
new_shape
=
tensor
.
join
(
0
,
batch_size
,
input
.
shape
[
batched_ndim
:
non_pool_ndim
],
img_shape
)
# store in the required shape
new_shape
=
tensor
.
cast
(
new_shape
,
'int64'
)
input_ND
=
GpuReshape
(
leftdims
+
rightdims
)(
input
,
new_shape
)
return
input_ND
...
...
theano/sandbox/cuda/opt_util.py
浏览文件 @
d9a66a7d
...
...
@@ -138,6 +138,24 @@ def pad_dims(input, leftdims, rightdims):
This reduces or expands the number of dimensions of the input to
exactly `leftdims`, by adding extra dimensions on the left or by
combining some existing dimensions on the left of the input.
Use `unpad_dims` to reshape back to the original dimensions.
Examples
--------
Given input of shape (3, 5, 7), ``pad_dims(input, 2, 2)``
adds a singleton dimension and reshapes to (3, 1, 5, 7).
Given that output from pad_dims, ``unpad_dims(output, input, 2, 2)``
reshapes back to (3, 5, 7).
Given input of shape (3, 5, 7, 9), ``pad_dims(input, 2, 2)``
does not reshape and returns output with shape (3, 5, 7, 9).
Given input of shape (3, 5, 7, 9, 11), ``pad_dims(input, 2, 2)``
combines the first two dimensions and reshapes to (8, 7, 9, 11).
Given input of shape (3, 5, 7, 9), ``pad_dims(input, 2, 3)``
adds a singleton dimension and reshapes to (3, 1, 5, 7, 9).
"""
assert
input
.
ndim
>=
rightdims
...
...
@@ -147,15 +165,25 @@ def pad_dims(input, leftdims, rightdims):
# extract image dimensions
img_shape
=
input
.
shape
[
-
rightdims
:]
# count the number of "leading" dimensions, store as dmatrix
batch_size
=
tensor
.
prod
(
input
.
shape
[:
-
rightdims
])
batch_size
=
tensor
.
shape_padright
(
batch_size
,
1
)
# store in the required shape, for example as a 4D tensor
# with shape: (batch_size,1,height,width)
new_shape
=
tensor
.
cast
(
tensor
.
join
(
0
,
batch_size
,
tensor
.
as_tensor
([
1
]
*
(
leftdims
-
1
)),
img_shape
),
'int64'
)
non_pool_ndim
=
input
.
ndim
-
rightdims
if
non_pool_ndim
<
leftdims
:
# too few dimensions, pad on the left
dummy_dims
=
tensor
.
as_tensor
([
1
]
*
(
leftdims
-
non_pool_ndim
))
new_shape
=
tensor
.
join
(
0
,
dummy_dims
,
input
.
shape
[:
non_pool_ndim
],
img_shape
)
else
:
# too many dimensions, combine the leading dimensions
batched_ndim
=
non_pool_ndim
-
leftdims
+
1
batch_size
=
tensor
.
prod
(
input
.
shape
[:
batched_ndim
])
# convert to a vector for tensor.join
batch_size
=
tensor
.
shape_padright
(
batch_size
,
1
)
new_shape
=
tensor
.
join
(
0
,
batch_size
,
input
.
shape
[
batched_ndim
:
non_pool_ndim
],
img_shape
)
# store in the required shape
new_shape
=
tensor
.
cast
(
new_shape
,
'int64'
)
input_ND
=
GpuReshape
(
leftdims
+
rightdims
)(
input
,
new_shape
)
return
input_ND
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论