Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f3ae6acc
提交
f3ae6acc
authored
11月 23, 2010
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
white space fix.
上级
8188f49f
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
163 行增加
和
167 行删除
+163
-167
test_tutorial.py
theano/tests/test_tutorial.py
+163
-167
没有找到文件。
theano/tests/test_tutorial.py
浏览文件 @
f3ae6acc
...
@@ -33,7 +33,7 @@ class T_extending(unittest.TestCase):
...
@@ -33,7 +33,7 @@ class T_extending(unittest.TestCase):
return
float
(
x
)
return
float
(
x
)
def
values_eq_approx
(
x
,
y
,
tolerance
=
1e-4
):
def
values_eq_approx
(
x
,
y
,
tolerance
=
1e-4
):
return
abs
(
x
-
y
)
/
(
abs
(
x
)
+
abs
(
y
))
<
tolerance
return
abs
(
x
-
y
)
/
(
abs
(
x
)
+
abs
(
y
))
<
tolerance
from
theano
import
gof
from
theano
import
gof
...
@@ -47,13 +47,13 @@ class T_extending(unittest.TestCase):
...
@@ -47,13 +47,13 @@ class T_extending(unittest.TestCase):
class
Double
(
gof
.
Type
):
class
Double
(
gof
.
Type
):
def
filter
(
self
,
x
,
strict
=
False
):
def
filter
(
self
,
x
,
strict
=
False
):
if
strict
and
not
isinstance
(
x
,
float
):
if
strict
and
not
isinstance
(
x
,
float
):
raise
TypeError
(
'Expected a float!'
)
raise
TypeError
(
'Expected a float!'
)
return
float
(
x
)
return
float
(
x
)
def
values_eq_approx
(
self
,
x
,
y
,
tolerance
=
1e-4
):
def
values_eq_approx
(
self
,
x
,
y
,
tolerance
=
1e-4
):
return
abs
(
x
-
y
)
/
(
abs
(
x
)
+
abs
(
y
))
<
tolerance
return
abs
(
x
-
y
)
/
(
abs
(
x
)
+
abs
(
y
))
<
tolerance
double
=
Double
()
double
=
Double
()
...
@@ -67,16 +67,16 @@ class T_extending(unittest.TestCase):
...
@@ -67,16 +67,16 @@ class T_extending(unittest.TestCase):
class
Double
(
gof
.
Type
):
class
Double
(
gof
.
Type
):
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
if
strict
and
not
isinstance
(
x
,
float
):
if
strict
and
not
isinstance
(
x
,
float
):
raise
TypeError
(
'Expected a float!'
)
raise
TypeError
(
'Expected a float!'
)
return
float
(
x
)
return
float
(
x
)
def
values_eq_approx
(
self
,
x
,
y
,
tolerance
=
1e-4
):
def
values_eq_approx
(
self
,
x
,
y
,
tolerance
=
1e-4
):
return
abs
(
x
-
y
)
/
(
abs
(
x
)
+
abs
(
y
))
<
tolerance
return
abs
(
x
-
y
)
/
(
abs
(
x
)
+
abs
(
y
))
<
tolerance
def
__str__
(
self
):
def
__str__
(
self
):
return
"double"
return
"double"
double
=
Double
()
double
=
Double
()
...
@@ -107,13 +107,13 @@ class T_extending(unittest.TestCase):
...
@@ -107,13 +107,13 @@ class T_extending(unittest.TestCase):
def
make_node
(
x
,
y
):
def
make_node
(
x
,
y
):
if
isinstance
(
x
,
(
int
,
float
)):
if
isinstance
(
x
,
(
int
,
float
)):
x
=
gof
.
Constant
(
double
,
x
)
x
=
gof
.
Constant
(
double
,
x
)
if
isinstance
(
y
,
(
int
,
float
)):
if
isinstance
(
y
,
(
int
,
float
)):
y
=
gof
.
Constant
(
double
,
y
)
y
=
gof
.
Constant
(
double
,
y
)
if
x
.
type
!=
double
or
y
.
type
!=
double
:
if
x
.
type
!=
double
or
y
.
type
!=
double
:
raise
TypeError
(
'mul only works on doubles'
)
raise
TypeError
(
'mul only works on doubles'
)
return
gof
.
Apply
(
mul
,
[
x
,
y
],
[
double
()])
return
gof
.
Apply
(
mul
,
[
x
,
y
],
[
double
()])
mul
.
make_node
=
make_node
mul
.
make_node
=
make_node
...
@@ -125,30 +125,30 @@ class T_extending(unittest.TestCase):
...
@@ -125,30 +125,30 @@ class T_extending(unittest.TestCase):
from
theano
import
gof
from
theano
import
gof
class
BinaryDoubleOp
(
gof
.
Op
):
class
BinaryDoubleOp
(
gof
.
Op
):
def
__init__
(
self
,
name
,
fn
):
def
__init__
(
self
,
name
,
fn
):
self
.
name
=
name
self
.
name
=
name
self
.
fn
=
fn
self
.
fn
=
fn
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
(
self
.
name
==
other
.
name
)
and
(
self
.
fn
==
other
.
fn
)
return
type
(
self
)
==
type
(
other
)
and
(
self
.
name
==
other
.
name
)
and
(
self
.
fn
==
other
.
fn
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
name
)
^
hash
(
self
.
fn
)
return
hash
(
type
(
self
))
^
hash
(
self
.
name
)
^
hash
(
self
.
fn
)
def
make_node
(
self
,
x
,
y
):
def
make_node
(
self
,
x
,
y
):
if
isinstance
(
x
,
(
int
,
float
)):
if
isinstance
(
x
,
(
int
,
float
)):
x
=
gof
.
Constant
(
double
,
x
)
x
=
gof
.
Constant
(
double
,
x
)
if
isinstance
(
y
,
(
int
,
float
)):
if
isinstance
(
y
,
(
int
,
float
)):
y
=
gof
.
Constant
(
double
,
y
)
y
=
gof
.
Constant
(
double
,
y
)
if
x
.
type
!=
double
or
y
.
type
!=
double
:
if
x
.
type
!=
double
or
y
.
type
!=
double
:
raise
TypeError
(
'
%
s only works on doubles'
%
self
.
name
)
raise
TypeError
(
'
%
s only works on doubles'
%
self
.
name
)
return
gof
.
Apply
(
self
,
[
x
,
y
],
[
double
()])
return
gof
.
Apply
(
self
,
[
x
,
y
],
[
double
()])
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
z
[
0
]
=
self
.
fn
(
x
,
y
)
z
[
0
]
=
self
.
fn
(
x
,
y
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
name
return
self
.
name
add
=
BinaryDoubleOp
(
name
=
'add'
,
add
=
BinaryDoubleOp
(
name
=
'add'
,
fn
=
lambda
x
,
y
:
x
+
y
)
fn
=
lambda
x
,
y
:
x
+
y
)
...
@@ -175,44 +175,44 @@ class T_extending(unittest.TestCase):
...
@@ -175,44 +175,44 @@ class T_extending(unittest.TestCase):
class
Double
(
gof
.
Type
):
class
Double
(
gof
.
Type
):
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
if
strict
and
not
isinstance
(
x
,
float
):
if
strict
and
not
isinstance
(
x
,
float
):
raise
TypeError
(
'Expected a float!'
)
raise
TypeError
(
'Expected a float!'
)
return
float
(
x
)
return
float
(
x
)
def
values_eq_approx
(
self
,
x
,
y
,
tolerance
=
1e-4
):
def
values_eq_approx
(
self
,
x
,
y
,
tolerance
=
1e-4
):
return
abs
(
x
-
y
)
/
(
abs
(
x
)
+
abs
(
y
))
<
tolerance
return
abs
(
x
-
y
)
/
(
abs
(
x
)
+
abs
(
y
))
<
tolerance
def
__str__
(
self
):
def
__str__
(
self
):
return
"double"
return
"double"
double
=
Double
()
double
=
Double
()
class
BinaryDoubleOp
(
gof
.
Op
):
class
BinaryDoubleOp
(
gof
.
Op
):
def
__init__
(
self
,
name
,
fn
):
def
__init__
(
self
,
name
,
fn
):
self
.
name
=
name
self
.
name
=
name
self
.
fn
=
fn
self
.
fn
=
fn
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
(
self
.
name
==
other
.
name
)
and
(
self
.
fn
==
other
.
fn
)
return
type
(
self
)
==
type
(
other
)
and
(
self
.
name
==
other
.
name
)
and
(
self
.
fn
==
other
.
fn
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
name
)
^
hash
(
self
.
fn
)
return
hash
(
type
(
self
))
^
hash
(
self
.
name
)
^
hash
(
self
.
fn
)
def
make_node
(
self
,
x
,
y
):
def
make_node
(
self
,
x
,
y
):
if
isinstance
(
x
,
(
int
,
float
)):
if
isinstance
(
x
,
(
int
,
float
)):
x
=
gof
.
Constant
(
double
,
x
)
x
=
gof
.
Constant
(
double
,
x
)
if
isinstance
(
y
,
(
int
,
float
)):
if
isinstance
(
y
,
(
int
,
float
)):
y
=
gof
.
Constant
(
double
,
y
)
y
=
gof
.
Constant
(
double
,
y
)
if
x
.
type
!=
double
or
y
.
type
!=
double
:
if
x
.
type
!=
double
or
y
.
type
!=
double
:
raise
TypeError
(
'
%
s only works on doubles'
%
self
.
name
)
raise
TypeError
(
'
%
s only works on doubles'
%
self
.
name
)
return
gof
.
Apply
(
self
,
[
x
,
y
],
[
double
()])
return
gof
.
Apply
(
self
,
[
x
,
y
],
[
double
()])
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
z
[
0
]
=
self
.
fn
(
x
,
y
)
z
[
0
]
=
self
.
fn
(
x
,
y
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
name
return
self
.
name
add
=
BinaryDoubleOp
(
name
=
'add'
,
add
=
BinaryDoubleOp
(
name
=
'add'
,
fn
=
lambda
x
,
y
:
x
+
y
)
fn
=
lambda
x
,
y
:
x
+
y
)
...
@@ -281,87 +281,87 @@ class T_extending(unittest.TestCase):
...
@@ -281,87 +281,87 @@ class T_extending(unittest.TestCase):
from
theano
import
gof
from
theano
import
gof
class
Double
(
gof
.
Type
):
class
Double
(
gof
.
Type
):
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
def
filter
(
self
,
x
,
strict
=
False
,
allow_downcast
=
None
):
if
strict
and
not
isinstance
(
x
,
float
):
if
strict
and
not
isinstance
(
x
,
float
):
raise
TypeError
(
'Expected a float!'
)
raise
TypeError
(
'Expected a float!'
)
return
float
(
x
)
return
float
(
x
)
def
values_eq_approx
(
self
,
x
,
y
,
tolerance
=
1e-4
):
def
values_eq_approx
(
self
,
x
,
y
,
tolerance
=
1e-4
):
return
abs
(
x
-
y
)
/
(
x
+
y
)
<
tolerance
return
abs
(
x
-
y
)
/
(
x
+
y
)
<
tolerance
def
__str__
(
self
):
def
__str__
(
self
):
return
"double"
return
"double"
def
c_declare
(
self
,
name
,
sub
):
def
c_declare
(
self
,
name
,
sub
):
return
"""
return
"""
double
%(name)
s;
double
%(name)
s;
"""
%
dict
(
name
=
name
)
"""
%
dict
(
name
=
name
)
def
c_init
(
self
,
name
,
sub
):
def
c_init
(
self
,
name
,
sub
):
return
"""
return
"""
%(name)
s = 0.0;
%(name)
s = 0.0;
"""
%
dict
(
name
=
name
)
"""
%
dict
(
name
=
name
)
def
c_extract
(
self
,
name
,
sub
):
def
c_extract
(
self
,
name
,
sub
):
return
"""
return
"""
if (!PyFloat_Check(py_
%(name)
s)) {
if (!PyFloat_Check(py_
%(name)
s)) {
PyErr_SetString(PyExc_TypeError, "expected a float");
PyErr_SetString(PyExc_TypeError, "expected a float");
%(fail)
s
%(fail)
s
}
}
%(name)
s = PyFloat_AsDouble(py_
%(name)
s);
%(name)
s = PyFloat_AsDouble(py_
%(name)
s);
"""
%
dict
(
sub
,
name
=
name
)
"""
%
dict
(
sub
,
name
=
name
)
def
c_sync
(
self
,
name
,
sub
):
def
c_sync
(
self
,
name
,
sub
):
return
"""
return
"""
Py_XDECREF(py_
%(name)
s);
Py_XDECREF(py_
%(name)
s);
py_
%(name)
s = PyFloat_FromDouble(
%(name)
s);
py_
%(name)
s = PyFloat_FromDouble(
%(name)
s);
if (!py_
%(name)
s) {
if (!py_
%(name)
s) {
printf("PyFloat_FromDouble failed on:
%%
f
\\
n",
%(name)
s);
printf("PyFloat_FromDouble failed on:
%%
f
\\
n",
%(name)
s);
Py_XINCREF(Py_None);
Py_XINCREF(Py_None);
py_
%(name)
s = Py_None;
py_
%(name)
s = Py_None;
}
}
"""
%
dict
(
name
=
name
)
"""
%
dict
(
name
=
name
)
def
c_cleanup
(
self
,
name
,
sub
):
def
c_cleanup
(
self
,
name
,
sub
):
return
""
return
""
double
=
Double
()
double
=
Double
()
def
c_code
(
node
,
name
,
input_names
,
output_names
,
sub
):
def
c_code
(
node
,
name
,
input_names
,
output_names
,
sub
):
x_name
,
y_name
=
input_names
[
0
],
input_names
[
1
]
x_name
,
y_name
=
input_names
[
0
],
input_names
[
1
]
output_name
=
output_names
[
0
]
output_name
=
output_names
[
0
]
return
"""
return
"""
%(output_name)
s =
%(x_name)
s *
%(y_name)
s;
%(output_name)
s =
%(x_name)
s *
%(y_name)
s;
"""
%
locals
()
"""
%
locals
()
mul
.
c_code
=
c_code
mul
.
c_code
=
c_code
from
theano
import
gof
from
theano
import
gof
class
BinaryDoubleOp
(
gof
.
Op
):
class
BinaryDoubleOp
(
gof
.
Op
):
def
__init__
(
self
,
name
,
fn
,
ccode
):
def
__init__
(
self
,
name
,
fn
,
ccode
):
self
.
name
=
name
self
.
name
=
name
self
.
fn
=
fn
self
.
fn
=
fn
self
.
ccode
=
ccode
self
.
ccode
=
ccode
def
make_node
(
self
,
x
,
y
):
def
make_node
(
self
,
x
,
y
):
if
isinstance
(
x
,
(
int
,
float
)):
if
isinstance
(
x
,
(
int
,
float
)):
x
=
gof
.
Constant
(
double
,
x
)
x
=
gof
.
Constant
(
double
,
x
)
if
isinstance
(
y
,
(
int
,
float
)):
if
isinstance
(
y
,
(
int
,
float
)):
y
=
gof
.
Constant
(
double
,
y
)
y
=
gof
.
Constant
(
double
,
y
)
if
x
.
type
!=
double
or
y
.
type
!=
double
:
if
x
.
type
!=
double
or
y
.
type
!=
double
:
raise
TypeError
(
'
%
s only works on doubles'
%
self
.
name
)
raise
TypeError
(
'
%
s only works on doubles'
%
self
.
name
)
return
gof
.
Apply
(
self
,
[
x
,
y
],
[
double
()])
return
gof
.
Apply
(
self
,
[
x
,
y
],
[
double
()])
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
def
perform
(
self
,
node
,
(
x
,
y
),
(
z
,
)):
z
[
0
]
=
self
.
fn
(
x
,
y
)
z
[
0
]
=
self
.
fn
(
x
,
y
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
name
return
self
.
name
def
c_code
(
self
,
node
,
name
,
(
x
,
y
),
(
z
,
),
sub
):
def
c_code
(
self
,
node
,
name
,
(
x
,
y
),
(
z
,
),
sub
):
return
self
.
ccode
%
locals
()
return
self
.
ccode
%
locals
()
add
=
BinaryDoubleOp
(
name
=
'add'
,
add
=
BinaryDoubleOp
(
name
=
'add'
,
...
@@ -384,19 +384,19 @@ class T_extending(unittest.TestCase):
...
@@ -384,19 +384,19 @@ class T_extending(unittest.TestCase):
from
theano.gof
import
toolbox
from
theano.gof
import
toolbox
class
Simplify
(
gof
.
Optimizer
):
class
Simplify
(
gof
.
Optimizer
):
def
add_requirements
(
self
,
env
):
def
add_requirements
(
self
,
env
):
env
.
extend
(
toolbox
.
ReplaceValidate
())
env
.
extend
(
toolbox
.
ReplaceValidate
())
def
apply
(
self
,
env
):
def
apply
(
self
,
env
):
for
node
in
env
.
toposort
():
for
node
in
env
.
toposort
():
if
node
.
op
==
div
:
if
node
.
op
==
div
:
x
,
y
=
node
.
inputs
x
,
y
=
node
.
inputs
z
=
node
.
outputs
[
0
]
z
=
node
.
outputs
[
0
]
if
x
.
owner
and
x
.
owner
.
op
==
mul
:
if
x
.
owner
and
x
.
owner
.
op
==
mul
:
a
,
b
=
x
.
owner
.
inputs
a
,
b
=
x
.
owner
.
inputs
if
y
==
a
:
if
y
==
a
:
env
.
replace_validate
(
z
,
b
)
env
.
replace_validate
(
z
,
b
)
elif
y
==
b
:
elif
y
==
b
:
env
.
replace_validate
(
z
,
a
)
env
.
replace_validate
(
z
,
a
)
simplify
=
Simplify
()
simplify
=
Simplify
()
x
=
double
(
'x'
)
x
=
double
(
'x'
)
...
@@ -407,21 +407,21 @@ class T_extending(unittest.TestCase):
...
@@ -407,21 +407,21 @@ class T_extending(unittest.TestCase):
simplify
.
optimize
(
e
)
simplify
.
optimize
(
e
)
class
LocalSimplify
(
gof
.
LocalOptimizer
):
class
LocalSimplify
(
gof
.
LocalOptimizer
):
def
transform
(
self
,
node
):
def
transform
(
self
,
node
):
if
node
.
op
==
div
:
if
node
.
op
==
div
:
x
,
y
=
node
.
inputs
x
,
y
=
node
.
inputs
if
x
.
owner
and
x
.
owner
.
op
==
mul
:
if
x
.
owner
and
x
.
owner
.
op
==
mul
:
a
,
b
=
x
.
owner
.
inputs
a
,
b
=
x
.
owner
.
inputs
if
y
==
a
:
if
y
==
a
:
return
[
b
]
return
[
b
]
elif
y
==
b
:
elif
y
==
b
:
return
[
a
]
return
[
a
]
return
False
return
False
def
tracks
(
self
):
def
tracks
(
self
):
# This should be needed for the EquilibriumOptimizer
# This should be needed for the EquilibriumOptimizer
# but it isn't now
# but it isn't now
# TODO: do this and explain it
# TODO: do this and explain it
return
[]
# that's not what you should do
return
[]
# that's not what you should do
local_simplify
=
LocalSimplify
()
local_simplify
=
LocalSimplify
()
...
@@ -595,7 +595,7 @@ class T_examples(unittest.TestCase):
...
@@ -595,7 +595,7 @@ class T_examples(unittest.TestCase):
fn_of_state
=
state
*
2
+
inc
fn_of_state
=
state
*
2
+
inc
foo
=
T
.
lscalar
()
# the type (lscalar) must match the shared variable we
foo
=
T
.
lscalar
()
# the type (lscalar) must match the shared variable we
# are replacing with the ``givens`` list
# are replacing with the ``givens`` list
skip_shared
=
function
([
inc
,
foo
],
fn_of_state
,
skip_shared
=
function
([
inc
,
foo
],
fn_of_state
,
givens
=
[(
state
,
foo
)])
givens
=
[(
state
,
foo
)])
assert
skip_shared
(
1
,
3
)
==
array
(
7
)
assert
skip_shared
(
1
,
3
)
==
array
(
7
)
...
@@ -967,7 +967,3 @@ class T_graphstructures(unittest.TestCase):
...
@@ -967,7 +967,3 @@ class T_graphstructures(unittest.TestCase):
assert
e
.
owner
.
inputs
[
1
]
is
mul_variable
assert
e
.
owner
.
inputs
[
1
]
is
mul_variable
assert
e
.
owner
.
inputs
[
1
]
.
owner
.
inputs
[
0
]
is
y
assert
e
.
owner
.
inputs
[
1
]
.
owner
.
inputs
[
0
]
is
y
assert
e
.
owner
.
inputs
[
1
]
.
owner
.
inputs
[
1
]
is
z
assert
e
.
owner
.
inputs
[
1
]
.
owner
.
inputs
[
1
]
is
z
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论