Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9815eca2
提交
9815eca2
authored
9月 17, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #923 from mrocklin/give_variables_names
add function to give variables unique names
上级
18be4e51
353ae858
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
66 行增加
和
7 行删除
+66
-7
test_utils.py
theano/gof/tests/test_utils.py
+33
-0
utils.py
theano/gof/utils.py
+33
-7
没有找到文件。
theano/gof/tests/test_utils.py
0 → 100644
浏览文件 @
9815eca2
import
theano
from
theano.gof.utils
import
give_variables_names
,
unique
def
test_give_variables_names
():
x
=
theano
.
tensor
.
matrix
(
'x'
)
y
=
x
+
1
z
=
theano
.
tensor
.
dot
(
x
,
y
)
variables
=
(
x
,
y
,
z
)
give_variables_names
(
variables
)
assert
all
(
var
.
name
for
var
in
variables
)
assert
unique
([
var
.
name
for
var
in
variables
])
def
test_give_variables_names_idempotence
():
x
=
theano
.
tensor
.
matrix
(
'x'
)
y
=
x
+
1
z
=
theano
.
tensor
.
dot
(
x
,
y
)
variables
=
(
x
,
y
,
z
)
give_variables_names
(
variables
)
names
=
[
var
.
name
for
var
in
variables
]
give_variables_names
(
variables
)
names2
=
[
var
.
name
for
var
in
variables
]
assert
names
==
names2
def
test_give_variables_names_small
():
x
=
theano
.
tensor
.
matrix
(
'x'
)
y
=
theano
.
tensor
.
dot
(
x
,
x
)
fgraph
=
theano
.
FunctionGraph
((
x
,),
(
y
,))
give_variables_names
(
fgraph
.
variables
)
assert
all
(
var
.
name
for
var
in
fgraph
.
variables
)
assert
unique
([
var
.
name
for
var
in
fgraph
.
variables
])
theano/gof/utils.py
浏览文件 @
9815eca2
...
@@ -7,7 +7,7 @@ import re, os, traceback
...
@@ -7,7 +7,7 @@ import re, os, traceback
def
add_tag_trace
(
thing
):
def
add_tag_trace
(
thing
):
"""Add tag.trace to an node or variable.
"""Add tag.trace to an node or variable.
The argument is returned after being affected (inplace).
The argument is returned after being affected (inplace).
"""
"""
limit
=
config
.
traceback
.
limit
limit
=
config
.
traceback
.
limit
...
@@ -20,7 +20,7 @@ def hashgen():
...
@@ -20,7 +20,7 @@ def hashgen():
return
hashgen
.
next
return
hashgen
.
next
hashgen
.
next
=
0
hashgen
.
next
=
0
class
MethodNotDefined
(
Exception
):
class
MethodNotDefined
(
Exception
):
"""
"""
To be raised by functions defined as part of an interface.
To be raised by functions defined as part of an interface.
...
@@ -78,7 +78,7 @@ def memoize(f):
...
@@ -78,7 +78,7 @@ def memoize(f):
def
deprecated
(
filename
,
msg
=
''
):
def
deprecated
(
filename
,
msg
=
''
):
"""Decorator which will print a warning message on the first call.
"""Decorator which will print a warning message on the first call.
Use it like this:
Use it like this:
@deprecated('myfile', 'do something different...')
@deprecated('myfile', 'do something different...')
...
@@ -107,9 +107,9 @@ def uniq(seq):
...
@@ -107,9 +107,9 @@ def uniq(seq):
def
difference
(
seq1
,
seq2
):
def
difference
(
seq1
,
seq2
):
"""
"""
Returns all elements in seq1 which are not in seq2: i.e seq1
\
seq2
Returns all elements in seq1 which are not in seq2: i.e seq1
\
seq2
"""
"""
try
:
try
:
# try to use O(const * len(seq1)) algo
# try to use O(const * len(seq1)) algo
if
len
(
seq2
)
<
4
:
# I'm guessing this threshold -JB
if
len
(
seq2
)
<
4
:
# I'm guessing this threshold -JB
raise
Exception
(
'not worth it'
)
raise
Exception
(
'not worth it'
)
...
@@ -131,7 +131,7 @@ def partition(f, seq):
...
@@ -131,7 +131,7 @@ def partition(f, seq):
else
:
else
:
seqf
.
append
(
elem
)
seqf
.
append
(
elem
)
return
seqt
,
seqf
return
seqt
,
seqf
def
attr_checker
(
*
attrs
):
def
attr_checker
(
*
attrs
):
def
f
(
candidate
):
def
f
(
candidate
):
for
attr
in
attrs
:
for
attr
in
attrs
:
...
@@ -204,7 +204,7 @@ def toposort(prereqs_d):
...
@@ -204,7 +204,7 @@ def toposort(prereqs_d):
# for x, y in prereqs_d.items():
# for x, y in prereqs_d.items():
# all2.update(y)
# all2.update(y)
# print all1.difference(all2)
# print all1.difference(all2)
seq
=
[]
seq
=
[]
done
=
set
()
done
=
set
()
postreqs_d
=
{}
postreqs_d
=
{}
...
@@ -330,3 +330,29 @@ def flatten(a):
...
@@ -330,3 +330,29 @@ def flatten(a):
return
l
return
l
else
:
else
:
return
[
a
]
return
[
a
]
def
unique
(
x
):
return
len
(
set
(
x
))
==
len
(
x
)
def
hist
(
coll
):
counts
=
{}
for
elem
in
coll
:
counts
[
elem
]
=
counts
.
get
(
elem
,
0
)
+
1
return
counts
def
give_variables_names
(
variables
):
""" Gives unique names to an iterable of variables. Modifies input.
This function is idempotent."""
names
=
map
(
lambda
var
:
var
.
name
,
variables
)
h
=
hist
(
names
)
bad_var
=
lambda
var
:
not
var
.
name
or
h
[
var
.
name
]
>
1
for
i
,
var
in
enumerate
(
filter
(
bad_var
,
variables
)):
var
.
name
=
(
var
.
name
or
""
)
+
"_
%
d"
%
i
if
not
unique
(
map
(
str
,
variables
)):
raise
ValueError
(
"Not all variables have unique names."
"Maybe you've named some of the variables identically"
)
return
variables
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论