Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
45f56231
提交
45f56231
authored
2月 20, 2008
作者:
bergstrj@iro.umontreal.ca
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed core.literal(), all tests pass including getslice!
上级
cd8be514
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
72 行增加
和
39 行删除
+72
-39
core.py
core.py
+67
-37
lib.py
gof/lib.py
+2
-2
op.py
gof/op.py
+3
-0
没有找到文件。
core.py
浏览文件 @
45f56231
...
...
@@ -48,14 +48,6 @@ def _approx_eq(a,b,eps=1.0e-9):
return
numpy
.
all
(
d
<
eps
)
literals_db
=
{}
#literals_id_db = weakref.WeakValueDictionary()
literals_id_db
=
{}
#input floating point scalars will be cast to arrays of this type
# see TRAC(#31)
default_input_scalar_dtype
=
'float64'
@blas._constant
# TODO: move this decorator to a utility script
def
_compile_dir
():
"""Return the directory in which scipy.weave should store code objects.
...
...
@@ -99,8 +91,12 @@ def input(x):
# being cast to floating-point (can that cause incorrectness?)
if
isinstance
(
x
,
numpy
.
ndarray
):
return
NumpyR
(
x
)
elif
isinstance
(
x
,
(
int
,
float
)):
z
=
numpy
.
zeros
((),
dtype
=
default_input_scalar_dtype
)
elif
isinstance
(
x
,
int
):
z
=
numpy
.
zeros
((),
dtype
=
input
.
int_dtype
)
z
+=
x
return
NumpyR
(
z
)
elif
isinstance
(
x
,
float
):
z
=
numpy
.
zeros
((),
dtype
=
input
.
float_dtype
)
z
+=
x
return
NumpyR
(
z
)
elif
isinstance
(
x
,
gof
.
Result
):
...
...
@@ -108,6 +104,9 @@ def input(x):
else
:
return
ResultValue
(
x
)
input
.
float_dtype
=
'float64'
input
.
int_dtype
=
'int64'
def
wrap
(
x
):
if
isinstance
(
x
,
NumpyR
):
return
x
...
...
@@ -118,38 +117,69 @@ def wrap(x):
else
:
return
literal
(
x
)
def
_hashable
(
x
):
class
_testCase_wrap
(
unittest
.
TestCase
):
def
setUp
(
self
):
literal
.
hdb
=
{}
literal
.
udb
=
{}
def
test_input_int
(
self
):
w
=
input
(
3
)
self
.
failUnless
(
isinstance
(
w
,
NumpyR
))
self
.
failUnless
(
str
(
w
.
data
.
dtype
)
==
input
.
int_dtype
)
self
.
failUnless
(
w
.
data
==
3
)
def
test_input_float
(
self
):
w
=
input
(
3.0
)
self
.
failUnless
(
isinstance
(
w
,
NumpyR
))
self
.
failUnless
(
str
(
w
.
data
.
dtype
)
==
input
.
float_dtype
)
self
.
failUnless
(
w
.
data
==
3.0
)
def
test_literal_int
(
self
):
w
=
literal
(
3
)
self
.
failUnless
(
isinstance
(
w
,
NumpyR
))
self
.
failUnless
(
str
(
w
.
data
.
dtype
)
==
input
.
int_dtype
)
self
.
failUnless
(
w
.
data
==
3
)
def
test_literal_float
(
self
):
w
=
literal
(
3.0
)
self
.
failUnless
(
isinstance
(
w
,
NumpyR
))
self
.
failUnless
(
str
(
w
.
data
.
dtype
)
==
input
.
float_dtype
)
self
.
failUnless
(
w
.
data
==
3.0
)
def
test_wrap_int
(
self
):
w
=
wrap
(
3
)
self
.
failUnless
(
isinstance
(
w
,
NumpyR
))
self
.
failUnless
(
str
(
w
.
data
.
dtype
)
==
input
.
int_dtype
)
self
.
failUnless
(
w
.
data
==
3
)
def
test_wrap_float
(
self
):
w
=
wrap
(
3.0
)
self
.
failUnless
(
isinstance
(
w
,
NumpyR
))
self
.
failUnless
(
str
(
w
.
data
.
dtype
)
==
input
.
float_dtype
)
self
.
failUnless
(
w
.
data
==
3.0
)
def
literal
(
x
):
"""Return a ResultValue instance wrapping a literal."""
def
_hashable
(
x
):
try
:
x
in
{}
return
True
except
TypeError
:
# x is unhashable
return
False
def
_literal_hashable
(
x
):
if
x
in
literals_db
:
return
literals_db
[
x
]
else
:
r
=
input
(
x
)
r
.
constant
=
True
literals_db
[
x
]
=
r
return
r
#static member initialization
if
not
hasattr
(
literal
,
'hdb'
):
literal
.
hdb
=
{}
literal
.
udb
=
{}
def
_literal_unhashable
(
x
):
idx
=
id
(
x
)
if
idx
in
literals_id_db
:
return
literals_id_db
[
idx
]
if
_hashable
(
x
):
db
=
literal
.
hdb
key
=
(
id
(
x
),
x
)
else
:
r
=
input
(
x
)
r
.
constant
=
True
literals_id_db
[
idx
]
=
r
return
r
db
=
literal
.
udb
key
=
(
id
(
x
),)
def
literal
(
x
):
"""Return a ResultValue instance wrapping a literal."""
if
_hashable
(
x
):
return
_literal_hashable
(
x
)
if
key
in
db
:
return
db
[
key
]
else
:
return
_literal_unhashable
(
x
)
rval
=
input
(
x
)
rval
.
constant
=
True
db
[
key
]
=
rval
return
rval
...
...
@@ -682,9 +712,9 @@ class NumpyR(gof.ResultValue):
raise
ValueError
()
else
:
if
0
==
len
(
self
.
data
.
shape
):
self
.
data
.
itemset
(
value
)
self
.
data
.
itemset
(
value
)
# for scalars
else
:
self
.
data
[:]
=
value
self
.
data
[:]
=
value
# for matrices
self
.
refresh
()
self
.
up_to_date
=
True
...
...
@@ -693,7 +723,8 @@ class NumpyR(gof.ResultValue):
self
.
spec
=
(
numpy
.
ndarray
,
self
.
data
.
dtype
,
self
.
data
.
shape
)
def
alloc
(
self
):
self
.
data
=
numpy
.
ndarray
(
self
.
spec
[
2
],
self
.
spec
[
1
])
shape
,
dtype
=
self
.
spec
[
2
],
self
.
spec
[
1
]
self
.
data
=
numpy
.
ndarray
(
shape
,
dtype
=
dtype
)
def
__add__
(
self
,
y
):
return
add
(
self
,
y
)
def
__radd__
(
self
,
x
):
return
add
(
x
,
self
)
...
...
@@ -1487,10 +1518,9 @@ class _testCase_slicing(unittest.TestCase):
self
.
fail
(
'add should not have succeeded'
)
def
test_getitem1
(
self
):
#TODO: re-enable this test
return
a
=
numpy
.
ones
((
4
,
4
))
wa1
=
wrap
(
a
)[
1
]
self
.
failUnless
(
wa1
.
data
.
shape
==
(
4
,))
def
test_getslice_0d_all
(
self
):
"""Test getslice does not work on 0d array """
...
...
gof/lib.py
浏览文件 @
45f56231
...
...
@@ -126,10 +126,10 @@ class ResultValue(Result):
def
__init__
(
self
,
x
=
UNCOMPUTED
,
constant
=
False
):
self
.
constant
=
False
self
.
set_value
(
x
)
self
.
set_value
(
x
)
# allow set_value before constant = True
self
.
constant
=
constant
self
.
up_to_date
=
True
self
.
spec
=
None
self
.
refresh
()
# to set spec
def
__str__
(
self
):
return
str
(
self
.
data
)
...
...
gof/op.py
浏览文件 @
45f56231
...
...
@@ -118,6 +118,9 @@ class Op(object):
try
:
self
.
validate
()
except
:
# this call gives a subclass the chance to undo the set_outputs
# that it may have triggered...
# TODO: test this functionality!
self
.
set_input
(
i
,
previous
,
True
,
False
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论