Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ccf6deb0
提交
ccf6deb0
authored
1月 27, 2017
作者:
ballasn
提交者:
GitHub
1月 27, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5452 from bscellier/import_numpy
Update "import numpy" to "import numpy as np" (theano/compile directory)
上级
8c5cad8f
3b7aa75e
隐藏空白字符变更
内嵌
并排
正在显示
19 个修改的文件
包含
369 行增加
和
372 行删除
+369
-372
builders.py
theano/compile/builders.py
+2
-2
debugmode.py
theano/compile/debugmode.py
+31
-31
function.py
theano/compile/function.py
+2
-2
function_module.py
theano/compile/function_module.py
+8
-8
monitormode.py
theano/compile/monitormode.py
+3
-3
ops.py
theano/compile/ops.py
+6
-6
profiling.py
theano/compile/profiling.py
+5
-5
sharedvalue.py
theano/compile/sharedvalue.py
+2
-2
test_builders.py
theano/compile/tests/test_builders.py
+34
-34
test_debugmode.py
theano/compile/tests/test_debugmode.py
+19
-19
test_function.py
theano/compile/tests/test_function.py
+20
-20
test_function_module.py
theano/compile/tests/test_function_module.py
+23
-25
test_misc.py
theano/compile/tests/test_misc.py
+5
-5
test_monitormode.py
theano/compile/tests/test_monitormode.py
+4
-4
test_nanguardmode.py
theano/compile/tests/test_nanguardmode.py
+16
-16
test_ops.py
theano/compile/tests/test_ops.py
+2
-3
test_pfunc.py
theano/compile/tests/test_pfunc.py
+136
-136
test_profiling.py
theano/compile/tests/test_profiling.py
+2
-2
test_shared.py
theano/compile/tests/test_shared.py
+49
-49
没有找到文件。
theano/compile/builders.py
浏览文件 @
ccf6deb0
...
...
@@ -59,11 +59,11 @@ class OpFromGraph(gof.Op):
.. code-block:: python
import numpy
import numpy
as np
import theano
from theano import config, function, OpFromGraph, tensor
x, y, z = tensor.scalars('xyz')
s = theano.shared(n
umpy
.random.rand(2, 2).astype(config.floatX))
s = theano.shared(n
p
.random.rand(2, 2).astype(config.floatX))
e = x + y * z + s
op = OpFromGraph([x, y, z], [e])
# op behaves like a normal theano op
...
...
theano/compile/debugmode.py
浏览文件 @
ccf6deb0
...
...
@@ -14,7 +14,7 @@ import six.moves.copyreg as copyreg
from
itertools
import
chain
,
product
as
itertools_product
from
theano.compat
import
izip
import
numpy
import
numpy
as
np
import
theano
from
theano
import
gof
,
config
...
...
@@ -270,15 +270,15 @@ class BadOptimization(DebugModeError):
print
(
" New Value: "
,
str
(
self
.
new_r_val
),
file
=
sio
)
try
:
ov
=
n
umpy
.
asarray
(
self
.
old_r_val
)
nv
=
n
umpy
.
asarray
(
self
.
new_r_val
)
ov
=
n
p
.
asarray
(
self
.
old_r_val
)
nv
=
n
p
.
asarray
(
self
.
new_r_val
)
ssio
=
StringIO
()
abs_diff
=
n
umpy
.
absolute
(
nv
-
ov
)
print
(
" Max Abs Diff: "
,
n
umpy
.
max
(
abs_diff
),
file
=
ssio
)
print
(
" Mean Abs Diff: "
,
n
umpy
.
mean
(
abs_diff
),
file
=
ssio
)
print
(
" Median Abs Diff: "
,
n
umpy
.
median
(
abs_diff
),
file
=
ssio
)
print
(
" Std Abs Diff: "
,
n
umpy
.
std
(
abs_diff
),
file
=
ssio
)
arg_max_val
=
n
umpy
.
argmax
(
abs_diff
)
abs_diff
=
n
p
.
absolute
(
nv
-
ov
)
print
(
" Max Abs Diff: "
,
n
p
.
max
(
abs_diff
),
file
=
ssio
)
print
(
" Mean Abs Diff: "
,
n
p
.
mean
(
abs_diff
),
file
=
ssio
)
print
(
" Median Abs Diff: "
,
n
p
.
median
(
abs_diff
),
file
=
ssio
)
print
(
" Std Abs Diff: "
,
n
p
.
std
(
abs_diff
),
file
=
ssio
)
arg_max_val
=
n
p
.
argmax
(
abs_diff
)
values_at_max
=
(
nv
.
flatten
()[
arg_max_val
],
ov
.
flatten
()[
arg_max_val
])
print
(
" Value at Max Diff: "
,
values_at_max
,
file
=
ssio
)
...
...
@@ -286,13 +286,13 @@ class BadOptimization(DebugModeError):
# N.B. the maximum(..., 1e-8) protects against div by 0 when
# nv == ov == 0
reldiff
=
(
abs_diff
/
n
umpy
.
maaximum
(
numpy
.
absolute
(
nv
)
+
numpy
.
absolute
(
ov
),
1e-8
))
print
(
" Max Rel Diff: "
,
n
umpy
.
max
(
reldiff
),
file
=
ssio
)
print
(
" Mean Rel Diff: "
,
n
umpy
.
mean
(
reldiff
),
file
=
ssio
)
print
(
" Median Rel Diff: "
,
n
umpy
.
median
(
reldiff
),
file
=
ssio
)
print
(
" Std Rel Diff: "
,
n
umpy
.
std
(
reldiff
),
file
=
ssio
)
arg_max_val
=
n
umpy
.
argmax
(
reldiff
)
n
p
.
maximum
(
np
.
absolute
(
nv
)
+
np
.
absolute
(
ov
),
1e-8
))
print
(
" Max Rel Diff: "
,
n
p
.
max
(
reldiff
),
file
=
ssio
)
print
(
" Mean Rel Diff: "
,
n
p
.
mean
(
reldiff
),
file
=
ssio
)
print
(
" Median Rel Diff: "
,
n
p
.
median
(
reldiff
),
file
=
ssio
)
print
(
" Std Rel Diff: "
,
n
p
.
std
(
reldiff
),
file
=
ssio
)
arg_max_val
=
n
p
.
argmax
(
reldiff
)
values_at_max
=
(
nv
.
flatten
()[
arg_max_val
],
ov
.
flatten
()[
arg_max_val
])
print
(
" Value at Max Diff: "
,
values_at_max
,
file
=
ssio
)
...
...
@@ -342,8 +342,8 @@ class BadDestroyMap(DebugModeError):
print
(
" repr (old val):"
,
repr
(
self
.
old_val
),
file
=
sio
)
print
(
" repr (new val):"
,
repr
(
self
.
new_val
),
file
=
sio
)
try
:
npy_old_val
=
n
umpy
.
asarray
(
self
.
old_val
)
npy_new_val
=
n
umpy
.
asarray
(
self
.
new_val
)
npy_old_val
=
n
p
.
asarray
(
self
.
old_val
)
npy_new_val
=
n
p
.
asarray
(
self
.
new_val
)
print
(
" value dtype (new <space> old):"
,
npy_new_val
.
dtype
,
npy_old_val
.
dtype
,
file
=
sio
)
print
(
" value shape (new <space> old):"
,
npy_new_val
.
shape
,
...
...
@@ -356,13 +356,13 @@ class BadDestroyMap(DebugModeError):
print
(
" value min (new-old):"
,
delta
.
min
(),
file
=
sio
)
print
(
" value max (new-old):"
,
delta
.
max
(),
file
=
sio
)
print
(
" value argmin (new-old):"
,
n
umpy
.
unravel_index
(
delta
.
argmin
(),
npy_new_val
.
shape
),
n
p
.
unravel_index
(
delta
.
argmin
(),
npy_new_val
.
shape
),
file
=
sio
)
print
(
" value argmax (new-old):"
,
n
umpy
.
unravel_index
(
delta
.
argmax
(),
npy_new_val
.
shape
),
n
p
.
unravel_index
(
delta
.
argmax
(),
npy_new_val
.
shape
),
file
=
sio
)
print
(
" location of first 10 mismatches:"
,
n
umpy
.
transpose
(
numpy
.
nonzero
(
delta
))[:
10
],
file
=
sio
)
n
p
.
transpose
(
np
.
nonzero
(
delta
))[:
10
],
file
=
sio
)
print
(
""
,
file
=
sio
)
except
Exception
as
e
:
print
(
"(Numpy-hints failed with:
%
s)"
%
str
(
e
),
file
=
sio
)
...
...
@@ -453,7 +453,7 @@ class InvalidValueError(DebugModeError):
v_dtype
=
v
.
dtype
v_min
=
v
.
min
()
v_max
=
v
.
max
()
v_isfinite
=
n
umpy
.
all
(
numpy
.
isfinite
(
v
))
v_isfinite
=
n
p
.
all
(
np
.
isfinite
(
v
))
except
Exception
:
pass
client_node
=
self
.
client_node
...
...
@@ -1025,7 +1025,7 @@ def _lessbroken_deepcopy(a):
# this exists because copy.deepcopy on numpy arrays is broken
# This logic is also in link.py
from
theano.gof.type
import
_cdata_type
if
type
(
a
)
in
(
n
umpy
.
ndarray
,
numpy
.
memmap
):
if
type
(
a
)
in
(
n
p
.
ndarray
,
np
.
memmap
):
rval
=
a
.
copy
()
elif
type
(
a
)
is
_cdata_type
:
# This is not copyable (and should be used for constant data).
...
...
@@ -1034,7 +1034,7 @@ def _lessbroken_deepcopy(a):
rval
=
copy
.
deepcopy
(
a
)
assert
type
(
rval
)
==
type
(
a
),
(
type
(
rval
),
type
(
a
))
if
isinstance
(
rval
,
n
umpy
.
ndarray
):
if
isinstance
(
rval
,
n
p
.
ndarray
):
assert
rval
.
dtype
==
a
.
dtype
return
rval
...
...
@@ -1241,7 +1241,7 @@ def _get_preallocated_maps(node, thunk, prealloc_modes, def_val,
# There is no risk to overwrite inputs, since r does not work
# inplace.
if
isinstance
(
r
.
type
,
(
TensorType
,
CudaNdarrayType
)):
reuse_outputs
[
r
][
...
]
=
n
umpy
.
asarray
(
reuse_outputs
[
r
][
...
]
=
n
p
.
asarray
(
def_val
)
.
astype
(
r
.
type
.
dtype
)
if
reuse_outputs
:
...
...
@@ -1259,7 +1259,7 @@ def _get_preallocated_maps(node, thunk, prealloc_modes, def_val,
new_buf
=
r
.
type
.
value_zeros
(
r_vals
[
r
]
.
shape
)
# CudaNdarray don't have flags field
# assert new_buf.flags["C_CONTIGUOUS"]
new_buf
[
...
]
=
n
umpy
.
asarray
(
def_val
)
.
astype
(
r
.
type
.
dtype
)
new_buf
[
...
]
=
n
p
.
asarray
(
def_val
)
.
astype
(
r
.
type
.
dtype
)
c_cont_outputs
[
r
]
=
new_buf
...
...
@@ -1273,7 +1273,7 @@ def _get_preallocated_maps(node, thunk, prealloc_modes, def_val,
f_cont_outputs
=
{}
for
r
in
considered_outputs
:
if
isinstance
(
r
.
type
,
(
TensorType
,
CudaNdarrayType
)):
new_buf
=
n
umpy
.
zeros
(
new_buf
=
n
p
.
zeros
(
shape
=
r_vals
[
r
]
.
shape
,
dtype
=
r_vals
[
r
]
.
dtype
,
order
=
'F'
)
...
...
@@ -1331,7 +1331,7 @@ def _get_preallocated_maps(node, thunk, prealloc_modes, def_val,
else
:
buf_shape
.
append
(
s
*
2
)
new_buf
=
r
.
type
.
value_zeros
(
buf_shape
)
new_buf
[
...
]
=
n
umpy
.
asarray
(
def_val
)
.
astype
(
r
.
type
.
dtype
)
new_buf
[
...
]
=
n
p
.
asarray
(
def_val
)
.
astype
(
r
.
type
.
dtype
)
init_strided
[
r
]
=
new_buf
# The number of combinations is exponential in the number of
...
...
@@ -1377,7 +1377,7 @@ def _get_preallocated_maps(node, thunk, prealloc_modes, def_val,
r_buf
=
r_buf
[
tuple
(
strides
)][
tuple
(
shapes
)]
assert
r_buf
.
shape
==
r_vals
[
r
]
.
shape
r_buf
[
...
]
=
n
umpy
.
asarray
(
def_val
)
.
astype
(
r_buf
.
dtype
)
r_buf
[
...
]
=
n
p
.
asarray
(
def_val
)
.
astype
(
r_buf
.
dtype
)
strided
[
r
]
=
r_buf
if
strided
:
...
...
@@ -1405,7 +1405,7 @@ def _get_preallocated_maps(node, thunk, prealloc_modes, def_val,
for
s
,
sd
in
zip
(
r_vals
[
r
]
.
shape
,
r_shape_diff
)]
new_buf
=
r
.
type
.
value_zeros
(
out_shape
)
new_buf
[
...
]
=
n
umpy
.
asarray
(
new_buf
[
...
]
=
n
p
.
asarray
(
def_val
)
.
astype
(
r
.
type
.
dtype
)
wrong_size
[
r
]
=
new_buf
...
...
@@ -2261,7 +2261,7 @@ class _Linker(gof.link.LocalLinker):
# HACK TO LOOK LIKE A REAL DESTRUCTIVE ACTION
# TOOK PLACE
if
((
type
(
dr_vals
[
r
][
0
])
in
(
n
umpy
.
ndarray
,
numpy
.
memmap
))
and
(
n
p
.
ndarray
,
np
.
memmap
))
and
(
dr_vals
[
r
][
0
]
.
dtype
==
storage_map
[
r
][
0
]
.
dtype
)
and
(
dr_vals
[
r
][
0
]
.
shape
==
...
...
theano/compile/function.py
浏览文件 @
ccf6deb0
...
...
@@ -13,7 +13,7 @@ from six import string_types
from
theano.compile.io
import
In
from
theano.compile.function_module
import
orig_function
from
theano.compile.pfunc
import
pfunc
from
numpy
import
any
import
numpy
as
np
import
warnings
from
theano
import
compat
...
...
@@ -286,7 +286,7 @@ def function(inputs, outputs=None, mode=None, updates=None, givens=None,
"input."
)
# compute some features of the arguments:
uses_tuple
=
any
([
isinstance
(
i
,
(
list
,
tuple
))
for
i
in
inputs
])
uses_tuple
=
np
.
any
([
isinstance
(
i
,
(
list
,
tuple
))
for
i
in
inputs
])
uses_updates
=
bool
(
updates
)
uses_givens
=
bool
(
givens
)
...
...
theano/compile/function_module.py
浏览文件 @
ccf6deb0
...
...
@@ -12,7 +12,7 @@ import six.moves.cPickle as pickle
from
itertools
import
chain
import
time
import
warnings
import
numpy
import
numpy
as
np
import
theano
from
theano
import
config
,
gof
...
...
@@ -837,9 +837,9 @@ class Function(object):
in
args_share_memory
[
j
]],
[
self
.
input_storage
[
k
]
.
storage
[
0
]
for
k
in
args_share_memory
[
j
]])
if
n
umpy
.
any
([(
var
.
type
is
i_var
.
type
and
var
.
type
.
may_share_memory
(
val
,
i_val
))
for
(
var
,
val
)
in
group_j
]):
if
n
p
.
any
([(
var
.
type
is
i_var
.
type
and
var
.
type
.
may_share_memory
(
val
,
i_val
))
for
(
var
,
val
)
in
group_j
]):
is_aliased
=
True
args_share_memory
[
j
]
.
append
(
i
)
...
...
@@ -1028,9 +1028,9 @@ def _pickle_Function(f):
all_data
=
input_storage
+
inputs_data
for
i
,
d_i
in
enumerate
(
all_data
):
for
j
,
d_j
in
enumerate
(
all_data
):
if
((
i
<
j
)
and
isinstance
(
d_i
,
n
umpy
.
ndarray
)
and
isinstance
(
d_j
,
n
umpy
.
ndarray
)):
if
n
umpy
.
may_share_memory
(
d_i
,
d_j
):
if
((
i
<
j
)
and
isinstance
(
d_i
,
n
p
.
ndarray
)
and
isinstance
(
d_j
,
n
p
.
ndarray
)):
if
n
p
.
may_share_memory
(
d_i
,
d_j
):
if
f
.
pickle_aliased_memory_strategy
==
'warn'
:
_logger
.
warning
(
'aliased relationship between '
'Function arguments
%
s,
%
s '
...
...
@@ -1050,7 +1050,7 @@ def _constructor_Function(maker, input_storage, inputs_data):
assert
len
(
f
.
input_storage
)
==
len
(
inputs_data
)
for
container
,
x
in
zip
(
f
.
input_storage
,
inputs_data
):
assert
(
container
.
data
is
x
)
or
\
(
isinstance
(
x
,
n
umpy
.
ndarray
)
and
(
container
.
data
==
x
)
.
all
())
or
\
(
isinstance
(
x
,
n
p
.
ndarray
)
and
(
container
.
data
==
x
)
.
all
())
or
\
(
container
.
data
==
x
)
return
f
...
...
theano/compile/monitormode.py
浏览文件 @
ccf6deb0
from
__future__
import
absolute_import
,
print_function
,
division
# Note: this code was initially copied from the 'pyutools' package by its
# original author, and re-licensed under Theano's license.
import
numpy
import
numpy
as
np
import
theano
from
theano.compile.mode
import
Mode
...
...
@@ -93,8 +93,8 @@ class MonitorMode(Mode):
def
detect_nan
(
i
,
node
,
fn
):
for
output
in
fn
.
outputs
:
if
(
not
isinstance
(
output
[
0
],
n
umpy
.
random
.
RandomState
)
and
n
umpy
.
isnan
(
output
[
0
])
.
any
()):
if
(
not
isinstance
(
output
[
0
],
n
p
.
random
.
RandomState
)
and
n
p
.
isnan
(
output
[
0
])
.
any
()):
print
(
'*** NaN detected ***'
)
theano
.
printing
.
debugprint
(
node
)
print
(
'Inputs :
%
s'
%
[
input
[
0
]
for
input
in
fn
.
inputs
])
...
...
theano/compile/ops.py
浏览文件 @
ccf6deb0
...
...
@@ -17,7 +17,7 @@ from six import iteritems, integer_types
from
six.moves
import
xrange
import
numpy
import
numpy
as
np
def
register_view_op_c_code
(
type
,
code
,
version
=
()):
...
...
@@ -338,7 +338,7 @@ class Shape_i(gof.Op):
def
__init__
(
self
,
i
):
# As i will be used in the hash and that ndarray are not hashable,
# we need to convert it to an int as it is hashable.
if
isinstance
(
i
,
n
umpy
.
ndarray
):
if
isinstance
(
i
,
n
p
.
ndarray
):
assert
i
.
dtype
in
theano
.
tensor
.
integer_dtypes
assert
i
==
int
(
i
)
i
=
int
(
i
)
...
...
@@ -665,11 +665,11 @@ class Rebroadcast(gof.Op):
items
=
sorted
(
axis
)
self
.
axis
=
OrderedDict
(
items
)
for
axis
,
broad
in
iteritems
(
self
.
axis
):
if
not
isinstance
(
axis
,
(
n
umpy
.
integer
,
integer_types
)):
if
not
isinstance
(
axis
,
(
n
p
.
integer
,
integer_types
)):
raise
TypeError
(
"Rebroadcast needs integer axes. "
"Got {}"
.
format
(
axis
))
if
not
isinstance
(
broad
,
(
n
umpy
.
bool_
,
bool
)):
if
not
isinstance
(
broad
,
(
n
p
.
bool_
,
bool
)):
raise
TypeError
(
"Rebroadcast needs bool for new broadcast "
"pattern. Got {}"
.
format
(
broad
))
...
...
@@ -835,8 +835,8 @@ class SpecifyShape(gof.Op):
x
,
shape
=
inp
out
,
=
out_
assert
x
.
ndim
==
shape
.
size
assert
n
umpy
.
all
(
x
.
shape
==
shape
),
(
"got shape"
,
x
.
shape
,
"expected"
,
shape
)
assert
n
p
.
all
(
x
.
shape
==
shape
),
(
"got shape"
,
x
.
shape
,
"expected"
,
shape
)
out
[
0
]
=
x
def
infer_shape
(
self
,
node
,
shapes
):
...
...
theano/compile/profiling.py
浏览文件 @
ccf6deb0
...
...
@@ -27,7 +27,7 @@ import sys
import
time
from
collections
import
defaultdict
import
numpy
import
numpy
as
np
import
theano
from
six
import
iteritems
...
...
@@ -477,7 +477,7 @@ class ProfileStats(object):
hs
+=
[
'<#apply>'
]
es
+=
[
'
%4
d '
]
upto_length
=
n
umpy
.
sum
([
len
(
x
)
for
x
in
hs
])
+
len
(
hs
)
upto_length
=
n
p
.
sum
([
len
(
x
)
for
x
in
hs
])
+
len
(
hs
)
maxlen
=
max
(
self
.
line_width
-
upto_length
,
0
)
hs
+=
[
'<Class name>'
]
es
+=
[
'
%
s'
]
...
...
@@ -559,7 +559,7 @@ class ProfileStats(object):
hs
+=
[
'<#apply>'
]
es
+=
[
'
%4
d '
]
upto_length
=
n
umpy
.
sum
([
len
(
x
)
for
x
in
hs
])
+
len
(
hs
)
upto_length
=
n
p
.
sum
([
len
(
x
)
for
x
in
hs
])
+
len
(
hs
)
maxlen
=
max
(
self
.
line_width
-
upto_length
,
0
)
hs
+=
[
'<Op name>'
]
es
+=
[
'
%
s'
]
...
...
@@ -627,7 +627,7 @@ class ProfileStats(object):
if
self
.
variable_shape
:
hs
+=
[
'<Mflops>'
,
'<Gflops/s>'
]
upto_length
=
n
umpy
.
sum
([
len
(
x
)
for
x
in
hs
])
+
len
(
hs
)
upto_length
=
n
p
.
sum
([
len
(
x
)
for
x
in
hs
])
+
len
(
hs
)
maxlen
=
max
(
self
.
line_width
-
upto_length
,
0
)
hs
+=
[
'<Apply name>'
]
es
+=
[
'
%
s'
]
...
...
@@ -929,7 +929,7 @@ class ProfileStats(object):
node_list
=
list
(
node_list
)
mem_count
=
0
max_mem_count
=
0
mem_bound
=
n
umpy
.
inf
mem_bound
=
n
p
.
inf
# This take only the inputs/outputs dependencies.
dependencies
=
fgraph
.
profile
.
dependencies
done_set
=
set
([])
...
...
theano/compile/sharedvalue.py
浏览文件 @
ccf6deb0
...
...
@@ -9,7 +9,7 @@ import copy
import
logging
# Third-party imports
import
numpy
import
numpy
as
np
# Theano imports
from
theano.gof
import
Container
,
Variable
,
generic
,
utils
...
...
@@ -187,7 +187,7 @@ class SharedVariable(Variable):
# implemented at all, but with a more explicit error message to help
# Theano users figure out the root of the problem more easily.
value
=
self
.
get_value
(
borrow
=
True
)
if
isinstance
(
value
,
n
umpy
.
ndarray
):
if
isinstance
(
value
,
n
p
.
ndarray
):
# Array probably had an unknown dtype.
msg
=
(
"a Numpy array with dtype: '
%
s'. This data type is not "
"currently recognized by Theano tensors: please cast "
...
...
theano/compile/tests/test_builders.py
浏览文件 @
ccf6deb0
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
theano
import
config
,
shared
...
...
@@ -23,14 +23,14 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
f
=
op
(
x
,
y
,
z
)
-
op
(
y
,
z
,
x
)
fn
=
function
([
x
,
y
,
z
],
f
)
xv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
xv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
# print function, function.__module__
# print fn.maker.fgraph.toposort()
fn
(
xv
,
yv
,
zv
)
assert
n
umpy
.
all
(
8.0
==
fn
(
xv
,
yv
,
zv
))
assert
n
umpy
.
all
(
8.0
==
fn
(
xv
,
yv
,
zv
))
assert
n
p
.
all
(
8.0
==
fn
(
xv
,
yv
,
zv
))
assert
n
p
.
all
(
8.0
==
fn
(
xv
,
yv
,
zv
))
def
test_size_changes
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
...
...
@@ -38,15 +38,15 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
op
=
OpFromGraph
([
x
,
y
],
[
e
])
f
=
op
(
x
,
op
(
y
,
z
))
fn
=
function
([
x
,
y
,
z
],
f
)
xv
=
n
umpy
.
ones
((
2
,
3
),
dtype
=
config
.
floatX
)
yv
=
n
umpy
.
ones
((
3
,
4
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
umpy
.
ones
((
4
,
5
),
dtype
=
config
.
floatX
)
*
5
xv
=
n
p
.
ones
((
2
,
3
),
dtype
=
config
.
floatX
)
yv
=
n
p
.
ones
((
3
,
4
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
p
.
ones
((
4
,
5
),
dtype
=
config
.
floatX
)
*
5
res
=
fn
(
xv
,
yv
,
zv
)
assert
res
.
shape
==
(
2
,
5
)
assert
n
umpy
.
all
(
180.0
==
res
)
assert
n
p
.
all
(
180.0
==
res
)
res
=
fn
(
xv
,
yv
,
zv
)
assert
res
.
shape
==
(
2
,
5
)
assert
n
umpy
.
all
(
180.0
==
res
)
assert
n
p
.
all
(
180.0
==
res
)
def
test_grad
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
...
...
@@ -55,10 +55,10 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
f
=
op
(
x
,
y
,
z
)
f
=
f
-
T
.
grad
(
T
.
sum
(
f
),
y
)
fn
=
function
([
x
,
y
,
z
],
f
)
xv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
assert
n
umpy
.
all
(
11.0
==
fn
(
xv
,
yv
,
zv
))
xv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
assert
n
p
.
all
(
11.0
==
fn
(
xv
,
yv
,
zv
))
def
test_grad_grad
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
...
...
@@ -68,47 +68,47 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
f
=
f
-
T
.
grad
(
T
.
sum
(
f
),
y
)
f
=
f
-
T
.
grad
(
T
.
sum
(
f
),
y
)
fn
=
function
([
x
,
y
,
z
],
f
)
xv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
assert
n
umpy
.
allclose
(
6.0
,
fn
(
xv
,
yv
,
zv
))
xv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
assert
n
p
.
allclose
(
6.0
,
fn
(
xv
,
yv
,
zv
))
def
test_shared
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
s
=
shared
(
n
umpy
.
random
.
rand
(
2
,
2
)
.
astype
(
config
.
floatX
))
s
=
shared
(
n
p
.
random
.
rand
(
2
,
2
)
.
astype
(
config
.
floatX
))
e
=
x
+
y
*
z
+
s
op
=
OpFromGraph
([
x
,
y
,
z
],
[
e
])
# (1+3*5=array of 16) - (3+1*5=array of 8)
f
=
op
(
x
,
y
,
z
)
-
op
(
y
,
z
,
x
)
fn
=
function
([
x
,
y
,
z
],
f
)
xv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
xv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
# print function, function.__module__
# print fn.maker.fgraph.toposort()
assert
n
umpy
.
allclose
(
8.0
,
fn
(
xv
,
yv
,
zv
))
assert
n
umpy
.
allclose
(
8.0
,
fn
(
xv
,
yv
,
zv
))
assert
n
p
.
allclose
(
8.0
,
fn
(
xv
,
yv
,
zv
))
assert
n
p
.
allclose
(
8.0
,
fn
(
xv
,
yv
,
zv
))
def
test_shared_grad
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
s
=
shared
(
n
umpy
.
random
.
rand
(
2
,
2
)
.
astype
(
config
.
floatX
))
s
=
shared
(
n
p
.
random
.
rand
(
2
,
2
)
.
astype
(
config
.
floatX
))
e
=
x
+
y
*
z
+
s
op
=
OpFromGraph
([
x
,
y
,
z
],
[
e
])
f
=
op
(
x
,
y
,
z
)
f
=
f
-
T
.
grad
(
T
.
sum
(
f
),
y
)
fn
=
function
([
x
,
y
,
z
],
f
)
xv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
umpy
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
assert
n
umpy
.
allclose
(
11.0
+
s
.
get_value
(),
fn
(
xv
,
yv
,
zv
))
xv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
yv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
3
zv
=
n
p
.
ones
((
2
,
2
),
dtype
=
config
.
floatX
)
*
5
assert
n
p
.
allclose
(
11.0
+
s
.
get_value
(),
fn
(
xv
,
yv
,
zv
))
# grad again the shared variable
f
=
op
(
x
,
y
,
z
)
f
=
f
-
T
.
grad
(
T
.
sum
(
f
),
s
)
fn
=
function
([
x
,
y
,
z
],
f
)
assert
n
umpy
.
allclose
(
15.0
+
s
.
get_value
(),
fn
(
xv
,
yv
,
zv
))
assert
n
p
.
allclose
(
15.0
+
s
.
get_value
(),
fn
(
xv
,
yv
,
zv
))
def
test_connection_pattern
(
self
):
# Basic case
...
...
@@ -163,6 +163,6 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
p
=
T
.
matrix
(
'p'
)
self
.
_compile_and_check
([
q
,
p
],
op_graph
(
q
,
p
),
[
n
umpy
.
ones
([
3
,
4
],
dtype
=
config
.
floatX
),
n
umpy
.
ones
([
3
,
4
],
dtype
=
config
.
floatX
)],
[
n
p
.
ones
([
3
,
4
],
dtype
=
config
.
floatX
),
n
p
.
ones
([
3
,
4
],
dtype
=
config
.
floatX
)],
OpFromGraph
)
theano/compile/tests/test_debugmode.py
浏览文件 @
ccf6deb0
...
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
from
nose.plugins.skip
import
SkipTest
import
unittest
import
numpy
import
numpy
as
np
from
theano
import
config
from
theano
import
gof
...
...
@@ -316,7 +316,7 @@ def test_just_c_code():
x
=
theano
.
tensor
.
dvector
()
f
=
theano
.
function
([
x
],
wb2
(
x
),
mode
=
debugmode
.
DebugMode
(
check_py_code
=
False
))
assert
n
umpy
.
all
(
f
([
1
,
2
])
==
[
2
,
4
])
assert
n
p
.
all
(
f
([
1
,
2
])
==
[
2
,
4
])
def
test_baddestroymap
():
...
...
@@ -349,7 +349,7 @@ def test_baddestroymap_c():
f
=
theano
.
function
([
x
],
wb2i
(
x
),
mode
=
debugmode
.
DebugMode
(
check_py_code
=
False
))
try
:
assert
n
umpy
.
all
(
f
([
1
,
2
])
==
[
2
,
4
])
assert
n
p
.
all
(
f
([
1
,
2
])
==
[
2
,
4
])
assert
False
# failed to raise error
except
debugmode
.
BadDestroyMap
:
pass
...
...
@@ -445,8 +445,8 @@ class Test_ViewMap(unittest.TestCase):
r0
,
r1
=
f
([
1
,
2
,
3
,
4
],
[
5
,
6
,
7
,
8
])
assert
n
umpy
.
all
(
r0
==
[
1
,
2
,
3
,
4
])
assert
n
umpy
.
all
(
r1
==
[
2
,
3
,
4
])
assert
n
p
.
all
(
r0
==
[
1
,
2
,
3
,
4
])
assert
n
p
.
all
(
r1
==
[
2
,
3
,
4
])
def
test_aliased_outputs_ok_output
(
self
):
# here aliased outputs is ok because they are both outputs of the
...
...
@@ -470,8 +470,8 @@ class Test_ViewMap(unittest.TestCase):
r0
,
r1
=
f
([
1
,
2
,
3
,
4
],
[
5
,
6
,
7
,
8
])
assert
n
umpy
.
all
(
r0
==
[
2
,
4
,
6
,
8
])
assert
n
umpy
.
all
(
r1
==
[
4
,
6
,
8
])
assert
n
p
.
all
(
r0
==
[
2
,
4
,
6
,
8
])
assert
n
p
.
all
(
r1
==
[
4
,
6
,
8
])
def
test_aliased_outputs_ok_shadow
(
self
):
# here the alias between outputs is ok because one of them is not used
...
...
@@ -496,7 +496,7 @@ class Test_ViewMap(unittest.TestCase):
r0
=
f
([
1
,
2
,
3
,
4
],
[
5
,
6
,
7
,
8
])
assert
n
umpy
.
all
(
r0
==
[
2
,
4
,
6
,
8
])
assert
n
p
.
all
(
r0
==
[
2
,
4
,
6
,
8
])
def
test_aliased_outputs_bad
(
self
):
# here the alias between outputs is not ok because destroying one
...
...
@@ -555,31 +555,31 @@ class Test_check_isfinite(unittest.TestCase):
g
=
theano
.
function
([
x
],
theano
.
tensor
.
log
(
x
),
mode
=
'DEBUG_MODE'
)
# this should work
f
(
n
umpy
.
log
([
3
,
4
,
5
])
.
astype
(
config
.
floatX
))
f
(
n
p
.
log
([
3
,
4
,
5
])
.
astype
(
config
.
floatX
))
# if TensorType.filter_checks_isfinite were true, these would raise
# ValueError
# if not, DebugMode will check internally, and raise InvalidValueError
# passing an invalid value as an input should trigger ValueError
self
.
assertRaises
(
debugmode
.
InvalidValueError
,
f
,
n
umpy
.
log
([
3
,
-
4
,
5
])
.
astype
(
config
.
floatX
))
n
p
.
log
([
3
,
-
4
,
5
])
.
astype
(
config
.
floatX
))
self
.
assertRaises
(
debugmode
.
InvalidValueError
,
f
,
(
n
umpy
.
asarray
([
0
,
1.0
,
0
])
/
0
)
.
astype
(
config
.
floatX
))
(
n
p
.
asarray
([
0
,
1.0
,
0
])
/
0
)
.
astype
(
config
.
floatX
))
self
.
assertRaises
(
debugmode
.
InvalidValueError
,
f
,
(
n
umpy
.
asarray
([
1.0
,
1.0
,
1.0
])
/
0
)
.
astype
(
config
.
floatX
))
(
n
p
.
asarray
([
1.0
,
1.0
,
1.0
])
/
0
)
.
astype
(
config
.
floatX
))
# generating an invalid value internally should trigger
# InvalidValueError
self
.
assertRaises
(
debugmode
.
InvalidValueError
,
g
,
n
umpy
.
asarray
([
3
,
-
4
,
5
],
dtype
=
config
.
floatX
))
n
p
.
asarray
([
3
,
-
4
,
5
],
dtype
=
config
.
floatX
))
# this should disable the exception
theano
.
tensor
.
TensorType
.
filter_checks_isfinite
=
False
theano
.
compile
.
mode
.
predefined_modes
[
'DEBUG_MODE'
]
.
check_isfinite
=
False
# insert several Inf
f
(
n
umpy
.
asarray
(
numpy
.
asarray
([
1.0
,
1.0
,
1.0
])
/
0
,
dtype
=
config
.
floatX
))
f
(
n
p
.
asarray
(
np
.
asarray
([
1.0
,
1.0
,
1.0
])
/
0
,
dtype
=
config
.
floatX
))
def
test_check_isfinite_disabled
(
self
):
x
=
theano
.
tensor
.
dvector
()
...
...
@@ -587,10 +587,10 @@ class Test_check_isfinite(unittest.TestCase):
mode
=
debugmode
.
DebugMode
(
check_isfinite
=
False
))
# nan should go through
f
(
n
umpy
.
log
([
3
,
-
4
,
5
]))
f
(
n
p
.
log
([
3
,
-
4
,
5
]))
# inf should go through
infs
=
n
umpy
.
asarray
([
1.0
,
1.
,
1.
])
/
0
infs
=
n
p
.
asarray
([
1.0
,
1.
,
1.
])
/
0
# print infs
f
(
infs
)
return
...
...
@@ -721,14 +721,14 @@ class VecAsRowAndCol(gof.Op):
class
Test_preallocated_output
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
rng
=
n
umpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
self
.
rng
=
n
p
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
def
test_f_contiguous
(
self
):
a
=
theano
.
tensor
.
fmatrix
(
'a'
)
b
=
theano
.
tensor
.
fmatrix
(
'b'
)
z
=
BrokenCImplementationAdd
()(
a
,
b
)
# In this test, we do not want z to be an output of the graph.
out
=
theano
.
tensor
.
dot
(
z
,
n
umpy
.
eye
(
7
))
out
=
theano
.
tensor
.
dot
(
z
,
n
p
.
eye
(
7
))
a_val
=
self
.
rng
.
randn
(
7
,
7
)
.
astype
(
'float32'
)
b_val
=
self
.
rng
.
randn
(
7
,
7
)
.
astype
(
'float32'
)
...
...
theano/compile/tests/test_function.py
浏览文件 @
ccf6deb0
...
...
@@ -5,7 +5,7 @@ import shutil
import
tempfile
import
unittest
import
numpy
import
numpy
as
np
import
theano
from
theano.compile.io
import
In
...
...
@@ -27,7 +27,7 @@ def test_function_dump():
fct2
=
theano
.
function
(
**
l
)
x
=
[
1
,
2
,
3
]
assert
n
umpy
.
allclose
(
fct1
(
x
),
fct2
(
x
))
assert
n
p
.
allclose
(
fct1
(
x
),
fct2
(
x
))
class
TestFunctionIn
(
unittest
.
TestCase
):
...
...
@@ -40,14 +40,14 @@ class TestFunctionIn(unittest.TestCase):
f
=
theano
.
function
([
In
(
a
,
strict
=
False
)],
out
)
# works, rand generates float64 by default
f
(
n
umpy
.
random
.
rand
(
8
))
f
(
n
p
.
random
.
rand
(
8
))
# works, casting is allowed
f
(
n
umpy
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
(
n
p
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
=
theano
.
function
([
In
(
a
,
strict
=
True
)],
out
)
try
:
# fails, f expects float64
f
(
n
umpy
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
(
n
p
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
except
TypeError
:
pass
...
...
@@ -70,17 +70,17 @@ class TestFunctionIn(unittest.TestCase):
# using mutable=True will let f change the value in aval
f
=
theano
.
function
([
In
(
a
,
mutable
=
True
)],
a_out
,
mode
=
'FAST_RUN'
)
aval
=
n
umpy
.
random
.
rand
(
10
)
aval
=
n
p
.
random
.
rand
(
10
)
aval2
=
aval
.
copy
()
assert
n
umpy
.
all
(
f
(
aval
)
==
(
aval2
*
2
))
assert
not
n
umpy
.
all
(
aval
==
aval2
)
assert
n
p
.
all
(
f
(
aval
)
==
(
aval2
*
2
))
assert
not
n
p
.
all
(
aval
==
aval2
)
# using mutable=False should leave the input untouched
f
=
theano
.
function
([
In
(
a
,
mutable
=
False
)],
a_out
,
mode
=
'FAST_RUN'
)
aval
=
n
umpy
.
random
.
rand
(
10
)
aval
=
n
p
.
random
.
rand
(
10
)
aval2
=
aval
.
copy
()
assert
n
umpy
.
all
(
f
(
aval
)
==
(
aval2
*
2
))
assert
n
umpy
.
all
(
aval
==
aval2
)
assert
n
p
.
all
(
f
(
aval
)
==
(
aval2
*
2
))
assert
n
p
.
all
(
aval
==
aval2
)
def
test_in_update
(
self
):
a
=
theano
.
tensor
.
dscalar
(
'a'
)
...
...
@@ -115,7 +115,7 @@ class TestFunctionIn(unittest.TestCase):
# changes occur at the same time and one doesn't overwrite the other.
for
i
in
range
(
5
):
f
()
assert
n
umpy
.
allclose
(
shared_var
.
get_value
(),
i
%
2
)
assert
n
p
.
allclose
(
shared_var
.
get_value
(),
i
%
2
)
def
test_in_allow_downcast_int
(
self
):
a
=
theano
.
tensor
.
wvector
(
'a'
)
# int16
...
...
@@ -128,16 +128,16 @@ class TestFunctionIn(unittest.TestCase):
# Both values are in range. Since they're not ndarrays (but lists),
# they will be converted, and their value checked.
assert
n
umpy
.
all
(
f
([
3
],
[
6
],
1
)
==
10
)
assert
n
p
.
all
(
f
([
3
],
[
6
],
1
)
==
10
)
# Values are in range, but a dtype too large has explicitly been given
# For performance reasons, no check of the data is explicitly performed
# (It might be OK to change this in the future.)
self
.
assertRaises
(
TypeError
,
f
,
[
3
],
n
umpy
.
array
([
6
],
dtype
=
'int16'
),
self
.
assertRaises
(
TypeError
,
f
,
[
3
],
n
p
.
array
([
6
],
dtype
=
'int16'
),
1
)
# Value too big for a, silently ignored
assert
n
umpy
.
all
(
f
([
2
**
20
],
numpy
.
ones
(
1
,
dtype
=
'int8'
),
1
)
==
2
)
assert
n
p
.
all
(
f
([
2
**
20
],
np
.
ones
(
1
,
dtype
=
'int8'
),
1
)
==
2
)
# Value too big for b, raises TypeError
self
.
assertRaises
(
TypeError
,
f
,
[
3
],
[
312
],
1
)
...
...
@@ -156,17 +156,17 @@ class TestFunctionIn(unittest.TestCase):
(
a
+
b
+
c
))
# If the values can be accurately represented, everything is OK
assert
n
umpy
.
all
(
f
(
0
,
0
,
0
)
==
0
)
assert
n
p
.
all
(
f
(
0
,
0
,
0
)
==
0
)
# If allow_downcast is True, idem
assert
n
umpy
.
allclose
(
f
(
0.1
,
0
,
0
),
0.1
)
assert
n
p
.
allclose
(
f
(
0.1
,
0
,
0
),
0.1
)
# If allow_downcast is False, nope
self
.
assertRaises
(
TypeError
,
f
,
0
,
0.1
,
0
)
# If allow_downcast is None, it should work iff floatX=float32
if
theano
.
config
.
floatX
==
'float32'
:
assert
n
umpy
.
allclose
(
f
(
0
,
0
,
0.1
),
0.1
)
assert
n
p
.
allclose
(
f
(
0
,
0
,
0.1
),
0.1
)
else
:
self
.
assertRaises
(
TypeError
,
f
,
0
,
0
,
0.1
)
...
...
@@ -182,10 +182,10 @@ class TestFunctionIn(unittest.TestCase):
# If the values can be accurately represented, everything is OK
z
=
[
0
]
assert
n
umpy
.
all
(
f
(
z
,
z
,
z
)
==
0
)
assert
n
p
.
all
(
f
(
z
,
z
,
z
)
==
0
)
# If allow_downcast is True, idem
assert
n
umpy
.
allclose
(
f
([
0.1
],
z
,
z
),
0.1
)
assert
n
p
.
allclose
(
f
([
0.1
],
z
,
z
),
0.1
)
# If allow_downcast is False, nope
self
.
assertRaises
(
TypeError
,
f
,
z
,
[
0.1
],
z
)
...
...
theano/compile/tests/test_function_module.py
浏览文件 @
ccf6deb0
from
__future__
import
absolute_import
,
print_function
,
division
import
copy
import
six.moves.cPickle
as
pickle
import
numpy
import
numpy
as
np
import
unittest
...
...
@@ -18,8 +18,6 @@ from theano import tensor
from
theano
import
tensor
as
T
import
theano
import
numpy
as
N
def
PatternOptimizer
(
p1
,
p2
,
ign
=
True
):
return
gof
.
OpKeyOptimizer
(
gof
.
PatternSub
(
p1
,
p2
),
ignore_newtrees
=
ign
)
...
...
@@ -281,7 +279,7 @@ class T_function(unittest.TestCase):
def
test_swap_SharedVariable
(
self
):
i
=
T
.
iscalar
()
x_list
=
theano
.
shared
(
value
=
n
umpy
.
random
.
rand
(
10
)
.
astype
(
config
.
floatX
))
x_list
=
theano
.
shared
(
value
=
n
p
.
random
.
rand
(
10
)
.
astype
(
config
.
floatX
))
x
=
T
.
scalar
(
'x'
)
# SharedVariable for tests, one of them has update
...
...
@@ -343,11 +341,11 @@ class T_function(unittest.TestCase):
A special testcase for logistic_sgd.py in Deep Learning Tutorial
This test assert that SharedVariable in different function have same storage
"""
train_x
=
theano
.
shared
(
value
=
n
umpy
.
random
.
rand
(
10
,
10
)
.
astype
(
config
.
floatX
))
test_x
=
theano
.
shared
(
value
=
n
umpy
.
random
.
rand
(
10
,
10
)
.
astype
(
config
.
floatX
))
train_x
=
theano
.
shared
(
value
=
n
p
.
random
.
rand
(
10
,
10
)
.
astype
(
config
.
floatX
))
test_x
=
theano
.
shared
(
value
=
n
p
.
random
.
rand
(
10
,
10
)
.
astype
(
config
.
floatX
))
train_y
=
theano
.
shared
(
value
=
n
umpy
.
random
.
rand
(
10
,
1
)
.
astype
(
config
.
floatX
))
test_y
=
theano
.
shared
(
value
=
n
umpy
.
random
.
rand
(
10
,
1
)
.
astype
(
config
.
floatX
))
train_y
=
theano
.
shared
(
value
=
n
p
.
random
.
rand
(
10
,
1
)
.
astype
(
config
.
floatX
))
test_y
=
theano
.
shared
(
value
=
n
p
.
random
.
rand
(
10
,
1
)
.
astype
(
config
.
floatX
))
i
=
T
.
iscalar
(
'index'
)
x
=
T
.
vector
(
'x'
)
...
...
@@ -500,42 +498,42 @@ class T_function(unittest.TestCase):
when borrow=True is implemented.
"""
a
=
T
.
dmatrix
()
aval
=
n
umpy
.
random
.
rand
(
3
,
3
)
aval
=
n
p
.
random
.
rand
(
3
,
3
)
# when borrow=False, test that a destroy map cannot alias output to input
f
=
theano
.
function
([
In
(
a
,
borrow
=
False
)],
Out
(
a
+
1
,
borrow
=
True
))
assert
n
umpy
.
all
(
f
(
aval
)
==
aval
+
1
)
assert
not
n
umpy
.
may_share_memory
(
aval
,
f
(
aval
))
assert
n
p
.
all
(
f
(
aval
)
==
aval
+
1
)
assert
not
n
p
.
may_share_memory
(
aval
,
f
(
aval
))
# when borrow=False, test that a viewmap cannot alias output to input
f
=
theano
.
function
([
In
(
a
,
borrow
=
False
)],
Out
(
a
[
0
,
:],
borrow
=
True
))
assert
n
umpy
.
all
(
f
(
aval
)
==
aval
[
0
,
:])
assert
not
n
umpy
.
may_share_memory
(
aval
,
f
(
aval
))
assert
n
p
.
all
(
f
(
aval
)
==
aval
[
0
,
:])
assert
not
n
p
.
may_share_memory
(
aval
,
f
(
aval
))
def
test_borrow_output
(
self
):
a
=
T
.
dmatrix
()
f
=
function
([
a
],
Out
(
a
,
borrow
=
False
))
o
=
N
.
ones
((
3
,
3
))
o
=
np
.
ones
((
3
,
3
))
assert
o
is
not
f
(
o
)
# function no longer permits aliasing outputs to inputs
f
=
function
([
a
],
Out
(
a
*
4
,
borrow
=
False
))
o
=
N
.
ones
((
3
,
3
))
o
=
np
.
ones
((
3
,
3
))
four
=
f
(
o
)
assert
n
umpy
.
all
(
four
==
4
)
assert
n
p
.
all
(
four
==
4
)
f
(
o
+
.
1
)
# should not clobber the memory used to store four
assert
n
umpy
.
all
(
four
==
4
)
assert
n
p
.
all
(
four
==
4
)
f
=
function
([
a
],
Out
(
a
*
4
,
borrow
=
True
),
mode
=
theano
.
Mode
(
'c|py_nogc'
,
'fast_run'
))
o
=
N
.
ones
((
3
,
3
))
o
=
np
.
ones
((
3
,
3
))
four
=
f
(
o
)
assert
n
umpy
.
all
(
four
==
4
)
assert
n
p
.
all
(
four
==
4
)
f
(
o
+
.
1
)
# should clobber the memory used to store four
if
theano
.
config
.
cxx
:
assert
not
n
umpy
.
all
(
four
==
4
)
assert
not
n
p
.
all
(
four
==
4
)
else
:
# The Elemwise.perform method don't reuse memory
# as some numpy version don't support that correctly.
assert
n
umpy
.
all
(
four
==
4
)
assert
n
p
.
all
(
four
==
4
)
def
test_disconnected_input
(
self
):
a
=
T
.
scalar
(
'a'
)
...
...
@@ -767,7 +765,7 @@ class T_picklefunction(unittest.TestCase):
assert
f2
.
container
[
s
]
.
storage
is
f1
.
container
[
s
]
.
storage
# now put in a function with non-scalar
v_value
=
n
umpy
.
asarray
([
2
,
3
,
4.
],
dtype
=
config
.
floatX
)
v_value
=
n
p
.
asarray
([
2
,
3
,
4.
],
dtype
=
config
.
floatX
)
f3
=
function
([
x
,
In
(
v
,
value
=
v_value
)],
x
+
v
)
list_of_things
.
append
(
f3
)
...
...
@@ -814,13 +812,13 @@ class T_picklefunction(unittest.TestCase):
assert
nl
[
5
](
3
)
==
ol
[
5
](
3
)
assert
nl
[
4
]
.
value
[
nl
[
0
]]
==
6
assert
n
umpy
.
all
(
nl
[
6
][
nl
[
2
]]
==
numpy
.
asarray
([
2
,
3.
,
4
]))
assert
n
p
.
all
(
nl
[
6
][
nl
[
2
]]
==
np
.
asarray
([
2
,
3.
,
4
]))
def
test_broken_pickle_with_shared
(
self
):
saves
=
[]
def
pers_save
(
obj
):
if
isinstance
(
obj
,
n
umpy
.
ndarray
):
if
isinstance
(
obj
,
n
p
.
ndarray
):
saves
.
append
(
obj
)
return
len
(
saves
)
-
1
else
:
...
...
@@ -829,7 +827,7 @@ class T_picklefunction(unittest.TestCase):
def
pers_load
(
id
):
return
saves
[
id
]
b
=
n
umpy
.
random
.
rand
(
5
,
4
)
b
=
n
p
.
random
.
rand
(
5
,
4
)
x
=
theano
.
tensor
.
matrix
()
y
=
theano
.
shared
(
b
)
...
...
theano/compile/tests/test_misc.py
浏览文件 @
ccf6deb0
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
unittest
from
theano.compile.pfunc
import
pfunc
...
...
@@ -20,8 +20,8 @@ class NNet(object):
self
.
input
=
input
self
.
target
=
target
self
.
lr
=
shared
(
lr
,
'learning_rate'
)
self
.
w1
=
shared
(
n
umpy
.
zeros
((
n_hidden
,
n_input
)),
'w1'
)
self
.
w2
=
shared
(
n
umpy
.
zeros
((
n_output
,
n_hidden
)),
'w2'
)
self
.
w1
=
shared
(
n
p
.
zeros
((
n_hidden
,
n_input
)),
'w1'
)
self
.
w2
=
shared
(
n
p
.
zeros
((
n_output
,
n_hidden
)),
'w2'
)
# print self.lr.type
self
.
hidden
=
sigmoid
(
tensor
.
dot
(
self
.
w1
,
self
.
input
))
...
...
@@ -45,7 +45,7 @@ class NNet(object):
class
TestNnet
(
unittest
.
TestCase
):
def
test_nnet
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
1827
)
rng
=
n
p
.
random
.
RandomState
(
1827
)
data
=
rng
.
rand
(
10
,
4
)
nnet
=
NNet
(
n_input
=
3
,
n_hidden
=
10
)
for
epoch
in
range
(
3
):
...
...
@@ -60,4 +60,4 @@ class TestNnet(unittest.TestCase):
self
.
assertTrue
(
abs
(
mean_cost
-
0.20588975452
)
<
1e-6
)
# Just call functions to make sure they do not crash.
nnet
.
compute_output
(
input
)
nnet
.
output_from_hidden
(
n
umpy
.
ones
(
10
))
nnet
.
output_from_hidden
(
n
p
.
ones
(
10
))
theano/compile/tests/test_monitormode.py
浏览文件 @
ccf6deb0
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
...
...
@@ -12,7 +12,7 @@ def test_detect_nan():
def
detect_nan
(
i
,
node
,
fn
):
for
output
in
fn
.
outputs
:
if
n
umpy
.
isnan
(
output
[
0
])
.
any
():
if
n
p
.
isnan
(
output
[
0
])
.
any
():
print
(
'*** NaN detected ***'
)
theano
.
printing
.
debugprint
(
node
)
print
(
'Inputs :
%
s'
%
[
input
[
0
]
for
input
in
fn
.
inputs
])
...
...
@@ -36,7 +36,7 @@ def test_optimizer():
def
detect_nan
(
i
,
node
,
fn
):
for
output
in
fn
.
outputs
:
if
n
umpy
.
isnan
(
output
[
0
])
.
any
():
if
n
p
.
isnan
(
output
[
0
])
.
any
():
print
(
'*** NaN detected ***'
)
theano
.
printing
.
debugprint
(
node
)
print
(
'Inputs :
%
s'
%
[
input
[
0
]
for
input
in
fn
.
inputs
])
...
...
@@ -65,7 +65,7 @@ def test_not_inplace():
def
detect_nan
(
i
,
node
,
fn
):
for
output
in
fn
.
outputs
:
if
n
umpy
.
isnan
(
output
[
0
])
.
any
():
if
n
p
.
isnan
(
output
[
0
])
.
any
():
print
(
'*** NaN detected ***'
)
theano
.
printing
.
debugprint
(
node
)
print
(
'Inputs :
%
s'
%
[
input
[
0
]
for
input
in
fn
.
inputs
])
...
...
theano/compile/tests/test_nanguardmode.py
浏览文件 @
ccf6deb0
...
...
@@ -6,7 +6,7 @@ from __future__ import absolute_import, print_function, division
import
logging
from
nose.tools
import
assert_raises
import
numpy
import
numpy
as
np
from
theano.compile.nanguardmode
import
NanGuardMode
import
theano
...
...
@@ -18,20 +18,20 @@ def test_NanGuardMode():
# intentionally. A working implementation should be able to capture all
# the abnormalties.
x
=
T
.
matrix
()
w
=
theano
.
shared
(
n
umpy
.
random
.
randn
(
5
,
7
)
.
astype
(
theano
.
config
.
floatX
))
w
=
theano
.
shared
(
n
p
.
random
.
randn
(
5
,
7
)
.
astype
(
theano
.
config
.
floatX
))
y
=
T
.
dot
(
x
,
w
)
fun
=
theano
.
function
(
[
x
],
y
,
mode
=
NanGuardMode
(
nan_is_error
=
True
,
inf_is_error
=
True
)
)
a
=
n
umpy
.
random
.
randn
(
3
,
5
)
.
astype
(
theano
.
config
.
floatX
)
infa
=
n
umpy
.
tile
(
(
n
umpy
.
asarray
(
100.
)
**
1000000
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
5
))
nana
=
n
umpy
.
tile
(
n
umpy
.
asarray
(
numpy
.
nan
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
5
))
biga
=
n
umpy
.
tile
(
n
umpy
.
asarray
(
1e20
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
5
))
a
=
n
p
.
random
.
randn
(
3
,
5
)
.
astype
(
theano
.
config
.
floatX
)
infa
=
n
p
.
tile
(
(
n
p
.
asarray
(
100.
)
**
1000000
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
5
))
nana
=
n
p
.
tile
(
n
p
.
asarray
(
np
.
nan
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
5
))
biga
=
n
p
.
tile
(
n
p
.
asarray
(
1e20
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
5
))
fun
(
a
)
# normal values
...
...
@@ -46,14 +46,14 @@ def test_NanGuardMode():
_logger
.
propagate
=
True
# slices
a
=
n
umpy
.
random
.
randn
(
3
,
4
,
5
)
.
astype
(
theano
.
config
.
floatX
)
infa
=
n
umpy
.
tile
(
(
n
umpy
.
asarray
(
100.
)
**
1000000
)
.
astype
(
theano
.
config
.
floatX
),
a
=
n
p
.
random
.
randn
(
3
,
4
,
5
)
.
astype
(
theano
.
config
.
floatX
)
infa
=
n
p
.
tile
(
(
n
p
.
asarray
(
100.
)
**
1000000
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
4
,
5
))
nana
=
n
umpy
.
tile
(
n
umpy
.
asarray
(
numpy
.
nan
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
4
,
5
))
biga
=
n
umpy
.
tile
(
n
umpy
.
asarray
(
1e20
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
4
,
5
))
nana
=
n
p
.
tile
(
n
p
.
asarray
(
np
.
nan
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
4
,
5
))
biga
=
n
p
.
tile
(
n
p
.
asarray
(
1e20
)
.
astype
(
theano
.
config
.
floatX
),
(
3
,
4
,
5
))
x
=
T
.
tensor3
()
y
=
x
[:,
T
.
arange
(
2
),
T
.
arange
(
2
)]
...
...
theano/compile/tests/test_ops.py
浏览文件 @
ccf6deb0
...
...
@@ -9,7 +9,6 @@ from theano.tests import unittest_tools as utt
from
theano
import
function
import
theano
from
theano.tensor
import
dmatrix
,
dvector
from
numpy
import
allclose
from
theano.compile
import
as_op
import
pickle
...
...
@@ -34,7 +33,7 @@ class OpDecoratorTests(utt.InferShapeTester):
r
=
fn
([[
1.5
,
5
],
[
2
,
2
]])
r0
=
np
.
array
([
1.5
,
7.5
,
15.
,
30.
])
assert
allclose
(
r
,
r0
),
(
r
,
r0
)
assert
np
.
allclose
(
r
,
r0
),
(
r
,
r0
)
def
test_2arg
(
self
):
x
=
dmatrix
(
'x'
)
...
...
@@ -50,7 +49,7 @@ class OpDecoratorTests(utt.InferShapeTester):
r
=
fn
([[
1.5
,
5
],
[
2
,
2
]],
[
1
,
100
,
2
,
200
])
r0
=
np
.
array
([
2.5
,
107.5
,
17.
,
230.
])
assert
allclose
(
r
,
r0
),
(
r
,
r0
)
assert
np
.
allclose
(
r
,
r0
),
(
r
,
r0
)
def
test_infer_shape
(
self
):
x
=
dmatrix
(
'x'
)
...
...
theano/compile/tests/test_pfunc.py
浏览文件 @
ccf6deb0
...
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
import
theano
from
theano.tensor
import
dmatrix
,
iscalar
,
lscalar
,
dmatrices
...
...
@@ -50,7 +50,7 @@ class Test_pfunc(unittest.TestCase):
def
test_shared
(
self
):
# CHECK: two functions (f1 and f2) can share w
w
=
shared
(
n
umpy
.
random
.
rand
(
2
,
2
),
'w'
)
w
=
shared
(
n
p
.
random
.
rand
(
2
,
2
),
'w'
)
wval
=
w
.
get_value
(
borrow
=
False
)
x
=
dmatrix
()
...
...
@@ -58,24 +58,24 @@ class Test_pfunc(unittest.TestCase):
out2
=
w
*
x
f1
=
pfunc
([
x
],
[
out1
])
f2
=
pfunc
([
x
],
[
out2
])
xval
=
n
umpy
.
random
.
rand
(
2
,
2
)
assert
n
umpy
.
all
(
f1
(
xval
)
==
xval
+
wval
)
assert
n
umpy
.
all
(
f2
(
xval
)
==
xval
*
wval
)
xval
=
n
p
.
random
.
rand
(
2
,
2
)
assert
n
p
.
all
(
f1
(
xval
)
==
xval
+
wval
)
assert
n
p
.
all
(
f2
(
xval
)
==
xval
*
wval
)
# CHECK: updating a shared value
f3
=
pfunc
([
x
],
out1
,
updates
=
[(
w
,
(
w
-
1
))])
# f3 changes the value of w
assert
n
umpy
.
all
(
f3
(
xval
)
==
xval
+
wval
)
assert
n
p
.
all
(
f3
(
xval
)
==
xval
+
wval
)
# this same value is read by f1
assert
n
umpy
.
all
(
f1
(
xval
)
==
xval
+
(
wval
-
1
))
assert
n
p
.
all
(
f1
(
xval
)
==
xval
+
(
wval
-
1
))
w
.
set_value
(
w
.
get_value
(
borrow
=
True
)
*
10
,
borrow
=
True
)
# this same value is read by f1
assert
n
umpy
.
all
(
f1
(
xval
)
==
xval
+
w
.
get_value
(
borrow
=
True
))
assert
n
p
.
all
(
f1
(
xval
)
==
xval
+
w
.
get_value
(
borrow
=
True
))
def
test_no_shared_as_input
(
self
):
"""Test that shared variables cannot be used as function inputs."""
w_init
=
n
umpy
.
random
.
rand
(
2
,
2
)
w_init
=
n
p
.
random
.
rand
(
2
,
2
)
w
=
shared
(
w_init
.
copy
(),
'w'
)
try
:
pfunc
([
w
],
theano
.
tensor
.
sum
(
w
*
w
))
...
...
@@ -89,16 +89,16 @@ class Test_pfunc(unittest.TestCase):
# Ensure it is possible to (implicitly) use a shared variable in a
# function, as a 'state' that can be updated at will.
rng
=
n
umpy
.
random
.
RandomState
(
1827
)
rng
=
n
p
.
random
.
RandomState
(
1827
)
w_init
=
rng
.
rand
(
5
)
w
=
shared
(
w_init
.
copy
(),
'w'
)
reg
=
theano
.
tensor
.
sum
(
w
*
w
)
f
=
pfunc
([],
reg
)
assert
f
()
==
n
umpy
.
sum
(
w_init
*
w_init
)
assert
f
()
==
n
p
.
sum
(
w_init
*
w_init
)
# Change the value of w and ensure the output changes accordingly.
w
.
set_value
(
w
.
get_value
(
borrow
=
True
)
+
1.0
,
borrow
=
True
)
assert
f
()
==
n
umpy
.
sum
((
w_init
+
1
)
**
2
)
assert
f
()
==
n
p
.
sum
((
w_init
+
1
)
**
2
)
def
test_default_scalar_container
(
self
):
# Similar in spirit to test_default_container, but updating a scalar
...
...
@@ -117,14 +117,14 @@ class Test_pfunc(unittest.TestCase):
f
=
pfunc
([
In
(
a
,
strict
=
False
)],
[
out
])
# works, rand generates float64 by default
f
(
n
umpy
.
random
.
rand
(
8
))
f
(
n
p
.
random
.
rand
(
8
))
# works, casting is allowed
f
(
n
umpy
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
(
n
p
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
=
pfunc
([
In
(
a
,
strict
=
True
)],
[
out
])
try
:
# fails, f expects float64
f
(
n
umpy
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
(
n
p
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
except
TypeError
:
pass
...
...
@@ -134,20 +134,20 @@ class Test_pfunc(unittest.TestCase):
# using mutable=True will let fip change the value in aval
fip
=
pfunc
([
In
(
a
,
mutable
=
True
)],
[
a_out
],
mode
=
'FAST_RUN'
)
aval
=
n
umpy
.
random
.
rand
(
10
)
aval
=
n
p
.
random
.
rand
(
10
)
aval2
=
aval
.
copy
()
assert
n
umpy
.
all
(
fip
(
aval
)
==
(
aval2
*
2
))
assert
not
n
umpy
.
all
(
aval
==
aval2
)
assert
n
p
.
all
(
fip
(
aval
)
==
(
aval2
*
2
))
assert
not
n
p
.
all
(
aval
==
aval2
)
# using mutable=False should leave the input untouched
f
=
pfunc
([
In
(
a
,
mutable
=
False
)],
[
a_out
],
mode
=
'FAST_RUN'
)
aval
=
n
umpy
.
random
.
rand
(
10
)
aval
=
n
p
.
random
.
rand
(
10
)
aval2
=
aval
.
copy
()
assert
n
umpy
.
all
(
f
(
aval
)
==
(
aval2
*
2
))
assert
n
umpy
.
all
(
aval
==
aval2
)
assert
n
p
.
all
(
f
(
aval
)
==
(
aval2
*
2
))
assert
n
p
.
all
(
aval
==
aval2
)
def
test_shared_mutable
(
self
):
bval
=
n
umpy
.
arange
(
5
)
bval
=
n
p
.
arange
(
5
)
b
=
shared
(
bval
)
b_out
=
b
*
2
...
...
@@ -158,26 +158,26 @@ class Test_pfunc(unittest.TestCase):
# by default, shared are not mutable unless doing an explicit update
f
=
pfunc
([],
[
b_out
],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
n
umpy
.
arange
(
5
)
*
2
)
.
all
()
assert
n
umpy
.
all
(
b
.
get_value
(
borrow
=
True
)
==
numpy
.
arange
(
5
))
assert
(
f
()
==
n
p
.
arange
(
5
)
*
2
)
.
all
()
assert
n
p
.
all
(
b
.
get_value
(
borrow
=
True
)
==
np
.
arange
(
5
))
# using updates, b is now a mutable parameter
f
=
pfunc
([],
[
b_out
],
updates
=
[(
b
,
b_out
)],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
(
n
umpy
.
arange
(
5
)
*
2
))
.
all
()
assert
(
f
()
==
(
n
p
.
arange
(
5
)
*
2
))
.
all
()
# because of the update
assert
(
b
.
get_value
(
borrow
=
True
)
==
(
n
umpy
.
arange
(
5
)
*
2
))
.
all
()
assert
(
bval
==
(
n
umpy
.
arange
(
5
)
*
2
))
.
all
()
# because of mutable=True
assert
(
b
.
get_value
(
borrow
=
True
)
==
(
n
p
.
arange
(
5
)
*
2
))
.
all
()
assert
(
bval
==
(
n
p
.
arange
(
5
)
*
2
))
.
all
()
# because of mutable=True
# do not depend on updates being in-place though!
bval
=
n
umpy
.
arange
(
5
)
bval
=
n
p
.
arange
(
5
)
b
.
set_value
(
bval
,
borrow
=
True
)
bval
=
data_of
(
b
)
f
=
pfunc
([],
[
b_out
],
updates
=
[(
b
,
(
b_out
+
3
))],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
(
n
umpy
.
arange
(
5
)
*
2
))
.
all
()
assert
(
f
()
==
(
n
p
.
arange
(
5
)
*
2
))
.
all
()
# because of the update
assert
(
b
.
get_value
(
borrow
=
True
)
==
((
n
umpy
.
arange
(
5
)
*
2
)
+
3
))
.
all
()
assert
(
b
.
get_value
(
borrow
=
True
)
==
((
n
p
.
arange
(
5
)
*
2
)
+
3
))
.
all
()
# bval got modified to something...
assert
not
(
bval
==
n
umpy
.
arange
(
5
))
.
all
()
assert
not
(
bval
==
n
p
.
arange
(
5
))
.
all
()
# ... but not to b.value !
assert
not
(
bval
==
b
.
get_value
(
borrow
=
True
))
.
all
()
...
...
@@ -192,16 +192,16 @@ class Test_pfunc(unittest.TestCase):
# Both values are in range. Since they're not ndarrays (but lists),
# they will be converted, and their value checked.
assert
n
umpy
.
all
(
f
([
3
],
[
6
],
1
)
==
10
)
assert
n
p
.
all
(
f
([
3
],
[
6
],
1
)
==
10
)
# Values are in range, but a dtype too large has explicitly been given
# For performance reasons, no check of the data is explicitly performed
# (It might be OK to change this in the future.)
self
.
assertRaises
(
TypeError
,
f
,
[
3
],
n
umpy
.
array
([
6
],
dtype
=
'int16'
),
1
)
[
3
],
n
p
.
array
([
6
],
dtype
=
'int16'
),
1
)
# Value too big for a, silently ignored
assert
n
umpy
.
all
(
f
([
2
**
20
],
numpy
.
ones
(
1
,
dtype
=
'int8'
),
1
)
==
2
)
assert
n
p
.
all
(
f
([
2
**
20
],
np
.
ones
(
1
,
dtype
=
'int8'
),
1
)
==
2
)
# Value too big for b, raises TypeError
self
.
assertRaises
(
TypeError
,
f
,
[
3
],
[
312
],
1
)
...
...
@@ -220,17 +220,17 @@ class Test_pfunc(unittest.TestCase):
(
a
+
b
+
c
))
# If the values can be accurately represented, everything is OK
assert
n
umpy
.
all
(
f
(
0
,
0
,
0
)
==
0
)
assert
n
p
.
all
(
f
(
0
,
0
,
0
)
==
0
)
# If allow_downcast is True, idem
assert
n
umpy
.
allclose
(
f
(
0.1
,
0
,
0
),
0.1
)
assert
n
p
.
allclose
(
f
(
0.1
,
0
,
0
),
0.1
)
# If allow_downcast is False, nope
self
.
assertRaises
(
TypeError
,
f
,
0
,
0.1
,
0
)
# If allow_downcast is None, it should work iff floatX=float32
if
config
.
floatX
==
'float32'
:
assert
n
umpy
.
allclose
(
f
(
0
,
0
,
0.1
),
0.1
)
assert
n
p
.
allclose
(
f
(
0
,
0
,
0.1
),
0.1
)
else
:
self
.
assertRaises
(
TypeError
,
f
,
0
,
0
,
0.1
)
...
...
@@ -246,10 +246,10 @@ class Test_pfunc(unittest.TestCase):
# If the values can be accurately represented, everything is OK
z
=
[
0
]
assert
n
umpy
.
all
(
f
(
z
,
z
,
z
)
==
0
)
assert
n
p
.
all
(
f
(
z
,
z
,
z
)
==
0
)
# If allow_downcast is True, idem
assert
n
umpy
.
allclose
(
f
([
0.1
],
z
,
z
),
0.1
)
assert
n
p
.
allclose
(
f
([
0.1
],
z
,
z
),
0.1
)
# If allow_downcast is False, nope
self
.
assertRaises
(
TypeError
,
f
,
z
,
[
0.1
],
z
)
...
...
@@ -271,22 +271,22 @@ class Test_pfunc(unittest.TestCase):
g
=
pfunc
([
a
,
b
,
c
],
(
a
+
b
+
c
),
allow_input_downcast
=
False
)
# All values are in range. Since they're not ndarrays (but lists
# or scalars), they will be converted, and their value checked.
assert
n
umpy
.
all
(
g
([
3
],
[
6
],
0
)
==
9
)
assert
n
p
.
all
(
g
([
3
],
[
6
],
0
)
==
9
)
# Values are in range, but a dtype too large has explicitly been given
# For performance reasons, no check of the data is explicitly performed
# (It might be OK to change this in the future.)
self
.
assertRaises
(
TypeError
,
g
,
[
3
],
n
umpy
.
array
([
6
],
dtype
=
'int16'
),
0
)
[
3
],
n
p
.
array
([
6
],
dtype
=
'int16'
),
0
)
# Value too big for b, raises TypeError
self
.
assertRaises
(
TypeError
,
g
,
[
3
],
[
312
],
0
)
h
=
pfunc
([
a
,
b
,
c
],
(
a
+
b
+
c
))
# Default: allow_input_downcast=None
# Everything here should behave like with False
assert
n
umpy
.
all
(
h
([
3
],
[
6
],
0
)
==
9
)
assert
n
p
.
all
(
h
([
3
],
[
6
],
0
)
==
9
)
self
.
assertRaises
(
TypeError
,
h
,
[
3
],
n
umpy
.
array
([
6
],
dtype
=
'int16'
),
0
)
[
3
],
n
p
.
array
([
6
],
dtype
=
'int16'
),
0
)
self
.
assertRaises
(
TypeError
,
h
,
[
3
],
[
312
],
0
)
def
test_allow_downcast_floatX
(
self
):
...
...
@@ -298,21 +298,21 @@ class Test_pfunc(unittest.TestCase):
h
=
pfunc
([
a
,
b
],
(
a
+
b
),
allow_input_downcast
=
None
)
# If the values can be accurately represented, OK
assert
n
umpy
.
all
(
f
(
0
,
[
0
])
==
0
)
assert
n
umpy
.
all
(
g
(
0
,
[
0
])
==
0
)
assert
n
umpy
.
all
(
h
(
0
,
[
0
])
==
0
)
assert
n
p
.
all
(
f
(
0
,
[
0
])
==
0
)
assert
n
p
.
all
(
g
(
0
,
[
0
])
==
0
)
assert
n
p
.
all
(
h
(
0
,
[
0
])
==
0
)
# For the vector: OK iff allow_input_downcast is True
assert
n
umpy
.
allclose
(
f
(
0
,
[
0.1
]),
0.1
)
assert
n
p
.
allclose
(
f
(
0
,
[
0.1
]),
0.1
)
self
.
assertRaises
(
TypeError
,
g
,
0
,
[
0.1
])
self
.
assertRaises
(
TypeError
,
h
,
0
,
[
0.1
])
# For the scalar: OK if allow_input_downcast is True,
# or None and floatX==float32
assert
n
umpy
.
allclose
(
f
(
0.1
,
[
0
]),
0.1
)
assert
n
p
.
allclose
(
f
(
0.1
,
[
0
]),
0.1
)
self
.
assertRaises
(
TypeError
,
g
,
0.1
,
[
0
])
if
config
.
floatX
==
'float32'
:
assert
n
umpy
.
allclose
(
h
(
0.1
,
[
0
]),
0.1
)
assert
n
p
.
allclose
(
h
(
0.1
,
[
0
]),
0.1
)
else
:
self
.
assertRaises
(
TypeError
,
h
,
0.1
,
[
0
])
...
...
@@ -340,7 +340,7 @@ class Test_pfunc(unittest.TestCase):
def
test_update_err_broadcast
(
self
):
# Test that broadcastable dimensions raise error
data
=
n
umpy
.
random
.
rand
(
10
,
10
)
.
astype
(
'float32'
)
data
=
n
p
.
random
.
rand
(
10
,
10
)
.
astype
(
'float32'
)
output_var
=
shared
(
name
=
"output"
,
value
=
data
)
# the update_var has type matrix, and the update expression
...
...
@@ -350,7 +350,7 @@ class Test_pfunc(unittest.TestCase):
def
test_duplicate_updates
(
self
):
x
,
y
=
dmatrices
(
'x'
,
'y'
)
z
=
shared
(
n
umpy
.
ones
((
2
,
3
)))
z
=
shared
(
n
p
.
ones
((
2
,
3
)))
self
.
assertRaises
(
ValueError
,
theano
.
function
,
[
x
,
y
],
[
z
],
updates
=
[(
z
,
(
z
+
x
+
y
)),
(
z
,
(
z
-
x
))])
...
...
@@ -362,29 +362,29 @@ class Test_pfunc(unittest.TestCase):
y
=
tensor
.
ivector
()
f
=
pfunc
([
y
],
(
y
*
x
),
givens
=
{
x
:
6
})
assert
n
umpy
.
all
(
f
([
1
,
1
,
1
])
==
[
6
,
6
,
6
])
assert
n
p
.
all
(
f
([
1
,
1
,
1
])
==
[
6
,
6
,
6
])
assert
x
.
get_value
()
==
0
z
=
tensor
.
ivector
()
c
=
z
*
y
f
=
pfunc
([
y
],
(
c
+
7
),
givens
=
{
z
:
theano
.
_asarray
([
4
,
4
,
4
],
dtype
=
'int32'
)})
assert
n
umpy
.
all
(
f
([
1
,
1
,
1
])
==
[
11
,
11
,
11
])
assert
n
p
.
all
(
f
([
1
,
1
,
1
])
==
[
11
,
11
,
11
])
assert
x
.
get_value
()
==
0
def
test_clone0
(
self
):
x
=
shared
(
n
umpy
.
asarray
([
4
,
4
,
4
]))
y
=
shared
(
n
umpy
.
asarray
([
4
,
4
,
4
]))
z
=
shared
(
n
umpy
.
asarray
([
2
,
2
,
2
]))
x
=
shared
(
n
p
.
asarray
([
4
,
4
,
4
]))
y
=
shared
(
n
p
.
asarray
([
4
,
4
,
4
]))
z
=
shared
(
n
p
.
asarray
([
2
,
2
,
2
]))
up
=
pfunc
([],
[],
updates
=
{
x
:
(
x
*
5
),
y
:
((
x
*
5
)
+
y
),
z
:
(((
x
*
5
)
+
y
)
**
z
)})
up
()
assert
n
umpy
.
all
(
x
.
get_value
()
==
20
)
assert
n
umpy
.
all
(
y
.
get_value
()
==
24
)
assert
n
umpy
.
all
(
z
.
get_value
()
==
(
24
**
2
))
assert
n
p
.
all
(
x
.
get_value
()
==
20
)
assert
n
p
.
all
(
y
.
get_value
()
==
24
)
assert
n
p
.
all
(
z
.
get_value
()
==
(
24
**
2
))
def
test_default_updates
(
self
):
x
=
shared
(
0
)
...
...
@@ -625,7 +625,7 @@ class Test_pfunc(unittest.TestCase):
# There was a bug in CVM, triggered when a shared variable
# was its own update expression.
a
=
shared
(
1.
,
'a'
)
b
=
shared
(
n
umpy
.
ones
((
2
,
3
)),
'b'
)
b
=
shared
(
n
p
.
ones
((
2
,
3
)),
'b'
)
# The order of the variables is not determined, so we try
# both shared variables.
...
...
@@ -650,7 +650,7 @@ class Test_pfunc(unittest.TestCase):
# Like test_update_same, but the update expression is simplified until
# it is found to be equal to the original variable
a
=
shared
(
1.
,
'a'
)
b
=
shared
(
n
umpy
.
ones
((
2
,
3
)),
'b'
)
b
=
shared
(
n
p
.
ones
((
2
,
3
)),
'b'
)
# See comment in test_update_same about why we try both
# shared variables.
...
...
@@ -695,13 +695,13 @@ class Test_aliasing_rules(unittest.TestCase):
def
test_shared_constructor_copies
(
self
):
# shared constructor makes copy
# (rule #2)
orig_a
=
n
umpy
.
zeros
((
2
,
2
))
orig_a
=
n
p
.
zeros
((
2
,
2
))
A
=
self
.
shared
(
orig_a
)
assert
not
n
umpy
.
may_share_memory
(
orig_a
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
orig_a
,
data_of
(
A
))
# rule #2 reading back from theano-managed memory
assert
not
n
umpy
.
may_share_memory
(
A
.
get_value
(
borrow
=
False
),
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
A
.
get_value
(
borrow
=
False
),
data_of
(
A
))
def
test_sparse_input_aliasing_affecting_inplace_operations
(
self
):
##
...
...
@@ -732,7 +732,7 @@ class Test_aliasing_rules(unittest.TestCase):
# Test 1. If the same variable is given twice
# Compute bogus values
m
=
sp
.
csc_matrix
(
n
umpy
.
asarray
(
m
=
sp
.
csc_matrix
(
n
p
.
asarray
(
[[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
...
...
@@ -742,7 +742,7 @@ class Test_aliasing_rules(unittest.TestCase):
# Since we used inplace operation v and m may be corrupted
# so we need to recreate them
m
=
sp
.
csc_matrix
(
n
umpy
.
asarray
(
m
=
sp
.
csc_matrix
(
n
p
.
asarray
(
[[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
...
...
@@ -751,7 +751,7 @@ class Test_aliasing_rules(unittest.TestCase):
m_copy
=
m
.
copy
()
vals
=
f
(
m
,
m_copy
)
assert
n
umpy
.
allclose
(
vals
.
todense
(),
bogus_vals
.
todense
())
assert
n
p
.
allclose
(
vals
.
todense
(),
bogus_vals
.
todense
())
def
test_input_aliasing_affecting_inplace_operations
(
self
):
...
...
@@ -771,27 +771,27 @@ class Test_aliasing_rules(unittest.TestCase):
# Test 1. If the same variable is given twice
# Compute bogus values
v
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
umpy
.
asarray
([[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
[
0
,
0
,
0
,
1
,
0
],
[
0
,
0
,
0
,
0
,
1
]],
dtype
=
'float64'
)
v
=
n
p
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
p
.
asarray
([[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
[
0
,
0
,
0
,
1
,
0
],
[
0
,
0
,
0
,
0
,
1
]],
dtype
=
'float64'
)
bogus_vals
=
f
(
v
,
v
,
m
,
m
)
# Since we used inplace operation v and m may be corrupted
# so we need to recreate them
v
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
umpy
.
asarray
([[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
[
0
,
0
,
0
,
1
,
0
],
[
0
,
0
,
0
,
0
,
1
]],
dtype
=
'float64'
)
v
=
n
p
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
p
.
asarray
([[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
[
0
,
0
,
0
,
1
,
0
],
[
0
,
0
,
0
,
0
,
1
]],
dtype
=
'float64'
)
m_copy
=
m
.
copy
()
v_copy
=
v
.
copy
()
vals
=
f
(
v
,
v_copy
,
m
,
m_copy
)
assert
n
umpy
.
allclose
(
vals
,
bogus_vals
)
assert
n
p
.
allclose
(
vals
,
bogus_vals
)
def
test_partial_input_aliasing_affecting_inplace_operations
(
self
):
...
...
@@ -822,102 +822,102 @@ class Test_aliasing_rules(unittest.TestCase):
theano
.
dot
((
z
*
4
),
m3
)))
# Compute bogus values
v
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
umpy
.
asarray
([[
1
,
0
],
[
0
,
1
]],
dtype
=
'float64'
)
v
=
n
p
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
p
.
asarray
([[
1
,
0
],
[
0
,
1
]],
dtype
=
'float64'
)
bogus_vals
=
f
(
v
[:
2
],
v
[
1
:
3
],
v
[
2
:
4
],
m
,
m
,
m
)
# Since we used inplace operation v and m may be corrupted
# so we need to recreate them
v
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
umpy
.
asarray
([[
1
,
0
],
[
0
,
1
]],
dtype
=
'float64'
)
v
=
n
p
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
p
.
asarray
([[
1
,
0
],
[
0
,
1
]],
dtype
=
'float64'
)
m_copy1
=
m
.
copy
()
v_copy1
=
v
.
copy
()
m_copy2
=
m
.
copy
()
v_copy2
=
v
.
copy
()
vals
=
f
(
v
[:
2
],
v_copy1
[
1
:
3
],
v_copy2
[
2
:
4
],
m
,
m_copy1
,
m_copy2
)
assert
n
umpy
.
allclose
(
vals
,
bogus_vals
)
assert
n
p
.
allclose
(
vals
,
bogus_vals
)
def
test_potential_output_aliasing_induced_by_updates
(
self
):
A
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
)))
B
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
)))
C
=
n
umpy
.
zeros
((
2
,
2
))
A
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
)))
B
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
)))
C
=
n
p
.
zeros
((
2
,
2
))
D
=
tensor
.
dmatrix
()
DD
=
D
+
5
f
=
pfunc
([
D
],
[],
updates
=
[(
A
,
D
),
(
B
,
D
)])
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
f
=
pfunc
([
D
],
[],
updates
=
[(
A
,
D
[:]),
(
B
,
D
)])
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
f
=
pfunc
([
D
],
[],
updates
=
[(
A
,
(
D
+
5
)),
(
B
,
D
[:])])
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
f
=
pfunc
([
D
],
[],
updates
=
[(
A
,
(
D
+
5
)),
(
B
,
D
)])
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
f
=
pfunc
([
D
],
DD
,
updates
=
[(
A
,
DD
[:
1
]),
(
B
,
DD
)])
R
=
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
A
))
f
=
pfunc
([
D
],
DD
,
updates
=
[(
A
,
DD
[:
1
]),
(
B
,
(
DD
[:
1
]
*
2
))])
R
=
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
A
))
f
=
pfunc
([
D
],
(
DD
*
4
),
updates
=
[(
A
,
(
DD
[:
1
]
*
3
)),
(
B
,
(
DD
[:
1
]
*
2
))])
R
=
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
A
))
f
=
pfunc
([
D
],
(
DD
*
4
),
updates
=
[(
A
,
(
DD
[:
1
]
*
3
)),
(
B
,
(
DD
[:
1
]
*
3
))])
R
=
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
A
))
def
test_no_aliasing_0
(
self
):
# B is a shared variable, A is updated with B's contents
# we need A to be copied to avoid aliasing
A
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
))
+
.
5
)
B
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
))
-
.
5
)
A
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
))
+
.
5
)
B
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
))
-
.
5
)
f
=
pfunc
([],
[],
updates
=
[(
A
,
B
)])
f
()
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
def
test_no_aliasing_1
(
self
):
# B is a shared variable, A is updated with B's contents
# since B is being updated as well, we don't need to copy anything
# to avoid aliasing shared variables.
A
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
))
+
.
5
)
B
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
))
-
.
5
)
A
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
))
+
.
5
)
B
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
))
-
.
5
)
C
=
tensor
.
dmatrix
()
f
=
pfunc
([
C
],
[],
updates
=
[(
A
,
B
),
(
B
,
C
)])
z
=
n
umpy
.
zeros
((
2
,
2
))
z
=
n
p
.
zeros
((
2
,
2
))
f
(
z
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
# Theano tries to maintain its own memory space.
assert
not
n
umpy
.
may_share_memory
(
z
,
data_of
(
B
))
assert
n
umpy
.
all
(
data_of
(
B
)
==
z
)
assert
not
n
p
.
may_share_memory
(
z
,
data_of
(
B
))
assert
n
p
.
all
(
data_of
(
B
)
==
z
)
def
test_no_aliasing_2
(
self
):
# B and A take one another's values
# no copying is necessary since each one is updated.
orig_a
=
n
umpy
.
zeros
((
2
,
2
))
+
.
5
orig_b
=
n
umpy
.
zeros
((
2
,
2
))
-
.
5
orig_a
=
n
p
.
zeros
((
2
,
2
))
+
.
5
orig_b
=
n
p
.
zeros
((
2
,
2
))
-
.
5
A
=
self
.
shared
(
orig_a
)
B
=
self
.
shared
(
orig_b
)
...
...
@@ -927,15 +927,15 @@ class Test_aliasing_rules(unittest.TestCase):
f
=
pfunc
([],
[],
updates
=
[(
A
,
B
),
(
B
,
A
)])
f
()
# correctness
assert
n
umpy
.
all
(
data_of
(
A
)
==
-.
5
)
assert
n
umpy
.
all
(
data_of
(
B
)
==
+.
5
)
assert
n
p
.
all
(
data_of
(
A
)
==
-.
5
)
assert
n
p
.
all
(
data_of
(
B
)
==
+.
5
)
# shared vars may not be aliased
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
# theano should have been smart enough to not make copies
assert
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of_b
)
assert
n
umpy
.
may_share_memory
(
data_of
(
B
),
data_of_a
)
assert
n
p
.
may_share_memory
(
data_of
(
A
),
data_of_b
)
assert
n
p
.
may_share_memory
(
data_of
(
B
),
data_of_a
)
def
test_no_aliasing_2b
(
self
):
# B and A take one another's values
...
...
@@ -943,8 +943,8 @@ class Test_aliasing_rules(unittest.TestCase):
# The twist one `test_no_aliasing_2` is that each shared var is updated
# with a view of the other one.
orig_a
=
n
umpy
.
zeros
((
2
,
2
))
+
.
5
orig_b
=
n
umpy
.
zeros
((
2
,
2
))
-
.
5
orig_a
=
n
p
.
zeros
((
2
,
2
))
+
.
5
orig_b
=
n
p
.
zeros
((
2
,
2
))
-
.
5
A
=
self
.
shared
(
orig_a
)
B
=
self
.
shared
(
orig_b
)
...
...
@@ -955,30 +955,30 @@ class Test_aliasing_rules(unittest.TestCase):
# theano.printing.debugprint(f)
f
()
# correctness (doesn't actually test the view...)
assert
n
umpy
.
all
(
data_of
(
A
)
==
-.
5
)
assert
n
umpy
.
all
(
data_of
(
B
)
==
+.
5
)
assert
n
p
.
all
(
data_of
(
A
)
==
-.
5
)
assert
n
p
.
all
(
data_of
(
B
)
==
+.
5
)
# shared vars may not be aliased
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
# theano should have been smart enough to not make copies
if
theano
.
config
.
mode
not
in
[
'DebugMode'
,
'DEBUG_MODE'
,
'FAST_COMPILE'
]:
# We don't ask DebugMode and FAST_COMPILE not to make copy.
# We have the right to do so.
assert
n
umpy
.
all
(
data_of
(
A
)
<
5
)
assert
n
p
.
all
(
data_of
(
A
)
<
5
)
data_of_b
+=
10
assert
n
umpy
.
all
(
data_of
(
A
)
>
5
)
assert
n
p
.
all
(
data_of
(
A
)
>
5
)
data_of_b
-=
10
assert
n
umpy
.
all
(
data_of
(
B
)
<
5
)
assert
n
p
.
all
(
data_of
(
B
)
<
5
)
data_of_a
+=
10
assert
n
umpy
.
all
(
data_of
(
B
)
>
5
)
assert
n
p
.
all
(
data_of
(
B
)
>
5
)
data_of_a
-=
10
# N.B. may_share_memory is what we mean, but does it work?
assert
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of_b
)
assert
n
umpy
.
may_share_memory
(
data_of
(
B
),
data_of_a
)
assert
n
p
.
may_share_memory
(
data_of
(
A
),
data_of_b
)
assert
n
p
.
may_share_memory
(
data_of
(
B
),
data_of_a
)
# N.B. This pattern could form a memory leak - each shared
# variable always points to a view, and that view gets
...
...
@@ -995,9 +995,9 @@ class Test_rebuild_strict(unittest.TestCase):
x
,
y
=
tensor
.
ivectors
(
'x'
,
'y'
)
z
=
x
*
y
f
=
theano
.
function
([
w
,
y
],
z
,
givens
=
[(
x
,
w
)],
rebuild_strict
=
False
)
z_val
=
f
(
n
umpy
.
ones
((
3
,
5
),
dtype
=
'int32'
),
numpy
.
arange
(
5
,
dtype
=
'int32'
))
z_val
=
f
(
n
p
.
ones
((
3
,
5
),
dtype
=
'int32'
),
np
.
arange
(
5
,
dtype
=
'int32'
))
assert
z_val
.
ndim
==
2
assert
n
umpy
.
all
(
z_val
==
numpy
.
ones
((
3
,
5
))
*
numpy
.
arange
(
5
))
assert
n
p
.
all
(
z_val
==
np
.
ones
((
3
,
5
))
*
np
.
arange
(
5
))
if
__name__
==
'__main__'
:
...
...
theano/compile/tests/test_profiling.py
浏览文件 @
ccf6deb0
...
...
@@ -6,7 +6,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
import
numpy
import
numpy
as
np
import
theano
from
six.moves
import
StringIO
...
...
@@ -45,7 +45,7 @@ class Test_profiling(unittest.TestCase):
f
=
theano
.
function
(
x
,
z
,
profile
=
p
,
name
=
"test_profiling"
,
mode
=
m
)
inp
=
[
n
umpy
.
arange
(
1024
,
dtype
=
'float32'
)
+
1
for
i
in
range
(
len
(
x
))]
inp
=
[
n
p
.
arange
(
1024
,
dtype
=
'float32'
)
+
1
for
i
in
range
(
len
(
x
))]
f
(
*
inp
)
buf
=
StringIO
()
...
...
theano/compile/tests/test_shared.py
浏览文件 @
ccf6deb0
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
unittest
import
theano
...
...
@@ -18,14 +18,14 @@ class Test_SharedVariable(unittest.TestCase):
else
:
assert
shared
(
7
)
.
type
==
theano
.
tensor
.
lscalar
,
shared
(
7
)
.
type
assert
shared
(
7.0
)
.
type
==
theano
.
tensor
.
dscalar
assert
shared
(
n
umpy
.
float32
(
7
))
.
type
==
theano
.
tensor
.
fscalar
assert
shared
(
n
p
.
float32
(
7
))
.
type
==
theano
.
tensor
.
fscalar
# test tensor constructor
b
=
shared
(
n
umpy
.
zeros
((
5
,
5
),
dtype
=
'int32'
))
b
=
shared
(
n
p
.
zeros
((
5
,
5
),
dtype
=
'int32'
))
assert
b
.
type
==
TensorType
(
'int32'
,
broadcastable
=
[
False
,
False
])
b
=
shared
(
n
umpy
.
random
.
rand
(
4
,
5
))
b
=
shared
(
n
p
.
random
.
rand
(
4
,
5
))
assert
b
.
type
==
TensorType
(
'float64'
,
broadcastable
=
[
False
,
False
])
b
=
shared
(
n
umpy
.
random
.
rand
(
5
,
1
,
2
))
b
=
shared
(
n
p
.
random
.
rand
(
5
,
1
,
2
))
assert
b
.
type
==
TensorType
(
'float64'
,
broadcastable
=
[
False
,
False
,
False
])
assert
shared
([])
.
type
==
generic
...
...
@@ -52,7 +52,7 @@ class Test_SharedVariable(unittest.TestCase):
SharedVariable
(
name
=
'u'
,
type
=
Tensor
(
broadcastable
=
[
False
],
dtype
=
'float64'
),
value
=
n
umpy
.
asarray
([
1.
,
2.
]),
value
=
n
p
.
asarray
([
1.
,
2.
]),
strict
=
False
)
# here the value is castable, and we're not strict about it,
...
...
@@ -90,14 +90,14 @@ class Test_SharedVariable(unittest.TestCase):
u
=
SharedVariable
(
name
=
'u'
,
type
=
Tensor
(
broadcastable
=
[
False
],
dtype
=
'float64'
),
value
=
n
umpy
.
asarray
([
1.
,
2.
]),
value
=
n
p
.
asarray
([
1.
,
2.
]),
strict
=
False
)
# check that assignments to value are cast properly
u
.
set_value
([
3
,
4
])
assert
type
(
u
.
get_value
())
is
n
umpy
.
ndarray
assert
type
(
u
.
get_value
())
is
n
p
.
ndarray
assert
str
(
u
.
get_value
(
borrow
=
True
)
.
dtype
)
==
'float64'
assert
n
umpy
.
all
(
u
.
get_value
()
==
[
3
,
4
])
assert
n
p
.
all
(
u
.
get_value
()
==
[
3
,
4
])
# check that assignments of nonsense fail
try
:
...
...
@@ -115,31 +115,31 @@ class Test_SharedVariable(unittest.TestCase):
def
f
(
var
,
val
):
var
.
set_value
(
val
)
b
=
shared
(
n
umpy
.
int64
(
7
),
strict
=
True
)
b
=
shared
(
n
p
.
int64
(
7
),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
lscalar
self
.
assertRaises
(
TypeError
,
f
,
b
,
8.23
)
b
=
shared
(
n
umpy
.
int32
(
7
),
strict
=
True
)
b
=
shared
(
n
p
.
int32
(
7
),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
iscalar
self
.
assertRaises
(
TypeError
,
f
,
b
,
8.23
)
b
=
shared
(
n
umpy
.
int16
(
7
),
strict
=
True
)
b
=
shared
(
n
p
.
int16
(
7
),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
wscalar
self
.
assertRaises
(
TypeError
,
f
,
b
,
8.23
)
b
=
shared
(
n
umpy
.
int8
(
7
),
strict
=
True
)
b
=
shared
(
n
p
.
int8
(
7
),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
bscalar
self
.
assertRaises
(
TypeError
,
f
,
b
,
8.23
)
b
=
shared
(
n
umpy
.
float64
(
7.234
),
strict
=
True
)
b
=
shared
(
n
p
.
float64
(
7.234
),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dscalar
self
.
assertRaises
(
TypeError
,
f
,
b
,
8
)
b
=
shared
(
n
umpy
.
float32
(
7.234
),
strict
=
True
)
b
=
shared
(
n
p
.
float32
(
7.234
),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
fscalar
self
.
assertRaises
(
TypeError
,
f
,
b
,
8
)
b
=
shared
(
n
umpy
.
float
(
7.234
),
strict
=
True
)
b
=
shared
(
n
p
.
float
(
7.234
),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dscalar
self
.
assertRaises
(
TypeError
,
f
,
b
,
8
)
...
...
@@ -147,39 +147,39 @@ class Test_SharedVariable(unittest.TestCase):
assert
b
.
type
==
theano
.
tensor
.
dscalar
self
.
assertRaises
(
TypeError
,
f
,
b
,
8
)
b
=
shared
(
n
umpy
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
self
.
assertRaises
(
TypeError
,
f
,
b
,
n
umpy
.
random
.
rand
(
5
,
5
))
b
=
shared
(
n
p
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
self
.
assertRaises
(
TypeError
,
f
,
b
,
n
p
.
random
.
rand
(
5
,
5
))
def
test_tensor_strict
(
self
):
def
f
(
var
,
val
):
var
.
set_value
(
val
)
b
=
shared
(
n
umpy
.
int64
([
7
]),
strict
=
True
)
b
=
shared
(
n
p
.
int64
([
7
]),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
lvector
self
.
assertRaises
(
TypeError
,
f
,
b
,
8.23
)
b
=
shared
(
n
umpy
.
int32
([
7
]),
strict
=
True
)
b
=
shared
(
n
p
.
int32
([
7
]),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
ivector
self
.
assertRaises
(
TypeError
,
f
,
b
,
8.23
)
b
=
shared
(
n
umpy
.
int16
([
7
]),
strict
=
True
)
b
=
shared
(
n
p
.
int16
([
7
]),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
wvector
self
.
assertRaises
(
TypeError
,
f
,
b
,
8.23
)
b
=
shared
(
n
umpy
.
int8
([
7
]),
strict
=
True
)
b
=
shared
(
n
p
.
int8
([
7
]),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
bvector
self
.
assertRaises
(
TypeError
,
f
,
b
,
8.23
)
b
=
shared
(
n
umpy
.
float64
([
7.234
]),
strict
=
True
)
b
=
shared
(
n
p
.
float64
([
7.234
]),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dvector
self
.
assertRaises
(
TypeError
,
f
,
b
,
8
)
b
=
shared
(
n
umpy
.
float32
([
7.234
]),
strict
=
True
)
b
=
shared
(
n
p
.
float32
([
7.234
]),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
fvector
self
.
assertRaises
(
TypeError
,
f
,
b
,
8
)
# n
umpy
.float([7.234]) don't work
# b = shared(n
umpy
.float([7.234]), strict=True)
# n
p
.float([7.234]) don't work
# b = shared(n
p
.float([7.234]), strict=True)
# assert b.type == theano.tensor.dvector
# self.assertRaises(TypeError, f, b, 8)
...
...
@@ -188,8 +188,8 @@ class Test_SharedVariable(unittest.TestCase):
# assert b.type == theano.tensor.dvector
# self.assertRaises(TypeError, f, b, 8)
b
=
shared
(
n
umpy
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
self
.
assertRaises
(
TypeError
,
f
,
b
,
n
umpy
.
random
.
rand
(
5
,
5
))
b
=
shared
(
n
p
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
self
.
assertRaises
(
TypeError
,
f
,
b
,
n
p
.
random
.
rand
(
5
,
5
))
def
test_scalar_floatX
(
self
):
...
...
@@ -204,37 +204,37 @@ class Test_SharedVariable(unittest.TestCase):
def
f
(
var
,
val
):
var
.
set_value
(
val
)
b
=
shared
(
n
umpy
.
int64
(
7
),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
int64
(
7
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
lscalar
f
(
b
,
8.23
)
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
int32
(
7
),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
int32
(
7
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
iscalar
f
(
b
,
8.23
)
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
int16
(
7
),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
int16
(
7
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
wscalar
f
(
b
,
8.23
)
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
int8
(
7
),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
int8
(
7
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
bscalar
f
(
b
,
8.23
)
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
float64
(
7.234
),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
float64
(
7.234
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dscalar
f
(
b
,
8
)
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
float32
(
7.234
),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
float32
(
7.234
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
fscalar
f
(
b
,
8
)
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
float
(
7.234
),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
float
(
7.234
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dscalar
f
(
b
,
8
)
assert
b
.
get_value
()
==
8
...
...
@@ -244,45 +244,45 @@ class Test_SharedVariable(unittest.TestCase):
f
(
b
,
8
)
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
self
.
assertRaises
(
TypeError
,
f
,
b
,
n
umpy
.
random
.
rand
(
5
,
5
))
b
=
shared
(
n
p
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
self
.
assertRaises
(
TypeError
,
f
,
b
,
n
p
.
random
.
rand
(
5
,
5
))
def
test_tensor_floatX
(
self
):
def
f
(
var
,
val
):
var
.
set_value
(
val
)
b
=
shared
(
n
umpy
.
int64
([
7
]),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
int64
([
7
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
lvector
f
(
b
,
[
8.23
])
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
int32
([
7
]),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
int32
([
7
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
ivector
f
(
b
,
[
8.23
])
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
int16
([
7
]),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
int16
([
7
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
wvector
f
(
b
,
[
8.23
])
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
int8
([
7
]),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
int8
([
7
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
bvector
f
(
b
,
[
8.23
])
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
float64
([
7.234
]),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
float64
([
7.234
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dvector
f
(
b
,
[
8
])
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
float32
([
7.234
]),
allow_downcast
=
True
)
b
=
shared
(
n
p
.
float32
([
7.234
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
fvector
f
(
b
,
[
8
])
assert
b
.
get_value
()
==
8
# n
umpy
.float([7.234]) don't work
# b = shared(n
umpy
.float([7.234]))
# n
p
.float([7.234]) don't work
# b = shared(n
p
.float([7.234]))
# assert b.type == theano.tensor.dvector
# f(b,[8])
...
...
@@ -291,15 +291,15 @@ class Test_SharedVariable(unittest.TestCase):
# assert b.type == theano.tensor.dvector
# f(b,[8])
b
=
shared
(
n
umpy
.
asarray
([
7.234
],
dtype
=
theano
.
config
.
floatX
),
b
=
shared
(
n
p
.
asarray
([
7.234
],
dtype
=
theano
.
config
.
floatX
),
allow_downcast
=
True
)
assert
b
.
dtype
==
theano
.
config
.
floatX
f
(
b
,
[
8
])
assert
b
.
get_value
()
==
8
b
=
shared
(
n
umpy
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
self
.
assertRaises
(
TypeError
,
f
,
b
,
n
umpy
.
random
.
rand
(
5
,
5
))
b
=
shared
(
n
p
.
zeros
((
5
,
5
),
dtype
=
'float32'
))
self
.
assertRaises
(
TypeError
,
f
,
b
,
n
p
.
random
.
rand
(
5
,
5
))
def
test_err_symbolic_variable
(
self
):
self
.
assertRaises
(
TypeError
,
shared
,
theano
.
tensor
.
ones
((
2
,
3
)))
shared
(
n
umpy
.
ones
((
2
,
4
)))
shared
(
n
p
.
ones
((
2
,
4
)))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论