Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ea99dcb4
提交
ea99dcb4
authored
4月 24, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make an utility function to hash dict/list for inplace pattern.
上级
3d69604e
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
45 行增加
和
12 行删除
+45
-12
elemwise.py
theano/tensor/elemwise.py
+4
-11
test_utils.py
theano/tensor/tests/test_utils.py
+16
-1
utils.py
theano/tensor/utils.py
+25
-0
没有找到文件。
theano/tensor/elemwise.py
浏览文件 @
ea99dcb4
...
...
@@ -13,6 +13,8 @@ from theano import scalar
from
theano.scalar
import
Scalar
from
theano.printing
import
min_informative_str
,
pprint
from
theano.gof.python25
import
all
,
any
from
theano.tensor.utils
import
hash_from_dict
config
=
theano
.
config
...
...
@@ -563,17 +565,8 @@ class Elemwise(Op):
return
False
def
_rehash
(
self
):
items
=
self
.
inplace_pattern
.
items
()
items
.
sort
()
first_part
=
[
k
for
k
,
v
in
items
]
second_part
=
[]
for
k
,
v
in
items
:
if
isinstance
(
v
,
(
tuple
,
list
)):
second_part
+=
[
tuple
(
v
)]
else
:
second_part
+=
[
v
]
tuple_items
=
tuple
(
first_part
+
second_part
)
h
=
hash
(
'Elemwise'
)
^
hash
(
self
.
scalar_op
)
^
hash
(
tuple_items
)
inplace_pattern_hash
=
hash_from_dict
(
self
.
inplace_pattern
)
h
=
hash
(
'Elemwise'
)
^
hash
(
self
.
scalar_op
)
^
inplace_pattern_hash
assert
h
==
getattr
(
self
,
'_hashval'
,
h
)
self
.
_hashval
=
h
...
...
theano/tensor/tests/test_utils.py
浏览文件 @
ea99dcb4
import
numpy
from
theano.tensor.utils
import
hash_from_ndarray
from
theano.tensor.utils
import
hash_from_ndarray
,
hash_from_dict
def
test_hash_from_ndarray
():
...
...
@@ -31,3 +31,18 @@ def test_hash_from_ndarray():
assert
hash_from_ndarray
(
rng
[:
4
])
==
hash_from_ndarray
(
rng
[:
4
]
.
copy
())
assert
hash_from_ndarray
(
rng
[::
2
])
==
hash_from_ndarray
(
rng
[::
2
]
.
copy
())
assert
hash_from_ndarray
(
rng
[::
-
1
])
==
hash_from_ndarray
(
rng
[::
-
1
]
.
copy
())
def
test_hash_from_dict
():
dicts
=
[{},
{
0
:
0
},
{
0
:
1
},
{
1
:
0
},
{
1
:
1
},
{
0
:
(
0
,)},
{
0
:
[
1
]},
{
0
:
(
0
,
1
)},
{
0
:
[
1
,
0
]},
]
hashs
=
[]
for
idx
,
d
in
enumerate
(
dicts
):
h
=
hash_from_dict
(
d
)
assert
h
not
in
hashs
hashs
.
append
(
h
)
# List are not hashable. So they are transformed into tuple.
assert
hash_from_dict
({
0
:
(
0
,)})
==
hash_from_dict
({
0
:
[
0
]})
theano/tensor/utils.py
浏览文件 @
ea99dcb4
...
...
@@ -18,3 +18,28 @@ def hash_from_ndarray(data):
hash_from_code
(
str
(
data
.
shape
))
+
hash_from_code
(
str
(
data
.
strides
))
+
hash_from_code
(
str
(
data
.
dtype
)))
def
hash_from_dict
(
d
):
"""Work around the fact that dict are not hashable in python
This request that all object have a sorted order that depend only
on the value of the object. This is true for integer/float/string
We do not verify that the objects in the dict what this properties
Also, we transform values that are list into tuple as list are not
hashable.
"""
items
=
d
.
items
()
items
.
sort
()
first_part
=
[
k
for
k
,
v
in
items
]
second_part
=
[]
for
k
,
v
in
items
:
if
isinstance
(
v
,
(
tuple
,
list
)):
second_part
+=
[
tuple
(
v
)]
else
:
second_part
+=
[
v
]
tuple_items
=
tuple
(
first_part
+
second_part
)
return
hash
(
tuple_items
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论