Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f1ef401b
提交
f1ef401b
authored
2月 23, 2012
作者:
Olivier Delalleau
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #479 from nouiz/except
Better exception handling
上级
46f8a271
232671a8
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
98 行增加
和
114 行删除
+98
-114
env.py
theano/gof/env.py
+1
-1
test_optdb.py
theano/gof/tests/test_optdb.py
+10
-10
utils.py
theano/gof/utils.py
+4
-13
test_scan.py
theano/sandbox/scan_module/tests/test_scan.py
+2
-2
test_conv.py
theano/tensor/nnet/tests/test_conv.py
+6
-19
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+17
-17
sharedvar.py
theano/tensor/sharedvar.py
+24
-16
test_conv.py
theano/tensor/signal/tests/test_conv.py
+34
-36
没有找到文件。
theano/gof/env.py
浏览文件 @
f1ef401b
...
@@ -443,7 +443,7 @@ class Env(utils.object2):
...
@@ -443,7 +443,7 @@ class Env(utils.object2):
"""
"""
try
:
try
:
self
.
_features
.
remove
(
feature
)
self
.
_features
.
remove
(
feature
)
except
:
except
Exception
:
return
return
detach
=
getattr
(
feature
,
'on_detach'
,
None
)
detach
=
getattr
(
feature
,
'on_detach'
,
None
)
if
detach
is
not
None
:
if
detach
is
not
None
:
...
...
theano/gof/tests/test_optdb.py
浏览文件 @
f1ef401b
from
theano.gof.optdb
import
*
from
unittest
import
TestCase
from
unittest
import
TestCase
from
theano.gof.optdb
import
opt
,
DB
class
Test_DB
(
TestCase
):
class
Test_DB
(
TestCase
):
def
test_0
(
self
):
def
test_0
(
self
):
class
Opt
(
opt
.
Optimizer
):
#inheritance buys __hash__
class
Opt
(
opt
.
Optimizer
):
#
inheritance buys __hash__
name
=
'blah'
name
=
'blah'
db
=
DB
()
db
=
DB
()
...
@@ -16,36 +18,34 @@ class Test_DB(TestCase):
...
@@ -16,36 +18,34 @@ class Test_DB(TestCase):
db
.
register
(
'c'
,
Opt
(),
'z'
,
'asdf'
)
db
.
register
(
'c'
,
Opt
(),
'z'
,
'asdf'
)
try
:
try
:
db
.
register
(
'c'
,
Opt
())
#
name taken
db
.
register
(
'c'
,
Opt
())
#
name taken
self
.
fail
()
self
.
fail
()
except
ValueError
,
e
:
except
ValueError
,
e
:
if
e
[
0
]
.
startswith
(
"The name"
):
if
e
[
0
]
.
startswith
(
"The name"
):
pass
pass
else
:
else
:
raise
raise
except
:
except
Exception
:
self
.
fail
()
self
.
fail
()
try
:
try
:
db
.
register
(
'z'
,
Opt
())
#
name collides with tag
db
.
register
(
'z'
,
Opt
())
#
name collides with tag
self
.
fail
()
self
.
fail
()
except
ValueError
,
e
:
except
ValueError
,
e
:
if
e
[
0
]
.
startswith
(
"The name"
):
if
e
[
0
]
.
startswith
(
"The name"
):
pass
pass
else
:
else
:
raise
raise
except
:
except
Exception
:
self
.
fail
()
self
.
fail
()
try
:
try
:
db
.
register
(
'u'
,
Opt
(),
'b'
)
#
name new but tag collides with name
db
.
register
(
'u'
,
Opt
(),
'b'
)
#
name new but tag collides with name
self
.
fail
()
self
.
fail
()
except
ValueError
,
e
:
except
ValueError
,
e
:
if
e
[
0
]
.
startswith
(
"The tag"
):
if
e
[
0
]
.
startswith
(
"The tag"
):
pass
pass
else
:
else
:
raise
raise
except
:
except
Exception
:
self
.
fail
()
self
.
fail
()
theano/gof/utils.py
浏览文件 @
f1ef401b
...
@@ -285,20 +285,11 @@ def comm_guard(type1, type2):
...
@@ -285,20 +285,11 @@ def comm_guard(type1, type2):
and
(
type2
is
ANY_TYPE
or
isinstance
(
arg1
,
type2
)):
and
(
type2
is
ANY_TYPE
or
isinstance
(
arg1
,
type2
)):
arg1
,
arg2
=
arg2
,
arg1
arg1
,
arg2
=
arg2
,
arg1
else
:
else
:
try
:
return
old_f
(
arg1
,
arg2
,
*
rest
)
return
old_f
(
arg1
,
arg2
,
*
rest
)
except
:
variable
=
f
(
arg1
,
arg2
,
*
rest
)
raise
try
:
variable
=
f
(
arg1
,
arg2
,
*
rest
)
except
:
raise
if
variable
is
FALL_THROUGH
:
if
variable
is
FALL_THROUGH
:
try
:
return
old_f
(
arg1
,
arg2
,
*
rest
)
return
old_f
(
arg1
,
arg2
,
*
rest
)
except
:
raise
else
:
else
:
return
variable
return
variable
...
...
theano/sandbox/scan_module/tests/test_scan.py
浏览文件 @
f1ef401b
...
@@ -299,13 +299,13 @@ class TestScan(unittest.TestCase):
...
@@ -299,13 +299,13 @@ class TestScan(unittest.TestCase):
for
th_out
,
num_out
in
zip
(
theano_outs
,
numpy_outs
):
for
th_out
,
num_out
in
zip
(
theano_outs
,
numpy_outs
):
try
:
try
:
assert
numpy
.
allclose
(
th_out
,
num_out
)
assert
numpy
.
allclose
(
th_out
,
num_out
)
except
:
except
Exception
:
#import ipdb; ipdb.set_trace()
#import ipdb; ipdb.set_trace()
raise
raise
for
th_out
,
num_out
in
zip
(
shared_vars
,
numpy_shared
):
for
th_out
,
num_out
in
zip
(
shared_vars
,
numpy_shared
):
try
:
try
:
assert
numpy
.
allclose
(
th_out
.
get_value
(),
num_out
)
assert
numpy
.
allclose
(
th_out
.
get_value
(),
num_out
)
except
:
except
Exception
:
#import ipdb; ipdb.set_trace()
#import ipdb; ipdb.set_trace()
raise
raise
# Scenario 2 : Loose fit (sequences longer then required)
# Scenario 2 : Loose fit (sequences longer then required)
...
...
theano/tensor/nnet/tests/test_conv.py
浏览文件 @
f1ef401b
...
@@ -310,25 +310,12 @@ class TestConv2D(unittest.TestCase):
...
@@ -310,25 +310,12 @@ class TestConv2D(unittest.TestCase):
"""
"""
Make sure errors are raised when image and kernel are not 4D tensors
Make sure errors are raised when image and kernel are not 4D tensors
"""
"""
try
:
self
.
assertRaises
(
Exception
,
self
.
validate
,
(
3
,
2
,
8
,
8
),
(
4
,
2
,
5
,
5
),
self
.
validate
((
3
,
2
,
8
,
8
),
(
4
,
2
,
5
,
5
),
'valid'
,
input
=
T
.
dmatrix
())
'valid'
,
input
=
T
.
dmatrix
())
# should never reach here
self
.
assertRaises
(
Exception
,
self
.
validate
,
(
3
,
2
,
8
,
8
),
(
4
,
2
,
5
,
5
),
self
.
fail
()
'valid'
,
filters
=
T
.
dvector
())
except
:
self
.
assertRaises
(
Exception
,
self
.
validate
,
(
3
,
2
,
8
,
8
),
(
4
,
2
,
5
,
5
),
pass
'valid'
,
input
=
T
.
dtensor3
())
try
:
self
.
validate
((
3
,
2
,
8
,
8
),
(
4
,
2
,
5
,
5
),
'valid'
,
filters
=
T
.
dvector
())
# should never reach here
self
.
fail
()
except
:
pass
try
:
self
.
validate
((
3
,
2
,
8
,
8
),
(
4
,
2
,
5
,
5
),
'valid'
,
input
=
T
.
dtensor3
())
# should never reach here
self
.
fail
()
except
:
pass
def
test_gcc_crash
(
self
):
def
test_gcc_crash
(
self
):
"""
"""
...
...
theano/tensor/nnet/tests/test_nnet.py
浏览文件 @
f1ef401b
...
@@ -496,7 +496,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -496,7 +496,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try
:
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
4
assert
len
(
f
.
maker
.
env
.
toposort
())
==
4
f
(
x_val
,
y_val
)
f
(
x_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
raise
raise
...
@@ -507,7 +507,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -507,7 +507,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try
:
try
:
assert
len
(
g
.
maker
.
env
.
toposort
())
==
4
assert
len
(
g
.
maker
.
env
.
toposort
())
==
4
g
(
x_val
,
y_val
)
g
(
x_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
g
)
theano
.
printing
.
debugprint
(
g
)
raise
raise
...
@@ -525,7 +525,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -525,7 +525,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try
:
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
2
# [big_op, sum]
assert
len
(
f
.
maker
.
env
.
toposort
())
==
2
# [big_op, sum]
f
(
x_val
,
b_val
,
y_val
)
f
(
x_val
,
b_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
raise
raise
...
@@ -535,7 +535,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -535,7 +535,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try
:
try
:
assert
len
(
g
.
maker
.
env
.
toposort
())
==
4
assert
len
(
g
.
maker
.
env
.
toposort
())
==
4
g
(
x_val
,
b_val
,
y_val
)
g
(
x_val
,
b_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
g
)
theano
.
printing
.
debugprint
(
g
)
raise
raise
...
@@ -553,7 +553,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -553,7 +553,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try
:
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
6
assert
len
(
f
.
maker
.
env
.
toposort
())
==
6
f
(
x_val
,
y_val
)
f
(
x_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
raise
raise
...
@@ -564,7 +564,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -564,7 +564,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert
len
(
g
.
maker
.
env
.
toposort
())
in
(
6
,
7
)
#there's an extra dimshuffle in there
assert
len
(
g
.
maker
.
env
.
toposort
())
in
(
6
,
7
)
#there's an extra dimshuffle in there
# but I can't think of a good rule to get rid of it
# but I can't think of a good rule to get rid of it
g
(
x_val
,
y_val
)
g
(
x_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
g
)
theano
.
printing
.
debugprint
(
g
)
raise
raise
...
@@ -580,7 +580,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -580,7 +580,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
try
:
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
4
assert
len
(
f
.
maker
.
env
.
toposort
())
==
4
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
raise
raise
...
@@ -590,7 +590,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -590,7 +590,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try
:
try
:
assert
len
(
g
.
maker
.
env
.
toposort
())
in
(
6
,
7
)
assert
len
(
g
.
maker
.
env
.
toposort
())
in
(
6
,
7
)
g
(
x_val
,
b_val
,
y_val
)
g
(
x_val
,
b_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
g
)
theano
.
printing
.
debugprint
(
g
)
raise
raise
...
@@ -624,7 +624,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -624,7 +624,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try
:
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
5
assert
len
(
f
.
maker
.
env
.
toposort
())
==
5
f
(
x_val
,
y_val
)
f
(
x_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
raise
raise
...
@@ -635,7 +635,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -635,7 +635,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
try
:
try
:
assert
len
(
g
.
maker
.
env
.
toposort
())
==
5
assert
len
(
g
.
maker
.
env
.
toposort
())
==
5
g
(
x_val
,
y_val
)
g
(
x_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
g
)
theano
.
printing
.
debugprint
(
g
)
raise
raise
...
@@ -671,7 +671,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -671,7 +671,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
prev
,
last
=
f
.
maker
.
env
.
toposort
()[
-
2
:]
prev
,
last
=
f
.
maker
.
env
.
toposort
()[
-
2
:]
assert
len
(
f
.
maker
.
env
.
toposort
())
==
5
assert
len
(
f
.
maker
.
env
.
toposort
())
==
5
f
(
x_val
,
y_val
)
f
(
x_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
raise
raise
...
@@ -684,7 +684,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -684,7 +684,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert
softmax
in
ops
assert
softmax
in
ops
assert
softmax_grad
not
in
ops
assert
softmax_grad
not
in
ops
g
(
x_val
,
y_val
)
g
(
x_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
g
)
theano
.
printing
.
debugprint
(
g
)
raise
raise
...
@@ -723,7 +723,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -723,7 +723,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
prev
,
last
=
f
.
maker
.
env
.
toposort
()[
-
2
:]
prev
,
last
=
f
.
maker
.
env
.
toposort
()[
-
2
:]
assert
len
(
f
.
maker
.
env
.
toposort
())
==
3
# [big_op, sum, dim_shuffle]
assert
len
(
f
.
maker
.
env
.
toposort
())
==
3
# [big_op, sum, dim_shuffle]
f
(
x_val
,
b_val
,
y_val
)
f
(
x_val
,
b_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
raise
raise
...
@@ -742,7 +742,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -742,7 +742,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert
softmax_with_bias
in
ops
assert
softmax_with_bias
in
ops
assert
softmax_grad
not
in
ops
assert
softmax_grad
not
in
ops
g
(
x_val
,
b_val
,
y_val
)
g
(
x_val
,
b_val
,
y_val
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
g
)
theano
.
printing
.
debugprint
(
g
)
raise
raise
...
@@ -831,7 +831,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -831,7 +831,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert
5
<=
len
(
f
.
maker
.
env
.
toposort
())
<=
10
assert
5
<=
len
(
f
.
maker
.
env
.
toposort
())
<=
10
validate_fn_graph
(
f
)
validate_fn_graph
(
f
)
f
(
x_val
,
y_val
,
0.1
)
f
(
x_val
,
y_val
,
0.1
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
f
)
theano
.
printing
.
debugprint
(
f
)
raise
raise
...
@@ -841,7 +841,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -841,7 +841,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert
5
<=
len
(
g
.
maker
.
env
.
toposort
())
<=
12
assert
5
<=
len
(
g
.
maker
.
env
.
toposort
())
<=
12
validate_grad_graph
(
g
)
validate_grad_graph
(
g
)
g
(
x_val
,
y_val
,
0.1
)
g
(
x_val
,
y_val
,
0.1
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
g
)
theano
.
printing
.
debugprint
(
g
)
raise
raise
...
@@ -851,7 +851,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -851,7 +851,7 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
assert
8
<=
len
(
h
.
maker
.
env
.
toposort
())
<=
17
assert
8
<=
len
(
h
.
maker
.
env
.
toposort
())
<=
17
validate_grad_graph
(
h
)
validate_grad_graph
(
h
)
h
(
x_val
,
y_val
,
0.1
)
h
(
x_val
,
y_val
,
0.1
)
except
:
except
Exception
:
theano
.
printing
.
debugprint
(
h
)
theano
.
printing
.
debugprint
(
h
)
raise
raise
...
...
theano/tensor/sharedvar.py
浏览文件 @
f1ef401b
import
traceback
import
traceback
import
numpy
import
numpy
import
theano.tensor.basic
import
theano.tensor.basic
from
basic
import
TensorType
,
_tensor_py_operators
,
autocast_int
,
autocast_float
from
basic
import
TensorType
,
_tensor_py_operators
from
theano.compile
import
shared_constructor
,
SharedVariable
from
theano.compile
import
shared_constructor
,
SharedVariable
from
theano
import
config
def
load_shared_variable
(
val
):
def
load_shared_variable
(
val
):
"""This function is only here to keep some pickles loading
"""This function is only here to keep some pickles loading
...
@@ -11,35 +13,40 @@ def load_shared_variable(val):
...
@@ -11,35 +13,40 @@ def load_shared_variable(val):
It can be removed after sufficient time has passed."""
It can be removed after sufficient time has passed."""
return
tensor_constructor
(
val
)
return
tensor_constructor
(
val
)
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
class
TensorSharedVariable
(
_tensor_py_operators
,
SharedVariable
):
class
TensorSharedVariable
(
_tensor_py_operators
,
SharedVariable
):
pass
pass
@shared_constructor
@shared_constructor
def
tensor_constructor
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
,
borrow
=
False
,
broadcastable
=
None
):
def
tensor_constructor
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
,
borrow
=
False
,
broadcastable
=
None
):
"""SharedVariable Constructor for TensorType
"""SharedVariable Constructor for TensorType
:note: Regarding the inference of the broadcastable pattern...
:note: Regarding the inference of the broadcastable pattern...
The default is to assume that the value might be resized in any dimension, so the default
The default is to assume that the value might be resized in any
broadcastable is ``(False,)*len(value.shape)``. The optional `broadcastable` argument will
dimension, so the default broadcastable is
override this default.
``(False,)*len(value.shape)``. The optional `broadcastable`
argument will override this default.
"""
"""
if
not
isinstance
(
value
,
numpy
.
ndarray
):
if
not
isinstance
(
value
,
numpy
.
ndarray
):
raise
TypeError
()
raise
TypeError
()
# if no broadcastable is given, then the default is to assume that
the value might be
# if no broadcastable is given, then the default is to assume that
# resized in any dimension in the future.
#
the value might be
resized in any dimension in the future.
#
#
if
broadcastable
is
None
:
if
broadcastable
is
None
:
broadcastable
=
(
False
,)
*
len
(
value
.
shape
)
broadcastable
=
(
False
,)
*
len
(
value
.
shape
)
type
=
TensorType
(
value
.
dtype
,
broadcastable
=
broadcastable
)
type
=
TensorType
(
value
.
dtype
,
broadcastable
=
broadcastable
)
return
TensorSharedVariable
(
type
=
type
,
return
TensorSharedVariable
(
type
=
type
,
value
=
numpy
.
array
(
value
,
copy
=
(
not
borrow
)),
value
=
numpy
.
array
(
value
,
copy
=
(
not
borrow
)),
name
=
name
,
name
=
name
,
strict
=
strict
,
strict
=
strict
,
allow_downcast
=
allow_downcast
)
allow_downcast
=
allow_downcast
)
# TensorSharedVariable brings in the tensor operators, is not ideal, but works
# TensorSharedVariable brings in the tensor operators, is not ideal, but works
# as long as we dont do purely scalar-scalar operations
# as long as we dont do purely scalar-scalar operations
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
...
@@ -50,6 +57,7 @@ def tensor_constructor(value, name=None, strict=False, allow_downcast=None, borr
...
@@ -50,6 +57,7 @@ def tensor_constructor(value, name=None, strict=False, allow_downcast=None, borr
class
ScalarSharedVariable
(
_tensor_py_operators
,
SharedVariable
):
class
ScalarSharedVariable
(
_tensor_py_operators
,
SharedVariable
):
pass
pass
@shared_constructor
@shared_constructor
def
scalar_constructor
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
):
def
scalar_constructor
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
):
"""SharedVariable constructor for scalar values. Default: int64 or float64.
"""SharedVariable constructor for scalar values. Default: int64 or float64.
...
@@ -57,14 +65,14 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None):
...
@@ -57,14 +65,14 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None):
:note: We implement this using 0-d tensors for now.
:note: We implement this using 0-d tensors for now.
"""
"""
if
not
isinstance
(
value
,
(
numpy
.
number
,
float
,
int
,
complex
)):
if
not
isinstance
(
value
,
(
numpy
.
number
,
float
,
int
,
complex
)):
raise
TypeError
()
raise
TypeError
()
try
:
try
:
dtype
=
value
.
dtype
dtype
=
value
.
dtype
except
:
except
Exception
:
dtype
=
numpy
.
asarray
(
value
)
.
dtype
dtype
=
numpy
.
asarray
(
value
)
.
dtype
dtype
=
str
(
dtype
)
dtype
=
str
(
dtype
)
value
=
theano
.
_asarray
(
value
,
dtype
=
dtype
)
value
=
theano
.
_asarray
(
value
,
dtype
=
dtype
)
tensor_type
=
TensorType
(
dtype
=
str
(
value
.
dtype
),
broadcastable
=
[])
tensor_type
=
TensorType
(
dtype
=
str
(
value
.
dtype
),
broadcastable
=
[])
...
@@ -75,6 +83,6 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None):
...
@@ -75,6 +83,6 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None):
value
=
numpy
.
array
(
value
,
copy
=
True
),
value
=
numpy
.
array
(
value
,
copy
=
True
),
name
=
name
,
strict
=
strict
,
allow_downcast
=
allow_downcast
)
name
=
name
,
strict
=
strict
,
allow_downcast
=
allow_downcast
)
return
rval
return
rval
except
:
except
Exception
:
traceback
.
print_exc
()
traceback
.
print_exc
()
raise
raise
theano/tensor/signal/tests/test_conv.py
浏览文件 @
f1ef401b
import
sys
,
time
,
unittest
import
unittest
import
numpy
import
numpy
import
theano
import
theano
import
theano.tensor
as
T
import
theano.tensor
as
T
from
theano
import
function
,
Mode
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.signal
import
conv
from
theano.tensor.signal
import
conv
from
theano.tensor.basic
import
_allclose
from
theano.tensor.basic
import
_allclose
class
TestSignalConv2D
(
unittest
.
TestCase
):
class
TestSignalConv2D
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -19,13 +20,15 @@ class TestSignalConv2D(unittest.TestCase):
...
@@ -19,13 +20,15 @@ class TestSignalConv2D(unittest.TestCase):
image_dim
=
len
(
image_shape
)
image_dim
=
len
(
image_shape
)
filter_dim
=
len
(
filter_shape
)
filter_dim
=
len
(
filter_shape
)
input
=
T
.
TensorType
(
'float64'
,
[
False
]
*
image_dim
)()
input
=
T
.
TensorType
(
'float64'
,
[
False
]
*
image_dim
)()
filters
=
T
.
TensorType
(
'float64'
,
[
False
]
*
filter_dim
)()
filters
=
T
.
TensorType
(
'float64'
,
[
False
]
*
filter_dim
)()
bsize
=
image_shape
[
0
]
bsize
=
image_shape
[
0
]
if
image_dim
!=
3
:
bsize
=
1
if
image_dim
!=
3
:
bsize
=
1
nkern
=
filter_shape
[
0
]
nkern
=
filter_shape
[
0
]
if
filter_dim
!=
3
:
nkern
=
1
if
filter_dim
!=
3
:
nkern
=
1
############# THEANO IMPLEMENTATION ############
############# THEANO IMPLEMENTATION ############
# we create a symbolic function so that verify_grad can work
# we create a symbolic function so that verify_grad can work
...
@@ -35,7 +38,7 @@ class TestSignalConv2D(unittest.TestCase):
...
@@ -35,7 +38,7 @@ class TestSignalConv2D(unittest.TestCase):
theano_conv
=
theano
.
function
([
input
,
filters
],
output
)
theano_conv
=
theano
.
function
([
input
,
filters
],
output
)
# initialize input and compute result
# initialize input and compute result
image_data
=
numpy
.
random
.
random
(
image_shape
)
image_data
=
numpy
.
random
.
random
(
image_shape
)
filter_data
=
numpy
.
random
.
random
(
filter_shape
)
filter_data
=
numpy
.
random
.
random
(
filter_shape
)
theano_output
=
theano_conv
(
image_data
,
filter_data
)
theano_output
=
theano_conv
(
image_data
,
filter_data
)
...
@@ -45,10 +48,11 @@ class TestSignalConv2D(unittest.TestCase):
...
@@ -45,10 +48,11 @@ class TestSignalConv2D(unittest.TestCase):
ref_output
=
numpy
.
zeros
(
tuple
(
out_shape2d
))
ref_output
=
numpy
.
zeros
(
tuple
(
out_shape2d
))
# reshape as 3D input tensors to make life easier
# reshape as 3D input tensors to make life easier
image_data3d
=
image_data
.
reshape
((
bsize
,)
+
image_shape
[
-
2
:])
image_data3d
=
image_data
.
reshape
((
bsize
,)
+
image_shape
[
-
2
:])
filter_data3d
=
filter_data
.
reshape
((
nkern
,)
+
filter_shape
[
-
2
:])
filter_data3d
=
filter_data
.
reshape
((
nkern
,)
+
filter_shape
[
-
2
:])
# reshape theano output as 4D to make life easier
# reshape theano output as 4D to make life easier
theano_output4d
=
theano_output
.
reshape
((
bsize
,
nkern
,)
+
theano_output
.
shape
[
-
2
:])
theano_output4d
=
theano_output
.
reshape
((
bsize
,
nkern
,)
+
theano_output
.
shape
[
-
2
:])
# loop over mini-batches (if required)
# loop over mini-batches (if required)
for
b
in
range
(
bsize
):
for
b
in
range
(
bsize
):
...
@@ -56,17 +60,19 @@ class TestSignalConv2D(unittest.TestCase):
...
@@ -56,17 +60,19 @@ class TestSignalConv2D(unittest.TestCase):
# loop over filters (if required)
# loop over filters (if required)
for
k
in
range
(
nkern
):
for
k
in
range
(
nkern
):
image2d
=
image_data3d
[
b
,
:,
:]
image2d
=
image_data3d
[
b
,
:,
:]
filter2d
=
filter_data3d
[
k
,
:,
:]
filter2d
=
filter_data3d
[
k
,
:,
:]
output2d
=
numpy
.
zeros
(
ref_output
.
shape
)
output2d
=
numpy
.
zeros
(
ref_output
.
shape
)
for
row
in
range
(
ref_output
.
shape
[
0
]):
for
row
in
range
(
ref_output
.
shape
[
0
]):
for
col
in
range
(
ref_output
.
shape
[
1
]):
for
col
in
range
(
ref_output
.
shape
[
1
]):
output2d
[
row
,
col
]
+=
(
image2d
[
row
:
row
+
filter2d
.
shape
[
0
],
output2d
[
row
,
col
]
+=
(
col
:
col
+
filter2d
.
shape
[
1
]]
*
filter2d
[::
-
1
,::
-
1
]
image2d
[
row
:
row
+
filter2d
.
shape
[
0
],
)
.
sum
()
col
:
col
+
filter2d
.
shape
[
1
]]
*
filter2d
[::
-
1
,
::
-
1
]
)
.
sum
()
self
.
assertTrue
(
_allclose
(
theano_output4d
[
b
,
k
,
:,
:],
self
.
assertTrue
(
_allclose
(
theano_output4d
[
b
,
k
,:,:],
output2d
))
output2d
))
############# TEST GRADIENT ############
############# TEST GRADIENT ############
if
verify_grad
:
if
verify_grad
:
...
@@ -74,29 +80,22 @@ class TestSignalConv2D(unittest.TestCase):
...
@@ -74,29 +80,22 @@ class TestSignalConv2D(unittest.TestCase):
def
test_basic
(
self
):
def
test_basic
(
self
):
"""
"""
Basic functionality of nnet.conv.ConvOp is already tested by its own test suite. We
Basic functionality of nnet.conv.ConvOp is already tested by
just have to test whether or not signal.conv.conv2d can support inputs and filters of
its own test suite. We just have to test whether or not
type matrix or tensor3.
signal.conv.conv2d can support inputs and filters of type
matrix or tensor3.
"""
"""
self
.
validate
((
1
,
4
,
5
),
(
2
,
2
,
3
),
verify_grad
=
True
)
self
.
validate
((
1
,
4
,
5
),
(
2
,
2
,
3
),
verify_grad
=
True
)
self
.
validate
((
7
,
5
),
(
5
,
2
,
3
),
verify_grad
=
False
)
self
.
validate
((
7
,
5
),
(
5
,
2
,
3
),
verify_grad
=
False
)
self
.
validate
((
3
,
7
,
5
),
(
2
,
3
),
verify_grad
=
False
)
self
.
validate
((
3
,
7
,
5
),
(
2
,
3
),
verify_grad
=
False
)
self
.
validate
((
7
,
5
),
(
2
,
3
),
verify_grad
=
False
)
self
.
validate
((
7
,
5
),
(
2
,
3
),
verify_grad
=
False
)
def
test_fail
(
self
):
def
test_fail
(
self
):
"""
"""
Test that conv2d fails for dimensions other than 2 or 3.
Test that conv2d fails for dimensions other than 2 or 3.
"""
"""
try
:
self
.
assertRaises
(
Exception
,
conv
.
conv2d
,
T
.
dtensor4
(),
T
.
dtensor3
())
conv
.
conv2d
(
T
.
dtensor4
(),
T
.
dtensor3
())
self
.
assertRaises
(
Exception
,
conv
.
conv2d
,
T
.
dtensor3
(),
T
.
dvector
())
self
.
fail
()
except
:
pass
try
:
conv
.
conv2d
(
T
.
dtensor3
(),
T
.
dvector
())
self
.
fail
()
except
:
pass
def
test_bug_josh_reported
(
self
):
def
test_bug_josh_reported
(
self
):
"""
"""
...
@@ -106,5 +105,4 @@ class TestSignalConv2D(unittest.TestCase):
...
@@ -106,5 +105,4 @@ class TestSignalConv2D(unittest.TestCase):
"""
"""
m1
=
theano
.
tensor
.
matrix
()
m1
=
theano
.
tensor
.
matrix
()
m2
=
theano
.
tensor
.
matrix
()
m2
=
theano
.
tensor
.
matrix
()
rval
=
conv
.
conv2d
(
m1
,
m2
)
conv
.
conv2d
(
m1
,
m2
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论