Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f34525e5
提交
f34525e5
authored
6月 05, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #675 from mrocklin/shape-of-variables
Added shape_of_variables function
上级
3775cef2
0a7a5538
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
66 行增加
和
1 行删除
+66
-1
test_utils.py
theano/tensor/tests/test_utils.py
+23
-1
utils.py
theano/tensor/utils.py
+43
-0
没有找到文件。
theano/tensor/tests/test_utils.py
浏览文件 @
f34525e5
import
numpy
from
theano.tensor.utils
import
hash_from_ndarray
,
hash_from_dict
import
theano
from
theano.tensor.utils
import
(
hash_from_ndarray
,
hash_from_dict
,
shape_of_variables
)
def
test_hash_from_ndarray
():
...
...
@@ -46,3 +48,23 @@ def test_hash_from_dict():
# List are not hashable. So they are transformed into tuple.
assert
hash_from_dict
({
0
:
(
0
,)})
==
hash_from_dict
({
0
:
[
0
]})
def
test_shape_of_variables_simple
():
x
=
theano
.
tensor
.
matrix
(
'x'
)
y
=
x
+
x
env
=
theano
.
Env
([
x
],
[
y
])
assert
shape_of_variables
(
env
,
{
x
:
(
5
,
5
)})
==
{
x
:
(
5
,
5
),
y
:
(
5
,
5
)}
x
=
theano
.
tensor
.
matrix
(
'x'
)
y
=
theano
.
tensor
.
dot
(
x
,
x
.
T
)
env
=
theano
.
Env
([
x
],
[
y
])
shapes
=
shape_of_variables
(
env
,
{
x
:
(
5
,
1
)})
assert
shapes
[
x
]
==
(
5
,
1
)
assert
shapes
[
y
]
==
(
5
,
5
)
def
test_shape_of_variables_subtensor
():
x
=
theano
.
tensor
.
matrix
(
'x'
)
subx
=
x
[
1
:]
env
=
theano
.
Env
([
x
],
[
subx
])
shapes
=
shape_of_variables
(
env
,
{
x
:
(
10
,
10
)})
assert
shapes
[
subx
]
==
(
9
,
10
)
theano/tensor/utils.py
浏览文件 @
f34525e5
import
numpy
import
theano
from
theano.gof.cc
import
hash_from_code
...
...
@@ -43,3 +44,45 @@ def hash_from_dict(d):
second_part
+=
[
v
]
tuple_items
=
tuple
(
first_part
+
second_part
)
return
hash
(
tuple_items
)
def
shape_of_variables
(
env
,
input_shapes
):
"""
Compute the numeric shape of all intermediate variables given input shapes
Inputs:
env - the theano.Env in question
input_shapes - a dict mapping input to shape
Outputs:
shapes - a dict mapping variable to shape
WARNING : This modifies the env. Not pure.
>>> import theano
>>> x = theano.tensor.matrix('x')
>>> y = x[512:]; y.name = 'y'
>>> env = theano.Env([x], [y])
>>> shape_of_variables(env, {x: (1024, 1024)})
{y: (512, 1024), x: (1024, 1024)}
"""
if
not
hasattr
(
env
,
'shape_feature'
):
env
.
extend
(
theano
.
tensor
.
opt
.
ShapeFeature
())
input_dims
=
[
dimension
for
inp
in
env
.
inputs
for
dimension
in
env
.
shape_feature
.
shape_of
[
inp
]]
output_dims
=
[
dimension
for
shape
in
env
.
shape_feature
.
shape_of
.
values
()
for
dimension
in
shape
]
compute_shapes
=
theano
.
function
(
input_dims
,
output_dims
)
numeric_input_dims
=
[
dim
for
inp
in
env
.
inputs
for
dim
in
input_shapes
[
inp
]]
numeric_output_dims
=
compute_shapes
(
*
numeric_input_dims
)
sym_to_num_dict
=
dict
(
zip
(
output_dims
,
numeric_output_dims
))
return
{
var
:
tuple
(
sym_to_num_dict
[
sym
]
for
sym
in
env
.
shape_feature
.
shape_of
[
var
])
for
var
in
env
.
shape_feature
.
shape_of
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论