Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ef9f6efc
提交
ef9f6efc
authored
3月 28, 2017
作者:
Frédéric Bastien
提交者:
GitHub
3月 28, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5763 from Amrithasuresh/master
Updated numpy as np #4218
上级
fd685902
77db5733
隐藏空白字符变更
内嵌
并排
正在显示
30 个修改的文件
包含
572 行增加
和
573 行删除
+572
-573
basic.py
theano/tensor/basic.py
+101
-101
blas_scipy.py
theano/tensor/blas_scipy.py
+6
-6
elemwise.py
theano/tensor/elemwise.py
+37
-37
extra_ops.py
theano/tensor/extra_ops.py
+4
-5
fourier.py
theano/tensor/fourier.py
+5
-5
io.py
theano/tensor/io.py
+5
-5
nlinalg.py
theano/tensor/nlinalg.py
+22
-22
opt.py
theano/tensor/opt.py
+43
-43
raw_random.py
theano/tensor/raw_random.py
+19
-19
shared_randomstreams.py
theano/tensor/shared_randomstreams.py
+6
-6
sharedvar.py
theano/tensor/sharedvar.py
+6
-6
pool.py
theano/tensor/signal/pool.py
+29
-29
test_conv.py
theano/tensor/signal/tests/test_conv.py
+6
-6
test_pool.py
theano/tensor/signal/tests/test_pool.py
+68
-68
slinalg.py
theano/tensor/slinalg.py
+17
-17
subtensor.py
theano/tensor/subtensor.py
+18
-18
mlp_test.py
theano/tensor/tests/mlp_test.py
+2
-2
test_basic.py
theano/tensor/tests/test_basic.py
+2
-2
test_blas_c.py
theano/tensor/tests/test_blas_c.py
+42
-42
test_casting.py
theano/tensor/tests/test_casting.py
+14
-14
test_nlinalg.py
theano/tensor/tests/test_nlinalg.py
+2
-2
type.py
theano/tensor/type.py
+29
-29
type_other.py
theano/tensor/type_other.py
+4
-4
utils.py
theano/tensor/utils.py
+2
-2
var.py
theano/tensor/var.py
+21
-21
xlogx.py
theano/tensor/xlogx.py
+3
-3
basic.py
theano/typed_list/basic.py
+4
-4
test_basic.py
theano/typed_list/tests/test_basic.py
+40
-40
test_opt.py
theano/typed_list/tests/test_opt.py
+8
-8
test_type.py
theano/typed_list/tests/test_type.py
+7
-7
没有找到文件。
theano/tensor/basic.py
浏览文件 @
ef9f6efc
...
@@ -5,7 +5,7 @@ from six.moves import builtins
...
@@ -5,7 +5,7 @@ from six.moves import builtins
import
sys
import
sys
import
warnings
import
warnings
import
numpy
import
numpy
as
np
from
six
import
integer_types
from
six
import
integer_types
from
six.moves
import
xrange
from
six.moves
import
xrange
import
numbers
import
numbers
...
@@ -72,12 +72,12 @@ def check_equal_numpy(x, y):
...
@@ -72,12 +72,12 @@ def check_equal_numpy(x, y):
Checks the dtype and shape if x and y are numpy.ndarray instances.
Checks the dtype and shape if x and y are numpy.ndarray instances.
"""
"""
if
isinstance
(
x
,
n
umpy
.
ndarray
)
and
isinstance
(
y
,
numpy
.
ndarray
):
if
isinstance
(
x
,
n
p
.
ndarray
)
and
isinstance
(
y
,
np
.
ndarray
):
return
(
x
.
dtype
==
y
.
dtype
and
x
.
shape
==
y
.
shape
and
return
(
x
.
dtype
==
y
.
dtype
and
x
.
shape
==
y
.
shape
and
n
umpy
.
all
(
abs
(
x
-
y
)
<
1e-10
))
n
p
.
all
(
abs
(
x
-
y
)
<
1e-10
))
elif
(
isinstance
(
x
,
n
umpy
.
random
.
RandomState
)
and
elif
(
isinstance
(
x
,
n
p
.
random
.
RandomState
)
and
isinstance
(
y
,
n
umpy
.
random
.
RandomState
)):
isinstance
(
y
,
n
p
.
random
.
RandomState
)):
return
python_all
(
n
umpy
.
all
(
a
==
b
)
for
a
,
b
in
return
python_all
(
n
p
.
all
(
a
==
b
)
for
a
,
b
in
izip
(
x
.
__getstate__
(),
y
.
__getstate__
()))
izip
(
x
.
__getstate__
(),
y
.
__getstate__
()))
else
:
else
:
return
x
==
y
return
x
==
y
...
@@ -348,15 +348,15 @@ def _get_atol_rtol(a, b):
...
@@ -348,15 +348,15 @@ def _get_atol_rtol(a, b):
def
_allclose
(
a
,
b
,
rtol
=
None
,
atol
=
None
):
def
_allclose
(
a
,
b
,
rtol
=
None
,
atol
=
None
):
a
=
n
umpy
.
asarray
(
a
)
a
=
n
p
.
asarray
(
a
)
b
=
n
umpy
.
asarray
(
b
)
b
=
n
p
.
asarray
(
b
)
atol_
,
rtol_
=
_get_atol_rtol
(
a
,
b
)
atol_
,
rtol_
=
_get_atol_rtol
(
a
,
b
)
if
rtol
is
not
None
:
if
rtol
is
not
None
:
rtol_
=
rtol
rtol_
=
rtol
if
atol
is
not
None
:
if
atol
is
not
None
:
atol_
=
atol
atol_
=
atol
return
n
umpy
.
allclose
(
a
,
b
,
atol
=
atol_
,
rtol
=
rtol_
)
return
n
p
.
allclose
(
a
,
b
,
atol
=
atol_
,
rtol
=
rtol_
)
class
NotScalarConstantError
(
Exception
):
class
NotScalarConstantError
(
Exception
):
...
@@ -387,10 +387,10 @@ def numpy_scalar(data):
...
@@ -387,10 +387,10 @@ def numpy_scalar(data):
if
(
data
.
ndim
>
0
and
if
(
data
.
ndim
>
0
and
(
len
(
data
.
shape
)
==
0
or
(
len
(
data
.
shape
)
==
0
or
builtins
.
max
(
data
.
shape
)
==
0
)):
builtins
.
max
(
data
.
shape
)
==
0
)):
assert
n
umpy
.
all
(
numpy
.
array
([])
==
data
)
assert
n
p
.
all
(
np
.
array
([])
==
data
)
raise
EmptyConstantError
()
raise
EmptyConstantError
()
try
:
try
:
n
umpy
.
complex
(
data
)
# works for all numeric scalars
n
p
.
complex
(
data
)
# works for all numeric scalars
return
data
return
data
except
Exception
:
except
Exception
:
raise
NotScalarConstantError
(
raise
NotScalarConstantError
(
...
@@ -444,10 +444,10 @@ def get_scalar_constant_value(orig_v, elemwise=True,
...
@@ -444,10 +444,10 @@ def get_scalar_constant_value(orig_v, elemwise=True,
# to depend on passing it None)
# to depend on passing it None)
raise
NotScalarConstantError
()
raise
NotScalarConstantError
()
if
isinstance
(
v
,
(
n
umpy
.
integer
,
integer_types
,
float
)):
if
isinstance
(
v
,
(
n
p
.
integer
,
integer_types
,
float
)):
return
n
umpy
.
asarray
(
v
)
return
n
p
.
asarray
(
v
)
if
isinstance
(
v
,
n
umpy
.
ndarray
):
if
isinstance
(
v
,
n
p
.
ndarray
):
return
numpy_scalar
(
v
)
.
copy
()
return
numpy_scalar
(
v
)
.
copy
()
if
isinstance
(
v
,
Constant
):
if
isinstance
(
v
,
Constant
):
...
@@ -470,11 +470,11 @@ def get_scalar_constant_value(orig_v, elemwise=True,
...
@@ -470,11 +470,11 @@ def get_scalar_constant_value(orig_v, elemwise=True,
i
=
v
.
owner
.
op
.
i
i
=
v
.
owner
.
op
.
i
inp
=
v
.
owner
.
inputs
[
0
]
inp
=
v
.
owner
.
inputs
[
0
]
if
isinstance
(
inp
,
Constant
):
if
isinstance
(
inp
,
Constant
):
return
n
umpy
.
asarray
(
inp
.
data
.
shape
[
i
])
return
n
p
.
asarray
(
inp
.
data
.
shape
[
i
])
# The shape of a broadcastable dimension is 1
# The shape of a broadcastable dimension is 1
if
(
hasattr
(
inp
.
type
,
'broadcastable'
)
and
if
(
hasattr
(
inp
.
type
,
'broadcastable'
)
and
inp
.
type
.
broadcastable
[
i
]):
inp
.
type
.
broadcastable
[
i
]):
return
n
umpy
.
asarray
(
1
)
return
n
p
.
asarray
(
1
)
# Don't act as the constant_folding optimization here as this
# Don't act as the constant_folding optimization here as this
# fct is used too early in the optimization phase. This would
# fct is used too early in the optimization phase. This would
...
@@ -639,7 +639,7 @@ def get_scalar_constant_value(orig_v, elemwise=True,
...
@@ -639,7 +639,7 @@ def get_scalar_constant_value(orig_v, elemwise=True,
raise
ValueError
(
msg
)
raise
ValueError
(
msg
)
if
gp_broadcastable
[
idx
]:
if
gp_broadcastable
[
idx
]:
return
n
umpy
.
asarray
(
1
)
return
n
p
.
asarray
(
1
)
raise
NotScalarConstantError
(
v
)
raise
NotScalarConstantError
(
v
)
...
@@ -1002,7 +1002,7 @@ class TensorFromScalar(Op):
...
@@ -1002,7 +1002,7 @@ class TensorFromScalar(Op):
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
s
,
=
inp
s
,
=
inp
out
,
=
out_
out
,
=
out_
out
[
0
]
=
n
umpy
.
asarray
(
s
)
out
[
0
]
=
n
p
.
asarray
(
s
)
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
return
[()]
return
[()]
...
@@ -1216,23 +1216,23 @@ class MaxAndArgmax(Op):
...
@@ -1216,23 +1216,23 @@ class MaxAndArgmax(Op):
axes
=
tuple
(
range
(
x
.
ndim
))
axes
=
tuple
(
range
(
x
.
ndim
))
else
:
else
:
axes
=
tuple
(
int
(
ax
)
for
ax
in
axes
)
axes
=
tuple
(
int
(
ax
)
for
ax
in
axes
)
max
[
0
]
=
theano
.
_asarray
(
n
umpy
.
max
(
x
,
axes
),
max
[
0
]
=
theano
.
_asarray
(
n
p
.
max
(
x
,
axes
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
dtype
=
node
.
outputs
[
0
]
.
dtype
)
# Numpy does not support multiple axes for argmax
# Numpy does not support multiple axes for argmax
# Work around
# Work around
keep_axes
=
n
umpy
.
array
([
i
for
i
in
range
(
x
.
ndim
)
if
i
not
in
axes
],
keep_axes
=
n
p
.
array
([
i
for
i
in
range
(
x
.
ndim
)
if
i
not
in
axes
],
dtype
=
'int64'
)
dtype
=
'int64'
)
# Not-reduced axes in front
# Not-reduced axes in front
transposed_x
=
n
umpy
.
transpose
(
x
,
numpy
.
concatenate
((
keep_axes
,
axes
)))
transposed_x
=
n
p
.
transpose
(
x
,
np
.
concatenate
((
keep_axes
,
axes
)))
kept_shape
=
transposed_x
.
shape
[:
len
(
keep_axes
)]
kept_shape
=
transposed_x
.
shape
[:
len
(
keep_axes
)]
reduced_shape
=
transposed_x
.
shape
[
len
(
keep_axes
):]
reduced_shape
=
transposed_x
.
shape
[
len
(
keep_axes
):]
# Numpy.prod returns 1.0 when arg is empty, so we cast it to int64
# Numpy.prod returns 1.0 when arg is empty, so we cast it to int64
# Otherwise reshape would complain citing float arg
# Otherwise reshape would complain citing float arg
new_shape
=
kept_shape
+
(
n
umpy
.
prod
(
reduced_shape
,
dtype
=
'int64'
),)
new_shape
=
kept_shape
+
(
n
p
.
prod
(
reduced_shape
,
dtype
=
'int64'
),)
reshaped_x
=
transposed_x
.
reshape
(
new_shape
)
reshaped_x
=
transposed_x
.
reshape
(
new_shape
)
max_idx
[
0
]
=
theano
.
_asarray
(
n
umpy
.
argmax
(
reshaped_x
,
axis
=-
1
),
max_idx
[
0
]
=
theano
.
_asarray
(
n
p
.
argmax
(
reshaped_x
,
axis
=-
1
),
dtype
=
'int64'
)
dtype
=
'int64'
)
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
...
@@ -1399,11 +1399,11 @@ class Argmax(Op):
...
@@ -1399,11 +1399,11 @@ class Argmax(Op):
def
make_node
(
self
,
x
,
axis
=
None
):
def
make_node
(
self
,
x
,
axis
=
None
):
x
=
_as_tensor_variable
(
x
)
x
=
_as_tensor_variable
(
x
)
if
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
if
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
axis
=
[
int
(
axis
)]
axis
=
[
int
(
axis
)]
elif
isinstance
(
axis
,
n
umpy
.
ndarray
)
and
axis
.
ndim
==
0
:
elif
isinstance
(
axis
,
n
p
.
ndarray
)
and
axis
.
ndim
==
0
:
axis
=
[
int
(
axis
)]
axis
=
[
int
(
axis
)]
elif
isinstance
(
axis
,
(
tuple
,
list
,
n
umpy
.
ndarray
)):
elif
isinstance
(
axis
,
(
tuple
,
list
,
n
p
.
ndarray
)):
axis
=
[
int
(
a
)
for
a
in
axis
]
axis
=
[
int
(
a
)
for
a
in
axis
]
if
axis
==
list
(
range
(
x
.
type
.
ndim
)):
if
axis
==
list
(
range
(
x
.
type
.
ndim
)):
axis
=
None
axis
=
None
...
@@ -1415,11 +1415,11 @@ class Argmax(Op):
...
@@ -1415,11 +1415,11 @@ class Argmax(Op):
"Argmax needs a constant axis. Got
%
s"
%
axis
)
"Argmax needs a constant axis. Got
%
s"
%
axis
)
else
:
else
:
assert
axis
.
dtype
in
integer_dtypes
assert
axis
.
dtype
in
integer_dtypes
if
isinstance
(
axis
.
data
,
(
integer_types
,
n
umpy
.
integer
))
or
\
if
isinstance
(
axis
.
data
,
(
integer_types
,
n
p
.
integer
))
or
\
(
isinstance
(
axis
.
data
,
n
umpy
.
ndarray
)
and
(
isinstance
(
axis
.
data
,
n
p
.
ndarray
)
and
axis
.
data
.
ndim
==
0
):
axis
.
data
.
ndim
==
0
):
axis
=
[
int
(
axis
.
data
)]
axis
=
[
int
(
axis
.
data
)]
elif
isinstance
(
axis
.
data
,
(
list
,
n
umpy
.
ndarray
)):
elif
isinstance
(
axis
.
data
,
(
list
,
n
p
.
ndarray
)):
axis
=
[
int
(
i
)
for
i
in
axis
.
data
]
axis
=
[
int
(
i
)
for
i
in
axis
.
data
]
# Make axis entries non-negative, and sort them
# Make axis entries non-negative, and sort them
...
@@ -1466,17 +1466,17 @@ class Argmax(Op):
...
@@ -1466,17 +1466,17 @@ class Argmax(Op):
# Numpy does not support multiple axes for argmax
# Numpy does not support multiple axes for argmax
# Work around
# Work around
keep_axes
=
n
umpy
.
array
([
i
for
i
in
range
(
x
.
ndim
)
if
i
not
in
axes
],
keep_axes
=
n
p
.
array
([
i
for
i
in
range
(
x
.
ndim
)
if
i
not
in
axes
],
dtype
=
'int64'
)
dtype
=
'int64'
)
# Not-reduced axes in front
# Not-reduced axes in front
transposed_x
=
n
umpy
.
transpose
(
x
,
numpy
.
concatenate
((
keep_axes
,
transposed_x
=
n
p
.
transpose
(
x
,
np
.
concatenate
((
keep_axes
,
axes
)))
axes
)))
kept_shape
=
transposed_x
.
shape
[:
len
(
keep_axes
)]
kept_shape
=
transposed_x
.
shape
[:
len
(
keep_axes
)]
reduced_shape
=
transposed_x
.
shape
[
len
(
keep_axes
):]
reduced_shape
=
transposed_x
.
shape
[
len
(
keep_axes
):]
new_shape
=
kept_shape
+
(
n
umpy
.
prod
(
reduced_shape
),)
new_shape
=
kept_shape
+
(
n
p
.
prod
(
reduced_shape
),)
reshaped_x
=
transposed_x
.
reshape
(
new_shape
)
reshaped_x
=
transposed_x
.
reshape
(
new_shape
)
max_idx
[
0
]
=
theano
.
_asarray
(
n
umpy
.
argmax
(
reshaped_x
,
axis
=-
1
),
max_idx
[
0
]
=
theano
.
_asarray
(
n
p
.
argmax
(
reshaped_x
,
axis
=-
1
),
dtype
=
'int64'
)
dtype
=
'int64'
)
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
...
@@ -1562,9 +1562,9 @@ def makeKeepDims(x, y, axis):
...
@@ -1562,9 +1562,9 @@ def makeKeepDims(x, y, axis):
if
axis
is
None
:
if
axis
is
None
:
axis
=
list
(
range
(
x
.
type
.
ndim
))
axis
=
list
(
range
(
x
.
type
.
ndim
))
elif
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
axis
=
[
axis
]
axis
=
[
axis
]
elif
isinstance
(
axis
,
n
umpy
.
ndarray
)
and
axis
.
ndim
==
0
:
elif
isinstance
(
axis
,
n
p
.
ndarray
)
and
axis
.
ndim
==
0
:
axis
=
[
int
(
axis
)]
axis
=
[
int
(
axis
)]
else
:
else
:
axis
=
[
int
(
a
)
for
a
in
axis
]
axis
=
[
int
(
a
)
for
a
in
axis
]
...
@@ -1609,10 +1609,10 @@ def max_and_argmax(a, axis=None, keepdims=False):
...
@@ -1609,10 +1609,10 @@ def max_and_argmax(a, axis=None, keepdims=False):
a
=
as_tensor_variable
(
a
)
a
=
as_tensor_variable
(
a
)
if
axis
is
None
:
if
axis
is
None
:
axis
=
list
(
range
(
a
.
type
.
ndim
))
axis
=
list
(
range
(
a
.
type
.
ndim
))
elif
(
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
))
or
elif
(
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
))
or
(
isinstance
(
axis
,
n
umpy
.
ndarray
)
and
axis
.
ndim
==
0
)):
(
isinstance
(
axis
,
n
p
.
ndarray
)
and
axis
.
ndim
==
0
)):
axis
=
[
int
(
axis
)]
axis
=
[
int
(
axis
)]
elif
isinstance
(
axis
,
(
tuple
,
list
,
n
umpy
.
ndarray
)):
elif
isinstance
(
axis
,
(
tuple
,
list
,
n
p
.
ndarray
)):
axis
=
[
int
(
i
)
for
i
in
axis
]
axis
=
[
int
(
i
)
for
i
in
axis
]
elif
isinstance
(
axis
,
Variable
):
elif
isinstance
(
axis
,
Variable
):
if
NoneConst
.
equals
(
axis
):
if
NoneConst
.
equals
(
axis
):
...
@@ -1621,10 +1621,10 @@ def max_and_argmax(a, axis=None, keepdims=False):
...
@@ -1621,10 +1621,10 @@ def max_and_argmax(a, axis=None, keepdims=False):
raise
TypeError
(
"max and argmax computation needs a constant axis. Got
%
s"
%
axis
)
raise
TypeError
(
"max and argmax computation needs a constant axis. Got
%
s"
%
axis
)
else
:
else
:
assert
axis
.
dtype
in
integer_dtypes
assert
axis
.
dtype
in
integer_dtypes
if
(
isinstance
(
axis
.
data
,
(
integer_types
,
n
umpy
.
integer
))
or
if
(
isinstance
(
axis
.
data
,
(
integer_types
,
n
p
.
integer
))
or
(
isinstance
(
axis
.
data
,
n
umpy
.
ndarray
)
and
axis
.
data
.
ndim
==
0
)):
(
isinstance
(
axis
.
data
,
n
p
.
ndarray
)
and
axis
.
data
.
ndim
==
0
)):
axis
=
[
int
(
axis
.
data
)]
axis
=
[
int
(
axis
.
data
)]
elif
isinstance
(
axis
.
data
,
(
list
,
n
umpy
.
ndarray
)):
elif
isinstance
(
axis
.
data
,
(
list
,
n
p
.
ndarray
)):
axis
=
[
int
(
i
)
for
i
in
axis
.
data
]
axis
=
[
int
(
i
)
for
i
in
axis
.
data
]
if
len
(
axis
)
==
0
:
if
len
(
axis
)
==
0
:
axis
=
list
(
range
(
a
.
type
.
ndim
))
axis
=
list
(
range
(
a
.
type
.
ndim
))
...
@@ -1838,7 +1838,7 @@ def isnan(a):
...
@@ -1838,7 +1838,7 @@ def isnan(a):
"""isnan(a)"""
"""isnan(a)"""
a
=
as_tensor_variable
(
a
)
a
=
as_tensor_variable
(
a
)
if
a
.
dtype
in
discrete_dtypes
:
if
a
.
dtype
in
discrete_dtypes
:
return
alloc
(
n
umpy
.
asarray
(
False
,
dtype
=
"bool"
),
return
alloc
(
n
p
.
asarray
(
False
,
dtype
=
"bool"
),
*
[
a
.
shape
[
i
]
for
i
in
range
(
a
.
ndim
)])
*
[
a
.
shape
[
i
]
for
i
in
range
(
a
.
ndim
)])
return
isnan_
(
a
)
return
isnan_
(
a
)
...
@@ -1857,7 +1857,7 @@ def isinf(a):
...
@@ -1857,7 +1857,7 @@ def isinf(a):
"""isinf(a)"""
"""isinf(a)"""
a
=
as_tensor_variable
(
a
)
a
=
as_tensor_variable
(
a
)
if
a
.
dtype
in
discrete_dtypes
:
if
a
.
dtype
in
discrete_dtypes
:
return
alloc
(
n
umpy
.
asarray
(
False
,
dtype
=
"bool"
),
return
alloc
(
n
p
.
asarray
(
False
,
dtype
=
"bool"
),
*
[
a
.
shape
[
i
]
for
i
in
range
(
a
.
ndim
)])
*
[
a
.
shape
[
i
]
for
i
in
range
(
a
.
ndim
)])
return
isinf_
(
a
)
return
isinf_
(
a
)
...
@@ -2426,7 +2426,7 @@ def zeros(shape, dtype=None):
...
@@ -2426,7 +2426,7 @@ def zeros(shape, dtype=None):
shape
=
[
shape
]
shape
=
[
shape
]
if
dtype
is
None
:
if
dtype
is
None
:
dtype
=
config
.
floatX
dtype
=
config
.
floatX
return
alloc
(
n
umpy
.
array
(
0
,
dtype
=
dtype
),
*
shape
)
return
alloc
(
n
p
.
array
(
0
,
dtype
=
dtype
),
*
shape
)
def
ones
(
shape
,
dtype
=
None
):
def
ones
(
shape
,
dtype
=
None
):
...
@@ -2437,7 +2437,7 @@ def ones(shape, dtype=None):
...
@@ -2437,7 +2437,7 @@ def ones(shape, dtype=None):
shape
=
[
shape
]
shape
=
[
shape
]
if
dtype
is
None
:
if
dtype
is
None
:
dtype
=
config
.
floatX
dtype
=
config
.
floatX
return
alloc
(
n
umpy
.
array
(
1
,
dtype
=
dtype
),
*
shape
)
return
alloc
(
n
p
.
array
(
1
,
dtype
=
dtype
),
*
shape
)
class
Nonzero
(
gof
.
Op
):
class
Nonzero
(
gof
.
Op
):
...
@@ -2481,11 +2481,11 @@ class Nonzero(gof.Op):
...
@@ -2481,11 +2481,11 @@ class Nonzero(gof.Op):
a
=
inp
[
0
]
a
=
inp
[
0
]
out
,
=
out_
out
,
=
out_
result_tuple
=
n
umpy
.
nonzero
(
a
)
result_tuple
=
n
p
.
nonzero
(
a
)
if
len
(
result_tuple
[
0
])
>
0
:
if
len
(
result_tuple
[
0
])
>
0
:
result
=
n
umpy
.
vstack
(
result_tuple
)
result
=
n
p
.
vstack
(
result_tuple
)
else
:
else
:
result
=
n
umpy
.
zeros
((
len
(
result_tuple
),
0
))
result
=
n
p
.
zeros
((
len
(
result_tuple
),
0
))
out
[
0
]
=
result
.
astype
(
'int64'
)
out
[
0
]
=
result
.
astype
(
'int64'
)
...
@@ -2627,7 +2627,7 @@ class Tri(gof.Op):
...
@@ -2627,7 +2627,7 @@ class Tri(gof.Op):
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
N
,
M
,
k
=
inp
N
,
M
,
k
=
inp
out
,
=
out_
out
,
=
out_
out
[
0
]
=
n
umpy
.
tri
(
N
,
M
,
k
,
dtype
=
self
.
dtype
)
out
[
0
]
=
n
p
.
tri
(
N
,
M
,
k
,
dtype
=
self
.
dtype
)
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
out_shape
=
[
node
.
inputs
[
0
],
node
.
inputs
[
1
]]
out_shape
=
[
node
.
inputs
[
0
],
node
.
inputs
[
1
]]
...
@@ -2738,7 +2738,7 @@ class Eye(gof.Op):
...
@@ -2738,7 +2738,7 @@ class Eye(gof.Op):
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
n
,
m
,
k
=
inp
n
,
m
,
k
=
inp
out
,
=
out_
out
,
=
out_
out
[
0
]
=
n
umpy
.
eye
(
n
,
m
,
k
,
dtype
=
self
.
dtype
)
out
[
0
]
=
n
p
.
eye
(
n
,
m
,
k
,
dtype
=
self
.
dtype
)
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
out_shape
=
[
node
.
inputs
[
0
],
node
.
inputs
[
1
]]
out_shape
=
[
node
.
inputs
[
0
],
node
.
inputs
[
1
]]
...
@@ -2853,9 +2853,9 @@ class Alloc(gof.Op):
...
@@ -2853,9 +2853,9 @@ class Alloc(gof.Op):
sh
=
tuple
([
int
(
i
)
for
i
in
inputs
[
1
:]])
sh
=
tuple
([
int
(
i
)
for
i
in
inputs
[
1
:]])
if
out
[
0
]
is
None
or
out
[
0
]
.
shape
!=
sh
:
if
out
[
0
]
is
None
or
out
[
0
]
.
shape
!=
sh
:
if
v
.
size
==
1
and
v
.
item
()
==
0
:
if
v
.
size
==
1
and
v
.
item
()
==
0
:
out
[
0
]
=
n
umpy
.
zeros
(
sh
,
dtype
=
v
.
dtype
)
out
[
0
]
=
n
p
.
zeros
(
sh
,
dtype
=
v
.
dtype
)
else
:
else
:
out
[
0
]
=
n
umpy
.
empty
(
sh
,
dtype
=
v
.
dtype
)
out
[
0
]
=
n
p
.
empty
(
sh
,
dtype
=
v
.
dtype
)
out
[
0
][
...
]
=
v
# broadcast v to fill us up
out
[
0
][
...
]
=
v
# broadcast v to fill us up
else
:
else
:
# reuse the allocated memory.
# reuse the allocated memory.
...
@@ -3139,8 +3139,8 @@ class Mean(elemwise.CAReduce):
...
@@ -3139,8 +3139,8 @@ class Mean(elemwise.CAReduce):
axis
=
self
.
axis
[
0
]
axis
=
self
.
axis
[
0
]
# numpy.asarray is needed as otherwise we can end up with a
# numpy.asarray is needed as otherwise we can end up with a
# numpy scalar.
# numpy scalar.
output
[
0
]
=
n
umpy
.
asarray
(
numpy
.
mean
(
input
,
dtype
=
'float64'
,
output
[
0
]
=
n
p
.
asarray
(
np
.
mean
(
input
,
dtype
=
'float64'
,
axis
=
axis
))
axis
=
axis
))
def
c_code
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
def
c_code
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
if
self
.
axis
is
not
None
:
if
self
.
axis
is
not
None
:
...
@@ -3232,9 +3232,9 @@ def mean(input, axis=None, dtype=None, op=False, keepdims=False,
...
@@ -3232,9 +3232,9 @@ def mean(input, axis=None, dtype=None, op=False, keepdims=False,
if
axis
is
None
:
if
axis
is
None
:
axis
=
list
(
range
(
input
.
ndim
))
axis
=
list
(
range
(
input
.
ndim
))
elif
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
axis
=
[
axis
]
axis
=
[
axis
]
elif
isinstance
(
axis
,
n
umpy
.
ndarray
)
and
axis
.
ndim
==
0
:
elif
isinstance
(
axis
,
n
p
.
ndarray
)
and
axis
.
ndim
==
0
:
axis
=
[
int
(
axis
)]
axis
=
[
int
(
axis
)]
else
:
else
:
axis
=
[
int
(
a
)
for
a
in
axis
]
axis
=
[
int
(
a
)
for
a
in
axis
]
...
@@ -3291,9 +3291,9 @@ def var(input, axis=None, ddof=0, keepdims=False, corrected=False):
...
@@ -3291,9 +3291,9 @@ def var(input, axis=None, ddof=0, keepdims=False, corrected=False):
input_ndim
=
input
.
type
.
ndim
input_ndim
=
input
.
type
.
ndim
if
axis
is
None
:
if
axis
is
None
:
axis
=
list
(
range
(
input_ndim
))
axis
=
list
(
range
(
input_ndim
))
elif
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
axis
=
[
axis
]
axis
=
[
axis
]
elif
isinstance
(
axis
,
n
umpy
.
ndarray
)
and
axis
.
ndim
==
0
:
elif
isinstance
(
axis
,
n
p
.
ndarray
)
and
axis
.
ndim
==
0
:
axis
=
[
int
(
axis
)]
axis
=
[
int
(
axis
)]
else
:
else
:
axis
=
[
int
(
a
)
for
a
in
axis
]
axis
=
[
int
(
a
)
for
a
in
axis
]
...
@@ -3617,7 +3617,7 @@ def batched_dot(a, b):
...
@@ -3617,7 +3617,7 @@ def batched_dot(a, b):
return
a
*
b
.
dimshuffle
(
*
([
0
]
+
[
"x"
]
*
(
a
.
ndim
-
1
)))
return
a
*
b
.
dimshuffle
(
*
([
0
]
+
[
"x"
]
*
(
a
.
ndim
-
1
)))
elif
a
.
ndim
>
3
or
b
.
ndim
>
3
:
elif
a
.
ndim
>
3
or
b
.
ndim
>
3
:
return
batched_tensordot
(
return
batched_tensordot
(
a
,
b
,
[[
a
.
ndim
-
1
],
[
n
umpy
.
maximum
(
1
,
b
.
ndim
-
2
)]])
a
,
b
,
[[
a
.
ndim
-
1
],
[
n
p
.
maximum
(
1
,
b
.
ndim
-
2
)]])
else
:
else
:
# avoid circular import
# avoid circular import
return
theano
.
tensor
.
blas
.
BatchedDot
()(
a
,
b
)
return
theano
.
tensor
.
blas
.
BatchedDot
()(
a
,
b
)
...
@@ -3736,9 +3736,9 @@ class Split(Op):
...
@@ -3736,9 +3736,9 @@ class Split(Op):
raise
ValueError
(
'In Split.perform(), len(splits) != len_splits.'
,
raise
ValueError
(
'In Split.perform(), len(splits) != len_splits.'
,
(
len
(
splits
),
self
.
len_splits
))
(
len
(
splits
),
self
.
len_splits
))
if
n
umpy
.
sum
(
splits
)
!=
len_along_axis
:
if
n
p
.
sum
(
splits
)
!=
len_along_axis
:
raise
ValueError
(
'The splits sum to
%
s, expected
%
s'
%
raise
ValueError
(
'The splits sum to
%
s, expected
%
s'
%
(
n
umpy
.
sum
(
splits
),
len_along_axis
))
(
n
p
.
sum
(
splits
),
len_along_axis
))
if
python_any
([
nb
<
0
for
nb
in
splits
]):
if
python_any
([
nb
<
0
for
nb
in
splits
]):
raise
ValueError
(
'Split: you tried to make an ndarray with a '
raise
ValueError
(
'Split: you tried to make an ndarray with a '
'negative number of elements.'
)
'negative number of elements.'
)
...
@@ -3828,8 +3828,8 @@ class Split(Op):
...
@@ -3828,8 +3828,8 @@ class Split(Op):
outputs_pointers
=
'&'
+
(
', &'
.
join
(
outputs
))
outputs_pointers
=
'&'
+
(
', &'
.
join
(
outputs
))
x
,
axis
,
splits
=
inputs
x
,
axis
,
splits
=
inputs
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
x_typenum
=
n
umpy
.
dtype
(
node
.
inputs
[
0
]
.
dtype
)
.
num
x_typenum
=
n
p
.
dtype
(
node
.
inputs
[
0
]
.
dtype
)
.
num
x_itemsize
=
n
umpy
.
dtype
(
node
.
inputs
[
0
]
.
dtype
)
.
itemsize
x_itemsize
=
n
p
.
dtype
(
node
.
inputs
[
0
]
.
dtype
)
.
itemsize
axis_dtype
=
node
.
inputs
[
1
]
.
type
.
dtype_specs
()[
1
]
axis_dtype
=
node
.
inputs
[
1
]
.
type
.
dtype_specs
()[
1
]
splits_dtype
=
node
.
inputs
[
2
]
.
type
.
dtype_specs
()[
1
]
splits_dtype
=
node
.
inputs
[
2
]
.
type
.
dtype_specs
()[
1
]
expected_splits_count
=
self
.
len_splits
expected_splits_count
=
self
.
len_splits
...
@@ -4187,7 +4187,7 @@ class Join(Op):
...
@@ -4187,7 +4187,7 @@ class Join(Op):
view
=
self
.
view
view
=
self
.
view
axis
,
tensors
=
axis_and_tensors
[
0
],
axis_and_tensors
[
1
:]
axis
,
tensors
=
axis_and_tensors
[
0
],
axis_and_tensors
[
1
:]
# we check these tensors for being empty.
# we check these tensors for being empty.
if
(
view
!=
-
1
)
and
n
umpy
.
all
(
if
(
view
!=
-
1
)
and
n
p
.
all
(
[
tensor
.
shape
[
axis
]
==
0
for
tensor
in
[
tensor
.
shape
[
axis
]
==
0
for
tensor
in
tensors
[
0
:
view
]
+
tensors
[
view
+
1
:]]):
tensors
[
0
:
view
]
+
tensors
[
view
+
1
:]]):
out
[
0
]
=
tensors
[
view
]
out
[
0
]
=
tensors
[
view
]
...
@@ -4198,7 +4198,7 @@ class Join(Op):
...
@@ -4198,7 +4198,7 @@ class Join(Op):
raise
IndexError
(
"Join axis
%
d out of bounds [0,
%
d)"
%
raise
IndexError
(
"Join axis
%
d out of bounds [0,
%
d)"
%
(
axis
,
ndim
))
(
axis
,
ndim
))
out
[
0
]
=
theano
.
_asarray
(
n
umpy
.
concatenate
(
tensors
,
axis
=
axis
),
out
[
0
]
=
theano
.
_asarray
(
n
p
.
concatenate
(
tensors
,
axis
=
axis
),
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
...
@@ -4584,9 +4584,9 @@ def stack(*tensors, **kwargs):
...
@@ -4584,9 +4584,9 @@ def stack(*tensors, **kwargs):
# And DebugMode can't detect error in this code as it is not in an
# And DebugMode can't detect error in this code as it is not in an
# optimization.
# optimization.
# See ticket #660
# See ticket #660
if
n
umpy
.
all
(
if
n
p
.
all
(
[
# in case there is direct int in tensors.
[
# in case there is direct int in tensors.
isinstance
(
t
,
(
n
umpy
.
number
,
float
,
integer_types
,
isinstance
(
t
,
(
n
p
.
number
,
float
,
integer_types
,
python_complex
))
or
python_complex
))
or
(
isinstance
(
t
,
Variable
)
and
(
isinstance
(
t
,
Variable
)
and
isinstance
(
t
.
type
,
TensorType
)
and
isinstance
(
t
.
type
,
TensorType
)
and
...
@@ -4669,7 +4669,7 @@ def get_vector_length(v):
...
@@ -4669,7 +4669,7 @@ def get_vector_length(v):
v
.
owner
.
inputs
,
v
.
owner
.
op
.
idx_list
)[
0
]
.
step
)
v
.
owner
.
inputs
,
v
.
owner
.
op
.
idx_list
)[
0
]
.
step
)
ndim
=
v
.
owner
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
.
ndim
ndim
=
v
.
owner
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
.
ndim
types
=
(
numbers
.
Integral
,
n
umpy
.
integer
)
types
=
(
numbers
.
Integral
,
n
p
.
integer
)
if
start
is
None
:
if
start
is
None
:
start
=
0
start
=
0
elif
isinstance
(
start
,
types
)
and
start
<
0
:
elif
isinstance
(
start
,
types
)
and
start
<
0
:
...
@@ -4790,7 +4790,7 @@ class Reshape(Op):
...
@@ -4790,7 +4790,7 @@ class Reshape(Op):
' length
%
i'
' length
%
i'
', should be
%
i'
%
(
len
(
shp
),
self
.
ndim
),
shp
)
', should be
%
i'
%
(
len
(
shp
),
self
.
ndim
),
shp
)
try
:
try
:
out
[
0
]
=
n
umpy
.
reshape
(
x
,
shp
)
out
[
0
]
=
n
p
.
reshape
(
x
,
shp
)
except
Exception
:
except
Exception
:
raise
ValueError
(
'Cannot reshape input of shape
%
s to shape
%
s'
%
raise
ValueError
(
'Cannot reshape input of shape
%
s to shape
%
s'
%
(
x
.
shape
,
shp
))
(
x
.
shape
,
shp
))
...
@@ -4976,12 +4976,12 @@ class Flatten(Op):
...
@@ -4976,12 +4976,12 @@ class Flatten(Op):
try
:
try
:
out
[
0
]
=
x
.
reshape
(
x
.
size
)
out
[
0
]
=
x
.
reshape
(
x
.
size
)
except
AttributeError
:
except
AttributeError
:
out
[
0
]
=
x
.
reshape
((
n
umpy
.
prod
(
x
.
shape
),))
out
[
0
]
=
x
.
reshape
((
n
p
.
prod
(
x
.
shape
),))
elif
outdim
==
len
(
x
.
shape
):
elif
outdim
==
len
(
x
.
shape
):
out
[
0
]
=
x
out
[
0
]
=
x
else
:
else
:
newshape
=
(
x
.
shape
[:
outdim
-
1
]
+
newshape
=
(
x
.
shape
[:
outdim
-
1
]
+
(
n
umpy
.
prod
(
x
.
shape
[
outdim
-
1
:]),))
(
n
p
.
prod
(
x
.
shape
[
outdim
-
1
:]),))
out
[
0
]
=
x
.
reshape
(
newshape
)
out
[
0
]
=
x
.
reshape
(
newshape
)
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
...
@@ -5196,16 +5196,16 @@ class Tile(Op):
...
@@ -5196,16 +5196,16 @@ class Tile(Op):
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
x
,
reps
=
inp
x
,
reps
=
inp
out
,
=
out_
out
,
=
out_
res
=
n
umpy
.
tile
(
x
,
reps
)
res
=
n
p
.
tile
(
x
,
reps
)
if
res
.
ndim
!=
self
.
ndim
:
if
res
.
ndim
!=
self
.
ndim
:
raise
ValueError
(
raise
ValueError
(
'Tile.perform produced incorrect number of dimensions'
)
'Tile.perform produced incorrect number of dimensions'
)
if
(
n
umpy
.
asarray
(
reps
)
==
1
)
.
all
():
if
(
n
p
.
asarray
(
reps
)
==
1
)
.
all
():
# In that case, some NumPy version return a view! As this
# In that case, some NumPy version return a view! As this
# op isn't declared as inplace, we need to check that and
# op isn't declared as inplace, we need to check that and
# copy the data.
# copy the data.
if
n
umpy
.
may_share_memory
(
res
,
x
):
if
n
p
.
may_share_memory
(
res
,
x
):
res
=
res
.
copy
()
res
=
res
.
copy
()
out
[
0
]
=
res
out
[
0
]
=
res
...
@@ -5289,9 +5289,9 @@ def tile(x, reps, ndim=None):
...
@@ -5289,9 +5289,9 @@ def tile(x, reps, ndim=None):
else
:
else
:
if
ndim
is
not
None
and
len
(
reps
)
>
ndim
:
if
ndim
is
not
None
and
len
(
reps
)
>
ndim
:
raise
ValueError
(
"len(reps) should be equal or less than ndim"
)
raise
ValueError
(
"len(reps) should be equal or less than ndim"
)
if
not
n
umpy
.
all
([
isinstance
(
r
,
integer_types
)
or
if
not
n
p
.
all
([
isinstance
(
r
,
integer_types
)
or
(
isinstance
(
r
,
TensorVariable
)
and
(
isinstance
(
r
,
TensorVariable
)
and
r
.
dtype
in
theano
.
tensor
.
discrete_dtypes
)
for
r
in
reps
]):
r
.
dtype
in
theano
.
tensor
.
discrete_dtypes
)
for
r
in
reps
]):
raise
ValueError
(
"elements of reps must be scalars of integer dtype"
)
raise
ValueError
(
"elements of reps must be scalars of integer dtype"
)
# if reps.ndim is less than x.ndim, we pad the reps with
# if reps.ndim is less than x.ndim, we pad the reps with
...
@@ -5305,7 +5305,7 @@ def tile(x, reps, ndim=None):
...
@@ -5305,7 +5305,7 @@ def tile(x, reps, ndim=None):
shape
=
[
1
]
*
(
ndim
-
x
.
ndim
)
+
[
x
.
shape
[
i
]
for
i
in
xrange
(
x
.
ndim
)]
shape
=
[
1
]
*
(
ndim
-
x
.
ndim
)
+
[
x
.
shape
[
i
]
for
i
in
xrange
(
x
.
ndim
)]
alloc_shape
=
reps
+
shape
alloc_shape
=
reps
+
shape
y
=
alloc
(
x
,
*
alloc_shape
)
y
=
alloc
(
x
,
*
alloc_shape
)
shuffle_ind
=
n
umpy
.
arange
(
ndim
*
2
)
.
reshape
(
2
,
ndim
)
shuffle_ind
=
n
p
.
arange
(
ndim
*
2
)
.
reshape
(
2
,
ndim
)
shuffle_ind
=
shuffle_ind
.
transpose
()
.
flatten
()
shuffle_ind
=
shuffle_ind
.
transpose
()
.
flatten
()
y
=
y
.
dimshuffle
(
*
shuffle_ind
)
y
=
y
.
dimshuffle
(
*
shuffle_ind
)
new_shapes
=
[
sh
*
reps
[
i
]
for
i
,
sh
in
enumerate
(
shape
)]
new_shapes
=
[
sh
*
reps
[
i
]
for
i
,
sh
in
enumerate
(
shape
)]
...
@@ -5343,7 +5343,7 @@ class ARange(Op):
...
@@ -5343,7 +5343,7 @@ class ARange(Op):
def
is_constant_value
(
var
,
value
):
def
is_constant_value
(
var
,
value
):
try
:
try
:
v
=
get_scalar_constant_value
(
var
)
v
=
get_scalar_constant_value
(
var
)
return
n
umpy
.
all
(
v
==
value
)
return
n
p
.
all
(
v
==
value
)
except
NotScalarConstantError
:
except
NotScalarConstantError
:
pass
pass
return
False
return
False
...
@@ -5378,7 +5378,7 @@ class ARange(Op):
...
@@ -5378,7 +5378,7 @@ class ARange(Op):
start
=
start
.
item
()
start
=
start
.
item
()
stop
=
stop
.
item
()
stop
=
stop
.
item
()
step
=
step
.
item
()
step
=
step
.
item
()
out
[
0
]
=
n
umpy
.
arange
(
start
,
stop
,
step
,
dtype
=
self
.
dtype
)
out
[
0
]
=
n
p
.
arange
(
start
,
stop
,
step
,
dtype
=
self
.
dtype
)
def
connection_pattern
(
self
,
node
):
def
connection_pattern
(
self
,
node
):
...
@@ -5424,10 +5424,10 @@ def arange(start, stop=None, step=1, dtype=None):
...
@@ -5424,10 +5424,10 @@ def arange(start, stop=None, step=1, dtype=None):
# As an example, if `start`, `stop` and `step` are all int32,
# As an example, if `start`, `stop` and `step` are all int32,
# `numpy.arange` returns an int64 array (on 64-bit platforms),
# `numpy.arange` returns an int64 array (on 64-bit platforms),
# while the upcast above returns int32.
# while the upcast above returns int32.
numpy_dtype
=
n
umpy
.
arange
(
numpy_dtype
=
n
p
.
arange
(
start
=
n
umpy
.
array
(
0
,
dtype
=
start
.
dtype
),
start
=
n
p
.
array
(
0
,
dtype
=
start
.
dtype
),
stop
=
n
umpy
.
array
(
1
,
dtype
=
stop
.
dtype
),
stop
=
n
p
.
array
(
1
,
dtype
=
stop
.
dtype
),
step
=
n
umpy
.
array
(
1
,
dtype
=
step
.
dtype
))
.
dtype
step
=
n
p
.
array
(
1
,
dtype
=
step
.
dtype
))
.
dtype
if
numpy_dtype
!=
dtype
:
if
numpy_dtype
!=
dtype
:
if
(
config
.
cast_policy
==
'numpy+floatX'
and
if
(
config
.
cast_policy
==
'numpy+floatX'
and
config
.
floatX
==
'float32'
and
config
.
floatX
==
'float32'
and
...
@@ -5653,7 +5653,7 @@ class PermuteRowElements(Op):
...
@@ -5653,7 +5653,7 @@ class PermuteRowElements(Op):
out_s
.
append
(
outdim
)
out_s
.
append
(
outdim
)
if
outs
[
0
]
is
None
or
outs
[
0
]
.
shape
!=
out_s
:
if
outs
[
0
]
is
None
or
outs
[
0
]
.
shape
!=
out_s
:
outs
[
0
]
=
n
umpy
.
empty
(
out_s
,
dtype
=
x
.
dtype
)
outs
[
0
]
=
n
p
.
empty
(
out_s
,
dtype
=
x
.
dtype
)
self
.
_rec_perform
(
node
,
x
,
y
,
inverse
,
outs
[
0
],
curdim
=
0
)
self
.
_rec_perform
(
node
,
x
,
y
,
inverse
,
outs
[
0
],
curdim
=
0
)
...
@@ -5796,7 +5796,7 @@ class Dot(Op):
...
@@ -5796,7 +5796,7 @@ class Dot(Op):
# the asarray is here because dot between two vectors
# the asarray is here because dot between two vectors
# gives a numpy float object but we need to return a 0d
# gives a numpy float object but we need to return a 0d
# ndarray
# ndarray
z
[
0
]
=
n
umpy
.
asarray
(
numpy
.
dot
(
x
,
y
))
z
[
0
]
=
n
p
.
asarray
(
np
.
dot
(
x
,
y
))
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
...
@@ -5976,7 +5976,7 @@ def dot(a, b):
...
@@ -5976,7 +5976,7 @@ def dot(a, b):
if
a
.
ndim
==
0
or
b
.
ndim
==
0
:
if
a
.
ndim
==
0
or
b
.
ndim
==
0
:
return
a
*
b
return
a
*
b
elif
a
.
ndim
>
2
or
b
.
ndim
>
2
:
elif
a
.
ndim
>
2
or
b
.
ndim
>
2
:
return
tensordot
(
a
,
b
,
[[
a
.
ndim
-
1
],
[
n
umpy
.
maximum
(
0
,
b
.
ndim
-
2
)]])
return
tensordot
(
a
,
b
,
[[
a
.
ndim
-
1
],
[
n
p
.
maximum
(
0
,
b
.
ndim
-
2
)]])
else
:
else
:
return
_dot
(
a
,
b
)
return
_dot
(
a
,
b
)
...
@@ -6012,14 +6012,14 @@ def _tensordot_as_dot(a, b, axes, dot, batched):
...
@@ -6012,14 +6012,14 @@ def _tensordot_as_dot(a, b, axes, dot, batched):
"""
"""
a
,
b
=
as_tensor_variable
(
a
),
as_tensor_variable
(
b
)
a
,
b
=
as_tensor_variable
(
a
),
as_tensor_variable
(
b
)
if
not
n
umpy
.
isscalar
(
axes
)
and
len
(
axes
)
!=
2
:
if
not
n
p
.
isscalar
(
axes
)
and
len
(
axes
)
!=
2
:
raise
ValueError
(
'Axes should be an integer or a '
raise
ValueError
(
'Axes should be an integer or a '
'list/tuple of len 2 (
%
s was provided)'
'list/tuple of len 2 (
%
s was provided)'
%
str
(
axes
))
%
str
(
axes
))
# if 'axes' is a number of axes to multiply and sum over (trailing axes
# if 'axes' is a number of axes to multiply and sum over (trailing axes
# of a, leading axes of b), we can just reshape and use dot.
# of a, leading axes of b), we can just reshape and use dot.
elif
n
umpy
.
isscalar
(
axes
):
elif
n
p
.
isscalar
(
axes
):
axes
=
int
(
axes
)
axes
=
int
(
axes
)
for
operand_name
,
operand
in
((
"a"
,
a
),
(
"b"
,
b
)):
for
operand_name
,
operand
in
((
"a"
,
a
),
(
"b"
,
b
)):
...
@@ -6083,12 +6083,12 @@ def _tensordot_as_dot(a, b, axes, dot, batched):
...
@@ -6083,12 +6083,12 @@ def _tensordot_as_dot(a, b, axes, dot, batched):
'the dimensions of
%
s (
%
s.ndim=
%
i, len(axes[0])=
%
i).'
%
'the dimensions of
%
s (
%
s.ndim=
%
i, len(axes[0])=
%
i).'
%
(
i
,
operand_name
,
operand_name
,
operand
.
ndim
,
(
i
,
operand_name
,
operand_name
,
operand
.
ndim
,
len
(
axes
[
i
])))
len
(
axes
[
i
])))
if
len
(
axes
[
i
])
>
0
and
n
umpy
.
max
(
axes
[
i
])
>=
operand
.
ndim
:
if
len
(
axes
[
i
])
>
0
and
n
p
.
max
(
axes
[
i
])
>=
operand
.
ndim
:
raise
ValueError
(
raise
ValueError
(
'axes[
%
i] contains dimensions greater than or equal '
'axes[
%
i] contains dimensions greater than or equal '
'to
%
s.ndim (
%
s.ndim=
%
i, max(axes[0])=
%
i).'
%
'to
%
s.ndim (
%
s.ndim=
%
i, max(axes[0])=
%
i).'
%
(
i
,
operand_name
,
operand_name
,
operand
.
ndim
,
(
i
,
operand_name
,
operand_name
,
operand
.
ndim
,
n
umpy
.
max
(
numpy
.
array
(
axes
[
i
]))))
n
p
.
max
(
np
.
array
(
axes
[
i
]))))
if
batched
and
0
in
axes
[
i
]:
if
batched
and
0
in
axes
[
i
]:
raise
ValueError
(
raise
ValueError
(
'axes to sum over must not contain the batch axis '
'axes to sum over must not contain the batch axis '
...
@@ -6243,8 +6243,8 @@ def all(x, axis=None, keepdims=False):
...
@@ -6243,8 +6243,8 @@ def all(x, axis=None, keepdims=False):
# Some NumPy version like 1.9.2 return a view for numpy.diagonal
# Some NumPy version like 1.9.2 return a view for numpy.diagonal
x
=
n
umpy
.
zeros
((
4
,
4
))
x
=
n
p
.
zeros
((
4
,
4
))
numpy_diagonal_return_view
=
n
umpy
.
may_share_memory
(
numpy
.
diagonal
(
x
),
x
)
numpy_diagonal_return_view
=
n
p
.
may_share_memory
(
np
.
diagonal
(
x
),
x
)
del
x
del
x
...
@@ -6271,7 +6271,7 @@ class ExtractDiag(Op):
...
@@ -6271,7 +6271,7 @@ class ExtractDiag(Op):
"set to True but numpy version
%
s and prior versions of "
"set to True but numpy version
%
s and prior versions of "
"numpy.diagonal() do not return a view. Update "
"numpy.diagonal() do not return a view. Update "
"numpy to use ExtractDiag(view=True)"
%
"numpy to use ExtractDiag(view=True)"
%
n
umpy
.
version
.
version
)
n
p
.
version
.
version
)
self
.
view
=
False
self
.
view
=
False
if
self
.
view
:
if
self
.
view
:
self
.
view_map
=
{
0
:
[
0
]}
self
.
view_map
=
{
0
:
[
0
]}
...
@@ -6395,7 +6395,7 @@ class AllocDiag(Op):
...
@@ -6395,7 +6395,7 @@ class AllocDiag(Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
z
,)
=
outputs
(
z
,)
=
outputs
z
[
0
]
=
n
umpy
.
diag
(
inputs
[
0
],
self
.
offset
)
z
[
0
]
=
n
p
.
diag
(
inputs
[
0
],
self
.
offset
)
def
grad
(
self
,
inputs
,
gout
):
def
grad
(
self
,
inputs
,
gout
):
(
gz
,)
=
gout
(
gz
,)
=
gout
...
@@ -6629,7 +6629,7 @@ class Choose(Op):
...
@@ -6629,7 +6629,7 @@ class Choose(Op):
choice
=
as_tensor_variable
(
choices
)
choice
=
as_tensor_variable
(
choices
)
choice_ndim
=
choice
.
ndim
-
1
choice_ndim
=
choice
.
ndim
-
1
choice_bcast
=
choice
.
broadcastable
[
1
:]
choice_bcast
=
choice
.
broadcastable
[
1
:]
out_ndim
=
n
umpy
.
max
([
a
.
ndim
,
choice_ndim
])
out_ndim
=
n
p
.
max
([
a
.
ndim
,
choice_ndim
])
# Make explicit all added broadcastable dimensions.
# Make explicit all added broadcastable dimensions.
a
=
shape_padleft
(
a
,
out_ndim
-
a
.
ndim
)
a
=
shape_padleft
(
a
,
out_ndim
-
a
.
ndim
)
...
@@ -6660,7 +6660,7 @@ class Choose(Op):
...
@@ -6660,7 +6660,7 @@ class Choose(Op):
a
=
inputs
[
0
]
a
=
inputs
[
0
]
choice
=
inputs
[
1
]
choice
=
inputs
[
1
]
# TODO reuse out?
# TODO reuse out?
z
[
0
]
=
n
umpy
.
choose
(
a
,
choice
,
mode
=
self
.
mode
)
z
[
0
]
=
n
p
.
choose
(
a
,
choice
,
mode
=
self
.
mode
)
class
AllocEmpty
(
gof
.
Op
):
class
AllocEmpty
(
gof
.
Op
):
...
@@ -6699,7 +6699,7 @@ class AllocEmpty(gof.Op):
...
@@ -6699,7 +6699,7 @@ class AllocEmpty(gof.Op):
out
,
=
out_
out
,
=
out_
sh
=
tuple
([
int
(
i
)
for
i
in
inputs
])
sh
=
tuple
([
int
(
i
)
for
i
in
inputs
])
if
out
[
0
]
is
None
or
out
[
0
]
.
shape
!=
sh
:
if
out
[
0
]
is
None
or
out
[
0
]
.
shape
!=
sh
:
out
[
0
]
=
n
umpy
.
empty
(
sh
,
dtype
=
self
.
dtype
)
out
[
0
]
=
n
p
.
empty
(
sh
,
dtype
=
self
.
dtype
)
def
c_code
(
self
,
node
,
name
,
inputs
,
out_
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
out_
,
sub
):
dtype
=
"NPY_"
+
self
.
dtype
.
upper
()
dtype
=
"NPY_"
+
self
.
dtype
.
upper
()
...
...
theano/tensor/blas_scipy.py
浏览文件 @
ef9f6efc
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
Implementations of BLAS Ops based on scipy's BLAS bindings.
Implementations of BLAS Ops based on scipy's BLAS bindings.
"""
"""
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
theano.tensor.blas
import
Ger
,
ger
,
ger_destructive
,
have_fblas
from
theano.tensor.blas
import
Ger
,
ger
,
ger_destructive
,
have_fblas
from
theano.tensor.blas
import
blas_optdb
,
optdb
,
local_optimizer
from
theano.tensor.blas
import
blas_optdb
,
optdb
,
local_optimizer
...
@@ -13,10 +13,10 @@ from theano.tensor.opt import in2out
...
@@ -13,10 +13,10 @@ from theano.tensor.opt import in2out
if
have_fblas
:
if
have_fblas
:
from
theano.tensor.blas
import
fblas
from
theano.tensor.blas
import
fblas
_blas_ger_fns
=
{
_blas_ger_fns
=
{
n
umpy
.
dtype
(
'float32'
):
fblas
.
sger
,
n
p
.
dtype
(
'float32'
):
fblas
.
sger
,
n
umpy
.
dtype
(
'float64'
):
fblas
.
dger
,
n
p
.
dtype
(
'float64'
):
fblas
.
dger
,
n
umpy
.
dtype
(
'complex64'
):
fblas
.
cgeru
,
n
p
.
dtype
(
'complex64'
):
fblas
.
cgeru
,
n
umpy
.
dtype
(
'complex128'
):
fblas
.
zgeru
,
n
p
.
dtype
(
'complex128'
):
fblas
.
zgeru
,
}
}
...
@@ -24,7 +24,7 @@ class ScipyGer(Ger):
...
@@ -24,7 +24,7 @@ class ScipyGer(Ger):
def
prepare_node
(
self
,
node
,
storage_map
,
compute_map
,
impl
):
def
prepare_node
(
self
,
node
,
storage_map
,
compute_map
,
impl
):
if
impl
==
'py'
:
if
impl
==
'py'
:
node
.
tag
.
local_ger
=
_blas_ger_fns
[
n
umpy
.
dtype
(
node
.
tag
.
local_ger
=
_blas_ger_fns
[
n
p
.
dtype
(
node
.
inputs
[
0
]
.
type
.
dtype
)]
node
.
inputs
[
0
]
.
type
.
dtype
)]
def
perform
(
self
,
node
,
inputs
,
output_storage
):
def
perform
(
self
,
node
,
inputs
,
output_storage
):
...
...
theano/tensor/elemwise.py
浏览文件 @
ef9f6efc
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
sys
import
sys
from
copy
import
copy
from
copy
import
copy
import
numpy
import
numpy
as
np
from
six
import
iteritems
,
integer_types
from
six
import
iteritems
,
integer_types
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -21,7 +21,7 @@ from theano.misc.frozendict import frozendict
...
@@ -21,7 +21,7 @@ from theano.misc.frozendict import frozendict
config
=
theano
.
config
config
=
theano
.
config
_numpy_ver
=
[
int
(
n
)
for
n
in
n
umpy
.
__version__
.
split
(
'.'
)[:
2
]]
_numpy_ver
=
[
int
(
n
)
for
n
in
n
p
.
__version__
.
split
(
'.'
)[:
2
]]
# tensor depends on elemwise to provide definitions for several ops
# tensor depends on elemwise to provide definitions for several ops
...
@@ -148,7 +148,7 @@ class DimShuffle(Op):
...
@@ -148,7 +148,7 @@ class DimShuffle(Op):
# isinstance(x, integer_types) returning False for
# isinstance(x, integer_types) returning False for
# numpy integers. See
# numpy integers. See
# <http://projects.scipy.org/numpy/ticket/2235>.
# <http://projects.scipy.org/numpy/ticket/2235>.
if
not
isinstance
(
j
,
(
integer_types
,
n
umpy
.
integer
)):
if
not
isinstance
(
j
,
(
integer_types
,
n
p
.
integer
)):
raise
TypeError
(
raise
TypeError
(
"DimShuffle indices must be python ints. "
"DimShuffle indices must be python ints. "
"Got: '
%
s' of type '
%
s'."
,
"Got: '
%
s' of type '
%
s'."
,
...
@@ -228,7 +228,7 @@ class DimShuffle(Op):
...
@@ -228,7 +228,7 @@ class DimShuffle(Op):
storage
,
=
out
storage
,
=
out
# drop
# drop
res
=
input
res
=
input
if
type
(
res
)
!=
n
umpy
.
ndarray
and
type
(
res
)
!=
numpy
.
memmap
:
if
type
(
res
)
!=
n
p
.
ndarray
and
type
(
res
)
!=
np
.
memmap
:
raise
TypeError
(
res
)
raise
TypeError
(
res
)
# transpose
# transpose
...
@@ -242,9 +242,9 @@ class DimShuffle(Op):
...
@@ -242,9 +242,9 @@ class DimShuffle(Op):
# copy (if not inplace)
# copy (if not inplace)
if
not
self
.
inplace
:
if
not
self
.
inplace
:
res
=
n
umpy
.
copy
(
res
)
res
=
n
p
.
copy
(
res
)
storage
[
0
]
=
n
umpy
.
asarray
(
res
)
# asarray puts scalars back into array
storage
[
0
]
=
n
p
.
asarray
(
res
)
# asarray puts scalars back into array
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
ishp
,
=
shapes
ishp
,
=
shapes
...
@@ -487,7 +487,7 @@ second dimension
...
@@ -487,7 +487,7 @@ second dimension
nfunc_spec
=
getattr
(
scalar_op
,
'nfunc_spec'
,
None
)
nfunc_spec
=
getattr
(
scalar_op
,
'nfunc_spec'
,
None
)
self
.
nfunc_spec
=
nfunc_spec
self
.
nfunc_spec
=
nfunc_spec
if
nfunc_spec
:
if
nfunc_spec
:
self
.
nfunc
=
getattr
(
n
umpy
,
nfunc_spec
[
0
])
self
.
nfunc
=
getattr
(
n
p
,
nfunc_spec
[
0
])
super
(
Elemwise
,
self
)
.
__init__
(
openmp
=
openmp
)
super
(
Elemwise
,
self
)
.
__init__
(
openmp
=
openmp
)
...
@@ -504,11 +504,11 @@ second dimension
...
@@ -504,11 +504,11 @@ second dimension
self
.
nfunc
=
None
self
.
nfunc
=
None
self
.
inplace_pattern
=
frozendict
(
self
.
inplace_pattern
)
self
.
inplace_pattern
=
frozendict
(
self
.
inplace_pattern
)
if
getattr
(
self
,
'nfunc_spec'
,
None
):
if
getattr
(
self
,
'nfunc_spec'
,
None
):
self
.
nfunc
=
getattr
(
n
umpy
,
self
.
nfunc_spec
[
0
])
self
.
nfunc
=
getattr
(
n
p
,
self
.
nfunc_spec
[
0
])
elif
0
<
self
.
scalar_op
.
nin
<
32
:
elif
0
<
self
.
scalar_op
.
nin
<
32
:
self
.
ufunc
=
n
umpy
.
frompyfunc
(
self
.
scalar_op
.
impl
,
self
.
ufunc
=
n
p
.
frompyfunc
(
self
.
scalar_op
.
impl
,
self
.
scalar_op
.
nin
,
self
.
scalar_op
.
nin
,
self
.
scalar_op
.
nout
)
self
.
scalar_op
.
nout
)
def
get_output_info
(
self
,
dim_shuffle
,
*
inputs
):
def
get_output_info
(
self
,
dim_shuffle
,
*
inputs
):
"""Return the outputs dtype and broadcastable pattern and the
"""Return the outputs dtype and broadcastable pattern and the
...
@@ -723,7 +723,7 @@ second dimension
...
@@ -723,7 +723,7 @@ second dimension
# the gradient contains a constant, translate it as
# the gradient contains a constant, translate it as
# an equivalent TensorType of size 1 and proper number of
# an equivalent TensorType of size 1 and proper number of
# dimensions
# dimensions
res
=
theano
.
tensor
.
constant
(
n
umpy
.
asarray
(
r
.
data
),
res
=
theano
.
tensor
.
constant
(
n
p
.
asarray
(
r
.
data
),
dtype
=
r
.
type
.
dtype
)
dtype
=
r
.
type
.
dtype
)
return
DimShuffle
((),
[
'x'
]
*
nd
)(
res
)
return
DimShuffle
((),
[
'x'
]
*
nd
)(
res
)
...
@@ -750,9 +750,9 @@ second dimension
...
@@ -750,9 +750,9 @@ second dimension
self
.
ufunc
is
None
and
self
.
ufunc
is
None
and
impl
==
'py'
):
impl
==
'py'
):
ufunc
=
n
umpy
.
frompyfunc
(
self
.
scalar_op
.
impl
,
ufunc
=
n
p
.
frompyfunc
(
self
.
scalar_op
.
impl
,
len
(
node
.
inputs
),
len
(
node
.
inputs
),
self
.
scalar_op
.
nout
)
self
.
scalar_op
.
nout
)
if
self
.
scalar_op
.
nin
>
0
:
if
self
.
scalar_op
.
nin
>
0
:
# We can reuse it for many nodes
# We can reuse it for many nodes
self
.
ufunc
=
ufunc
self
.
ufunc
=
ufunc
...
@@ -772,9 +772,9 @@ second dimension
...
@@ -772,9 +772,9 @@ second dimension
# when the input is complex. So add it only when inputs is int.
# when the input is complex. So add it only when inputs is int.
out_dtype
=
node
.
outputs
[
0
]
.
dtype
out_dtype
=
node
.
outputs
[
0
]
.
dtype
if
(
out_dtype
in
theano
.
tensor
.
float_dtypes
and
if
(
out_dtype
in
theano
.
tensor
.
float_dtypes
and
isinstance
(
self
.
nfunc
,
n
umpy
.
ufunc
)
and
isinstance
(
self
.
nfunc
,
n
p
.
ufunc
)
and
node
.
inputs
[
0
]
.
dtype
in
theano
.
tensor
.
discrete_dtypes
):
node
.
inputs
[
0
]
.
dtype
in
theano
.
tensor
.
discrete_dtypes
):
char
=
n
umpy
.
sctype2char
(
out_dtype
)
char
=
n
p
.
sctype2char
(
out_dtype
)
sig
=
char
*
node
.
nin
+
'->'
+
char
*
node
.
nout
sig
=
char
*
node
.
nin
+
'->'
+
char
*
node
.
nout
node
.
tag
.
sig
=
sig
node
.
tag
.
sig
=
sig
node
.
tag
.
fake_node
=
Apply
(
node
.
tag
.
fake_node
=
Apply
(
...
@@ -870,7 +870,7 @@ second dimension
...
@@ -870,7 +870,7 @@ second dimension
if
getattr
(
variable
,
"dtype"
,
""
)
==
'object'
:
if
getattr
(
variable
,
"dtype"
,
""
)
==
'object'
:
# Since numpy 1.6, function created with numpy.frompyfunc
# Since numpy 1.6, function created with numpy.frompyfunc
# always return an ndarray with dtype object
# always return an ndarray with dtype object
variable
=
n
umpy
.
asarray
(
variable
,
dtype
=
nout
.
dtype
)
variable
=
n
p
.
asarray
(
variable
,
dtype
=
nout
.
dtype
)
if
i
in
self
.
inplace_pattern
:
if
i
in
self
.
inplace_pattern
:
odat
=
inputs
[
self
.
inplace_pattern
[
i
]]
odat
=
inputs
[
self
.
inplace_pattern
[
i
]]
...
@@ -879,15 +879,15 @@ second dimension
...
@@ -879,15 +879,15 @@ second dimension
# Sometimes NumPy return a Python type.
# Sometimes NumPy return a Python type.
# Some Theano op return a different dtype like floor, ceil,
# Some Theano op return a different dtype like floor, ceil,
# trunc, eq, ...
# trunc, eq, ...
elif
(
not
isinstance
(
variable
,
n
umpy
.
ndarray
)
or
elif
(
not
isinstance
(
variable
,
n
p
.
ndarray
)
or
variable
.
dtype
!=
nout
.
dtype
):
variable
.
dtype
!=
nout
.
dtype
):
variable
=
n
umpy
.
asarray
(
variable
,
nout
.
dtype
)
variable
=
n
p
.
asarray
(
variable
,
nout
.
dtype
)
# The next line is needed for numpy 1.9. Otherwise
# The next line is needed for numpy 1.9. Otherwise
# there are tests that fail in DebugMode.
# there are tests that fail in DebugMode.
# Normally we would call theano.misc._asarray, but it
# Normally we would call theano.misc._asarray, but it
# is faster to inline the code. We know that the dtype
# is faster to inline the code. We know that the dtype
# are the same string, just different typenum.
# are the same string, just different typenum.
if
n
umpy
.
dtype
(
nout
.
dtype
)
.
num
!=
variable
.
dtype
.
num
:
if
n
p
.
dtype
(
nout
.
dtype
)
.
num
!=
variable
.
dtype
.
num
:
variable
=
variable
.
view
(
dtype
=
nout
.
dtype
)
variable
=
variable
.
view
(
dtype
=
nout
.
dtype
)
storage
[
0
]
=
variable
storage
[
0
]
=
variable
# numpy.real return a view!
# numpy.real return a view!
...
@@ -1302,9 +1302,9 @@ class CAReduce(Op):
...
@@ -1302,9 +1302,9 @@ class CAReduce(Op):
# There is a bug in numpy that results in isinstance(x,
# There is a bug in numpy that results in isinstance(x,
# integer_types) returning False for numpy integers. See
# integer_types) returning False for numpy integers. See
# <http://projects.scipy.org/numpy/ticket/2235>.
# <http://projects.scipy.org/numpy/ticket/2235>.
elif
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
self
.
axis
=
(
axis
,)
self
.
axis
=
(
axis
,)
elif
isinstance
(
axis
,
n
umpy
.
ndarray
)
and
axis
.
ndim
==
0
:
elif
isinstance
(
axis
,
n
p
.
ndarray
)
and
axis
.
ndim
==
0
:
self
.
axis
=
(
int
(
axis
),)
self
.
axis
=
(
int
(
axis
),)
else
:
else
:
self
.
axis
=
list
(
set
(
int
(
a
)
for
a
in
axis
))
self
.
axis
=
list
(
set
(
int
(
a
)
for
a
in
axis
))
...
@@ -1316,26 +1316,26 @@ class CAReduce(Op):
...
@@ -1316,26 +1316,26 @@ class CAReduce(Op):
def
set_ufunc
(
self
,
scalar_op
):
def
set_ufunc
(
self
,
scalar_op
):
# This is probably a speed up of the implementation
# This is probably a speed up of the implementation
if
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
Add
):
if
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
Add
):
self
.
ufunc
=
n
umpy
.
add
self
.
ufunc
=
n
p
.
add
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
Mul
):
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
Mul
):
self
.
ufunc
=
n
umpy
.
multiply
self
.
ufunc
=
n
p
.
multiply
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
Maximum
):
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
Maximum
):
self
.
ufunc
=
n
umpy
.
maximum
self
.
ufunc
=
n
p
.
maximum
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
Minimum
):
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
Minimum
):
self
.
ufunc
=
n
umpy
.
minimum
self
.
ufunc
=
n
p
.
minimum
elif
(
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
AND
)
and
elif
(
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
AND
)
and
_numpy_ver
>=
[
1
,
12
]):
_numpy_ver
>=
[
1
,
12
]):
# numpy.bitwise_and.identity was incorrect for versions before
# numpy.bitwise_and.identity was incorrect for versions before
# 1.12 (it was 1 instead of -1), so we skip it in that case.
# 1.12 (it was 1 instead of -1), so we skip it in that case.
# We will fall back to the "else:" case, which defines a
# We will fall back to the "else:" case, which defines a
# ufunc without identity.
# ufunc without identity.
self
.
ufunc
=
n
umpy
.
bitwise_and
self
.
ufunc
=
n
p
.
bitwise_and
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
OR
):
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
OR
):
self
.
ufunc
=
n
umpy
.
bitwise_or
self
.
ufunc
=
n
p
.
bitwise_or
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
XOR
):
elif
isinstance
(
scalar_op
,
theano
.
scalar
.
basic
.
XOR
):
self
.
ufunc
=
n
umpy
.
bitwise_xor
self
.
ufunc
=
n
p
.
bitwise_xor
else
:
else
:
self
.
ufunc
=
n
umpy
.
frompyfunc
(
scalar_op
.
impl
,
2
,
1
)
self
.
ufunc
=
n
p
.
frompyfunc
(
scalar_op
.
impl
,
2
,
1
)
def
_output_dtype
(
self
,
input_dtype
):
def
_output_dtype
(
self
,
input_dtype
):
return
input_dtype
return
input_dtype
...
@@ -1415,8 +1415,8 @@ class CAReduce(Op):
...
@@ -1415,8 +1415,8 @@ class CAReduce(Op):
# Compute the shape of the output
# Compute the shape of the output
v_shape
=
list
(
variable
.
shape
)
v_shape
=
list
(
variable
.
shape
)
del
v_shape
[
dimension
]
del
v_shape
[
dimension
]
variable
=
n
umpy
.
empty
(
tuple
(
v_shape
),
variable
=
n
p
.
empty
(
tuple
(
v_shape
),
dtype
=
acc_dtype
)
dtype
=
acc_dtype
)
variable
.
fill
(
self
.
scalar_op
.
identity
)
variable
.
fill
(
self
.
scalar_op
.
identity
)
else
:
else
:
raise
ValueError
((
raise
ValueError
((
...
@@ -1427,8 +1427,8 @@ class CAReduce(Op):
...
@@ -1427,8 +1427,8 @@ class CAReduce(Op):
variable
=
self
.
ufunc
.
reduce
(
variable
,
dimension
,
variable
=
self
.
ufunc
.
reduce
(
variable
,
dimension
,
dtype
=
acc_dtype
)
dtype
=
acc_dtype
)
variable
=
n
umpy
.
asarray
(
variable
)
variable
=
n
p
.
asarray
(
variable
)
if
n
umpy
.
may_share_memory
(
variable
,
input
):
if
n
p
.
may_share_memory
(
variable
,
input
):
# perhaps numpy is clever for reductions of size 1?
# perhaps numpy is clever for reductions of size 1?
# We don't want this.
# We don't want this.
variable
=
variable
.
copy
()
variable
=
variable
.
copy
()
...
@@ -1436,8 +1436,8 @@ class CAReduce(Op):
...
@@ -1436,8 +1436,8 @@ class CAReduce(Op):
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
else
:
else
:
# Force a copy
# Force a copy
output
[
0
]
=
n
umpy
.
array
(
variable
,
copy
=
True
,
output
[
0
]
=
n
p
.
array
(
variable
,
copy
=
True
,
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
ishape
,
=
shapes
ishape
,
=
shapes
...
...
theano/tensor/extra_ops.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
as
np
import
numpy
as
np
import
numpy
from
six.moves
import
xrange
from
six.moves
import
xrange
import
theano
import
theano
...
@@ -778,7 +777,7 @@ def repeat(x, repeats, axis=None):
...
@@ -778,7 +777,7 @@ def repeat(x, repeats, axis=None):
shape
[
axis
]
=
shape
[
axis
]
*
repeats
shape
[
axis
]
=
shape
[
axis
]
*
repeats
# dims_ is the dimension of that intermediate tensor.
# dims_ is the dimension of that intermediate tensor.
dims_
=
list
(
n
umpy
.
arange
(
x
.
ndim
))
dims_
=
list
(
n
p
.
arange
(
x
.
ndim
))
dims_
.
insert
(
axis
+
1
,
'x'
)
dims_
.
insert
(
axis
+
1
,
'x'
)
# After the original tensor is duplicated along the additional
# After the original tensor is duplicated along the additional
...
@@ -806,7 +805,7 @@ class Bartlett(gof.Op):
...
@@ -806,7 +805,7 @@ class Bartlett(gof.Op):
def
perform
(
self
,
node
,
inputs
,
out_
):
def
perform
(
self
,
node
,
inputs
,
out_
):
M
=
inputs
[
0
]
M
=
inputs
[
0
]
out
,
=
out_
out
,
=
out_
out
[
0
]
=
n
umpy
.
bartlett
(
M
)
out
[
0
]
=
n
p
.
bartlett
(
M
)
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
temp
=
node
.
inputs
[
0
]
temp
=
node
.
inputs
[
0
]
...
@@ -882,7 +881,7 @@ class FillDiagonal(gof.Op):
...
@@ -882,7 +881,7 @@ class FillDiagonal(gof.Op):
# Write the value out into the diagonal.
# Write the value out into the diagonal.
a
.
flat
[:
end
:
step
]
=
val
a
.
flat
[:
end
:
step
]
=
val
else
:
else
:
n
umpy
.
fill_diagonal
(
a
,
val
)
n
p
.
fill_diagonal
(
a
,
val
)
output_storage
[
0
][
0
]
=
a
output_storage
[
0
][
0
]
=
a
...
@@ -1132,7 +1131,7 @@ class Unique(theano.Op):
...
@@ -1132,7 +1131,7 @@ class Unique(theano.Op):
self
.
return_index
=
return_index
self
.
return_index
=
return_index
self
.
return_inverse
=
return_inverse
self
.
return_inverse
=
return_inverse
self
.
return_counts
=
return_counts
self
.
return_counts
=
return_counts
numpy_ver
=
[
int
(
n
)
for
n
in
n
umpy
.
__version__
.
split
(
'.'
)[:
2
]]
numpy_ver
=
[
int
(
n
)
for
n
in
n
p
.
__version__
.
split
(
'.'
)[:
2
]]
if
self
.
return_counts
and
bool
(
numpy_ver
<
[
1
,
9
]):
if
self
.
return_counts
and
bool
(
numpy_ver
<
[
1
,
9
]):
raise
RuntimeError
(
raise
RuntimeError
(
"Numpy version = "
+
np
.
__version__
+
"Numpy version = "
+
np
.
__version__
+
...
...
theano/tensor/fourier.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
math
import
math
from
theano
import
gof
,
tensor
from
theano
import
gof
,
tensor
...
@@ -98,7 +98,7 @@ class Fourier(gof.Op):
...
@@ -98,7 +98,7 @@ class Fourier(gof.Op):
a
=
inputs
[
0
]
a
=
inputs
[
0
]
n
=
inputs
[
1
]
n
=
inputs
[
1
]
axis
=
inputs
[
2
]
axis
=
inputs
[
2
]
output_storage
[
0
][
0
]
=
n
umpy
.
fft
.
fft
(
a
,
n
=
int
(
n
),
axis
=
axis
.
item
())
output_storage
[
0
][
0
]
=
n
p
.
fft
.
fft
(
a
,
n
=
int
(
n
),
axis
=
axis
.
item
())
def
grad
(
self
,
inputs
,
cost_grad
):
def
grad
(
self
,
inputs
,
cost_grad
):
"""
"""
...
@@ -128,7 +128,7 @@ class Fourier(gof.Op):
...
@@ -128,7 +128,7 @@ class Fourier(gof.Op):
# tensor.set_subtensor(res[...,n::], 0, False, False), res)
# tensor.set_subtensor(res[...,n::], 0, False, False), res)
# Instead we resort to that to account for truncation:
# Instead we resort to that to account for truncation:
flip_shape
=
list
(
n
umpy
.
arange
(
0
,
a
.
ndim
)[::
-
1
])
flip_shape
=
list
(
n
p
.
arange
(
0
,
a
.
ndim
)[::
-
1
])
res
=
res
.
dimshuffle
(
flip_shape
)
res
=
res
.
dimshuffle
(
flip_shape
)
res
=
tensor
.
switch
(
tensor
.
lt
(
n
,
tensor
.
shape
(
a
)[
axis
]),
res
=
tensor
.
switch
(
tensor
.
lt
(
n
,
tensor
.
shape
(
a
)[
axis
]),
tensor
.
set_subtensor
(
res
[
n
::,
],
0
,
False
,
False
),
tensor
.
set_subtensor
(
res
[
n
::,
],
0
,
False
,
False
),
...
@@ -136,8 +136,8 @@ class Fourier(gof.Op):
...
@@ -136,8 +136,8 @@ class Fourier(gof.Op):
res
=
res
.
dimshuffle
(
flip_shape
)
res
=
res
.
dimshuffle
(
flip_shape
)
# insures that gradient shape conforms to input shape:
# insures that gradient shape conforms to input shape:
out_shape
=
list
(
n
umpy
.
arange
(
0
,
axis
))
+
[
a
.
ndim
-
1
]
+
\
out_shape
=
list
(
n
p
.
arange
(
0
,
axis
))
+
[
a
.
ndim
-
1
]
+
\
list
(
n
umpy
.
arange
(
axis
,
a
.
ndim
-
1
))
list
(
n
p
.
arange
(
axis
,
a
.
ndim
-
1
))
res
=
res
.
dimshuffle
(
*
out_shape
)
res
=
res
.
dimshuffle
(
*
out_shape
)
return
[
res
,
None
,
None
]
return
[
res
,
None
,
None
]
...
...
theano/tensor/io.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
theano
import
gof
from
theano
import
gof
from
theano.gof
import
Constant
,
Generic
,
Op
from
theano.gof
import
Constant
,
Generic
,
Op
from
theano.gof.sched
import
key_to_cmp
from
theano.gof.sched
import
key_to_cmp
...
@@ -27,7 +27,7 @@ class LoadFromDisk(Op):
...
@@ -27,7 +27,7 @@ class LoadFromDisk(Op):
__props__
=
(
"dtype"
,
"broadcastable"
,
"mmap_mode"
)
__props__
=
(
"dtype"
,
"broadcastable"
,
"mmap_mode"
)
def
__init__
(
self
,
dtype
,
broadcastable
,
mmap_mode
=
None
):
def
__init__
(
self
,
dtype
,
broadcastable
,
mmap_mode
=
None
):
self
.
dtype
=
n
umpy
.
dtype
(
dtype
)
# turn "float64" into numpy
.float64
self
.
dtype
=
n
p
.
dtype
(
dtype
)
# turn "float64" into np
.float64
self
.
broadcastable
=
broadcastable
self
.
broadcastable
=
broadcastable
if
mmap_mode
not
in
(
None
,
'c'
):
if
mmap_mode
not
in
(
None
,
'c'
):
raise
ValueError
(
"The only supported values for mmap_mode "
raise
ValueError
(
"The only supported values for mmap_mode "
...
@@ -44,7 +44,7 @@ class LoadFromDisk(Op):
...
@@ -44,7 +44,7 @@ class LoadFromDisk(Op):
path
=
inp
[
0
]
path
=
inp
[
0
]
if
(
path
.
split
(
'.'
)[
-
1
]
==
'npz'
):
if
(
path
.
split
(
'.'
)[
-
1
]
==
'npz'
):
raise
ValueError
(
"Expected a .npy file, got
%
s instead"
%
path
)
raise
ValueError
(
"Expected a .npy file, got
%
s instead"
%
path
)
result
=
n
umpy
.
load
(
path
,
mmap_mode
=
self
.
mmap_mode
)
result
=
n
p
.
load
(
path
,
mmap_mode
=
self
.
mmap_mode
)
if
result
.
dtype
!=
self
.
dtype
:
if
result
.
dtype
!=
self
.
dtype
:
raise
TypeError
(
"Expected an array of type
%
s, got
%
s instead"
%
raise
TypeError
(
"Expected an array of type
%
s, got
%
s instead"
%
(
self
.
dtype
,
result
.
dtype
))
(
self
.
dtype
,
result
.
dtype
))
...
@@ -125,7 +125,7 @@ class MPIRecv(Op):
...
@@ -125,7 +125,7 @@ class MPIRecv(Op):
self
.
source
=
source
self
.
source
=
source
self
.
tag
=
tag
self
.
tag
=
tag
self
.
shape
=
shape
self
.
shape
=
shape
self
.
dtype
=
n
umpy
.
dtype
(
dtype
)
# turn "float64" into numpy.float64
self
.
dtype
=
n
p
.
dtype
(
dtype
)
# turn "float64" into numpy.float64
self
.
broadcastable
=
(
False
,)
*
len
(
shape
)
self
.
broadcastable
=
(
False
,)
*
len
(
shape
)
def
make_node
(
self
):
def
make_node
(
self
):
...
@@ -135,7 +135,7 @@ class MPIRecv(Op):
...
@@ -135,7 +135,7 @@ class MPIRecv(Op):
def
perform
(
self
,
node
,
inp
,
out
):
def
perform
(
self
,
node
,
inp
,
out
):
data
=
n
umpy
.
zeros
(
self
.
shape
,
dtype
=
self
.
dtype
)
data
=
n
p
.
zeros
(
self
.
shape
,
dtype
=
self
.
dtype
)
request
=
comm
.
Irecv
(
data
,
self
.
source
,
self
.
tag
)
request
=
comm
.
Irecv
(
data
,
self
.
source
,
self
.
tag
)
out
[
0
][
0
]
=
request
out
[
0
][
0
]
=
request
...
...
theano/tensor/nlinalg.py
浏览文件 @
ef9f6efc
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
logging
import
logging
import
warnings
import
warnings
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
theano
import
theano
...
@@ -44,7 +44,7 @@ class MatrixPinv(Op):
...
@@ -44,7 +44,7 @@ class MatrixPinv(Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
z
[
0
]
=
n
umpy
.
linalg
.
pinv
(
x
)
.
astype
(
x
.
dtype
)
z
[
0
]
=
n
p
.
linalg
.
pinv
(
x
)
.
astype
(
x
.
dtype
)
pinv
=
MatrixPinv
()
pinv
=
MatrixPinv
()
...
@@ -76,7 +76,7 @@ class MatrixInverse(Op):
...
@@ -76,7 +76,7 @@ class MatrixInverse(Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
z
[
0
]
=
n
umpy
.
linalg
.
inv
(
x
)
.
astype
(
x
.
dtype
)
z
[
0
]
=
n
p
.
linalg
.
inv
(
x
)
.
astype
(
x
.
dtype
)
def
grad
(
self
,
inputs
,
g_outputs
):
def
grad
(
self
,
inputs
,
g_outputs
):
r"""The gradient function should return
r"""The gradient function should return
...
@@ -162,7 +162,7 @@ class AllocDiag(Op):
...
@@ -162,7 +162,7 @@ class AllocDiag(Op):
(
z
,)
=
outputs
(
z
,)
=
outputs
if
x
.
ndim
!=
1
:
if
x
.
ndim
!=
1
:
raise
TypeError
(
x
)
raise
TypeError
(
x
)
z
[
0
]
=
n
umpy
.
diag
(
x
)
z
[
0
]
=
n
p
.
diag
(
x
)
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
x_s
,
=
shapes
x_s
,
=
shapes
...
@@ -289,7 +289,7 @@ class Det(Op):
...
@@ -289,7 +289,7 @@ class Det(Op):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
try
:
try
:
z
[
0
]
=
n
umpy
.
asarray
(
numpy
.
linalg
.
det
(
x
),
dtype
=
x
.
dtype
)
z
[
0
]
=
n
p
.
asarray
(
np
.
linalg
.
det
(
x
),
dtype
=
x
.
dtype
)
except
Exception
:
except
Exception
:
print
(
'Failed to compute determinant'
,
x
)
print
(
'Failed to compute determinant'
,
x
)
raise
raise
...
@@ -313,7 +313,7 @@ class Eig(Op):
...
@@ -313,7 +313,7 @@ class Eig(Op):
"""
"""
_numop
=
staticmethod
(
n
umpy
.
linalg
.
eig
)
_numop
=
staticmethod
(
n
p
.
linalg
.
eig
)
__props__
=
()
__props__
=
()
def
make_node
(
self
,
x
):
def
make_node
(
self
,
x
):
...
@@ -341,7 +341,7 @@ class Eigh(Eig):
...
@@ -341,7 +341,7 @@ class Eigh(Eig):
"""
"""
_numop
=
staticmethod
(
n
umpy
.
linalg
.
eigh
)
_numop
=
staticmethod
(
n
p
.
linalg
.
eigh
)
__props__
=
(
'UPLO'
,)
__props__
=
(
'UPLO'
,)
def
__init__
(
self
,
UPLO
=
'L'
):
def
__init__
(
self
,
UPLO
=
'L'
):
...
@@ -356,7 +356,7 @@ class Eigh(Eig):
...
@@ -356,7 +356,7 @@ class Eigh(Eig):
# LAPACK. Rather than trying to reproduce the (rather
# LAPACK. Rather than trying to reproduce the (rather
# involved) logic, we just probe linalg.eigh with a trivial
# involved) logic, we just probe linalg.eigh with a trivial
# input.
# input.
w_dtype
=
self
.
_numop
([[
n
umpy
.
dtype
(
x
.
dtype
)
.
type
()]])[
0
]
.
dtype
.
name
w_dtype
=
self
.
_numop
([[
n
p
.
dtype
(
x
.
dtype
)
.
type
()]])[
0
]
.
dtype
.
name
w
=
theano
.
tensor
.
vector
(
dtype
=
w_dtype
)
w
=
theano
.
tensor
.
vector
(
dtype
=
w_dtype
)
v
=
theano
.
tensor
.
matrix
(
dtype
=
x
.
dtype
)
v
=
theano
.
tensor
.
matrix
(
dtype
=
x
.
dtype
)
return
Apply
(
self
,
[
x
],
[
w
,
v
])
return
Apply
(
self
,
[
x
],
[
w
,
v
])
...
@@ -419,11 +419,11 @@ class EighGrad(Op):
...
@@ -419,11 +419,11 @@ class EighGrad(Op):
assert
UPLO
in
[
'L'
,
'U'
]
assert
UPLO
in
[
'L'
,
'U'
]
self
.
UPLO
=
UPLO
self
.
UPLO
=
UPLO
if
UPLO
==
'L'
:
if
UPLO
==
'L'
:
self
.
tri0
=
n
umpy
.
tril
self
.
tri0
=
n
p
.
tril
self
.
tri1
=
lambda
a
:
n
umpy
.
triu
(
a
,
1
)
self
.
tri1
=
lambda
a
:
n
p
.
triu
(
a
,
1
)
else
:
else
:
self
.
tri0
=
n
umpy
.
triu
self
.
tri0
=
n
p
.
triu
self
.
tri1
=
lambda
a
:
n
umpy
.
tril
(
a
,
-
1
)
self
.
tri1
=
lambda
a
:
n
p
.
tril
(
a
,
-
1
)
def
make_node
(
self
,
x
,
w
,
v
,
gw
,
gv
):
def
make_node
(
self
,
x
,
w
,
v
,
gw
,
gv
):
x
,
w
,
v
,
gw
,
gv
=
map
(
as_tensor_variable
,
(
x
,
w
,
v
,
gw
,
gv
))
x
,
w
,
v
,
gw
,
gv
=
map
(
as_tensor_variable
,
(
x
,
w
,
v
,
gw
,
gv
))
...
@@ -445,7 +445,7 @@ class EighGrad(Op):
...
@@ -445,7 +445,7 @@ class EighGrad(Op):
"""
"""
x
,
w
,
v
,
W
,
V
=
inputs
x
,
w
,
v
,
W
,
V
=
inputs
N
=
x
.
shape
[
0
]
N
=
x
.
shape
[
0
]
outer
=
n
umpy
.
outer
outer
=
n
p
.
outer
def
G
(
n
):
def
G
(
n
):
return
sum
(
v
[:,
m
]
*
V
.
T
[
n
]
.
dot
(
v
[:,
m
])
/
(
w
[
n
]
-
w
[
m
])
return
sum
(
v
[:,
m
]
*
V
.
T
[
n
]
.
dot
(
v
[:,
m
])
/
(
w
[
n
]
-
w
[
m
])
...
@@ -466,7 +466,7 @@ class EighGrad(Op):
...
@@ -466,7 +466,7 @@ class EighGrad(Op):
# Make sure we return the right dtype even if NumPy performed
# Make sure we return the right dtype even if NumPy performed
# upcasting in self.tri0.
# upcasting in self.tri0.
outputs
[
0
][
0
]
=
n
umpy
.
asarray
(
out
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
outputs
[
0
][
0
]
=
n
p
.
asarray
(
out
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
return
[
shapes
[
0
]]
return
[
shapes
[
0
]]
...
@@ -486,7 +486,7 @@ class QRFull(Op):
...
@@ -486,7 +486,7 @@ class QRFull(Op):
"""
"""
_numop
=
staticmethod
(
n
umpy
.
linalg
.
qr
)
_numop
=
staticmethod
(
n
p
.
linalg
.
qr
)
__props__
=
(
'mode'
,)
__props__
=
(
'mode'
,)
def
__init__
(
self
,
mode
):
def
__init__
(
self
,
mode
):
...
@@ -519,7 +519,7 @@ class QRIncomplete(Op):
...
@@ -519,7 +519,7 @@ class QRIncomplete(Op):
"""
"""
_numop
=
staticmethod
(
n
umpy
.
linalg
.
qr
)
_numop
=
staticmethod
(
n
p
.
linalg
.
qr
)
__props__
=
(
'mode'
,)
__props__
=
(
'mode'
,)
def
__init__
(
self
,
mode
):
def
__init__
(
self
,
mode
):
...
@@ -583,7 +583,7 @@ def qr(a, mode="reduced"):
...
@@ -583,7 +583,7 @@ def qr(a, mode="reduced"):
"""
"""
x
=
[[
2
,
1
],
[
3
,
4
]]
x
=
[[
2
,
1
],
[
3
,
4
]]
if
isinstance
(
n
umpy
.
linalg
.
qr
(
x
,
mode
),
tuple
):
if
isinstance
(
n
p
.
linalg
.
qr
(
x
,
mode
),
tuple
):
return
QRFull
(
mode
)(
a
)
return
QRFull
(
mode
)(
a
)
else
:
else
:
return
QRIncomplete
(
mode
)(
a
)
return
QRIncomplete
(
mode
)(
a
)
...
@@ -606,7 +606,7 @@ class SVD(Op):
...
@@ -606,7 +606,7 @@ class SVD(Op):
"""
"""
# See doc in the docstring of the function just after this class.
# See doc in the docstring of the function just after this class.
_numop
=
staticmethod
(
n
umpy
.
linalg
.
svd
)
_numop
=
staticmethod
(
n
p
.
linalg
.
svd
)
__props__
=
(
'full_matrices'
,
'compute_uv'
)
__props__
=
(
'full_matrices'
,
'compute_uv'
)
def
__init__
(
self
,
full_matrices
=
True
,
compute_uv
=
True
):
def
__init__
(
self
,
full_matrices
=
True
,
compute_uv
=
True
):
...
@@ -666,10 +666,10 @@ class lstsq(Op):
...
@@ -666,10 +666,10 @@ class lstsq(Op):
theano
.
tensor
.
lscalar
(),
theano
.
tensor
.
dvector
()])
theano
.
tensor
.
lscalar
(),
theano
.
tensor
.
dvector
()])
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
zz
=
n
umpy
.
linalg
.
lstsq
(
inputs
[
0
],
inputs
[
1
],
inputs
[
2
])
zz
=
n
p
.
linalg
.
lstsq
(
inputs
[
0
],
inputs
[
1
],
inputs
[
2
])
outputs
[
0
][
0
]
=
zz
[
0
]
outputs
[
0
][
0
]
=
zz
[
0
]
outputs
[
1
][
0
]
=
zz
[
1
]
outputs
[
1
][
0
]
=
zz
[
1
]
outputs
[
2
][
0
]
=
n
umpy
.
array
(
zz
[
2
])
outputs
[
2
][
0
]
=
n
p
.
array
(
zz
[
2
])
outputs
[
3
][
0
]
=
zz
[
3
]
outputs
[
3
][
0
]
=
zz
[
3
]
...
@@ -730,7 +730,7 @@ class TensorInv(Op):
...
@@ -730,7 +730,7 @@ class TensorInv(Op):
Class wrapper for tensorinv() function;
Class wrapper for tensorinv() function;
Theano utilization of numpy.linalg.tensorinv;
Theano utilization of numpy.linalg.tensorinv;
"""
"""
_numop
=
staticmethod
(
n
umpy
.
linalg
.
tensorinv
)
_numop
=
staticmethod
(
n
p
.
linalg
.
tensorinv
)
__props__
=
(
'ind'
,)
__props__
=
(
'ind'
,)
def
__init__
(
self
,
ind
=
2
):
def
__init__
(
self
,
ind
=
2
):
...
@@ -790,7 +790,7 @@ class TensorSolve(Op):
...
@@ -790,7 +790,7 @@ class TensorSolve(Op):
Class wrapper for tensorsolve function.
Class wrapper for tensorsolve function.
"""
"""
_numop
=
staticmethod
(
n
umpy
.
linalg
.
tensorsolve
)
_numop
=
staticmethod
(
n
p
.
linalg
.
tensorsolve
)
__props__
=
(
'axes'
,
)
__props__
=
(
'axes'
,
)
def
__init__
(
self
,
axes
=
None
):
def
__init__
(
self
,
axes
=
None
):
...
...
theano/tensor/opt.py
浏览文件 @
ef9f6efc
...
@@ -14,7 +14,7 @@ import time
...
@@ -14,7 +14,7 @@ import time
import
traceback
import
traceback
import
warnings
import
warnings
import
numpy
import
numpy
as
np
from
six
import
integer_types
,
iteritems
from
six
import
integer_types
,
iteritems
from
six.moves
import
reduce
,
xrange
from
six.moves
import
reduce
,
xrange
...
@@ -786,7 +786,7 @@ class MakeVector(T.Op):
...
@@ -786,7 +786,7 @@ class MakeVector(T.Op):
# So there will be (1 * nb_dtype) + ((nb len(inp) - 1 ))
# So there will be (1 * nb_dtype) + ((nb len(inp) - 1 ))
# different c code with the following algo
# different c code with the following algo
out_shape
=
len
(
inp
)
out_shape
=
len
(
inp
)
out_num
=
n
umpy
.
dtype
(
node
.
outputs
[
0
]
.
dtype
)
.
num
out_num
=
n
p
.
dtype
(
node
.
outputs
[
0
]
.
dtype
)
.
num
# don't use dtype_%(out)s as when check_input=False, it isn't defined.
# don't use dtype_%(out)s as when check_input=False, it isn't defined.
out_dtype
=
node
.
outputs
[
0
]
.
type
.
dtype_specs
()[
1
]
out_dtype
=
node
.
outputs
[
0
]
.
type
.
dtype_specs
()[
1
]
if
len
(
inp
)
>
0
:
if
len
(
inp
)
>
0
:
...
@@ -1064,8 +1064,8 @@ class ShapeFeature(object):
...
@@ -1064,8 +1064,8 @@ class ShapeFeature(object):
if
type
(
s_i
)
is
float
and
int
(
s_i
)
==
s_i
:
if
type
(
s_i
)
is
float
and
int
(
s_i
)
==
s_i
:
s_i
=
int
(
s_i
)
s_i
=
int
(
s_i
)
if
(
type
(
s_i
)
in
integer_types
or
if
(
type
(
s_i
)
in
integer_types
or
isinstance
(
s_i
,
n
umpy
.
integer
)
or
isinstance
(
s_i
,
n
p
.
integer
)
or
(
isinstance
(
s_i
,
n
umpy
.
ndarray
)
and
s_i
.
ndim
==
0
)):
(
isinstance
(
s_i
,
n
p
.
ndarray
)
and
s_i
.
ndim
==
0
)):
# this shape is a constant
# this shape is a constant
if
s_i
<
0
:
if
s_i
<
0
:
msg
=
"There is a negative shape in the graph!"
msg
=
"There is a negative shape in the graph!"
...
@@ -1975,7 +1975,7 @@ def local_subtensor_remove_broadcastable_index(node):
...
@@ -1975,7 +1975,7 @@ def local_subtensor_remove_broadcastable_index(node):
elif
isinstance
(
elem
,
slice
):
elif
isinstance
(
elem
,
slice
):
if
elem
!=
slice
(
None
):
if
elem
!=
slice
(
None
):
return
return
elif
isinstance
(
elem
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
elem
,
(
integer_types
,
n
p
.
integer
)):
if
elem
in
[
0
,
-
1
]
and
node
.
inputs
[
0
]
.
broadcastable
[
dim
]:
if
elem
in
[
0
,
-
1
]
and
node
.
inputs
[
0
]
.
broadcastable
[
dim
]:
remove_dim
.
append
(
dim
)
remove_dim
.
append
(
dim
)
else
:
else
:
...
@@ -2027,7 +2027,7 @@ def local_subtensor_make_vector(node):
...
@@ -2027,7 +2027,7 @@ def local_subtensor_make_vector(node):
else
:
else
:
return
return
if
isinstance
(
idx
,
(
integer_types
,
n
umpy
.
integer
)):
if
isinstance
(
idx
,
(
integer_types
,
n
p
.
integer
)):
# We don't need to copy over any stack traces here
# We don't need to copy over any stack traces here
return
[
x
.
owner
.
inputs
[
idx
]]
return
[
x
.
owner
.
inputs
[
idx
]]
elif
isinstance
(
idx
,
Variable
):
elif
isinstance
(
idx
,
Variable
):
...
@@ -2035,7 +2035,7 @@ def local_subtensor_make_vector(node):
...
@@ -2035,7 +2035,7 @@ def local_subtensor_make_vector(node):
# if it is a constant we can do something with it
# if it is a constant we can do something with it
try
:
try
:
v
=
get_scalar_constant_value
(
idx
,
only_process_constants
=
True
)
v
=
get_scalar_constant_value
(
idx
,
only_process_constants
=
True
)
if
isinstance
(
v
,
n
umpy
.
integer
):
if
isinstance
(
v
,
n
p
.
integer
):
# Python 2.4 wants to index only with Python integers
# Python 2.4 wants to index only with Python integers
v
=
int
(
v
)
v
=
int
(
v
)
# We don't need to copy over any stack traces here
# We don't need to copy over any stack traces here
...
@@ -2359,14 +2359,14 @@ class Assert(T.Op):
...
@@ -2359,14 +2359,14 @@ class Assert(T.Op):
if
not
isinstance
(
value
,
Variable
):
if
not
isinstance
(
value
,
Variable
):
value
=
T
.
as_tensor_variable
(
value
)
value
=
T
.
as_tensor_variable
(
value
)
cond
=
[
T
.
as_tensor_variable
(
c
)
for
c
in
conds
]
cond
=
[
T
.
as_tensor_variable
(
c
)
for
c
in
conds
]
assert
n
umpy
.
all
([
c
.
type
.
ndim
==
0
for
c
in
cond
])
assert
n
p
.
all
([
c
.
type
.
ndim
==
0
for
c
in
cond
])
return
gof
.
Apply
(
self
,
[
value
]
+
cond
,
[
value
.
type
()])
return
gof
.
Apply
(
self
,
[
value
]
+
cond
,
[
value
.
type
()])
def
perform
(
self
,
node
,
inputs
,
out_
):
def
perform
(
self
,
node
,
inputs
,
out_
):
out
,
=
out_
out
,
=
out_
v
=
inputs
[
0
]
v
=
inputs
[
0
]
out
[
0
]
=
v
out
[
0
]
=
v
assert
n
umpy
.
all
(
inputs
[
1
:]),
self
.
msg
assert
n
p
.
all
(
inputs
[
1
:]),
self
.
msg
def
grad
(
self
,
input
,
output_gradients
):
def
grad
(
self
,
input
,
output_gradients
):
return
output_gradients
+
[
DisconnectedType
()()]
*
(
len
(
input
)
-
1
)
return
output_gradients
+
[
DisconnectedType
()()]
*
(
len
(
input
)
-
1
)
...
@@ -2708,7 +2708,7 @@ def local_useless_subtensor(node):
...
@@ -2708,7 +2708,7 @@ def local_useless_subtensor(node):
length_pos
=
shape_of
[
node
.
inputs
[
0
]][
pos
]
length_pos
=
shape_of
[
node
.
inputs
[
0
]][
pos
]
if
isinstance
(
idx
.
stop
,
(
integer_types
,
n
umpy
.
integer
)):
if
isinstance
(
idx
.
stop
,
(
integer_types
,
n
p
.
integer
)):
length_pos_data
=
sys
.
maxsize
length_pos_data
=
sys
.
maxsize
try
:
try
:
length_pos_data
=
get_scalar_constant_value
(
length_pos
,
length_pos_data
=
get_scalar_constant_value
(
length_pos
,
...
@@ -2766,7 +2766,7 @@ def local_useless_subtensor(node):
...
@@ -2766,7 +2766,7 @@ def local_useless_subtensor(node):
idx
=
idx
.
value
idx
=
idx
.
value
if
len
(
idx
)
!=
length
:
if
len
(
idx
)
!=
length
:
return
False
return
False
if
n
umpy
.
any
(
idx
!=
numpy
.
arange
(
length
)):
if
n
p
.
any
(
idx
!=
np
.
arange
(
length
)):
return
False
return
False
elif
idx
.
owner
is
not
None
and
isinstance
(
idx
.
owner
.
op
,
T
.
ARange
):
elif
idx
.
owner
is
not
None
and
isinstance
(
idx
.
owner
.
op
,
T
.
ARange
):
try
:
try
:
...
@@ -3625,7 +3625,7 @@ def local_useless_rebroadcast(node):
...
@@ -3625,7 +3625,7 @@ def local_useless_rebroadcast(node):
"""
"""
if
isinstance
(
node
.
op
,
T
.
Rebroadcast
):
if
isinstance
(
node
.
op
,
T
.
Rebroadcast
):
x
=
node
.
inputs
[
0
]
x
=
node
.
inputs
[
0
]
if
n
umpy
.
all
(
x
.
broadcastable
==
node
.
outputs
[
0
]
.
broadcastable
):
if
n
p
.
all
(
x
.
broadcastable
==
node
.
outputs
[
0
]
.
broadcastable
):
# No broadcastable flag was modified
# No broadcastable flag was modified
# No need to copy over stack trace,
# No need to copy over stack trace,
# because x should already have a stack trace.
# because x should already have a stack trace.
...
@@ -3938,8 +3938,8 @@ def local_useless_switch(node):
...
@@ -3938,8 +3938,8 @@ def local_useless_switch(node):
isinstance
(
node
.
op
.
scalar_op
,
scalar
.
basic
.
Switch
)):
isinstance
(
node
.
op
.
scalar_op
,
scalar
.
basic
.
Switch
)):
cond
=
T
.
extract_constant
(
node
.
inputs
[
0
],
cond
=
T
.
extract_constant
(
node
.
inputs
[
0
],
only_process_constants
=
True
)
only_process_constants
=
True
)
if
((
type
(
cond
)
is
n
umpy
.
ndarray
and
cond
.
ndim
==
0
)
or
if
((
type
(
cond
)
is
n
p
.
ndarray
and
cond
.
ndim
==
0
)
or
isinstance
(
cond
,
n
umpy
.
number
)):
isinstance
(
cond
,
n
p
.
number
)):
if
cond
==
0
:
if
cond
==
0
:
correct_out
=
node
.
inputs
[
2
]
correct_out
=
node
.
inputs
[
2
]
else
:
else
:
...
@@ -4985,7 +4985,7 @@ class Canonizer(gof.LocalOptimizer):
...
@@ -4985,7 +4985,7 @@ class Canonizer(gof.LocalOptimizer):
num
,
denum
=
self
.
simplify
(
list
(
orig_num
),
list
(
orig_denum
),
out
.
type
)
num
,
denum
=
self
.
simplify
(
list
(
orig_num
),
list
(
orig_denum
),
out
.
type
)
def
same
(
x
,
y
):
def
same
(
x
,
y
):
return
len
(
x
)
==
len
(
y
)
and
all
(
n
umpy
.
all
(
xe
==
ye
)
for
xe
,
ye
in
return
len
(
x
)
==
len
(
y
)
and
all
(
n
p
.
all
(
xe
==
ye
)
for
xe
,
ye
in
zip
(
x
,
y
))
zip
(
x
,
y
))
if
same
(
orig_num
,
num
)
and
same
(
orig_denum
,
denum
):
if
same
(
orig_num
,
num
)
and
same
(
orig_denum
,
denum
):
...
@@ -5029,7 +5029,7 @@ def mul_calculate(num, denum, aslist=False, out_type=None):
...
@@ -5029,7 +5029,7 @@ def mul_calculate(num, denum, aslist=False, out_type=None):
if
aslist
:
if
aslist
:
return
[]
return
[]
else
:
else
:
return
n
umpy
.
int8
(
1
)
return
n
p
.
int8
(
1
)
# Make sure we do not accidently upcast data types.
# Make sure we do not accidently upcast data types.
if
out_type
is
None
:
if
out_type
is
None
:
...
@@ -5038,9 +5038,9 @@ def mul_calculate(num, denum, aslist=False, out_type=None):
...
@@ -5038,9 +5038,9 @@ def mul_calculate(num, denum, aslist=False, out_type=None):
out_dtype
=
out_type
.
dtype
out_dtype
=
out_type
.
dtype
one
=
theano
.
_asarray
(
1
,
dtype
=
out_dtype
)
one
=
theano
.
_asarray
(
1
,
dtype
=
out_dtype
)
v
=
reduce
(
n
umpy
.
multiply
,
num
,
one
)
/
reduce
(
numpy
.
multiply
,
denum
,
one
)
v
=
reduce
(
n
p
.
multiply
,
num
,
one
)
/
reduce
(
np
.
multiply
,
denum
,
one
)
if
aslist
:
if
aslist
:
if
n
umpy
.
all
(
v
==
1
):
if
n
p
.
all
(
v
==
1
):
return
[]
return
[]
else
:
else
:
return
[
v
]
return
[
v
]
...
@@ -5053,7 +5053,7 @@ register_canonicalize(local_mul_canonizer, name='local_mul_canonizer')
...
@@ -5053,7 +5053,7 @@ register_canonicalize(local_mul_canonizer, name='local_mul_canonizer')
@gof.local_optimizer
([
T
.
neg
])
@gof.local_optimizer
([
T
.
neg
])
def
local_neg_to_mul
(
node
):
def
local_neg_to_mul
(
node
):
if
node
.
op
==
T
.
neg
:
if
node
.
op
==
T
.
neg
:
return
[
T
.
mul
(
n
umpy
.
array
(
-
1
,
dtype
=
node
.
inputs
[
0
]
.
dtype
),
return
[
T
.
mul
(
n
p
.
array
(
-
1
,
dtype
=
node
.
inputs
[
0
]
.
dtype
),
node
.
inputs
[
0
])]
node
.
inputs
[
0
])]
register_canonicalize
(
local_neg_to_mul
)
register_canonicalize
(
local_neg_to_mul
)
...
@@ -5078,13 +5078,13 @@ def local_sum_prod_mul_by_scalar(node):
...
@@ -5078,13 +5078,13 @@ def local_sum_prod_mul_by_scalar(node):
if
node_inps
.
owner
and
node_inps
.
owner
.
op
==
T
.
mul
:
if
node_inps
.
owner
and
node_inps
.
owner
.
op
==
T
.
mul
:
terms
=
node_inps
.
owner
.
inputs
terms
=
node_inps
.
owner
.
inputs
scalars
=
[
t
.
dimshuffle
()
for
t
in
terms
if
scalars
=
[
t
.
dimshuffle
()
for
t
in
terms
if
n
umpy
.
all
(
t
.
type
.
broadcastable
)]
n
p
.
all
(
t
.
type
.
broadcastable
)]
if
len
(
scalars
)
==
0
:
if
len
(
scalars
)
==
0
:
# Nothing to optimize here
# Nothing to optimize here
return
return
non_scalars
=
[
t
for
t
in
terms
if
not
n
umpy
.
all
(
t
.
broadcastable
)]
non_scalars
=
[
t
for
t
in
terms
if
not
n
p
.
all
(
t
.
broadcastable
)]
# Perform the op only on the non-scalar inputs, if applicable
# Perform the op only on the non-scalar inputs, if applicable
if
len
(
non_scalars
)
==
0
:
if
len
(
non_scalars
)
==
0
:
...
@@ -5780,7 +5780,7 @@ def local_neg_div_neg(node):
...
@@ -5780,7 +5780,7 @@ def local_neg_div_neg(node):
# No other clients of the original division
# No other clients of the original division
new_num
=
num
.
owner
.
inputs
[
0
]
new_num
=
num
.
owner
.
inputs
[
0
]
return
[
T
.
true_div
(
new_num
,
denom
)]
return
[
T
.
true_div
(
new_num
,
denom
)]
elif
n
umpy
.
all
(
num
.
broadcastable
)
and
isinstance
(
num
,
Constant
):
elif
n
p
.
all
(
num
.
broadcastable
)
and
isinstance
(
num
,
Constant
):
if
len
(
frac
.
clients
)
==
1
:
if
len
(
frac
.
clients
)
==
1
:
new_num
=
-
num
.
data
new_num
=
-
num
.
data
return
[
T
.
true_div
(
new_num
,
denom
)]
return
[
T
.
true_div
(
new_num
,
denom
)]
...
@@ -5811,7 +5811,7 @@ register_canonicalize(local_mul_zero)
...
@@ -5811,7 +5811,7 @@ register_canonicalize(local_mul_zero)
@gof.local_optimizer
([
T
.
true_div
])
@gof.local_optimizer
([
T
.
true_div
])
def
local_div_to_inv
(
node
):
def
local_div_to_inv
(
node
):
if
node
.
op
==
T
.
true_div
and
n
umpy
.
all
(
if
node
.
op
==
T
.
true_div
and
n
p
.
all
(
local_mul_canonizer
.
get_constant
(
node
.
inputs
[
0
])
==
1.0
):
local_mul_canonizer
.
get_constant
(
node
.
inputs
[
0
])
==
1.0
):
out
=
node
.
outputs
[
0
]
out
=
node
.
outputs
[
0
]
new_out
=
T
.
inv
(
local_mul_canonizer
.
merge_num_denum
(
node
.
inputs
[
1
:],
new_out
=
T
.
inv
(
local_mul_canonizer
.
merge_num_denum
(
node
.
inputs
[
1
:],
...
@@ -5873,7 +5873,7 @@ def local_intdiv_by_one(node):
...
@@ -5873,7 +5873,7 @@ def local_intdiv_by_one(node):
"""
"""
if
node
.
op
in
[
T
.
int_div
]:
if
node
.
op
in
[
T
.
int_div
]:
if
isinstance
(
node
.
inputs
[
1
],
T
.
TensorConstant
)
and
\
if
isinstance
(
node
.
inputs
[
1
],
T
.
TensorConstant
)
and
\
n
umpy
.
all
(
node
.
inputs
[
1
]
.
value
==
1
):
n
p
.
all
(
node
.
inputs
[
1
]
.
value
==
1
):
return
[
node
.
inputs
[
0
]
.
astype
(
node
.
outputs
[
0
]
.
dtype
)]
return
[
node
.
inputs
[
0
]
.
astype
(
node
.
outputs
[
0
]
.
dtype
)]
...
@@ -5906,19 +5906,19 @@ def local_pow_specialize(node):
...
@@ -5906,19 +5906,19 @@ def local_pow_specialize(node):
ysym
.
type
.
broadcastable
):
ysym
.
type
.
broadcastable
):
rval
=
None
rval
=
None
if
n
umpy
.
all
(
y
==
2
):
if
n
p
.
all
(
y
==
2
):
rval
=
[
T
.
sqr
(
xsym
)]
rval
=
[
T
.
sqr
(
xsym
)]
if
n
umpy
.
all
(
y
==
1
):
if
n
p
.
all
(
y
==
1
):
rval
=
[
xsym
]
rval
=
[
xsym
]
if
n
umpy
.
all
(
y
==
0
):
if
n
p
.
all
(
y
==
0
):
rval
=
[
T
.
fill
(
xsym
,
n
umpy
.
asarray
(
1
,
dtype
=
odtype
))]
rval
=
[
T
.
fill
(
xsym
,
n
p
.
asarray
(
1
,
dtype
=
odtype
))]
if
n
umpy
.
all
(
y
==
0.5
):
if
n
p
.
all
(
y
==
0.5
):
rval
=
[
T
.
sqrt
(
xsym
)]
rval
=
[
T
.
sqrt
(
xsym
)]
if
n
umpy
.
all
(
y
==
-
0.5
):
if
n
p
.
all
(
y
==
-
0.5
):
rval
=
[
T
.
inv
(
T
.
sqrt
(
xsym
))]
rval
=
[
T
.
inv
(
T
.
sqrt
(
xsym
))]
if
n
umpy
.
all
(
y
==
-
1
):
if
n
p
.
all
(
y
==
-
1
):
rval
=
[
T
.
inv
(
xsym
)]
rval
=
[
T
.
inv
(
xsym
)]
if
n
umpy
.
all
(
y
==
-
2
):
if
n
p
.
all
(
y
==
-
2
):
rval
=
[
T
.
inv
(
T
.
sqr
(
xsym
))]
rval
=
[
T
.
inv
(
T
.
sqr
(
xsym
))]
if
rval
:
if
rval
:
rval
[
0
]
=
T
.
cast
(
rval
[
0
],
odtype
)
rval
[
0
]
=
T
.
cast
(
rval
[
0
],
odtype
)
...
@@ -5951,7 +5951,7 @@ def local_pow_specialize_device(node):
...
@@ -5951,7 +5951,7 @@ def local_pow_specialize_device(node):
# taking the value outside ndarray solve the problem.
# taking the value outside ndarray solve the problem.
# it could be that in that case, numpy make the comparaison
# it could be that in that case, numpy make the comparaison
# into the wrong type(do in int8 that overflow.)
# into the wrong type(do in int8 that overflow.)
if
isinstance
(
y
,
n
umpy
.
ndarray
):
if
isinstance
(
y
,
n
p
.
ndarray
):
assert
y
.
size
==
1
assert
y
.
size
==
1
try
:
try
:
y
=
y
[
0
]
y
=
y
[
0
]
...
@@ -5966,13 +5966,13 @@ def local_pow_specialize_device(node):
...
@@ -5966,13 +5966,13 @@ def local_pow_specialize_device(node):
pow2
=
[
xsym
]
pow2
=
[
xsym
]
pow2_scal
=
[
theano
.
scalar
.
get_scalar_type
(
xsym
.
dtype
)()]
pow2_scal
=
[
theano
.
scalar
.
get_scalar_type
(
xsym
.
dtype
)()]
y_to_do
=
abs
(
y
)
y_to_do
=
abs
(
y
)
for
i
in
xrange
(
int
(
n
umpy
.
log2
(
y_to_do
))):
for
i
in
xrange
(
int
(
n
p
.
log2
(
y_to_do
))):
pow2
.
append
(
T
.
sqr
(
pow2
[
i
]))
pow2
.
append
(
T
.
sqr
(
pow2
[
i
]))
pow2_scal
.
append
(
theano
.
scalar
.
sqr
(
pow2_scal
[
i
]))
pow2_scal
.
append
(
theano
.
scalar
.
sqr
(
pow2_scal
[
i
]))
rval1
=
None
rval1
=
None
rval1_scal
=
None
rval1_scal
=
None
while
y_to_do
>
0
:
while
y_to_do
>
0
:
log_to_do
=
int
(
n
umpy
.
log2
(
y_to_do
))
log_to_do
=
int
(
n
p
.
log2
(
y_to_do
))
if
rval1
:
if
rval1
:
rval1
*=
pow2
[
log_to_do
]
rval1
*=
pow2
[
log_to_do
]
rval1_scal
*=
pow2_scal
[
log_to_do
]
rval1_scal
*=
pow2_scal
[
log_to_do
]
...
@@ -6061,7 +6061,7 @@ def local_mul_specialize(node):
...
@@ -6061,7 +6061,7 @@ def local_mul_specialize(node):
elif
neg
:
elif
neg
:
# Don't add an extra neg node as we can't
# Don't add an extra neg node as we can't
# fully replace this mul by a neg.
# fully replace this mul by a neg.
m1
=
n
umpy
.
asarray
(
-
1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
m1
=
n
p
.
asarray
(
-
1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
new_inputs
=
[
m1
]
+
new_inputs
new_inputs
=
[
m1
]
+
new_inputs
rval
=
T
.
mul
(
*
new_inputs
)
rval
=
T
.
mul
(
*
new_inputs
)
...
@@ -6092,7 +6092,7 @@ def local_add_specialize(node):
...
@@ -6092,7 +6092,7 @@ def local_add_specialize(node):
y
=
get_scalar_constant_value
(
input
)
y
=
get_scalar_constant_value
(
input
)
except
NotScalarConstantError
:
except
NotScalarConstantError
:
y
=
input
y
=
input
if
n
umpy
.
all
(
y
==
0.0
):
if
n
p
.
all
(
y
==
0.0
):
continue
continue
new_inputs
.
append
(
input
)
new_inputs
.
append
(
input
)
...
@@ -6102,7 +6102,7 @@ def local_add_specialize(node):
...
@@ -6102,7 +6102,7 @@ def local_add_specialize(node):
# we got rid of the entire expression!
# we got rid of the entire expression!
ndim
=
node
.
outputs
[
0
]
.
type
.
ndim
ndim
=
node
.
outputs
[
0
]
.
type
.
ndim
# Reuse call to constant for cache()
# Reuse call to constant for cache()
cst
=
T
.
constant
(
n
umpy
.
zeros
((
1
,)
*
ndim
,
dtype
=
dtype
))
cst
=
T
.
constant
(
n
p
.
zeros
((
1
,)
*
ndim
,
dtype
=
dtype
))
assert
cst
.
type
.
broadcastable
==
(
True
,)
*
ndim
assert
cst
.
type
.
broadcastable
==
(
True
,)
*
ndim
return
fill_chain
(
cst
)
return
fill_chain
(
cst
)
...
@@ -6209,7 +6209,7 @@ def local_log1p(node):
...
@@ -6209,7 +6209,7 @@ def local_log1p(node):
scalars
,
scalar_inputs
,
nonconsts
=
scalarconsts_rest
(
scalars
,
scalar_inputs
,
nonconsts
=
scalarconsts_rest
(
log_arg
.
owner
.
inputs
,
only_process_constants
=
True
)
log_arg
.
owner
.
inputs
,
only_process_constants
=
True
)
# scalar_inputs are potentially dimshuffled and fill'd scalars
# scalar_inputs are potentially dimshuffled and fill'd scalars
if
scalars
and
n
umpy
.
allclose
(
numpy
.
sum
(
scalars
),
1
):
if
scalars
and
n
p
.
allclose
(
np
.
sum
(
scalars
),
1
):
if
nonconsts
:
if
nonconsts
:
if
len
(
nonconsts
)
>
1
:
if
len
(
nonconsts
)
>
1
:
ninp
=
T
.
add
(
*
nonconsts
)
ninp
=
T
.
add
(
*
nonconsts
)
...
@@ -6315,9 +6315,9 @@ def add_calculate(num, denum, aslist=False, out_type=None):
...
@@ -6315,9 +6315,9 @@ def add_calculate(num, denum, aslist=False, out_type=None):
zero
=
theano
.
_asarray
(
0
,
dtype
=
out_type
.
dtype
)
zero
=
theano
.
_asarray
(
0
,
dtype
=
out_type
.
dtype
)
# zero = 0.0 if out_type is None else theano._asarray(0,
# zero = 0.0 if out_type is None else theano._asarray(0,
# dtype=out_type.dtype)
# dtype=out_type.dtype)
v
=
reduce
(
n
umpy
.
add
,
num
,
zero
)
-
reduce
(
numpy
.
add
,
denum
,
zero
)
v
=
reduce
(
n
p
.
add
,
num
,
zero
)
-
reduce
(
np
.
add
,
denum
,
zero
)
if
aslist
:
if
aslist
:
if
n
umpy
.
all
(
v
==
0
):
if
n
p
.
all
(
v
==
0
):
return
[]
return
[]
else
:
else
:
return
[
v
]
return
[
v
]
...
@@ -6708,7 +6708,7 @@ def local_log_erfc(node):
...
@@ -6708,7 +6708,7 @@ def local_log_erfc(node):
node
.
tag
.
local_log_erfc_applied
=
True
node
.
tag
.
local_log_erfc_applied
=
True
x
=
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
x
=
node
.
inputs
[
0
]
.
owner
.
inputs
[
0
]
stab_value
=
(
-
x
**
2
-
T
.
log
(
x
)
-
.
5
*
T
.
log
(
n
umpy
.
pi
)
+
stab_value
=
(
-
x
**
2
-
T
.
log
(
x
)
-
.
5
*
T
.
log
(
n
p
.
pi
)
+
T
.
log
(
1
-
1
/
(
2
*
x
**
2
)
+
3
/
(
4
*
x
**
4
)
-
T
.
log
(
1
-
1
/
(
2
*
x
**
2
)
+
3
/
(
4
*
x
**
4
)
-
15
/
(
8
*
x
**
6
)))
15
/
(
8
*
x
**
6
)))
...
@@ -6863,7 +6863,7 @@ def local_grad_log_erfc_neg(node):
...
@@ -6863,7 +6863,7 @@ def local_grad_log_erfc_neg(node):
# aaron value
# aaron value
stab_value
=
(
x
*
T
.
pow
(
1
-
1
/
(
2
*
(
x
**
2
))
+
stab_value
=
(
x
*
T
.
pow
(
1
-
1
/
(
2
*
(
x
**
2
))
+
3
/
(
4
*
(
x
**
4
))
-
15
/
(
8
*
(
x
**
6
)),
-
1
)
*
3
/
(
4
*
(
x
**
4
))
-
15
/
(
8
*
(
x
**
6
)),
-
1
)
*
T
.
cast
(
T
.
sqrt
(
n
umpy
.
pi
),
dtype
=
x
.
dtype
))
T
.
cast
(
T
.
sqrt
(
n
p
.
pi
),
dtype
=
x
.
dtype
))
if
x
.
dtype
==
'float32'
or
x
.
dtype
==
'float16'
:
if
x
.
dtype
==
'float32'
or
x
.
dtype
==
'float16'
:
threshold
=
9.3
threshold
=
9.3
...
...
theano/tensor/raw_random.py
浏览文件 @
ef9f6efc
...
@@ -4,7 +4,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -4,7 +4,7 @@ from __future__ import absolute_import, print_function, division
import
sys
import
sys
from
copy
import
copy
from
copy
import
copy
import
numpy
import
numpy
as
np
from
six
import
string_types
from
six
import
string_types
from
six.moves
import
reduce
,
xrange
from
six.moves
import
reduce
,
xrange
...
@@ -38,7 +38,7 @@ class RandomStateType(gof.Type):
...
@@ -38,7 +38,7 @@ class RandomStateType(gof.Type):
raise
TypeError
()
raise
TypeError
()
def
is_valid_value
(
self
,
a
):
def
is_valid_value
(
self
,
a
):
return
type
(
a
)
==
n
umpy
.
random
.
RandomState
return
type
(
a
)
==
n
p
.
random
.
RandomState
def
values_eq
(
self
,
a
,
b
):
def
values_eq
(
self
,
a
,
b
):
sa
=
a
.
get_state
()
sa
=
a
.
get_state
()
...
@@ -47,7 +47,7 @@ class RandomStateType(gof.Type):
...
@@ -47,7 +47,7 @@ class RandomStateType(gof.Type):
if
sa
[
0
]
!=
sb
[
0
]:
if
sa
[
0
]
!=
sb
[
0
]:
return
False
return
False
# 1-D array of 624 unsigned integer keys
# 1-D array of 624 unsigned integer keys
if
not
n
umpy
.
all
(
sa
[
1
]
==
sb
[
1
]):
if
not
n
p
.
all
(
sa
[
1
]
==
sb
[
1
]):
return
False
return
False
# integer "pos" representing the position in the array
# integer "pos" representing the position in the array
if
sa
[
2
]
!=
sb
[
2
]:
if
sa
[
2
]
!=
sb
[
2
]:
...
@@ -67,17 +67,17 @@ class RandomStateType(gof.Type):
...
@@ -67,17 +67,17 @@ class RandomStateType(gof.Type):
def
get_size
(
self
,
shape_info
):
def
get_size
(
self
,
shape_info
):
# The size is the data, that have constant size.
# The size is the data, that have constant size.
state
=
n
umpy
.
random
.
RandomState
()
.
get_state
()
state
=
n
p
.
random
.
RandomState
()
.
get_state
()
size
=
0
size
=
0
for
elem
in
state
:
for
elem
in
state
:
if
isinstance
(
elem
,
str
):
if
isinstance
(
elem
,
str
):
size
+=
len
(
elem
)
size
+=
len
(
elem
)
elif
isinstance
(
elem
,
n
umpy
.
ndarray
):
elif
isinstance
(
elem
,
n
p
.
ndarray
):
size
+=
elem
.
size
*
elem
.
itemsize
size
+=
elem
.
size
*
elem
.
itemsize
elif
isinstance
(
elem
,
int
):
elif
isinstance
(
elem
,
int
):
size
+=
n
umpy
.
dtype
(
"int"
)
.
itemsize
size
+=
n
p
.
dtype
(
"int"
)
.
itemsize
elif
isinstance
(
elem
,
float
):
elif
isinstance
(
elem
,
float
):
size
+=
n
umpy
.
dtype
(
"float"
)
.
itemsize
size
+=
n
p
.
dtype
(
"float"
)
.
itemsize
else
:
else
:
raise
NotImplementedError
()
raise
NotImplementedError
()
return
size
return
size
...
@@ -151,7 +151,7 @@ class RandomFunction(gof.Op):
...
@@ -151,7 +151,7 @@ class RandomFunction(gof.Op):
fn
,
outtype
,
inplace
,
ndim_added
=
state
fn
,
outtype
,
inplace
,
ndim_added
=
state
self
.
fn
=
fn
self
.
fn
=
fn
if
isinstance
(
fn
,
string_types
):
if
isinstance
(
fn
,
string_types
):
self
.
exec_fn
=
getattr
(
n
umpy
.
random
.
RandomState
,
fn
)
self
.
exec_fn
=
getattr
(
n
p
.
random
.
RandomState
,
fn
)
else
:
else
:
self
.
exec_fn
=
fn
self
.
exec_fn
=
fn
self
.
outtype
=
outtype
self
.
outtype
=
outtype
...
@@ -240,7 +240,7 @@ class RandomFunction(gof.Op):
...
@@ -240,7 +240,7 @@ class RandomFunction(gof.Op):
# Numbers are drawn from r if self.inplace is True, and from a
# Numbers are drawn from r if self.inplace is True, and from a
# copy of r if self.inplace is False
# copy of r if self.inplace is False
r
,
shape
,
args
=
inputs
[
0
],
inputs
[
1
],
inputs
[
2
:]
r
,
shape
,
args
=
inputs
[
0
],
inputs
[
1
],
inputs
[
2
:]
assert
type
(
r
)
==
n
umpy
.
random
.
RandomState
,
(
type
(
r
),
r
)
assert
type
(
r
)
==
n
p
.
random
.
RandomState
,
(
type
(
r
),
r
)
# If shape == [], that means no shape is enforced, and numpy is
# If shape == [], that means no shape is enforced, and numpy is
# trusted to draw the appropriate number of samples, numpy uses
# trusted to draw the appropriate number of samples, numpy uses
...
@@ -260,7 +260,7 @@ class RandomFunction(gof.Op):
...
@@ -260,7 +260,7 @@ class RandomFunction(gof.Op):
r
=
copy
(
r
)
r
=
copy
(
r
)
rout
[
0
]
=
r
rout
[
0
]
=
r
rval
=
self
.
exec_fn
(
r
,
*
(
args
+
[
shape
]))
rval
=
self
.
exec_fn
(
r
,
*
(
args
+
[
shape
]))
if
(
not
isinstance
(
rval
,
n
umpy
.
ndarray
)
or
if
(
not
isinstance
(
rval
,
n
p
.
ndarray
)
or
str
(
rval
.
dtype
)
!=
node
.
outputs
[
1
]
.
type
.
dtype
):
str
(
rval
.
dtype
)
!=
node
.
outputs
[
1
]
.
type
.
dtype
):
rval
=
theano
.
_asarray
(
rval
,
dtype
=
node
.
outputs
[
1
]
.
type
.
dtype
)
rval
=
theano
.
_asarray
(
rval
,
dtype
=
node
.
outputs
[
1
]
.
type
.
dtype
)
...
@@ -527,13 +527,13 @@ def binomial(random_state, size=None, n=1, p=0.5, ndim=None,
...
@@ -527,13 +527,13 @@ def binomial(random_state, size=None, n=1, p=0.5, ndim=None,
"""
"""
if
prob
is
not
None
:
if
prob
is
not
None
:
p
=
prob
p
=
prob
print
(
"DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as n
umpy
."
,
file
=
sys
.
stderr
)
print
(
"DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as n
p
."
,
file
=
sys
.
stderr
)
n
=
tensor
.
as_tensor_variable
(
n
)
n
=
tensor
.
as_tensor_variable
(
n
)
p
=
tensor
.
as_tensor_variable
(
p
)
p
=
tensor
.
as_tensor_variable
(
p
)
ndim
,
size
,
bcast
=
_infer_ndim_bcast
(
ndim
,
size
,
n
,
p
)
ndim
,
size
,
bcast
=
_infer_ndim_bcast
(
ndim
,
size
,
n
,
p
)
if
n
.
dtype
==
'int64'
:
if
n
.
dtype
==
'int64'
:
try
:
try
:
n
umpy
.
random
.
binomial
(
n
=
numpy
.
asarray
([
2
,
3
,
4
],
dtype
=
'int64'
),
p
=
numpy
.
asarray
([
.
1
,
.
2
,
.
3
],
dtype
=
'float64'
))
n
p
.
random
.
binomial
(
n
=
np
.
asarray
([
2
,
3
,
4
],
dtype
=
'int64'
),
p
=
np
.
asarray
([
.
1
,
.
2
,
.
3
],
dtype
=
'float64'
))
except
TypeError
:
except
TypeError
:
# THIS WORKS AROUND A NUMPY BUG on 32bit machine
# THIS WORKS AROUND A NUMPY BUG on 32bit machine
n
=
tensor
.
cast
(
n
,
'int32'
)
n
=
tensor
.
cast
(
n
,
'int32'
)
...
@@ -583,7 +583,7 @@ def random_integers_helper(random_state, low, high, size):
...
@@ -583,7 +583,7 @@ def random_integers_helper(random_state, low, high, size):
out_size
=
out_size
+
(
dim_len
,)
out_size
=
out_size
+
(
dim_len
,)
# Build the indices over which to loop
# Build the indices over which to loop
out
=
n
umpy
.
ndarray
(
out_size
)
out
=
n
p
.
ndarray
(
out_size
)
broadcast_ind
=
_generate_broadcasting_indices
(
out_size
,
low
.
shape
,
broadcast_ind
=
_generate_broadcasting_indices
(
out_size
,
low
.
shape
,
high
.
shape
)
high
.
shape
)
# Iterate over these indices, drawing one sample at a time from numpy
# Iterate over these indices, drawing one sample at a time from numpy
...
@@ -716,8 +716,8 @@ def permutation_helper(random_state, n, shape):
...
@@ -716,8 +716,8 @@ def permutation_helper(random_state, n, shape):
shape
=
()
shape
=
()
out_shape
=
list
(
shape
)
out_shape
=
list
(
shape
)
out_shape
.
append
(
n
)
out_shape
.
append
(
n
)
out
=
n
umpy
.
empty
(
out_shape
,
int
)
out
=
n
p
.
empty
(
out_shape
,
int
)
for
i
in
n
umpy
.
ndindex
(
*
shape
):
for
i
in
n
p
.
ndindex
(
*
shape
):
out
[
i
]
=
random_state
.
permutation
(
n
)
out
[
i
]
=
random_state
.
permutation
(
n
)
# print 'RETURNING', out.shape
# print 'RETURNING', out.shape
...
@@ -801,7 +801,7 @@ def multinomial_helper(random_state, n, pvals, size):
...
@@ -801,7 +801,7 @@ def multinomial_helper(random_state, n, pvals, size):
# Build the indices over which to loop
# Build the indices over which to loop
# Note that here, the rows (inner-most 1D subtensors) of pvals and out
# Note that here, the rows (inner-most 1D subtensors) of pvals and out
# are indexed, not their individual elements
# are indexed, not their individual elements
out
=
n
umpy
.
ndarray
(
out_size
)
out
=
n
p
.
ndarray
(
out_size
)
broadcast_ind
=
_generate_broadcasting_indices
(
size
,
n
.
shape
,
broadcast_ind
=
_generate_broadcasting_indices
(
size
,
n
.
shape
,
pvals
.
shape
[:
-
1
])
pvals
.
shape
[:
-
1
])
# Iterate over these indices, drawing from one multinomial at a
# Iterate over these indices, drawing from one multinomial at a
...
@@ -815,16 +815,16 @@ def multinomial_helper(random_state, n, pvals, size):
...
@@ -815,16 +815,16 @@ def multinomial_helper(random_state, n, pvals, size):
# of probabilities meets or exceeds 1.0.
# of probabilities meets or exceeds 1.0.
# In perfect arithmetic this would be correct, but in float32 or
# In perfect arithmetic this would be correct, but in float32 or
# float64 it is too strict.
# float64 it is too strict.
pisum
=
n
umpy
.
sum
(
pvi
)
pisum
=
n
p
.
sum
(
pvi
)
if
1.0
<
pisum
<
1.0
+
1e-5
:
# correct if we went a little over
if
1.0
<
pisum
<
1.0
+
1e-5
:
# correct if we went a little over
# because mtrand.pyx has a ValueError that will trigger if
# because mtrand.pyx has a ValueError that will trigger if
# sum(pvals[:-1]) > 1.0
# sum(pvals[:-1]) > 1.0
pvi
=
pvi
*
(
1.0
-
5e-5
)
pvi
=
pvi
*
(
1.0
-
5e-5
)
# pvi = pvi * .9
# pvi = pvi * .9
pisum
=
n
umpy
.
sum
(
pvi
)
pisum
=
n
p
.
sum
(
pvi
)
elif
pvi
[
-
1
]
<
5e-5
:
# will this even work?
elif
pvi
[
-
1
]
<
5e-5
:
# will this even work?
pvi
=
pvi
*
(
1.0
-
5e-5
)
pvi
=
pvi
*
(
1.0
-
5e-5
)
pisum
=
n
umpy
.
sum
(
pvi
)
pisum
=
n
p
.
sum
(
pvi
)
assert
pisum
<=
1.0
,
pisum
assert
pisum
<=
1.0
,
pisum
out
[
mi
]
=
random_state
.
multinomial
(
n
=
n
[
ni
],
out
[
mi
]
=
random_state
.
multinomial
(
n
=
n
[
ni
],
pvals
=
pvi
.
astype
(
'float64'
))
pvals
=
pvi
.
astype
(
'float64'
))
...
...
theano/tensor/shared_randomstreams.py
浏览文件 @
ef9f6efc
...
@@ -7,7 +7,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -7,7 +7,7 @@ from __future__ import absolute_import, print_function, division
import
copy
import
copy
import
numpy
import
numpy
as
np
from
theano.compile.sharedvalue
import
(
SharedVariable
,
shared_constructor
,
from
theano.compile.sharedvalue
import
(
SharedVariable
,
shared_constructor
,
shared
)
shared
)
...
@@ -27,7 +27,7 @@ def randomstate_constructor(value, name=None, strict=False,
...
@@ -27,7 +27,7 @@ def randomstate_constructor(value, name=None, strict=False,
SharedVariable Constructor for RandomState.
SharedVariable Constructor for RandomState.
"""
"""
if
not
isinstance
(
value
,
n
umpy
.
random
.
RandomState
):
if
not
isinstance
(
value
,
n
p
.
random
.
RandomState
):
raise
TypeError
raise
TypeError
if
not
borrow
:
if
not
borrow
:
value
=
copy
.
deepcopy
(
value
)
value
=
copy
.
deepcopy
(
value
)
...
@@ -65,7 +65,7 @@ class RandomStreams(raw_random.RandomStreamsBase):
...
@@ -65,7 +65,7 @@ class RandomStreams(raw_random.RandomStreamsBase):
# random number generator that provides seeds for member streams.
# random number generator that provides seeds for member streams.
self
.
default_instance_seed
=
seed
self
.
default_instance_seed
=
seed
# numpy.RandomState instance that gen() uses to seed new streams.
# numpy.RandomState instance that gen() uses to seed new streams.
self
.
gen_seedgen
=
n
umpy
.
random
.
RandomState
(
seed
)
self
.
gen_seedgen
=
n
p
.
random
.
RandomState
(
seed
)
def
seed
(
self
,
seed
=
None
):
def
seed
(
self
,
seed
=
None
):
"""
"""
...
@@ -85,10 +85,10 @@ class RandomStreams(raw_random.RandomStreamsBase):
...
@@ -85,10 +85,10 @@ class RandomStreams(raw_random.RandomStreamsBase):
if
seed
is
None
:
if
seed
is
None
:
seed
=
self
.
default_instance_seed
seed
=
self
.
default_instance_seed
seedgen
=
n
umpy
.
random
.
RandomState
(
seed
)
seedgen
=
n
p
.
random
.
RandomState
(
seed
)
for
old_r
,
new_r
in
self
.
state_updates
:
for
old_r
,
new_r
in
self
.
state_updates
:
old_r_seed
=
seedgen
.
randint
(
2
**
30
)
old_r_seed
=
seedgen
.
randint
(
2
**
30
)
old_r
.
set_value
(
n
umpy
.
random
.
RandomState
(
int
(
old_r_seed
)),
old_r
.
set_value
(
n
p
.
random
.
RandomState
(
int
(
old_r_seed
)),
borrow
=
True
)
borrow
=
True
)
def
__getitem__
(
self
,
item
):
def
__getitem__
(
self
,
item
):
...
@@ -161,7 +161,7 @@ class RandomStreams(raw_random.RandomStreamsBase):
...
@@ -161,7 +161,7 @@ class RandomStreams(raw_random.RandomStreamsBase):
"""
"""
seed
=
int
(
self
.
gen_seedgen
.
randint
(
2
**
30
))
seed
=
int
(
self
.
gen_seedgen
.
randint
(
2
**
30
))
random_state_variable
=
shared
(
n
umpy
.
random
.
RandomState
(
seed
))
random_state_variable
=
shared
(
n
p
.
random
.
RandomState
(
seed
))
# Add a reference to distinguish from other shared variables
# Add a reference to distinguish from other shared variables
random_state_variable
.
tag
.
is_rng
=
True
random_state_variable
.
tag
.
is_rng
=
True
new_r
,
out
=
op
(
random_state_variable
,
*
args
,
**
kwargs
)
new_r
,
out
=
op
(
random_state_variable
,
*
args
,
**
kwargs
)
...
...
theano/tensor/sharedvar.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
traceback
import
traceback
import
numpy
import
numpy
as
np
from
six
import
integer_types
from
six
import
integer_types
import
theano.tensor.basic
import
theano.tensor.basic
...
@@ -41,7 +41,7 @@ def tensor_constructor(value, name=None, strict=False, allow_downcast=None,
...
@@ -41,7 +41,7 @@ def tensor_constructor(value, name=None, strict=False, allow_downcast=None,
if
target
!=
'cpu'
:
if
target
!=
'cpu'
:
raise
TypeError
(
'not for cpu'
)
raise
TypeError
(
'not for cpu'
)
if
not
isinstance
(
value
,
n
umpy
.
ndarray
):
if
not
isinstance
(
value
,
n
p
.
ndarray
):
raise
TypeError
()
raise
TypeError
()
# if no broadcastable is given, then the default is to assume that
# if no broadcastable is given, then the default is to assume that
...
@@ -51,7 +51,7 @@ def tensor_constructor(value, name=None, strict=False, allow_downcast=None,
...
@@ -51,7 +51,7 @@ def tensor_constructor(value, name=None, strict=False, allow_downcast=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
=
n
umpy
.
array
(
value
,
copy
=
(
not
borrow
)),
value
=
n
p
.
array
(
value
,
copy
=
(
not
borrow
)),
name
=
name
,
name
=
name
,
strict
=
strict
,
strict
=
strict
,
allow_downcast
=
allow_downcast
)
allow_downcast
=
allow_downcast
)
...
@@ -86,12 +86,12 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None,
...
@@ -86,12 +86,12 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None,
if
target
!=
'cpu'
:
if
target
!=
'cpu'
:
raise
TypeError
(
'not for cpu'
)
raise
TypeError
(
'not for cpu'
)
if
not
isinstance
(
value
,
(
n
umpy
.
number
,
float
,
integer_types
,
complex
)):
if
not
isinstance
(
value
,
(
n
p
.
number
,
float
,
integer_types
,
complex
)):
raise
TypeError
()
raise
TypeError
()
try
:
try
:
dtype
=
value
.
dtype
dtype
=
value
.
dtype
except
Exception
:
except
Exception
:
dtype
=
n
umpy
.
asarray
(
value
)
.
dtype
dtype
=
n
p
.
asarray
(
value
)
.
dtype
dtype
=
str
(
dtype
)
dtype
=
str
(
dtype
)
value
=
theano
.
_asarray
(
value
,
dtype
=
dtype
)
value
=
theano
.
_asarray
(
value
,
dtype
=
dtype
)
...
@@ -101,7 +101,7 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None,
...
@@ -101,7 +101,7 @@ def scalar_constructor(value, name=None, strict=False, allow_downcast=None,
# Do not pass the dtype to asarray because we want this to fail if
# Do not pass the dtype to asarray because we want this to fail if
# strict is True and the types do not match.
# strict is True and the types do not match.
rval
=
ScalarSharedVariable
(
type
=
tensor_type
,
rval
=
ScalarSharedVariable
(
type
=
tensor_type
,
value
=
n
umpy
.
array
(
value
,
copy
=
True
),
value
=
n
p
.
array
(
value
,
copy
=
True
),
name
=
name
,
name
=
name
,
strict
=
strict
,
strict
=
strict
,
allow_downcast
=
allow_downcast
)
allow_downcast
=
allow_downcast
)
...
...
theano/tensor/signal/pool.py
浏览文件 @
ef9f6efc
...
@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -9,7 +9,7 @@ from __future__ import absolute_import, print_function, division
import
warnings
import
warnings
import
itertools
import
itertools
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
six.moves.builtins
as
builtins
import
six.moves.builtins
as
builtins
import
theano
import
theano
...
@@ -412,7 +412,7 @@ class Pool(OpenMPOp):
...
@@ -412,7 +412,7 @@ class Pool(OpenMPOp):
if
isinstance
(
out
,
theano
.
Variable
):
if
isinstance
(
out
,
theano
.
Variable
):
return
tensor
.
maximum
(
out
,
0
)
return
tensor
.
maximum
(
out
,
0
)
else
:
else
:
return
n
umpy
.
maximum
(
out
,
0
)
return
n
p
.
maximum
(
out
,
0
)
else
:
else
:
if
isinstance
(
v
,
theano
.
Variable
):
if
isinstance
(
v
,
theano
.
Variable
):
return
tensor
.
switch
(
tensor
.
ge
(
stride
,
downsample
),
return
tensor
.
switch
(
tensor
.
ge
(
stride
,
downsample
),
...
@@ -516,7 +516,7 @@ class Pool(OpenMPOp):
...
@@ -516,7 +516,7 @@ class Pool(OpenMPOp):
if
not
self
.
ignore_border
:
if
not
self
.
ignore_border
:
assert
all
(
z
>
0
for
z
in
z_shape
[
-
nd
:])
assert
all
(
z
>
0
for
z
in
z_shape
[
-
nd
:])
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
z
[
0
]
=
n
umpy
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
z
[
0
]
=
n
p
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
zz
=
z
[
0
]
zz
=
z
[
0
]
# size of pooling output
# size of pooling output
pool_out_shp
=
zz
.
shape
[
-
nd
:]
pool_out_shp
=
zz
.
shape
[
-
nd
:]
...
@@ -525,16 +525,16 @@ class Pool(OpenMPOp):
...
@@ -525,16 +525,16 @@ class Pool(OpenMPOp):
# pad the image
# pad the image
if
max
(
pad
)
!=
0
:
if
max
(
pad
)
!=
0
:
y
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
else
:
else
:
y
=
x
y
=
x
func
=
n
umpy
.
max
func
=
n
p
.
max
if
self
.
mode
==
'sum'
:
if
self
.
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
self
.
mode
!=
'max'
:
elif
self
.
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
# precompute the region boundaries for each dimension
# precompute the region boundaries for each dimension
region_slices
=
[[]
for
i
in
xrange
(
nd
)]
region_slices
=
[[]
for
i
in
xrange
(
nd
)]
...
@@ -548,11 +548,11 @@ class Pool(OpenMPOp):
...
@@ -548,11 +548,11 @@ class Pool(OpenMPOp):
region_slices
[
i
]
.
append
(
slice
(
start
,
end
))
region_slices
[
i
]
.
append
(
slice
(
start
,
end
))
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
zzk
=
zz
[
k
]
zzk
=
zz
[
k
]
yk
=
y
[
k
]
yk
=
y
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
zzk
[
r
]
=
func
(
zzk
[
r
]
=
func
(
yk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]])
yk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]])
...
@@ -1020,7 +1020,7 @@ class PoolGrad(OpenMPOp):
...
@@ -1020,7 +1020,7 @@ class PoolGrad(OpenMPOp):
if
isinstance
(
out
,
theano
.
Variable
):
if
isinstance
(
out
,
theano
.
Variable
):
return
tensor
.
maximum
(
out
,
0
)
return
tensor
.
maximum
(
out
,
0
)
else
:
else
:
return
n
umpy
.
maximum
(
out
,
0
)
return
n
p
.
maximum
(
out
,
0
)
else
:
else
:
if
isinstance
(
v
,
theano
.
Variable
):
if
isinstance
(
v
,
theano
.
Variable
):
return
tensor
.
switch
(
tensor
.
ge
(
stride
,
downsample
),
return
tensor
.
switch
(
tensor
.
ge
(
stride
,
downsample
),
...
@@ -1128,12 +1128,12 @@ class MaxPoolGrad(PoolGrad):
...
@@ -1128,12 +1128,12 @@ class MaxPoolGrad(PoolGrad):
# pad the image
# pad the image
if
max
(
pad
)
!=
0
:
if
max
(
pad
)
!=
0
:
y
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
else
:
else
:
y
=
x
y
=
x
gx
=
n
umpy
.
zeros_like
(
y
)
gx
=
n
p
.
zeros_like
(
y
)
# precompute the region boundaries for each dimension
# precompute the region boundaries for each dimension
region_ranges
=
[[]
for
i
in
xrange
(
nd
)]
region_ranges
=
[[]
for
i
in
xrange
(
nd
)]
...
@@ -1144,13 +1144,13 @@ class MaxPoolGrad(PoolGrad):
...
@@ -1144,13 +1144,13 @@ class MaxPoolGrad(PoolGrad):
region_ranges
[
i
]
.
append
(
xrange
(
start
,
end
))
region_ranges
[
i
]
.
append
(
xrange
(
start
,
end
))
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
gxk
=
gx
[
k
]
gxk
=
gx
[
k
]
gzk
=
gz
[
k
]
gzk
=
gz
[
k
]
yk
=
y
[
k
]
yk
=
y
[
k
]
maxoutk
=
maxout
[
k
]
maxoutk
=
maxout
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
maxout_value
=
maxoutk
[
r
]
maxout_value
=
maxoutk
[
r
]
# iterate inside region
# iterate inside region
for
c
in
itertools
.
product
(
*
[
region_ranges
[
i
][
r
[
i
]]
for
c
in
itertools
.
product
(
*
[
region_ranges
[
i
][
r
[
i
]]
...
@@ -1444,7 +1444,7 @@ class AveragePoolGrad(PoolGrad):
...
@@ -1444,7 +1444,7 @@ class AveragePoolGrad(PoolGrad):
raise
NotImplementedError
()
raise
NotImplementedError
()
z_shape
=
self
.
out_shape
(
x
.
shape
,
ws
,
self
.
ignore_border
,
stride
,
pad
,
nd
)
z_shape
=
self
.
out_shape
(
x
.
shape
,
ws
,
self
.
ignore_border
,
stride
,
pad
,
nd
)
if
(
gx_stg
[
0
]
is
None
)
or
(
gx_stg
[
0
]
.
shape
!=
z_shape
):
if
(
gx_stg
[
0
]
is
None
)
or
(
gx_stg
[
0
]
.
shape
!=
z_shape
):
gx_stg
[
0
]
=
n
umpy
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
gx_stg
[
0
]
=
n
p
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
zz
=
gx_stg
[
0
]
zz
=
gx_stg
[
0
]
# size of pooling output
# size of pooling output
pool_out_shp
=
zz
.
shape
[
-
nd
:]
pool_out_shp
=
zz
.
shape
[
-
nd
:]
...
@@ -1453,7 +1453,7 @@ class AveragePoolGrad(PoolGrad):
...
@@ -1453,7 +1453,7 @@ class AveragePoolGrad(PoolGrad):
sum_mode
=
self
.
mode
==
'sum'
sum_mode
=
self
.
mode
==
'sum'
# initialize the padded output
# initialize the padded output
gx
=
n
umpy
.
zeros
((
x
.
shape
[:
-
nd
]
+
img_shp
),
dtype
=
x
.
dtype
)
gx
=
n
p
.
zeros
((
x
.
shape
[:
-
nd
]
+
img_shp
),
dtype
=
x
.
dtype
)
# precompute the region boundaries and sizes for each dimension
# precompute the region boundaries and sizes for each dimension
region_slices
=
[[]
for
i
in
xrange
(
nd
)]
region_slices
=
[[]
for
i
in
xrange
(
nd
)]
...
@@ -1470,11 +1470,11 @@ class AveragePoolGrad(PoolGrad):
...
@@ -1470,11 +1470,11 @@ class AveragePoolGrad(PoolGrad):
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
region_slice
=
[
None
]
*
nd
region_slice
=
[
None
]
*
nd
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
gzk
=
gz
[
k
]
gzk
=
gz
[
k
]
gxk
=
gx
[
k
]
gxk
=
gx
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
region_size
=
1
region_size
=
1
for
i
in
xrange
(
nd
):
for
i
in
xrange
(
nd
):
region_slice
[
i
]
=
region_slices
[
i
][
r
[
i
]]
region_slice
[
i
]
=
region_slices
[
i
][
r
[
i
]]
...
@@ -1783,7 +1783,7 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
...
@@ -1783,7 +1783,7 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
'DownsampleFactorMaxGradGrad requires input '
'DownsampleFactorMaxGradGrad requires input '
'with {} or more dimensions'
.
format
(
nd
))
'with {} or more dimensions'
.
format
(
nd
))
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
maxout
.
shape
):
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
maxout
.
shape
):
z
[
0
]
=
n
umpy
.
zeros
(
maxout
.
shape
,
dtype
=
x
.
dtype
)
z
[
0
]
=
n
p
.
zeros
(
maxout
.
shape
,
dtype
=
x
.
dtype
)
ggz
=
z
[
0
]
# grad wrt maxout_grad has the same shape as maxout
ggz
=
z
[
0
]
# grad wrt maxout_grad has the same shape as maxout
# size of pooling output
# size of pooling output
pool_out_shp
=
ggz
.
shape
[
-
nd
:]
pool_out_shp
=
ggz
.
shape
[
-
nd
:]
...
@@ -1791,10 +1791,10 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
...
@@ -1791,10 +1791,10 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
# pad the image and its gradients
# pad the image and its gradients
if
max
(
pad
)
>
0
:
if
max
(
pad
)
>
0
:
y_padded
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y_padded
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y_padded
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
y_padded
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
ggx_padded
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
ggx_padded
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
ggx_padded
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
ggx_padded
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
ggx
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
ggx
...
@@ -1811,13 +1811,13 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
...
@@ -1811,13 +1811,13 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
region_ranges
[
i
]
.
append
(
xrange
(
start
,
end
))
region_ranges
[
i
]
.
append
(
xrange
(
start
,
end
))
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
ggxk
=
ggx_padded
[
k
]
ggxk
=
ggx_padded
[
k
]
ggzk
=
ggz
[
k
]
ggzk
=
ggz
[
k
]
yk
=
y_padded
[
k
]
yk
=
y_padded
[
k
]
maxoutk
=
maxout
[
k
]
maxoutk
=
maxout
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
# iterate inside region
# iterate inside region
maxout_value
=
maxoutk
[
r
]
maxout_value
=
maxoutk
[
r
]
for
c
in
itertools
.
product
(
*
[
region_ranges
[
i
][
r
[
i
]]
for
c
in
itertools
.
product
(
*
[
region_ranges
[
i
][
r
[
i
]]
...
@@ -2113,7 +2113,7 @@ class MaxPoolRop(OpenMPOp):
...
@@ -2113,7 +2113,7 @@ class MaxPoolRop(OpenMPOp):
if
not
self
.
ignore_border
:
if
not
self
.
ignore_border
:
assert
all
(
z
>
0
for
z
in
z_shape
[
-
nd
:])
assert
all
(
z
>
0
for
z
in
z_shape
[
-
nd
:])
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
z
[
0
]
=
n
umpy
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
z
[
0
]
=
n
p
.
empty
(
z_shape
,
dtype
=
x
.
dtype
)
zz
=
z
[
0
]
zz
=
z
[
0
]
# size of pooling output
# size of pooling output
pool_out_shp
=
zz
.
shape
[
-
nd
:]
pool_out_shp
=
zz
.
shape
[
-
nd
:]
...
@@ -2122,10 +2122,10 @@ class MaxPoolRop(OpenMPOp):
...
@@ -2122,10 +2122,10 @@ class MaxPoolRop(OpenMPOp):
# pad the image and the eval point
# pad the image and the eval point
if
max
(
pad
)
!=
0
:
if
max
(
pad
)
!=
0
:
y
=
n
umpy
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
=
n
p
.
zeros
(
x
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
x
.
dtype
)
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
y
[(
slice
(
None
),)
*
(
len
(
x
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
x
ey
=
n
umpy
.
zeros
(
ex
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
ex
.
dtype
)
ey
=
n
p
.
zeros
(
ex
.
shape
[:
-
nd
]
+
img_shp
,
dtype
=
ex
.
dtype
)
ey
[(
slice
(
None
),)
*
(
len
(
ex
.
shape
)
-
nd
)
+
ey
[(
slice
(
None
),)
*
(
len
(
ex
.
shape
)
-
nd
)
+
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
ex
tuple
(
slice
(
pad
[
i
],
img_shp
[
i
]
-
pad
[
i
])
for
i
in
xrange
(
nd
))]
=
ex
else
:
else
:
...
@@ -2144,18 +2144,18 @@ class MaxPoolRop(OpenMPOp):
...
@@ -2144,18 +2144,18 @@ class MaxPoolRop(OpenMPOp):
region_slices
[
i
]
.
append
(
slice
(
start
,
end
))
region_slices
[
i
]
.
append
(
slice
(
start
,
end
))
# iterate over non-pooling dimensions
# iterate over non-pooling dimensions
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
nd
]):
zzk
=
zz
[
k
]
zzk
=
zz
[
k
]
yk
=
y
[
k
]
yk
=
y
[
k
]
eyk
=
ey
[
k
]
eyk
=
ey
[
k
]
# iterate over pooling regions
# iterate over pooling regions
for
r
in
n
umpy
.
ndindex
(
*
pool_out_shp
):
for
r
in
n
p
.
ndindex
(
*
pool_out_shp
):
# current slice in padded input
# current slice in padded input
ykslice
=
yk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]]
ykslice
=
yk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]]
# current slice in eval points
# current slice in eval points
eykslice
=
eyk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]]
eykslice
=
eyk
[[
region_slices
[
i
][
r
[
i
]]
for
i
in
xrange
(
nd
)]]
# indices of maximum
# indices of maximum
idx
=
n
umpy
.
unravel_index
(
numpy
.
argmax
(
ykslice
),
ykslice
.
shape
)
idx
=
n
p
.
unravel_index
(
np
.
argmax
(
ykslice
),
ykslice
.
shape
)
zzk
[
r
]
=
eykslice
[
idx
]
zzk
[
r
]
=
eykslice
[
idx
]
def
c_headers
(
self
):
def
c_headers
(
self
):
...
...
theano/tensor/signal/tests/test_conv.py
浏览文件 @
ef9f6efc
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
import
unittest
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
import
theano
import
theano
import
theano.tensor
as
T
import
theano.tensor
as
T
...
@@ -41,13 +41,13 @@ class TestSignalConv2D(unittest.TestCase):
...
@@ -41,13 +41,13 @@ 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
=
n
umpy
.
random
.
random
(
image_shape
)
image_data
=
n
p
.
random
.
random
(
image_shape
)
filter_data
=
n
umpy
.
random
.
random
(
filter_shape
)
filter_data
=
n
p
.
random
.
random
(
filter_shape
)
theano_output
=
theano_conv
(
image_data
,
filter_data
)
theano_output
=
theano_conv
(
image_data
,
filter_data
)
# REFERENCE IMPLEMENTATION ############
# REFERENCE IMPLEMENTATION ############
out_shape2d
=
n
umpy
.
array
(
image_shape
[
-
2
:])
-
numpy
.
array
(
filter_shape
[
-
2
:])
+
1
out_shape2d
=
n
p
.
array
(
image_shape
[
-
2
:])
-
np
.
array
(
filter_shape
[
-
2
:])
+
1
ref_output
=
n
umpy
.
zeros
(
tuple
(
out_shape2d
))
ref_output
=
n
p
.
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
:])
...
@@ -64,7 +64,7 @@ class TestSignalConv2D(unittest.TestCase):
...
@@ -64,7 +64,7 @@ class TestSignalConv2D(unittest.TestCase):
image2d
=
image_data3d
[
b
,
:,
:]
image2d
=
image_data3d
[
b
,
:,
:]
filter2d
=
filter_data3d
[
k
,
:,
:]
filter2d
=
filter_data3d
[
k
,
:,
:]
output2d
=
n
umpy
.
zeros
(
ref_output
.
shape
)
output2d
=
n
p
.
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
]
+=
(
output2d
[
row
,
col
]
+=
(
...
...
theano/tensor/signal/tests/test_pool.py
浏览文件 @
ef9f6efc
...
@@ -10,7 +10,7 @@ from six.moves import cPickle
...
@@ -10,7 +10,7 @@ from six.moves import cPickle
import
six.moves.builtins
as
builtins
import
six.moves.builtins
as
builtins
import
sys
import
sys
import
numpy
import
numpy
as
np
import
theano
import
theano
import
theano.tensor
as
tensor
import
theano.tensor
as
tensor
...
@@ -46,14 +46,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -46,14 +46,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
=
list
(
input
.
shape
[:
-
2
])
out_shp
=
list
(
input
.
shape
[:
-
2
])
out_shp
.
append
(
input
.
shape
[
-
2
]
//
ws
[
0
]
+
xi
)
out_shp
.
append
(
input
.
shape
[
-
2
]
//
ws
[
0
]
+
xi
)
out_shp
.
append
(
input
.
shape
[
-
1
]
//
ws
[
1
]
+
yi
)
out_shp
.
append
(
input
.
shape
[
-
1
]
//
ws
[
1
]
+
yi
)
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
for
k
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
k
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii
=
i
*
ws
[
0
]
ii
=
i
*
ws
[
0
]
for
j
in
range
(
output_val
.
shape
[
-
1
]):
for
j
in
range
(
output_val
.
shape
[
-
1
]):
...
@@ -78,15 +78,15 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -78,15 +78,15 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
=
list
(
input
.
shape
[:
-
nd
])
out_shp
=
list
(
input
.
shape
[:
-
nd
])
for
i
in
range
(
nd
):
for
i
in
range
(
nd
):
out_shp
.
append
(
input
.
shape
[
-
nd
+
i
]
//
ws
[
i
]
+
si
[
i
])
out_shp
.
append
(
input
.
shape
[
-
nd
+
i
]
//
ws
[
i
]
+
si
[
i
])
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
for
l
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
l
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
r
in
n
umpy
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
for
r
in
n
p
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
patch
=
input
[
l
][
tuple
(
slice
(
r
[
i
]
*
ws
[
i
],
(
r
[
i
]
+
1
)
*
ws
[
i
])
patch
=
input
[
l
][
tuple
(
slice
(
r
[
i
]
*
ws
[
i
],
(
r
[
i
]
+
1
)
*
ws
[
i
])
for
i
in
range
(
nd
))]
for
i
in
range
(
nd
))]
output_val
[
l
][
r
]
=
func
(
patch
)
output_val
[
l
][
r
]
=
func
(
patch
)
...
@@ -104,7 +104,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -104,7 +104,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
assert
ws
[
1
]
>
pad_w
assert
ws
[
1
]
>
pad_w
def
pad_img
(
x
):
def
pad_img
(
x
):
y
=
n
umpy
.
zeros
(
y
=
n
p
.
zeros
(
(
x
.
shape
[
0
],
x
.
shape
[
1
],
(
x
.
shape
[
0
],
x
.
shape
[
1
],
x
.
shape
[
2
]
+
pad_h
*
2
,
x
.
shape
[
3
]
+
pad_w
*
2
),
x
.
shape
[
2
]
+
pad_h
*
2
,
x
.
shape
[
3
]
+
pad_w
*
2
),
dtype
=
x
.
dtype
)
dtype
=
x
.
dtype
)
...
@@ -120,16 +120,16 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -120,16 +120,16 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
.
append
(
out_c
)
out_shp
.
append
(
out_c
)
ws0
,
ws1
=
ws
ws0
,
ws1
=
ws
stride0
,
stride1
=
stride
stride0
,
stride1
=
stride
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
y
=
pad_img
(
x
)
y
=
pad_img
(
x
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
inc_pad
=
mode
==
'average_inc_pad'
inc_pad
=
mode
==
'average_inc_pad'
for
k
in
n
umpy
.
ndindex
(
*
x
.
shape
[:
-
2
]):
for
k
in
n
p
.
ndindex
(
*
x
.
shape
[:
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii_stride
=
i
*
stride
[
0
]
ii_stride
=
i
*
stride
[
0
]
ii_end
=
builtins
.
min
(
ii_stride
+
ws
[
0
],
img_rows
)
ii_end
=
builtins
.
min
(
ii_stride
+
ws
[
0
],
img_rows
)
...
@@ -160,7 +160,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -160,7 +160,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
pad_img
(
x
):
def
pad_img
(
x
):
# initialize padded input
# initialize padded input
y
=
n
umpy
.
zeros
(
y
=
n
p
.
zeros
(
x
.
shape
[
0
:
-
nd
]
+
x
.
shape
[
0
:
-
nd
]
+
tuple
(
x
.
shape
[
-
nd
+
i
]
+
pad
[
i
]
*
2
for
i
in
range
(
nd
)),
tuple
(
x
.
shape
[
-
nd
+
i
]
+
pad
[
i
]
*
2
for
i
in
range
(
nd
)),
dtype
=
x
.
dtype
)
dtype
=
x
.
dtype
)
...
@@ -177,17 +177,17 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -177,17 +177,17 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
padded_size
=
input
.
shape
[
-
nd
+
i
]
+
2
*
pad
[
i
]
padded_size
=
input
.
shape
[
-
nd
+
i
]
+
2
*
pad
[
i
]
pad_img_shp
.
append
(
padded_size
)
pad_img_shp
.
append
(
padded_size
)
out_shp
.
append
((
padded_size
-
ws
[
i
])
//
stride
[
i
]
+
1
)
out_shp
.
append
((
padded_size
-
ws
[
i
])
//
stride
[
i
]
+
1
)
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
padded_input
=
pad_img
(
input
)
padded_input
=
pad_img
(
input
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
inc_pad
=
mode
==
'average_inc_pad'
inc_pad
=
mode
==
'average_inc_pad'
for
l
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
l
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
r
in
n
umpy
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
for
r
in
n
p
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
region
=
[]
region
=
[]
for
i
in
range
(
nd
):
for
i
in
range
(
nd
):
r_stride
=
r
[
i
]
*
stride
[
i
]
r_stride
=
r
[
i
]
*
stride
[
i
]
...
@@ -245,14 +245,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -245,14 +245,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
.
append
(
out_r
)
out_shp
.
append
(
out_r
)
out_shp
.
append
(
out_c
)
out_shp
.
append
(
out_c
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
for
k
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
k
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii_stride
=
i
*
stride
[
0
]
ii_stride
=
i
*
stride
[
0
]
ii_end
=
builtins
.
min
(
ii_stride
+
ws
[
0
],
img_rows
)
ii_end
=
builtins
.
min
(
ii_stride
+
ws
[
0
],
img_rows
)
...
@@ -289,15 +289,15 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -289,15 +289,15 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out
+=
1
out
+=
1
out_shp
.
append
(
out
)
out_shp
.
append
(
out
)
func
=
n
umpy
.
max
func
=
n
p
.
max
if
mode
==
'sum'
:
if
mode
==
'sum'
:
func
=
n
umpy
.
sum
func
=
n
p
.
sum
elif
mode
!=
'max'
:
elif
mode
!=
'max'
:
func
=
n
umpy
.
average
func
=
n
p
.
average
output_val
=
n
umpy
.
zeros
(
out_shp
)
output_val
=
n
p
.
zeros
(
out_shp
)
for
l
in
n
umpy
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
l
in
n
p
.
ndindex
(
*
input
.
shape
[:
-
nd
]):
for
r
in
n
umpy
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
for
r
in
n
p
.
ndindex
(
*
output_val
.
shape
[
-
nd
:]):
region
=
[]
region
=
[]
for
i
in
range
(
nd
):
for
i
in
range
(
nd
):
r_stride
=
r
[
i
]
*
stride
[
i
]
r_stride
=
r
[
i
]
*
stride
[
i
]
...
@@ -308,7 +308,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -308,7 +308,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
return
output_val
return
output_val
def
test_DownsampleFactorMax
(
self
):
def
test_DownsampleFactorMax
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, input size
# maxpool, input size
examples
=
(
examples
=
(
((
2
,),
(
16
,)),
((
2
,),
(
16
,)),
...
@@ -361,13 +361,13 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -361,13 +361,13 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
output_shape
=
Pool
.
out_shape
(
imval
.
shape
,
maxpoolshp
,
output_shape
=
Pool
.
out_shape
(
imval
.
shape
,
maxpoolshp
,
ndim
=
len
(
maxpoolshp
),
ndim
=
len
(
maxpoolshp
),
ignore_border
=
ignore_border
)
ignore_border
=
ignore_border
)
utt
.
assert_allclose
(
n
umpy
.
asarray
(
output_shape
),
numpy_output_val
.
shape
)
utt
.
assert_allclose
(
n
p
.
asarray
(
output_shape
),
numpy_output_val
.
shape
)
f
=
function
([],
maxpool_op
)
f
=
function
([],
maxpool_op
)
output_val
=
f
()
output_val
=
f
()
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
def
test_DownsampleFactorMaxStride
(
self
):
def
test_DownsampleFactorMaxStride
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, ignore_border, input, output sizes
# maxpool, stride, ignore_border, input, output sizes
examples
=
(
examples
=
(
((
1
,
1
),
(
1
,
1
),
True
,
(
4
,
10
,
16
,
16
),
(
4
,
10
,
16
,
16
)),
((
1
,
1
),
(
1
,
1
),
True
,
(
4
,
10
,
16
,
16
),
(
4
,
10
,
16
,
16
)),
...
@@ -426,7 +426,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -426,7 +426,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
def
test_DownsampleFactorMaxStrideExtra
(
self
):
def
test_DownsampleFactorMaxStrideExtra
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
5
,
3
),
(
5
,
3
),
(
5
,
3
),
(
5
,
5
),
(
3
,
2
),
(
7
,
7
),
(
9
,
9
))
maxpoolshps
=
((
5
,
3
),
(
5
,
3
),
(
5
,
3
),
(
5
,
5
),
(
3
,
2
),
(
7
,
7
),
(
9
,
9
))
stridesizes
=
((
3
,
2
),
(
7
,
5
),
(
10
,
6
),
(
1
,
1
),
stridesizes
=
((
3
,
2
),
(
7
,
5
),
(
10
,
6
),
(
1
,
1
),
(
2
,
3
),
(
10
,
10
),
(
1
,
1
))
(
2
,
3
),
(
10
,
10
),
(
1
,
1
))
...
@@ -438,7 +438,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -438,7 +438,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
(
4
,
10
,
4
,
2
),
(
4
,
10
,
1
,
0
),
(
4
,
10
,
1
,
1
),
(
4
,
10
,
4
,
2
),
(
4
,
10
,
1
,
0
),
(
4
,
10
,
1
,
1
),
(
4
,
10
,
0
,
0
),
(
4
,
10
,
1
,
1
))
(
4
,
10
,
0
,
0
),
(
4
,
10
,
1
,
1
))
images
=
tensor
.
dtensor4
()
images
=
tensor
.
dtensor4
()
for
indx
in
n
umpy
.
arange
(
len
(
maxpoolshps
)):
for
indx
in
n
p
.
arange
(
len
(
maxpoolshps
)):
imvsize
=
imvsizs
[
indx
]
imvsize
=
imvsizs
[
indx
]
imval
=
rng
.
rand
(
4
,
10
,
imvsize
[
0
],
imvsize
[
1
])
imval
=
rng
.
rand
(
4
,
10
,
imvsize
[
0
],
imvsize
[
1
])
stride
=
stridesizes
[
indx
]
stride
=
stridesizes
[
indx
]
...
@@ -468,7 +468,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -468,7 +468,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
test_DownsampleFactorMaxPaddingStride
(
self
):
def
test_DownsampleFactorMaxPaddingStride
(
self
):
ignore_border
=
True
# padding does not support ignore_border=False
ignore_border
=
True
# padding does not support ignore_border=False
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, pad, input sizes
# maxpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
3
,),
(
2
,),
(
2
,),
(
5
,)),
((
3
,),
(
2
,),
(
2
,),
(
5
,)),
...
@@ -503,7 +503,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -503,7 +503,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
def
test_DownsampleFactorMaxPaddingStride_grad
(
self
):
def
test_DownsampleFactorMaxPaddingStride_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, pad, input sizes
# maxpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
10
,),
(
5
,),
(
3
,),
(
2
,)),
((
10
,),
(
5
,),
(
3
,),
(
2
,)),
...
@@ -530,7 +530,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -530,7 +530,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_DownsampleFactorMax_grad
(
self
):
def
test_DownsampleFactorMax_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, input sizes
# maxpool, input sizes
examples
=
(
examples
=
(
((
2
,),
(
3
,)),
((
2
,),
(
3
,)),
...
@@ -599,7 +599,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -599,7 +599,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
testcase_func_name
=
utt
.
custom_name_func
)
testcase_func_name
=
utt
.
custom_name_func
)
def
test_DownsampleFactorMax_grad_stride
(
self
,
example
,
ignore_border
,
mode
):
def
test_DownsampleFactorMax_grad_stride
(
self
,
example
,
ignore_border
,
mode
):
# checks the gradient for the case that stride is used
# checks the gradient for the case that stride is used
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
(
maxpoolshp
,
stridesize
,
inputsize
)
=
example
(
maxpoolshp
,
stridesize
,
inputsize
)
=
example
imval
=
rng
.
rand
(
*
inputsize
)
imval
=
rng
.
rand
(
*
inputsize
)
...
@@ -611,7 +611,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -611,7 +611,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_DownsampleFactorMaxGrad_grad
(
self
):
def
test_DownsampleFactorMaxGrad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, input sizes
# maxpool, input sizes
examples
=
(
examples
=
(
((
2
,),
(
2
,)),
((
2
,),
(
2
,)),
...
@@ -649,7 +649,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -649,7 +649,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
def
test_AveragePoolGrad_grad
(
self
):
def
test_AveragePoolGrad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# avgpool, input sizes
# avgpool, input sizes
examples
=
(
examples
=
(
((
2
,),
(
2
,)),
((
2
,),
(
2
,)),
...
@@ -691,7 +691,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -691,7 +691,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
test_DownsampleFactorMaxGrad_grad_stride
(
self
,
example
,
ignore_border
):
def
test_DownsampleFactorMaxGrad_grad_stride
(
self
,
example
,
ignore_border
):
# checks the gradient of the gradient for
# checks the gradient of the gradient for
# the case that stride is used
# the case that stride is used
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
(
maxpoolshp
,
stride
,
inputsize
)
=
example
(
maxpoolshp
,
stride
,
inputsize
)
=
example
imval
=
rng
.
rand
(
*
inputsize
)
imval
=
rng
.
rand
(
*
inputsize
)
grad_shape
=
Pool
.
out_shape
(
grad_shape
=
Pool
.
out_shape
(
...
@@ -699,7 +699,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -699,7 +699,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
ignore_border
=
ignore_border
,
stride
=
stride
)
ignore_border
=
ignore_border
,
stride
=
stride
)
# skip the grad verification when the output is empty
# skip the grad verification when the output is empty
if
n
umpy
.
prod
(
grad_shape
)
!=
0
:
if
n
p
.
prod
(
grad_shape
)
!=
0
:
grad_val
=
rng
.
rand
(
*
grad_shape
)
grad_val
=
rng
.
rand
(
*
grad_shape
)
def
mp
(
input
,
grad
):
def
mp
(
input
,
grad
):
...
@@ -722,7 +722,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -722,7 +722,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def
test_AveragePoolGrad_grad_stride
(
self
,
example
,
ignore_border
,
mode
):
def
test_AveragePoolGrad_grad_stride
(
self
,
example
,
ignore_border
,
mode
):
# checks the gradient of the gradient for
# checks the gradient of the gradient for
# the case that stride is used
# the case that stride is used
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
(
avgpoolshp
,
stride
,
inputsize
)
=
example
(
avgpoolshp
,
stride
,
inputsize
)
=
example
imval
=
rng
.
rand
(
*
inputsize
)
imval
=
rng
.
rand
(
*
inputsize
)
grad_shape
=
Pool
.
out_shape
(
grad_shape
=
Pool
.
out_shape
(
...
@@ -731,7 +731,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -731,7 +731,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
ignore_border
=
ignore_border
,
stride
=
stride
)
ignore_border
=
ignore_border
,
stride
=
stride
)
# skip the grad verification when the output is empty
# skip the grad verification when the output is empty
if
n
umpy
.
prod
(
grad_shape
)
!=
0
:
if
n
p
.
prod
(
grad_shape
)
!=
0
:
grad_val
=
rng
.
rand
(
*
grad_shape
)
grad_val
=
rng
.
rand
(
*
grad_shape
)
def
mp
(
input
,
grad
):
def
mp
(
input
,
grad
):
...
@@ -744,7 +744,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -744,7 +744,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
def
test_DownsampleFactorMaxPaddingStride_grad_grad
(
self
):
def
test_DownsampleFactorMaxPaddingStride_grad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, pad, input sizes
# maxpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
...
@@ -781,7 +781,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -781,7 +781,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
,
grad_val
],
rng
=
rng
)
def
test_AveragePoolPaddingStride_grad_grad
(
self
):
def
test_AveragePoolPaddingStride_grad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# avgpool, stride, pad, input sizes
# avgpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
...
@@ -831,10 +831,10 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -831,10 +831,10 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
# The value has been manually computed from the theoretical gradient,
# The value has been manually computed from the theoretical gradient,
# and confirmed by the implementation.
# and confirmed by the implementation.
assert
n
umpy
.
allclose
(
fn_hess
([
1
,
2
]),
[[
0.
,
0.
],
[
0.
,
982.7667
]])
assert
n
p
.
allclose
(
fn_hess
([
1
,
2
]),
[[
0.
,
0.
],
[
0.
,
982.7667
]])
def
test_DownsampleFactorMaxGradGrad_grad
(
self
):
def
test_DownsampleFactorMaxGradGrad_grad
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
# maxpool, stride, pad, input sizes
# maxpool, stride, pad, input sizes
examples
=
(
examples
=
(
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
((
3
,),
(
2
,),
(
2
,),
(
10
,)),
...
@@ -864,7 +864,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -864,7 +864,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval1
,
imval2
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval1
,
imval2
],
rng
=
rng
)
def
test_max_pool_2d_2D
(
self
):
def
test_max_pool_2d_2D
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),
(
3
,
2
))
maxpoolshps
=
((
1
,
1
),
(
3
,
2
))
imval
=
rng
.
rand
(
4
,
5
)
imval
=
rng
.
rand
(
4
,
5
)
images
=
tensor
.
dmatrix
()
images
=
tensor
.
dmatrix
()
...
@@ -890,7 +890,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -890,7 +890,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool_3d_3D
(
self
):
def
test_max_pool_3d_3D
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
,
1
),
(
3
,
2
,
1
))
maxpoolshps
=
((
1
,
1
,
1
),
(
3
,
2
,
1
))
imval
=
rng
.
rand
(
4
,
5
,
6
)
imval
=
rng
.
rand
(
4
,
5
,
6
)
images
=
tensor
.
dtensor3
()
images
=
tensor
.
dtensor3
()
...
@@ -916,7 +916,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -916,7 +916,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool_3d_3D_deprecated_interface
(
self
):
def
test_max_pool_3d_3D_deprecated_interface
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
,
1
),
(
3
,
2
,
1
))
maxpoolshps
=
((
1
,
1
,
1
),
(
3
,
2
,
1
))
imval
=
rng
.
rand
(
4
,
5
,
6
)
imval
=
rng
.
rand
(
4
,
5
,
6
)
images
=
tensor
.
dtensor3
()
images
=
tensor
.
dtensor3
()
...
@@ -945,12 +945,12 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -945,12 +945,12 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
mode
=
mode
)
mode
=
mode
)
def
test_max_pool_2d_2D_same_size
(
self
):
def
test_max_pool_2d_2D_same_size
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
test_input_array
=
n
umpy
.
array
([[[
test_input_array
=
n
p
.
array
([[[
[
1.
,
2.
,
3.
,
4.
],
[
1.
,
2.
,
3.
,
4.
],
[
5.
,
6.
,
7.
,
8.
]
[
5.
,
6.
,
7.
,
8.
]
]]])
.
astype
(
theano
.
config
.
floatX
)
]]])
.
astype
(
theano
.
config
.
floatX
)
test_answer_array
=
n
umpy
.
array
([[[
test_answer_array
=
n
p
.
array
([[[
[
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
,
0.
],
[
0.
,
6.
,
0.
,
8.
]
[
0.
,
6.
,
0.
,
8.
]
]]])
.
astype
(
theano
.
config
.
floatX
)
]]])
.
astype
(
theano
.
config
.
floatX
)
...
@@ -965,7 +965,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -965,7 +965,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
utt
.
verify_grad
(
mp
,
[
test_input_array
],
rng
=
rng
)
utt
.
verify_grad
(
mp
,
[
test_input_array
],
rng
=
rng
)
def
test_max_pool_2d_3D
(
self
):
def
test_max_pool_2d_3D
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
[(
1
,
2
)]
maxpoolshps
=
[(
1
,
2
)]
imval
=
rng
.
rand
(
2
,
3
,
4
)
imval
=
rng
.
rand
(
2
,
3
,
4
)
images
=
tensor
.
dtensor3
()
images
=
tensor
.
dtensor3
()
...
@@ -992,7 +992,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -992,7 +992,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
# utt.verify_grad(mp, [imval], rng=rng)
# utt.verify_grad(mp, [imval], rng=rng)
def
test_max_pool_2d_6D
(
self
):
def
test_max_pool_2d_6D
(
self
):
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
[(
3
,
2
)]
maxpoolshps
=
[(
3
,
2
)]
imval
=
rng
.
rand
(
2
,
1
,
1
,
1
,
3
,
4
)
imval
=
rng
.
rand
(
2
,
1
,
1
,
1
,
3
,
4
)
images
=
tensor
.
TensorType
(
'float64'
,
[
False
]
*
6
)()
images
=
tensor
.
TensorType
(
'float64'
,
[
False
]
*
6
)()
...
@@ -1022,7 +1022,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -1022,7 +1022,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
image
=
tensor
.
dtensor4
()
image
=
tensor
.
dtensor4
()
maxout
=
tensor
.
dtensor4
()
maxout
=
tensor
.
dtensor4
()
gz
=
tensor
.
dtensor4
()
gz
=
tensor
.
dtensor4
()
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),
(
2
,
2
),
(
3
,
3
),
(
2
,
3
),
(
3
,
2
))
maxpoolshps
=
((
1
,
1
),
(
2
,
2
),
(
3
,
3
),
(
2
,
3
),
(
3
,
2
))
image_val
=
rng
.
rand
(
4
,
6
,
7
,
9
)
image_val
=
rng
.
rand
(
4
,
6
,
7
,
9
)
...
@@ -1078,7 +1078,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -1078,7 +1078,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
window_size
=
tensor
.
ivector
()
window_size
=
tensor
.
ivector
()
stride
=
tensor
.
ivector
()
stride
=
tensor
.
ivector
()
padding
=
tensor
.
ivector
()
padding
=
tensor
.
ivector
()
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
(
1
,
1
,
5
,
5
))
.
astype
(
'float32'
)
data
=
n
p
.
random
.
normal
(
0
,
1
,
(
1
,
1
,
5
,
5
))
.
astype
(
'float32'
)
# checking variable params vs fixed params
# checking variable params vs fixed params
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
...
@@ -1110,7 +1110,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -1110,7 +1110,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
window_size
=
tensor
.
ivector
()
window_size
=
tensor
.
ivector
()
stride
=
tensor
.
ivector
()
stride
=
tensor
.
ivector
()
padding
=
tensor
.
ivector
()
padding
=
tensor
.
ivector
()
data
=
n
umpy
.
random
.
normal
(
0
,
1
,
(
1
,
1
,
5
,
5
))
.
astype
(
'float32'
)
data
=
n
p
.
random
.
normal
(
0
,
1
,
(
1
,
1
,
5
,
5
))
.
astype
(
'float32'
)
# checking variable params vs fixed params
# checking variable params vs fixed params
for
ignore_border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
...
@@ -1172,8 +1172,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -1172,8 +1172,8 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
dz_dx
=
theano
.
gradient
.
grad
(
z
.
sum
(),
x
)
dz_dx
=
theano
.
gradient
.
grad
(
z
.
sum
(),
x
)
new_fct
=
theano
.
function
([
x
],
[
y
,
z
,
dy_dx
,
dz_dx
])
new_fct
=
theano
.
function
([
x
],
[
y
,
z
,
dy_dx
,
dz_dx
])
# 3. Assert that the answer is the same
# 3. Assert that the answer is the same
rng
=
n
umpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
utt
.
fetch_seed
())
image_val
=
rng
.
rand
(
4
,
6
,
7
,
9
)
.
astype
(
n
umpy
.
float32
)
image_val
=
rng
.
rand
(
4
,
6
,
7
,
9
)
.
astype
(
n
p
.
float32
)
old_out
=
old_fct
(
image_val
)
old_out
=
old_fct
(
image_val
)
new_out
=
new_fct
(
image_val
)
new_out
=
new_fct
(
image_val
)
for
o
,
n
in
zip
(
old_out
,
new_out
):
for
o
,
n
in
zip
(
old_out
,
new_out
):
...
...
theano/tensor/slinalg.py
浏览文件 @
ef9f6efc
...
@@ -3,7 +3,7 @@ import logging
...
@@ -3,7 +3,7 @@ import logging
import
warnings
import
warnings
from
six.moves
import
xrange
from
six.moves
import
xrange
import
numpy
import
numpy
as
np
try
:
try
:
import
scipy.linalg
import
scipy.linalg
...
@@ -145,7 +145,7 @@ class CholeskyGrad(Op):
...
@@ -145,7 +145,7 @@ class CholeskyGrad(Op):
dx
=
outputs
[
0
]
dx
=
outputs
[
0
]
N
=
x
.
shape
[
0
]
N
=
x
.
shape
[
0
]
if
self
.
lower
:
if
self
.
lower
:
F
=
n
umpy
.
tril
(
dz
)
F
=
n
p
.
tril
(
dz
)
for
k
in
xrange
(
N
-
1
,
-
1
,
-
1
):
for
k
in
xrange
(
N
-
1
,
-
1
,
-
1
):
for
j
in
xrange
(
k
+
1
,
N
):
for
j
in
xrange
(
k
+
1
,
N
):
for
i
in
xrange
(
j
,
N
):
for
i
in
xrange
(
j
,
N
):
...
@@ -156,7 +156,7 @@ class CholeskyGrad(Op):
...
@@ -156,7 +156,7 @@ class CholeskyGrad(Op):
F
[
k
,
k
]
-=
L
[
j
,
k
]
*
F
[
j
,
k
]
F
[
k
,
k
]
-=
L
[
j
,
k
]
*
F
[
j
,
k
]
F
[
k
,
k
]
/=
(
2
*
L
[
k
,
k
])
F
[
k
,
k
]
/=
(
2
*
L
[
k
,
k
])
else
:
else
:
F
=
n
umpy
.
triu
(
dz
)
F
=
n
p
.
triu
(
dz
)
for
k
in
xrange
(
N
-
1
,
-
1
,
-
1
):
for
k
in
xrange
(
N
-
1
,
-
1
,
-
1
):
for
j
in
xrange
(
k
+
1
,
N
):
for
j
in
xrange
(
k
+
1
,
N
):
for
i
in
xrange
(
j
,
N
):
for
i
in
xrange
(
j
,
N
):
...
@@ -206,8 +206,8 @@ class Solve(Op):
...
@@ -206,8 +206,8 @@ class Solve(Op):
# infer dtype by solving the most simple
# infer dtype by solving the most simple
# case with (1, 1) matrices
# case with (1, 1) matrices
o_dtype
=
scipy
.
linalg
.
solve
(
o_dtype
=
scipy
.
linalg
.
solve
(
n
umpy
.
eye
(
1
)
.
astype
(
A
.
dtype
),
n
p
.
eye
(
1
)
.
astype
(
A
.
dtype
),
n
umpy
.
eye
(
1
)
.
astype
(
b
.
dtype
))
.
dtype
n
p
.
eye
(
1
)
.
astype
(
b
.
dtype
))
.
dtype
x
=
tensor
.
tensor
(
x
=
tensor
.
tensor
(
broadcastable
=
b
.
broadcastable
,
broadcastable
=
b
.
broadcastable
,
dtype
=
o_dtype
)
dtype
=
o_dtype
)
...
@@ -370,11 +370,11 @@ class EigvalshGrad(Op):
...
@@ -370,11 +370,11 @@ class EigvalshGrad(Op):
assert
lower
in
[
True
,
False
]
assert
lower
in
[
True
,
False
]
self
.
lower
=
lower
self
.
lower
=
lower
if
lower
:
if
lower
:
self
.
tri0
=
n
umpy
.
tril
self
.
tri0
=
n
p
.
tril
self
.
tri1
=
lambda
a
:
n
umpy
.
triu
(
a
,
1
)
self
.
tri1
=
lambda
a
:
n
p
.
triu
(
a
,
1
)
else
:
else
:
self
.
tri0
=
n
umpy
.
triu
self
.
tri0
=
n
p
.
triu
self
.
tri1
=
lambda
a
:
n
umpy
.
tril
(
a
,
-
1
)
self
.
tri1
=
lambda
a
:
n
p
.
tril
(
a
,
-
1
)
def
make_node
(
self
,
a
,
b
,
gw
):
def
make_node
(
self
,
a
,
b
,
gw
):
assert
imported_scipy
,
(
assert
imported_scipy
,
(
...
@@ -394,14 +394,14 @@ class EigvalshGrad(Op):
...
@@ -394,14 +394,14 @@ class EigvalshGrad(Op):
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
a
,
b
,
gw
)
=
inputs
(
a
,
b
,
gw
)
=
inputs
w
,
v
=
scipy
.
linalg
.
eigh
(
a
,
b
,
lower
=
self
.
lower
)
w
,
v
=
scipy
.
linalg
.
eigh
(
a
,
b
,
lower
=
self
.
lower
)
gA
=
v
.
dot
(
n
umpy
.
diag
(
gw
)
.
dot
(
v
.
T
))
gA
=
v
.
dot
(
n
p
.
diag
(
gw
)
.
dot
(
v
.
T
))
gB
=
-
v
.
dot
(
n
umpy
.
diag
(
gw
*
w
)
.
dot
(
v
.
T
))
gB
=
-
v
.
dot
(
n
p
.
diag
(
gw
*
w
)
.
dot
(
v
.
T
))
# See EighGrad comments for an explanation of these lines
# See EighGrad comments for an explanation of these lines
out1
=
self
.
tri0
(
gA
)
+
self
.
tri1
(
gA
)
.
T
out1
=
self
.
tri0
(
gA
)
+
self
.
tri1
(
gA
)
.
T
out2
=
self
.
tri0
(
gB
)
+
self
.
tri1
(
gB
)
.
T
out2
=
self
.
tri0
(
gB
)
+
self
.
tri1
(
gB
)
.
T
outputs
[
0
][
0
]
=
n
umpy
.
asarray
(
out1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
outputs
[
0
][
0
]
=
n
p
.
asarray
(
out1
,
dtype
=
node
.
outputs
[
0
]
.
dtype
)
outputs
[
1
][
0
]
=
n
umpy
.
asarray
(
out2
,
dtype
=
node
.
outputs
[
1
]
.
dtype
)
outputs
[
1
][
0
]
=
n
p
.
asarray
(
out2
,
dtype
=
node
.
outputs
[
1
]
.
dtype
)
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
return
[
shapes
[
0
],
shapes
[
1
]]
return
[
shapes
[
0
],
shapes
[
1
]]
...
@@ -510,13 +510,13 @@ class ExpmGrad(Op):
...
@@ -510,13 +510,13 @@ class ExpmGrad(Op):
w
,
V
=
scipy
.
linalg
.
eig
(
A
,
right
=
True
)
w
,
V
=
scipy
.
linalg
.
eig
(
A
,
right
=
True
)
U
=
scipy
.
linalg
.
inv
(
V
)
.
T
U
=
scipy
.
linalg
.
inv
(
V
)
.
T
exp_w
=
n
umpy
.
exp
(
w
)
exp_w
=
n
p
.
exp
(
w
)
X
=
n
umpy
.
subtract
.
outer
(
exp_w
,
exp_w
)
/
numpy
.
subtract
.
outer
(
w
,
w
)
X
=
n
p
.
subtract
.
outer
(
exp_w
,
exp_w
)
/
np
.
subtract
.
outer
(
w
,
w
)
n
umpy
.
fill_diagonal
(
X
,
exp_w
)
n
p
.
fill_diagonal
(
X
,
exp_w
)
Y
=
U
.
dot
(
V
.
T
.
dot
(
gA
)
.
dot
(
U
)
*
X
)
.
dot
(
V
.
T
)
Y
=
U
.
dot
(
V
.
T
.
dot
(
gA
)
.
dot
(
U
)
*
X
)
.
dot
(
V
.
T
)
with
warnings
.
catch_warnings
():
with
warnings
.
catch_warnings
():
warnings
.
simplefilter
(
"ignore"
,
n
umpy
.
ComplexWarning
)
warnings
.
simplefilter
(
"ignore"
,
n
p
.
ComplexWarning
)
out
[
0
]
=
Y
.
astype
(
A
.
dtype
)
out
[
0
]
=
Y
.
astype
(
A
.
dtype
)
...
...
theano/tensor/subtensor.py
浏览文件 @
ef9f6efc
...
@@ -4,7 +4,7 @@ from textwrap import dedent
...
@@ -4,7 +4,7 @@ from textwrap import dedent
import
warnings
import
warnings
import
logging
import
logging
import
numpy
import
numpy
as
np
from
six
import
integer_types
from
six
import
integer_types
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -58,7 +58,7 @@ def make_constant(args):
...
@@ -58,7 +58,7 @@ def make_constant(args):
return
slice
(
conv
(
a
.
start
),
return
slice
(
conv
(
a
.
start
),
conv
(
a
.
stop
),
conv
(
a
.
stop
),
conv
(
a
.
step
))
conv
(
a
.
step
))
elif
isinstance
(
a
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
a
,
(
integer_types
,
n
p
.
integer
)):
return
scal
.
ScalarConstant
(
scal
.
int64
,
a
)
return
scal
.
ScalarConstant
(
scal
.
int64
,
a
)
else
:
else
:
return
a
return
a
...
@@ -355,11 +355,11 @@ class Subtensor(Op):
...
@@ -355,11 +355,11 @@ class Subtensor(Op):
if
(
isinstance
(
entry
,
gof
.
Variable
)
and
if
(
isinstance
(
entry
,
gof
.
Variable
)
and
entry
.
type
in
tensor_types
and
entry
.
type
in
tensor_types
and
n
umpy
.
all
(
entry
.
type
.
broadcastable
)):
n
p
.
all
(
entry
.
type
.
broadcastable
)):
return
scal
.
get_scalar_type
(
entry
.
type
.
dtype
)
return
scal
.
get_scalar_type
(
entry
.
type
.
dtype
)
elif
(
isinstance
(
entry
,
gof
.
Type
)
and
elif
(
isinstance
(
entry
,
gof
.
Type
)
and
entry
in
tensor_types
and
entry
in
tensor_types
and
n
umpy
.
all
(
entry
.
broadcastable
)):
n
p
.
all
(
entry
.
broadcastable
)):
return
scal
.
get_scalar_type
(
entry
.
dtype
)
return
scal
.
get_scalar_type
(
entry
.
dtype
)
elif
slice_ok
and
isinstance
(
entry
,
slice
):
elif
slice_ok
and
isinstance
(
entry
,
slice
):
a
=
entry
.
start
a
=
entry
.
start
...
@@ -385,7 +385,7 @@ class Subtensor(Op):
...
@@ -385,7 +385,7 @@ class Subtensor(Op):
slice_c
=
None
slice_c
=
None
return
slice
(
slice_a
,
slice_b
,
slice_c
)
return
slice
(
slice_a
,
slice_b
,
slice_c
)
elif
isinstance
(
entry
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
entry
,
(
integer_types
,
n
p
.
integer
)):
# Disallow the use of python scalars in idx_list
# Disallow the use of python scalars in idx_list
raise
TypeError
(
"Python scalar in idx_list."
raise
TypeError
(
"Python scalar in idx_list."
"Please report this error to theano-dev."
)
"Please report this error to theano-dev."
)
...
@@ -510,8 +510,8 @@ class Subtensor(Op):
...
@@ -510,8 +510,8 @@ class Subtensor(Op):
if
start
is
None
:
if
start
is
None
:
start
=
0
start
=
0
if
(
p
.
stop
is
None
or
if
(
p
.
stop
is
None
or
(
isinstance
(
p
.
stop
,
(
integer_types
,
n
umpy
.
integer
,
(
isinstance
(
p
.
stop
,
(
integer_types
,
n
p
.
integer
,
n
umpy
.
ndarray
))
and
n
p
.
ndarray
))
and
p
.
stop
>
start
)):
p
.
stop
>
start
)):
broadcastable
.
append
(
True
)
broadcastable
.
append
(
True
)
continue
continue
...
@@ -531,7 +531,7 @@ class Subtensor(Op):
...
@@ -531,7 +531,7 @@ class Subtensor(Op):
if
len
(
cdata
)
==
1
:
if
len
(
cdata
)
==
1
:
cdata
=
cdata
[
0
]
cdata
=
cdata
[
0
]
out
[
0
]
=
n
umpy
.
asarray
(
x
.
__getitem__
(
cdata
))
out
[
0
]
=
n
p
.
asarray
(
x
.
__getitem__
(
cdata
))
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
xshp
=
shapes
[
0
]
xshp
=
shapes
[
0
]
...
@@ -681,7 +681,7 @@ class Subtensor(Op):
...
@@ -681,7 +681,7 @@ class Subtensor(Op):
return
pos
[
1
]
return
pos
[
1
]
def
init_entry
(
entry
,
depth
=
0
):
def
init_entry
(
entry
,
depth
=
0
):
if
isinstance
(
entry
,
(
n
umpy
.
integer
,
integer_types
)):
if
isinstance
(
entry
,
(
n
p
.
integer
,
integer_types
)):
init_cmds
.
append
(
init_cmds
.
append
(
"subtensor_spec[
%
i] =
%
i;"
%
(
spec_pos
(),
"subtensor_spec[
%
i] =
%
i;"
%
(
spec_pos
(),
entry
))
entry
))
...
@@ -1390,8 +1390,8 @@ class IncSubtensor(Op):
...
@@ -1390,8 +1390,8 @@ class IncSubtensor(Op):
op_is_set
=
0
op_is_set
=
0
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
view_ndim
=
(
node
.
inputs
[
0
]
.
ndim
-
view_ndim
=
(
node
.
inputs
[
0
]
.
ndim
-
n
umpy
.
sum
([
not
isinstance
(
idx
,
slice
)
n
p
.
sum
([
not
isinstance
(
idx
,
slice
)
for
idx
in
self
.
idx_list
]))
for
idx
in
self
.
idx_list
]))
copy_of_x
=
self
.
copy_of_x
(
x
)
copy_of_x
=
self
.
copy_of_x
(
x
)
...
@@ -1712,11 +1712,11 @@ class AdvancedSubtensor1(Op):
...
@@ -1712,11 +1712,11 @@ class AdvancedSubtensor1(Op):
# We need to check if values in i can fit in numpy.intp, because
# We need to check if values in i can fit in numpy.intp, because
# if they don't, that should be an error (no array can have that
# if they don't, that should be an error (no array can have that
# many elements on a 32-bit arch).
# many elements on a 32-bit arch).
if
i
.
dtype
!=
n
umpy
.
intp
:
if
i
.
dtype
!=
n
p
.
intp
:
i_
=
theano
.
_asarray
(
i
,
dtype
=
n
umpy
.
intp
)
i_
=
theano
.
_asarray
(
i
,
dtype
=
n
p
.
intp
)
if
not
n
umpy
.
can_cast
(
i
.
dtype
,
numpy
.
intp
):
if
not
n
p
.
can_cast
(
i
.
dtype
,
np
.
intp
):
# Check if there was actually an incorrect conversion
# Check if there was actually an incorrect conversion
if
n
umpy
.
any
(
i
!=
i_
):
if
n
p
.
any
(
i
!=
i_
):
raise
IndexError
(
raise
IndexError
(
'index contains values that are bigger '
'index contains values that are bigger '
'than the maximum array size on this system.'
,
i
)
'than the maximum array size on this system.'
,
i
)
...
@@ -1946,7 +1946,7 @@ class AdvancedIncSubtensor1(Op):
...
@@ -1946,7 +1946,7 @@ class AdvancedIncSubtensor1(Op):
return
compile_cutils_code
()
return
compile_cutils_code
()
def
c_code
(
self
,
node
,
name
,
input_names
,
output_names
,
sub
):
def
c_code
(
self
,
node
,
name
,
input_names
,
output_names
,
sub
):
numpy_ver
=
[
int
(
n
)
for
n
in
n
umpy
.
__version__
.
split
(
'.'
)[:
2
]]
numpy_ver
=
[
int
(
n
)
for
n
in
n
p
.
__version__
.
split
(
'.'
)[:
2
]]
if
bool
(
numpy_ver
<
[
1
,
8
]):
if
bool
(
numpy_ver
<
[
1
,
8
]):
raise
NotImplementedError
raise
NotImplementedError
x
,
y
,
idx
=
input_names
x
,
y
,
idx
=
input_names
...
@@ -2113,13 +2113,13 @@ def adv_index_broadcastable_pattern(a, idx):
...
@@ -2113,13 +2113,13 @@ def adv_index_broadcastable_pattern(a, idx):
if
isinstance
(
v
.
type
,
SliceType
):
if
isinstance
(
v
.
type
,
SliceType
):
return
slice
(
None
,
None
)
return
slice
(
None
,
None
)
return
n
umpy
.
zeros
((
2
,)
*
v
.
ndim
,
int
)
return
n
p
.
zeros
((
2
,)
*
v
.
ndim
,
int
)
newidx
=
tuple
(
map
(
replace_slice
,
idx
))
newidx
=
tuple
(
map
(
replace_slice
,
idx
))
# 2 - True = 1; 2 - False = 2
# 2 - True = 1; 2 - False = 2
fakeshape
=
[
2
-
bc
for
bc
in
a
.
broadcastable
]
fakeshape
=
[
2
-
bc
for
bc
in
a
.
broadcastable
]
retshape
=
n
umpy
.
empty
(
fakeshape
)[
newidx
]
.
shape
retshape
=
n
p
.
empty
(
fakeshape
)[
newidx
]
.
shape
return
tuple
([
dim
==
1
for
dim
in
retshape
])
return
tuple
([
dim
==
1
for
dim
in
retshape
])
...
...
theano/tensor/tests/mlp_test.py
浏览文件 @
ef9f6efc
...
@@ -129,7 +129,7 @@ class HiddenLayer(object):
...
@@ -129,7 +129,7 @@ class HiddenLayer(object):
Hidden unit activation is given by: tanh(dot(input,W) + b)
Hidden unit activation is given by: tanh(dot(input,W) + b)
:type rng: n
p
.random.RandomState
:type rng: n
umpy
.random.RandomState
:param rng: a random number generator used to initialize weights
:param rng: a random number generator used to initialize weights
:type input: theano.tensor.dmatrix
:type input: theano.tensor.dmatrix
...
@@ -176,7 +176,7 @@ class MLP(object):
...
@@ -176,7 +176,7 @@ class MLP(object):
def
__init__
(
self
,
rng
,
input
,
n_in
,
n_hidden
,
n_out
):
def
__init__
(
self
,
rng
,
input
,
n_in
,
n_hidden
,
n_out
):
"""Initialize the parameters for the multilayer perceptron
"""Initialize the parameters for the multilayer perceptron
:type rng: n
p
.random.RandomState
:type rng: n
umpy
.random.RandomState
:param rng: a random number generator used to initialize weights
:param rng: a random number generator used to initialize weights
:type input: theano.tensor.TensorType
:type input: theano.tensor.TensorType
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
ef9f6efc
...
@@ -164,8 +164,8 @@ def get_numeric_types(with_int=True, with_float=True, with_complex=False,
...
@@ -164,8 +164,8 @@ def get_numeric_types(with_int=True, with_float=True, with_complex=False,
# Return True if scalars defined from `cls1` are within the hierarchy
# Return True if scalars defined from `cls1` are within the hierarchy
# starting from `cls2`.
# starting from `cls2`.
# The third test below is to catch for instance the fact that
# The third test below is to catch for instance the fact that
# one can use ``dtype=n
p
.number`` and obtain a float64 scalar, even
# one can use ``dtype=n
umpy
.number`` and obtain a float64 scalar, even
# though `n
p.number` is not under `np
.floating` in the class
# though `n
umpy.number` is not under `numpy
.floating` in the class
# hierarchy.
# hierarchy.
return
(
cls1
is
cls2
or
return
(
cls1
is
cls2
or
issubclass
(
cls1
,
cls2
)
or
issubclass
(
cls1
,
cls2
)
or
...
...
theano/tensor/tests/test_blas_c.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
sys
import
sys
import
numpy
import
numpy
as
np
from
unittest
import
TestCase
from
unittest
import
TestCase
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
...
@@ -44,9 +44,9 @@ class TestCGer(TestCase, TestOptimizationMixin):
...
@@ -44,9 +44,9 @@ class TestCGer(TestCase, TestOptimizationMixin):
self
.
a
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
())
self
.
a
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
())
self
.
x
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,))
self
.
x
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,))
self
.
y
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,))
self
.
y
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,))
self
.
Aval
=
n
umpy
.
ones
((
2
,
3
),
dtype
=
dtype
)
self
.
Aval
=
n
p
.
ones
((
2
,
3
),
dtype
=
dtype
)
self
.
xval
=
n
umpy
.
asarray
([
1
,
2
],
dtype
=
dtype
)
self
.
xval
=
n
p
.
asarray
([
1
,
2
],
dtype
=
dtype
)
self
.
yval
=
n
umpy
.
asarray
([
1.5
,
2.7
,
3.9
],
dtype
=
dtype
)
self
.
yval
=
n
p
.
asarray
([
1.5
,
2.7
,
3.9
],
dtype
=
dtype
)
def
function
(
self
,
inputs
,
outputs
):
def
function
(
self
,
inputs
,
outputs
):
return
theano
.
function
(
inputs
,
outputs
,
return
theano
.
function
(
inputs
,
outputs
,
...
@@ -59,7 +59,7 @@ class TestCGer(TestCase, TestOptimizationMixin):
...
@@ -59,7 +59,7 @@ class TestCGer(TestCase, TestOptimizationMixin):
f
(
self
.
Aval
[::
-
1
,
::
-
1
],
self
.
xval
,
self
.
yval
)
f
(
self
.
Aval
[::
-
1
,
::
-
1
],
self
.
xval
,
self
.
yval
)
def
b
(
self
,
bval
):
def
b
(
self
,
bval
):
return
tensor
.
as_tensor_variable
(
n
umpy
.
asarray
(
bval
,
dtype
=
self
.
dtype
))
return
tensor
.
as_tensor_variable
(
n
p
.
asarray
(
bval
,
dtype
=
self
.
dtype
))
def
test_eq
(
self
):
def
test_eq
(
self
):
self
.
assertTrue
(
CGer
(
True
)
==
CGer
(
True
))
self
.
assertTrue
(
CGer
(
True
)
==
CGer
(
True
))
...
@@ -127,13 +127,13 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -127,13 +127,13 @@ class TestCGemv(TestCase, TestOptimizationMixin):
self
.
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'fast_run'
)
self
.
mode
=
theano
.
compile
.
get_default_mode
()
.
including
(
'fast_run'
)
# matrix
# matrix
self
.
A
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,
False
))
self
.
A
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,
False
))
self
.
Aval
=
n
umpy
.
ones
((
2
,
3
),
dtype
=
dtype
)
self
.
Aval
=
n
p
.
ones
((
2
,
3
),
dtype
=
dtype
)
# vector
# vector
self
.
x
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,))
self
.
x
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,))
self
.
y
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,))
self
.
y
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
(
False
,))
self
.
xval
=
n
umpy
.
asarray
([
1
,
2
],
dtype
=
dtype
)
self
.
xval
=
n
p
.
asarray
([
1
,
2
],
dtype
=
dtype
)
self
.
yval
=
n
umpy
.
asarray
([
1.5
,
2.7
,
3.9
],
dtype
=
dtype
)
self
.
yval
=
n
p
.
asarray
([
1.5
,
2.7
,
3.9
],
dtype
=
dtype
)
# scalar
# scalar
self
.
a
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
())
self
.
a
=
tensor
.
tensor
(
dtype
=
dtype
,
broadcastable
=
())
...
@@ -144,11 +144,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -144,11 +144,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
f
=
theano
.
function
([
self
.
A
,
self
.
x
,
self
.
y
,
self
.
a
],
f
=
theano
.
function
([
self
.
A
,
self
.
x
,
self
.
y
,
self
.
a
],
self
.
a
*
self
.
y
+
theano
.
dot
(
self
.
A
,
self
.
x
),
self
.
a
*
self
.
y
+
theano
.
dot
(
self
.
A
,
self
.
x
),
mode
=
mode
)
mode
=
mode
)
Aval
=
n
umpy
.
ones
((
3
,
1
),
dtype
=
self
.
dtype
)
Aval
=
n
p
.
ones
((
3
,
1
),
dtype
=
self
.
dtype
)
xval
=
n
umpy
.
ones
((
1
,),
dtype
=
self
.
dtype
)
xval
=
n
p
.
ones
((
1
,),
dtype
=
self
.
dtype
)
yval
=
float
(
'NaN'
)
*
n
umpy
.
ones
((
3
,),
dtype
=
self
.
dtype
)
yval
=
float
(
'NaN'
)
*
n
p
.
ones
((
3
,),
dtype
=
self
.
dtype
)
zval
=
f
(
Aval
,
xval
,
yval
,
0
)
zval
=
f
(
Aval
,
xval
,
yval
,
0
)
assert
not
n
umpy
.
isnan
(
zval
)
.
any
()
assert
not
n
p
.
isnan
(
zval
)
.
any
()
def
test_optimizations_vm
(
self
):
def
test_optimizations_vm
(
self
):
skip_if_blas_ldflags_empty
()
skip_if_blas_ldflags_empty
()
...
@@ -165,12 +165,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -165,12 +165,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
)
)
# Assert they produce the same output
# Assert they produce the same output
assert
n
umpy
.
allclose
(
f
(
self
.
xval
,
self
.
Aval
),
assert
n
p
.
allclose
(
f
(
self
.
xval
,
self
.
Aval
),
n
umpy
.
dot
(
self
.
xval
,
self
.
Aval
))
n
p
.
dot
(
self
.
xval
,
self
.
Aval
))
# Test with negative strides on 2 dims
# Test with negative strides on 2 dims
assert
n
umpy
.
allclose
(
f
(
self
.
xval
,
self
.
Aval
[::
-
1
,
::
-
1
]),
assert
n
p
.
allclose
(
f
(
self
.
xval
,
self
.
Aval
[::
-
1
,
::
-
1
]),
n
umpy
.
dot
(
self
.
xval
,
self
.
Aval
[::
-
1
,
::
-
1
]))
n
p
.
dot
(
self
.
xval
,
self
.
Aval
[::
-
1
,
::
-
1
]))
def
test_optimizations_mv
(
self
):
def
test_optimizations_mv
(
self
):
skip_if_blas_ldflags_empty
()
skip_if_blas_ldflags_empty
()
...
@@ -187,11 +187,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -187,11 +187,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
)
)
# Assert they produce the same output
# Assert they produce the same output
assert
n
umpy
.
allclose
(
f
(
self
.
Aval
,
self
.
yval
),
assert
n
p
.
allclose
(
f
(
self
.
Aval
,
self
.
yval
),
n
umpy
.
dot
(
self
.
Aval
,
self
.
yval
))
n
p
.
dot
(
self
.
Aval
,
self
.
yval
))
# Test with negative strides on 2 dims
# Test with negative strides on 2 dims
assert
n
umpy
.
allclose
(
f
(
self
.
Aval
[::
-
1
,
::
-
1
],
self
.
yval
),
assert
n
p
.
allclose
(
f
(
self
.
Aval
[::
-
1
,
::
-
1
],
self
.
yval
),
n
umpy
.
dot
(
self
.
Aval
[::
-
1
,
::
-
1
],
self
.
yval
))
n
p
.
dot
(
self
.
Aval
[::
-
1
,
::
-
1
],
self
.
yval
))
def
test_force_gemv_init
(
self
):
def
test_force_gemv_init
(
self
):
if
check_force_gemv_init
():
if
check_force_gemv_init
():
...
@@ -203,20 +203,20 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -203,20 +203,20 @@ class TestCGemv(TestCase, TestOptimizationMixin):
def
t_gemv1
(
self
,
m_shp
):
def
t_gemv1
(
self
,
m_shp
):
''' test vector2 + dot(matrix, vector1) '''
''' test vector2 + dot(matrix, vector1) '''
rng
=
n
umpy
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
rng
=
n
p
.
random
.
RandomState
(
unittest_tools
.
fetch_seed
())
v1
=
theano
.
shared
(
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
m_shp
[
1
],)),
v1
=
theano
.
shared
(
n
p
.
array
(
rng
.
uniform
(
size
=
(
m_shp
[
1
],)),
dtype
=
'float32'
))
dtype
=
'float32'
))
v2_orig
=
n
umpy
.
array
(
rng
.
uniform
(
size
=
(
m_shp
[
0
],)),
dtype
=
'float32'
)
v2_orig
=
n
p
.
array
(
rng
.
uniform
(
size
=
(
m_shp
[
0
],)),
dtype
=
'float32'
)
v2
=
theano
.
shared
(
v2_orig
)
v2
=
theano
.
shared
(
v2_orig
)
m
=
theano
.
shared
(
n
umpy
.
array
(
rng
.
uniform
(
size
=
m_shp
),
m
=
theano
.
shared
(
n
p
.
array
(
rng
.
uniform
(
size
=
m_shp
),
dtype
=
'float32'
))
dtype
=
'float32'
))
f
=
theano
.
function
([],
v2
+
tensor
.
dot
(
m
,
v1
),
f
=
theano
.
function
([],
v2
+
tensor
.
dot
(
m
,
v1
),
mode
=
self
.
mode
)
mode
=
self
.
mode
)
# Assert they produce the same output
# Assert they produce the same output
assert
n
umpy
.
allclose
(
f
(),
assert
n
p
.
allclose
(
f
(),
n
umpy
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
n
p
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
topo
=
[
n
.
op
for
n
in
f
.
maker
.
fgraph
.
toposort
()]
topo
=
[
n
.
op
for
n
in
f
.
maker
.
fgraph
.
toposort
()]
assert
topo
==
[
CGemv
(
inplace
=
False
)],
topo
assert
topo
==
[
CGemv
(
inplace
=
False
)],
topo
...
@@ -227,8 +227,8 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -227,8 +227,8 @@ class TestCGemv(TestCase, TestOptimizationMixin):
# Assert they produce the same output
# Assert they produce the same output
g
()
g
()
assert
n
umpy
.
allclose
(
v2
.
get_value
(),
assert
n
p
.
allclose
(
v2
.
get_value
(),
n
umpy
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
n
p
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
topo
=
[
n
.
op
for
n
in
g
.
maker
.
fgraph
.
toposort
()]
topo
=
[
n
.
op
for
n
in
g
.
maker
.
fgraph
.
toposort
()]
assert
topo
==
[
CGemv
(
inplace
=
True
)]
assert
topo
==
[
CGemv
(
inplace
=
True
)]
...
@@ -237,11 +237,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -237,11 +237,11 @@ class TestCGemv(TestCase, TestOptimizationMixin):
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
m
.
get_value
(
borrow
=
True
)[::
-
1
,
::
-
1
],
borrow
=
True
)
borrow
=
True
)
v2
.
set_value
(
v2_orig
)
v2
.
set_value
(
v2_orig
)
assert
n
umpy
.
allclose
(
f
(),
assert
n
p
.
allclose
(
f
(),
n
umpy
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
n
p
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
g
()
g
()
assert
n
umpy
.
allclose
(
v2
.
get_value
(),
assert
n
p
.
allclose
(
v2
.
get_value
(),
n
umpy
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
n
p
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
def
test_gemv1
(
self
):
def
test_gemv1
(
self
):
skip_if_blas_ldflags_empty
()
skip_if_blas_ldflags_empty
()
...
@@ -265,12 +265,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -265,12 +265,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
mode
=
self
.
mode
)
mode
=
self
.
mode
)
# Matrix value
# Matrix value
A_val
=
n
umpy
.
ones
((
5
,
3
),
dtype
=
dtype
)
A_val
=
n
p
.
ones
((
5
,
3
),
dtype
=
dtype
)
# Different vector length
# Different vector length
ones_3
=
n
umpy
.
ones
(
3
,
dtype
=
dtype
)
ones_3
=
n
p
.
ones
(
3
,
dtype
=
dtype
)
ones_4
=
n
umpy
.
ones
(
4
,
dtype
=
dtype
)
ones_4
=
n
p
.
ones
(
4
,
dtype
=
dtype
)
ones_5
=
n
umpy
.
ones
(
5
,
dtype
=
dtype
)
ones_5
=
n
p
.
ones
(
5
,
dtype
=
dtype
)
ones_6
=
n
umpy
.
ones
(
6
,
dtype
=
dtype
)
ones_6
=
n
p
.
ones
(
6
,
dtype
=
dtype
)
f
(
A_val
,
ones_3
,
ones_5
)
f
(
A_val
,
ones_3
,
ones_5
)
f
(
A_val
[::
-
1
,
::
-
1
],
ones_3
,
ones_5
)
f
(
A_val
[::
-
1
,
::
-
1
],
ones_3
,
ones_5
)
...
@@ -286,12 +286,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
...
@@ -286,12 +286,12 @@ class TestCGemv(TestCase, TestOptimizationMixin):
f
=
theano
.
function
([
x
,
y
,
z
],
f
=
theano
.
function
([
x
,
y
,
z
],
[
tensor
.
dot
(
y
,
x
),
tensor
.
dot
(
z
,
x
)],
[
tensor
.
dot
(
y
,
x
),
tensor
.
dot
(
z
,
x
)],
mode
=
mode_blas_opt
)
mode
=
mode_blas_opt
)
vx
=
n
umpy
.
random
.
rand
(
3
,
3
)
vx
=
n
p
.
random
.
rand
(
3
,
3
)
vy
=
n
umpy
.
random
.
rand
(
3
)
vy
=
n
p
.
random
.
rand
(
3
)
vz
=
n
umpy
.
random
.
rand
(
3
)
vz
=
n
p
.
random
.
rand
(
3
)
out
=
f
(
vx
,
vy
,
vz
)
out
=
f
(
vx
,
vy
,
vz
)
assert
n
umpy
.
allclose
(
out
[
0
],
numpy
.
dot
(
vy
,
vx
))
assert
n
p
.
allclose
(
out
[
0
],
np
.
dot
(
vy
,
vx
))
assert
n
umpy
.
allclose
(
out
[
1
],
numpy
.
dot
(
vz
,
vx
))
assert
n
p
.
allclose
(
out
[
1
],
np
.
dot
(
vz
,
vx
))
assert
len
([
n
for
n
in
f
.
maker
.
fgraph
.
apply_nodes
assert
len
([
n
for
n
in
f
.
maker
.
fgraph
.
apply_nodes
if
isinstance
(
n
.
op
,
tensor
.
AllocEmpty
)])
==
2
if
isinstance
(
n
.
op
,
tensor
.
AllocEmpty
)])
==
2
...
...
theano/tensor/tests/test_casting.py
浏览文件 @
ef9f6efc
...
@@ -16,7 +16,7 @@ class test_casting(unittest.TestCase):
...
@@ -16,7 +16,7 @@ class test_casting(unittest.TestCase):
x
=
type_fn
()
x
=
type_fn
()
f
=
function
([
x
],
op_fn
(
x
))
f
=
function
([
x
],
op_fn
(
x
))
xval
=
theano
.
_asarray
(
n
umpy
.
random
.
rand
(
10
)
*
10
,
xval
=
theano
.
_asarray
(
n
p
.
random
.
rand
(
10
)
*
10
,
dtype
=
type_fn
.
dtype
)
dtype
=
type_fn
.
dtype
)
yval
=
f
(
xval
)
yval
=
f
(
xval
)
assert
(
str
(
yval
.
dtype
)
==
assert
(
str
(
yval
.
dtype
)
==
...
@@ -25,7 +25,7 @@ class test_casting(unittest.TestCase):
...
@@ -25,7 +25,7 @@ class test_casting(unittest.TestCase):
def
test_illegal
(
self
):
def
test_illegal
(
self
):
try
:
try
:
x
=
zmatrix
()
x
=
zmatrix
()
function
([
x
],
cast
(
x
,
'float64'
))(
n
umpy
.
ones
((
2
,
3
),
function
([
x
],
cast
(
x
,
'float64'
))(
n
p
.
ones
((
2
,
3
),
dtype
=
'complex128'
))
dtype
=
'complex128'
))
except
TypeError
:
except
TypeError
:
return
return
...
@@ -44,13 +44,13 @@ class test_casting(unittest.TestCase):
...
@@ -44,13 +44,13 @@ class test_casting(unittest.TestCase):
_convert_to_float64
]):
_convert_to_float64
]):
y
=
converter
(
x
)
y
=
converter
(
x
)
f
=
function
([
compile
.
In
(
x
,
strict
=
True
)],
y
)
f
=
function
([
compile
.
In
(
x
,
strict
=
True
)],
y
)
a
=
n
umpy
.
arange
(
10
,
dtype
=
type1
)
a
=
n
p
.
arange
(
10
,
dtype
=
type1
)
b
=
f
(
a
)
b
=
f
(
a
)
self
.
assertTrue
(
n
umpy
.
all
(
b
==
numpy
.
arange
(
10
,
dtype
=
type2
)))
self
.
assertTrue
(
n
p
.
all
(
b
==
np
.
arange
(
10
,
dtype
=
type2
)))
def
test_convert_to_complex
(
self
):
def
test_convert_to_complex
(
self
):
val64
=
n
umpy
.
ones
(
3
,
dtype
=
'complex64'
)
+
0.5
j
val64
=
n
p
.
ones
(
3
,
dtype
=
'complex64'
)
+
0.5
j
val128
=
n
umpy
.
ones
(
3
,
dtype
=
'complex128'
)
+
0.5
j
val128
=
n
p
.
ones
(
3
,
dtype
=
'complex128'
)
+
0.5
j
vec64
=
TensorType
(
'complex64'
,
(
False
,
))()
vec64
=
TensorType
(
'complex64'
,
(
False
,
))()
vec128
=
TensorType
(
'complex128'
,
(
False
,
))()
vec128
=
TensorType
(
'complex128'
,
(
False
,
))()
...
@@ -70,22 +70,22 @@ class test_casting(unittest.TestCase):
...
@@ -70,22 +70,22 @@ class test_casting(unittest.TestCase):
# upcasting to complex128
# upcasting to complex128
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
,
'float64'
]:
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
,
'float64'
]:
a
=
theano
.
shared
(
n
umpy
.
ones
(
3
,
dtype
=
t
))
a
=
theano
.
shared
(
n
p
.
ones
(
3
,
dtype
=
t
))
b
=
theano
.
shared
(
n
umpy
.
ones
(
3
,
dtype
=
'complex128'
))
b
=
theano
.
shared
(
n
p
.
ones
(
3
,
dtype
=
'complex128'
))
f
=
function
([],
basic
.
_convert_to_complex128
(
a
))
f
=
function
([],
basic
.
_convert_to_complex128
(
a
))
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
# upcasting to complex64
# upcasting to complex64
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
]:
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
]:
a
=
theano
.
shared
(
n
umpy
.
ones
(
3
,
dtype
=
t
))
a
=
theano
.
shared
(
n
p
.
ones
(
3
,
dtype
=
t
))
b
=
theano
.
shared
(
n
umpy
.
ones
(
3
,
dtype
=
'complex64'
))
b
=
theano
.
shared
(
n
p
.
ones
(
3
,
dtype
=
'complex64'
))
f
=
function
([],
basic
.
_convert_to_complex64
(
a
))
f
=
function
([],
basic
.
_convert_to_complex64
(
a
))
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
# downcast to complex64
# downcast to complex64
for
t
in
[
'float64'
]:
for
t
in
[
'float64'
]:
a
=
theano
.
shared
(
n
umpy
.
ones
(
3
,
dtype
=
t
))
a
=
theano
.
shared
(
n
p
.
ones
(
3
,
dtype
=
t
))
b
=
theano
.
shared
(
n
umpy
.
ones
(
3
,
dtype
=
'complex64'
))
b
=
theano
.
shared
(
n
p
.
ones
(
3
,
dtype
=
'complex64'
))
f
=
function
([],
basic
.
_convert_to_complex64
(
a
))
f
=
function
([],
basic
.
_convert_to_complex64
(
a
))
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
...
@@ -96,5 +96,5 @@ class test_casting(unittest.TestCase):
...
@@ -96,5 +96,5 @@ class test_casting(unittest.TestCase):
inputs
=
[
v0
]
inputs
=
[
v0
]
outputs
=
[
v1
]
outputs
=
[
v1
]
f
=
function
(
inputs
,
outputs
)
f
=
function
(
inputs
,
outputs
)
i
=
n
umpy
.
zeros
((
2
,
2
))
i
=
n
p
.
zeros
((
2
,
2
))
assert
(
f
(
i
)
==
n
umpy
.
zeros
((
2
,
2
)))
.
all
()
assert
(
f
(
i
)
==
n
p
.
zeros
((
2
,
2
)))
.
all
()
theano/tensor/tests/test_nlinalg.py
浏览文件 @
ef9f6efc
...
@@ -239,8 +239,8 @@ def test_det_shape():
...
@@ -239,8 +239,8 @@ def test_det_shape():
class
test_diag
(
unittest
.
TestCase
):
class
test_diag
(
unittest
.
TestCase
):
"""
"""
Test that linalg.diag has the same behavior as n
p
.diag.
Test that linalg.diag has the same behavior as n
umpy
.diag.
n
p
.diag has two behaviors:
n
umpy
.diag has two behaviors:
(1) when given a vector, it returns a matrix with that vector as the
(1) when given a vector, it returns a matrix with that vector as the
diagonal.
diagonal.
(2) when given a matrix, returns a vector which is the diagonal of the
(2) when given a matrix, returns a vector which is the diagonal of the
...
...
theano/tensor/type.py
浏览文件 @
ef9f6efc
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
logging
import
logging
import
warnings
import
warnings
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano
import
config
from
theano
import
config
...
@@ -50,7 +50,7 @@ class TensorType(Type):
...
@@ -50,7 +50,7 @@ class TensorType(Type):
self
.
broadcastable
=
tuple
(
bool
(
b
)
for
b
in
broadcastable
)
self
.
broadcastable
=
tuple
(
bool
(
b
)
for
b
in
broadcastable
)
self
.
dtype_specs
()
# error checking is done there
self
.
dtype_specs
()
# error checking is done there
self
.
name
=
name
self
.
name
=
name
self
.
numpy_dtype
=
n
umpy
.
dtype
(
self
.
dtype
)
self
.
numpy_dtype
=
n
p
.
dtype
(
self
.
dtype
)
self
.
sparse_grad
=
sparse_grad
self
.
sparse_grad
=
sparse_grad
if
sparse_grad
:
if
sparse_grad
:
warnings
.
warn
(
warnings
.
warn
(
...
@@ -88,12 +88,12 @@ class TensorType(Type):
...
@@ -88,12 +88,12 @@ class TensorType(Type):
'maybe you are trying to call a function on a (possibly '
'maybe you are trying to call a function on a (possibly '
'shared) variable instead of a numeric array?'
)
'shared) variable instead of a numeric array?'
)
if
((
type
(
data
)
is
n
umpy
.
ndarray
)
and
if
((
type
(
data
)
is
n
p
.
ndarray
)
and
(
data
.
dtype
==
self
.
numpy_dtype
)):
(
data
.
dtype
==
self
.
numpy_dtype
)):
if
data
.
dtype
.
num
!=
self
.
numpy_dtype
.
num
:
if
data
.
dtype
.
num
!=
self
.
numpy_dtype
.
num
:
data
=
theano
.
_asarray
(
data
,
dtype
=
self
.
dtype
)
data
=
theano
.
_asarray
(
data
,
dtype
=
self
.
dtype
)
# -- now fall through to ndim check
# -- now fall through to ndim check
elif
((
type
(
data
)
is
n
umpy
.
memmap
)
and
elif
((
type
(
data
)
is
n
p
.
memmap
)
and
(
data
.
dtype
==
self
.
numpy_dtype
)):
(
data
.
dtype
==
self
.
numpy_dtype
)):
# numpy.memmap is a "safe" subclass of ndarray,
# numpy.memmap is a "safe" subclass of ndarray,
# so we can use it whereever we expect a base ndarray.
# so we can use it whereever we expect a base ndarray.
...
@@ -103,7 +103,7 @@ class TensorType(Type):
...
@@ -103,7 +103,7 @@ class TensorType(Type):
elif
strict
:
elif
strict
:
# If any of the two conditions above was not met,
# If any of the two conditions above was not met,
# we raise a meaningful TypeError.
# we raise a meaningful TypeError.
if
not
(
type
(
data
)
is
n
umpy
.
ndarray
):
if
not
(
type
(
data
)
is
n
p
.
ndarray
):
raise
TypeError
(
"
%
s expected a ndarray object."
%
self
,
raise
TypeError
(
"
%
s expected a ndarray object."
%
self
,
data
,
type
(
data
))
data
,
type
(
data
))
if
data
.
dtype
!=
self
.
numpy_dtype
:
if
data
.
dtype
!=
self
.
numpy_dtype
:
...
@@ -118,7 +118,7 @@ class TensorType(Type):
...
@@ -118,7 +118,7 @@ class TensorType(Type):
# TODO: consider to pad shape with ones to make it consistent
# TODO: consider to pad shape with ones to make it consistent
# with self.broadcastable... like vector->row type thing
# with self.broadcastable... like vector->row type thing
else
:
else
:
if
isinstance
(
data
,
n
umpy
.
ndarray
):
if
isinstance
(
data
,
n
p
.
ndarray
):
# Check if self.dtype can accurately represent data
# Check if self.dtype can accurately represent data
# (do not try to convert the data)
# (do not try to convert the data)
up_dtype
=
scal
.
upcast
(
self
.
dtype
,
data
.
dtype
)
up_dtype
=
scal
.
upcast
(
self
.
dtype
,
data
.
dtype
)
...
@@ -150,7 +150,7 @@ class TensorType(Type):
...
@@ -150,7 +150,7 @@ class TensorType(Type):
converted_data
=
theano
.
_asarray
(
data
,
self
.
dtype
)
converted_data
=
theano
.
_asarray
(
data
,
self
.
dtype
)
# We use the `values_eq` static function from TensorType
# We use the `values_eq` static function from TensorType
# to handle NaN values.
# to handle NaN values.
if
TensorType
.
values_eq
(
n
umpy
.
asarray
(
data
),
if
TensorType
.
values_eq
(
n
p
.
asarray
(
data
),
converted_data
,
converted_data
,
force_same_dtype
=
False
):
force_same_dtype
=
False
):
data
=
converted_data
data
=
converted_data
...
@@ -195,7 +195,7 @@ class TensorType(Type):
...
@@ -195,7 +195,7 @@ class TensorType(Type):
" dimension."
,
data
.
shape
,
self
.
broadcastable
)
" dimension."
,
data
.
shape
,
self
.
broadcastable
)
i
+=
1
i
+=
1
if
(
self
.
filter_checks_isfinite
and
if
(
self
.
filter_checks_isfinite
and
not
n
umpy
.
all
(
numpy
.
isfinite
(
data
))):
not
n
p
.
all
(
np
.
isfinite
(
data
))):
raise
ValueError
(
"non-finite elements not allowed"
)
raise
ValueError
(
"non-finite elements not allowed"
)
return
data
return
data
...
@@ -294,8 +294,8 @@ class TensorType(Type):
...
@@ -294,8 +294,8 @@ class TensorType(Type):
@staticmethod
@staticmethod
def
may_share_memory
(
a
,
b
):
def
may_share_memory
(
a
,
b
):
# This is a method of TensorType, so both a and b should be ndarrays
# This is a method of TensorType, so both a and b should be ndarrays
if
isinstance
(
a
,
n
umpy
.
ndarray
)
and
isinstance
(
b
,
numpy
.
ndarray
):
if
isinstance
(
a
,
n
p
.
ndarray
)
and
isinstance
(
b
,
np
.
ndarray
):
return
n
umpy
.
may_share_memory
(
a
,
b
)
return
n
p
.
may_share_memory
(
a
,
b
)
else
:
else
:
return
False
return
False
...
@@ -308,14 +308,14 @@ class TensorType(Type):
...
@@ -308,14 +308,14 @@ class TensorType(Type):
if
force_same_dtype
and
a
.
dtype
!=
b
.
dtype
:
if
force_same_dtype
and
a
.
dtype
!=
b
.
dtype
:
return
False
return
False
a_eq_b
=
(
a
==
b
)
a_eq_b
=
(
a
==
b
)
r
=
n
umpy
.
all
(
a_eq_b
)
r
=
n
p
.
all
(
a_eq_b
)
if
r
:
if
r
:
return
True
return
True
# maybe the trouble is that there are NaNs
# maybe the trouble is that there are NaNs
a_missing
=
n
umpy
.
isnan
(
a
)
a_missing
=
n
p
.
isnan
(
a
)
if
a_missing
.
any
():
if
a_missing
.
any
():
b_missing
=
n
umpy
.
isnan
(
b
)
b_missing
=
n
p
.
isnan
(
b
)
return
n
umpy
.
all
(
a_eq_b
+
(
a_missing
==
b_missing
))
return
n
p
.
all
(
a_eq_b
+
(
a_missing
==
b_missing
))
else
:
else
:
return
False
return
False
...
@@ -553,7 +553,7 @@ class TensorType(Type):
...
@@ -553,7 +553,7 @@ class TensorType(Type):
Create an numpy ndarray full of 0 values.
Create an numpy ndarray full of 0 values.
"""
"""
return
n
umpy
.
zeros
(
shape
,
dtype
=
self
.
dtype
)
return
n
p
.
zeros
(
shape
,
dtype
=
self
.
dtype
)
def
get_shape_info
(
self
,
obj
):
def
get_shape_info
(
self
,
obj
):
"""
"""
...
@@ -601,9 +601,9 @@ class TensorType(Type):
...
@@ -601,9 +601,9 @@ class TensorType(Type):
"""
"""
if
shape_info
:
if
shape_info
:
return
n
umpy
.
prod
(
shape_info
)
*
numpy
.
dtype
(
self
.
dtype
)
.
itemsize
return
n
p
.
prod
(
shape_info
)
*
np
.
dtype
(
self
.
dtype
)
.
itemsize
else
:
# a scalar
else
:
# a scalar
return
n
umpy
.
dtype
(
self
.
dtype
)
.
itemsize
return
n
p
.
dtype
(
self
.
dtype
)
.
itemsize
theano
.
compile
.
ops
.
expandable_types
+=
(
TensorType
,)
theano
.
compile
.
ops
.
expandable_types
+=
(
TensorType
,)
...
@@ -624,13 +624,13 @@ def values_eq_approx(a, b, allow_remove_inf=False, allow_remove_nan=False,
...
@@ -624,13 +624,13 @@ def values_eq_approx(a, b, allow_remove_inf=False, allow_remove_nan=False,
Absolute tolerance, passed to _allclose.
Absolute tolerance, passed to _allclose.
"""
"""
if
isinstance
(
a
,
n
umpy
.
ndarray
)
and
isinstance
(
b
,
numpy
.
ndarray
):
if
isinstance
(
a
,
n
p
.
ndarray
)
and
isinstance
(
b
,
np
.
ndarray
):
if
a
.
shape
!=
b
.
shape
:
if
a
.
shape
!=
b
.
shape
:
return
False
return
False
if
a
.
dtype
!=
b
.
dtype
:
if
a
.
dtype
!=
b
.
dtype
:
return
False
return
False
if
str
(
a
.
dtype
)
not
in
theano
.
tensor
.
continuous_dtypes
:
if
str
(
a
.
dtype
)
not
in
theano
.
tensor
.
continuous_dtypes
:
return
n
umpy
.
all
(
a
==
b
)
return
n
p
.
all
(
a
==
b
)
else
:
else
:
cmp
=
theano
.
tensor
.
basic
.
_allclose
(
a
,
b
,
rtol
=
rtol
,
atol
=
atol
)
cmp
=
theano
.
tensor
.
basic
.
_allclose
(
a
,
b
,
rtol
=
rtol
,
atol
=
atol
)
if
cmp
:
if
cmp
:
...
@@ -644,38 +644,38 @@ def values_eq_approx(a, b, allow_remove_inf=False, allow_remove_nan=False,
...
@@ -644,38 +644,38 @@ def values_eq_approx(a, b, allow_remove_inf=False, allow_remove_nan=False,
# core recently, so it may not be available to everyone. Thus,
# core recently, so it may not be available to everyone. Thus,
# for now we use a home-made recipe, that should probably be
# for now we use a home-made recipe, that should probably be
# revisited in the future.
# revisited in the future.
a_missing
=
n
umpy
.
isnan
(
a
)
a_missing
=
n
p
.
isnan
(
a
)
a_inf
=
n
umpy
.
isinf
(
a
)
a_inf
=
n
p
.
isinf
(
a
)
if
not
(
a_missing
.
any
()
or
(
allow_remove_inf
and
a_inf
.
any
())):
if
not
(
a_missing
.
any
()
or
(
allow_remove_inf
and
a_inf
.
any
())):
# There are no missing values in a, thus this is not the
# There are no missing values in a, thus this is not the
# reason why numpy.allclose(a, b) returned False.
# reason why numpy.allclose(a, b) returned False.
_logger
.
info
(
_logger
.
info
(
'numpy allclose failed for abs_err
%
f and rel_err
%
f'
,
'numpy allclose failed for abs_err
%
f and rel_err
%
f'
,
n
umpy
.
max
(
abs
(
a
-
b
)),
n
p
.
max
(
abs
(
a
-
b
)),
n
umpy
.
max
(
abs
(
a
-
b
)
/
(
abs
(
a
)
+
abs
(
b
))))
n
p
.
max
(
abs
(
a
-
b
)
/
(
abs
(
a
)
+
abs
(
b
))))
return
False
return
False
# The following line is what numpy.allclose bases its decision
# The following line is what numpy.allclose bases its decision
# upon, according to its documentation.
# upon, according to its documentation.
rtol
=
1.0000000000000001e-05
rtol
=
1.0000000000000001e-05
atol
=
1e-8
atol
=
1e-8
cmp_elemwise
=
(
n
umpy
.
absolute
(
a
-
b
)
<=
cmp_elemwise
=
(
n
p
.
absolute
(
a
-
b
)
<=
(
atol
+
rtol
*
n
umpy
.
absolute
(
b
)))
(
atol
+
rtol
*
n
p
.
absolute
(
b
)))
# Find places where both a and b have missing values.
# Find places where both a and b have missing values.
both_missing
=
a_missing
*
n
umpy
.
isnan
(
b
)
both_missing
=
a_missing
*
n
p
.
isnan
(
b
)
# Find places where both a and b have inf of the same sign.
# Find places where both a and b have inf of the same sign.
both_inf
=
a_inf
*
n
umpy
.
isinf
(
b
)
both_inf
=
a_inf
*
n
p
.
isinf
(
b
)
# cmp_elemwise is weird when we have inf and -inf.
# cmp_elemwise is weird when we have inf and -inf.
# set it to False
# set it to False
cmp_elemwise
=
n
umpy
.
where
(
cmp_elemwise
=
n
p
.
where
(
both_inf
&
cmp_elemwise
,
both_inf
&
cmp_elemwise
,
a
==
b
,
a
==
b
,
cmp_elemwise
)
cmp_elemwise
)
# check the sign of the inf
# check the sign of the inf
both_inf
=
n
umpy
.
where
(
both_inf
,
(
a
==
b
),
both_inf
)
both_inf
=
n
p
.
where
(
both_inf
,
(
a
==
b
),
both_inf
)
if
allow_remove_inf
:
if
allow_remove_inf
:
both_inf
+=
a_inf
both_inf
+=
a_inf
...
...
theano/tensor/type_other.py
浏览文件 @
ef9f6efc
...
@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
# Slice type and Op. None Type and NoneConst.
# Slice type and Op. None Type and NoneConst.
#
#
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano.gof
import
Apply
,
Constant
,
Generic
,
Op
,
Type
,
hashtype
from
theano.gof
import
Apply
,
Constant
,
Generic
,
Op
,
Type
,
hashtype
...
@@ -78,15 +78,15 @@ class SliceConstant(Constant):
...
@@ -78,15 +78,15 @@ class SliceConstant(Constant):
def
__init__
(
self
,
type
,
data
,
name
=
None
):
def
__init__
(
self
,
type
,
data
,
name
=
None
):
assert
isinstance
(
data
,
slice
)
assert
isinstance
(
data
,
slice
)
# Numpy ndarray aren't hashable, so get rid of them.
# Numpy ndarray aren't hashable, so get rid of them.
if
isinstance
(
data
.
start
,
n
umpy
.
ndarray
):
if
isinstance
(
data
.
start
,
n
p
.
ndarray
):
assert
data
.
start
.
ndim
==
0
assert
data
.
start
.
ndim
==
0
assert
str
(
data
.
start
.
dtype
)
in
theano
.
tensor
.
integer_dtypes
assert
str
(
data
.
start
.
dtype
)
in
theano
.
tensor
.
integer_dtypes
data
=
slice
(
int
(
data
.
start
),
data
.
stop
,
data
.
step
)
data
=
slice
(
int
(
data
.
start
),
data
.
stop
,
data
.
step
)
elif
isinstance
(
data
.
stop
,
n
umpy
.
ndarray
):
elif
isinstance
(
data
.
stop
,
n
p
.
ndarray
):
assert
data
.
stop
.
ndim
==
0
assert
data
.
stop
.
ndim
==
0
assert
str
(
data
.
stop
.
dtype
)
in
theano
.
tensor
.
integer_dtypes
assert
str
(
data
.
stop
.
dtype
)
in
theano
.
tensor
.
integer_dtypes
data
=
slice
(
data
.
start
,
int
(
data
.
stop
),
data
.
step
)
data
=
slice
(
data
.
start
,
int
(
data
.
stop
),
data
.
step
)
elif
isinstance
(
data
.
step
,
n
umpy
.
ndarray
):
elif
isinstance
(
data
.
step
,
n
p
.
ndarray
):
assert
data
.
step
.
ndim
==
0
assert
data
.
step
.
ndim
==
0
assert
str
(
data
.
step
.
dtype
)
in
theano
.
tensor
.
integer_dtypes
assert
str
(
data
.
step
.
dtype
)
in
theano
.
tensor
.
integer_dtypes
data
=
slice
(
data
.
start
,
int
(
data
.
stop
),
data
.
step
)
data
=
slice
(
data
.
start
,
int
(
data
.
stop
),
data
.
step
)
...
...
theano/tensor/utils.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
import
theano
import
theano
from
theano.compat
import
izip
from
theano.compat
import
izip
...
@@ -23,7 +23,7 @@ def hash_from_ndarray(data):
...
@@ -23,7 +23,7 @@ def hash_from_ndarray(data):
# too long hash, I call it again on the concatenation of all parts.
# too long hash, I call it again on the concatenation of all parts.
if
not
data
.
flags
[
"C_CONTIGUOUS"
]:
if
not
data
.
flags
[
"C_CONTIGUOUS"
]:
# hash_from_code needs a C-contiguous array.
# hash_from_code needs a C-contiguous array.
data
=
n
umpy
.
ascontiguousarray
(
data
)
data
=
n
p
.
ascontiguousarray
(
data
)
return
hash_from_code
(
hash_from_code
(
data
)
+
return
hash_from_code
(
hash_from_code
(
data
)
+
hash_from_code
(
str
(
data
.
shape
))
+
hash_from_code
(
str
(
data
.
shape
))
+
hash_from_code
(
str
(
data
.
strides
))
+
hash_from_code
(
str
(
data
.
strides
))
+
...
...
theano/tensor/var.py
浏览文件 @
ef9f6efc
...
@@ -4,7 +4,7 @@ import copy
...
@@ -4,7 +4,7 @@ import copy
import
traceback
as
tb
import
traceback
as
tb
import
warnings
import
warnings
import
numpy
import
numpy
as
np
from
six
import
integer_types
from
six
import
integer_types
from
six.moves
import
xrange
from
six.moves
import
xrange
...
@@ -462,7 +462,7 @@ class _tensor_py_operators(object):
...
@@ -462,7 +462,7 @@ class _tensor_py_operators(object):
def
check_bool
(
args_el
):
def
check_bool
(
args_el
):
try
:
try
:
if
(
isinstance
(
args_el
,
(
n
umpy
.
bool_
,
bool
))
or
if
(
isinstance
(
args_el
,
(
n
p
.
bool_
,
bool
))
or
args_el
.
dtype
==
'bool'
):
args_el
.
dtype
==
'bool'
):
raise
TypeError
(
'TensorType does not support boolean '
raise
TypeError
(
'TensorType does not support boolean '
'mask for indexing such as tensor[x==0]. '
'mask for indexing such as tensor[x==0]. '
...
@@ -495,7 +495,7 @@ class _tensor_py_operators(object):
...
@@ -495,7 +495,7 @@ class _tensor_py_operators(object):
elif
len
(
ellipses
)
==
1
:
elif
len
(
ellipses
)
==
1
:
new_axes
=
sum
(
1
new_axes
=
sum
(
1
for
index
in
args
for
index
in
args
if
index
is
n
umpy
.
newaxis
)
# numpy.newaxis is None
if
index
is
n
p
.
newaxis
)
# numpy.newaxis is None
ellipsis_at
=
ellipses
[
0
]
ellipsis_at
=
ellipses
[
0
]
args
=
list
(
args
)
args
=
list
(
args
)
args
[
ellipsis_at
:
ellipsis_at
+
1
]
=
(
args
[
ellipsis_at
:
ellipsis_at
+
1
]
=
(
...
@@ -503,7 +503,7 @@ class _tensor_py_operators(object):
...
@@ -503,7 +503,7 @@ class _tensor_py_operators(object):
# Force input to be int64 datatype if input is an empty list or tuple
# Force input to be int64 datatype if input is an empty list or tuple
# Else leave it as is if it is a real number
# Else leave it as is if it is a real number
args
=
tuple
([
n
umpy
.
array
(
inp
,
dtype
=
numpy
.
int64
)
args
=
tuple
([
n
p
.
array
(
inp
,
dtype
=
np
.
int64
)
if
(
inp
==
[]
or
inp
==
())
else
inp
for
inp
in
args
])
if
(
inp
==
[]
or
inp
==
())
else
inp
for
inp
in
args
])
# Convert python literals to theano constants
# Convert python literals to theano constants
args
=
theano
.
tensor
.
subtensor
.
make_constant
(
args
)
args
=
theano
.
tensor
.
subtensor
.
make_constant
(
args
)
...
@@ -515,7 +515,7 @@ class _tensor_py_operators(object):
...
@@ -515,7 +515,7 @@ class _tensor_py_operators(object):
axis
=
None
axis
=
None
for
i
,
arg
in
enumerate
(
args
):
for
i
,
arg
in
enumerate
(
args
):
try
:
try
:
if
arg
is
not
n
umpy
.
newaxis
:
if
arg
is
not
n
p
.
newaxis
:
theano
.
tensor
.
subtensor
.
Subtensor
.
convert
(
arg
)
theano
.
tensor
.
subtensor
.
Subtensor
.
convert
(
arg
)
except
theano
.
tensor
.
subtensor
.
AdvancedIndexingError
:
except
theano
.
tensor
.
subtensor
.
AdvancedIndexingError
:
if
advanced
:
if
advanced
:
...
@@ -532,14 +532,14 @@ class _tensor_py_operators(object):
...
@@ -532,14 +532,14 @@ class _tensor_py_operators(object):
all
(
isinstance
(
a
,
slice
)
and
all
(
isinstance
(
a
,
slice
)
and
equal_slices
(
a
,
slice
(
None
))
for
a
in
args
[
axis
+
1
:])
and
equal_slices
(
a
,
slice
(
None
))
for
a
in
args
[
axis
+
1
:])
and
isinstance
(
args
[
axis
],
isinstance
(
args
[
axis
],
(
n
umpy
.
ndarray
,
list
,
(
n
p
.
ndarray
,
list
,
TensorVariable
,
TensorConstant
,
TensorVariable
,
TensorConstant
,
theano
.
tensor
.
sharedvar
.
TensorSharedVariable
))):
theano
.
tensor
.
sharedvar
.
TensorSharedVariable
))):
return
self
.
take
(
args
[
axis
],
axis
)
return
self
.
take
(
args
[
axis
],
axis
)
else
:
else
:
return
theano
.
tensor
.
subtensor
.
advanced_subtensor
(
self
,
*
args
)
return
theano
.
tensor
.
subtensor
.
advanced_subtensor
(
self
,
*
args
)
else
:
else
:
if
n
umpy
.
newaxis
in
args
:
if
n
p
.
newaxis
in
args
:
# None (aka np.newaxis) in numpy indexing means to add a
# None (aka np.newaxis) in numpy indexing means to add a
# broadcastable dimension, which theano traditionally did with
# broadcastable dimension, which theano traditionally did with
# the dimshuffle op. The following code converts numpy-style
# the dimshuffle op. The following code converts numpy-style
...
@@ -550,7 +550,7 @@ class _tensor_py_operators(object):
...
@@ -550,7 +550,7 @@ class _tensor_py_operators(object):
pattern
=
[]
pattern
=
[]
new_args
=
[]
new_args
=
[]
for
arg
in
args
:
for
arg
in
args
:
if
arg
==
n
umpy
.
newaxis
:
if
arg
==
n
p
.
newaxis
:
pattern
.
append
(
'x'
)
pattern
.
append
(
'x'
)
new_args
.
append
(
slice
(
None
,
None
,
None
))
new_args
.
append
(
slice
(
None
,
None
,
None
))
else
:
else
:
...
@@ -642,7 +642,7 @@ class _tensor_py_operators(object):
...
@@ -642,7 +642,7 @@ class _tensor_py_operators(object):
def
norm
(
self
,
L
,
axis
=
None
,
keepdims
=
False
):
def
norm
(
self
,
L
,
axis
=
None
,
keepdims
=
False
):
if
L
==
0
:
if
L
==
0
:
raise
NotImplementedError
()
raise
NotImplementedError
()
if
n
umpy
.
isinf
(
L
):
if
n
p
.
isinf
(
L
):
raise
NotImplementedError
()
raise
NotImplementedError
()
# optimizations will/should catch cases like L=1, L=2
# optimizations will/should catch cases like L=1, L=2
y
=
theano
.
tensor
.
basic
.
pow
(
y
=
theano
.
tensor
.
basic
.
pow
(
...
@@ -862,7 +862,7 @@ class TensorConstantSignature(tuple):
...
@@ -862,7 +862,7 @@ class TensorConstantSignature(tuple):
# (note that if there are NaN values in d1, this will return
# (note that if there are NaN values in d1, this will return
# False, which is why we do not bother with testing `other.has_nan`
# False, which is why we do not bother with testing `other.has_nan`
# here).
# here).
return
(
self
.
sum
==
other
.
sum
)
and
n
umpy
.
all
(
d0
==
d1
)
return
(
self
.
sum
==
other
.
sum
)
and
n
p
.
all
(
d0
==
d1
)
def
__hash__
(
self
):
def
__hash__
(
self
):
t
,
d
=
self
t
,
d
=
self
...
@@ -880,25 +880,25 @@ class TensorConstantSignature(tuple):
...
@@ -880,25 +880,25 @@ class TensorConstantSignature(tuple):
self
.
_sum
=
self
.
no_nan
.
sum
()
self
.
_sum
=
self
.
no_nan
.
sum
()
# The following 2 lines are needede as in Python 3.3 with NumPy
# The following 2 lines are needede as in Python 3.3 with NumPy
# 1.7.1, numpy.ndarray and numpy.memmap aren't hashable.
# 1.7.1, numpy.ndarray and numpy.memmap aren't hashable.
if
type
(
self
.
_sum
)
is
n
umpy
.
memmap
:
if
type
(
self
.
_sum
)
is
n
p
.
memmap
:
self
.
_sum
=
n
umpy
.
asarray
(
self
.
_sum
)
.
item
()
self
.
_sum
=
n
p
.
asarray
(
self
.
_sum
)
.
item
()
if
self
.
has_nan
and
self
.
no_nan
.
mask
.
all
():
if
self
.
has_nan
and
self
.
no_nan
.
mask
.
all
():
# In this case the sum is not properly computed by numpy.
# In this case the sum is not properly computed by numpy.
self
.
_sum
=
0
self
.
_sum
=
0
if
n
umpy
.
isinf
(
self
.
_sum
)
or
numpy
.
isnan
(
self
.
_sum
):
if
n
p
.
isinf
(
self
.
_sum
)
or
np
.
isnan
(
self
.
_sum
):
# NaN may happen when there are both -inf and +inf values.
# NaN may happen when there are both -inf and +inf values.
if
self
.
has_nan
:
if
self
.
has_nan
:
# Filter both NaN and Inf values.
# Filter both NaN and Inf values.
mask
=
self
.
no_nan
.
mask
+
n
umpy
.
isinf
(
self
[
1
])
mask
=
self
.
no_nan
.
mask
+
n
p
.
isinf
(
self
[
1
])
else
:
else
:
# Filter only Inf values.
# Filter only Inf values.
mask
=
n
umpy
.
isinf
(
self
[
1
])
mask
=
n
p
.
isinf
(
self
[
1
])
if
mask
.
all
():
if
mask
.
all
():
self
.
_sum
=
0
self
.
_sum
=
0
else
:
else
:
self
.
_sum
=
n
umpy
.
ma
.
masked_array
(
self
[
1
],
mask
)
.
sum
()
self
.
_sum
=
n
p
.
ma
.
masked_array
(
self
[
1
],
mask
)
.
sum
()
# At this point there should be no more NaN.
# At this point there should be no more NaN.
assert
not
n
umpy
.
isnan
(
self
.
_sum
)
assert
not
n
p
.
isnan
(
self
.
_sum
)
return
self
.
_sum
return
self
.
_sum
sum
=
property
(
_get_sum
)
sum
=
property
(
_get_sum
)
...
@@ -906,9 +906,9 @@ class TensorConstantSignature(tuple):
...
@@ -906,9 +906,9 @@ class TensorConstantSignature(tuple):
try
:
try
:
return
self
.
_no_nan
return
self
.
_no_nan
except
AttributeError
:
except
AttributeError
:
nan_mask
=
n
umpy
.
isnan
(
self
[
1
])
nan_mask
=
n
p
.
isnan
(
self
[
1
])
if
nan_mask
.
any
():
if
nan_mask
.
any
():
self
.
_no_nan
=
n
umpy
.
ma
.
masked_array
(
self
[
1
],
nan_mask
)
self
.
_no_nan
=
n
p
.
ma
.
masked_array
(
self
[
1
],
nan_mask
)
self
.
has_nan
=
True
self
.
has_nan
=
True
else
:
else
:
self
.
_no_nan
=
self
[
1
]
self
.
_no_nan
=
self
[
1
]
...
@@ -926,7 +926,7 @@ class TensorConstant(_tensor_py_operators, Constant):
...
@@ -926,7 +926,7 @@ class TensorConstant(_tensor_py_operators, Constant):
def
__init__
(
self
,
type
,
data
,
name
=
None
):
def
__init__
(
self
,
type
,
data
,
name
=
None
):
Constant
.
__init__
(
self
,
type
,
data
,
name
)
Constant
.
__init__
(
self
,
type
,
data
,
name
)
self
.
tag
.
unique_value
=
None
self
.
tag
.
unique_value
=
None
if
isinstance
(
data
,
n
umpy
.
ndarray
)
and
data
.
ndim
>
0
:
if
isinstance
(
data
,
n
p
.
ndarray
)
and
data
.
ndim
>
0
:
flat_data
=
data
.
ravel
()
flat_data
=
data
.
ravel
()
if
flat_data
.
shape
[
0
]:
if
flat_data
.
shape
[
0
]:
if
(
flat_data
==
flat_data
[
0
])
.
all
():
if
(
flat_data
==
flat_data
[
0
])
.
all
():
...
@@ -949,7 +949,7 @@ class TensorConstant(_tensor_py_operators, Constant):
...
@@ -949,7 +949,7 @@ class TensorConstant(_tensor_py_operators, Constant):
def
equals
(
self
,
other
):
def
equals
(
self
,
other
):
# Override Contant.equals to allow to compare with
# Override Contant.equals to allow to compare with
# numpy.ndarray, and python type.
# numpy.ndarray, and python type.
if
isinstance
(
other
,
(
n
umpy
.
ndarray
,
int
,
float
)):
if
isinstance
(
other
,
(
n
p
.
ndarray
,
int
,
float
)):
# Make a TensorConstant to be able to compare
# Make a TensorConstant to be able to compare
other
=
theano
.
tensor
.
basic
.
constant
(
other
)
other
=
theano
.
tensor
.
basic
.
constant
(
other
)
return
(
isinstance
(
other
,
TensorConstant
)
and
return
(
isinstance
(
other
,
TensorConstant
)
and
...
...
theano/tensor/xlogx.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
theano.tensor.elemwise
import
Elemwise
from
theano.tensor.elemwise
import
Elemwise
from
theano
import
scalar
from
theano
import
scalar
...
@@ -15,7 +15,7 @@ class XlogX(scalar.UnaryScalarOp):
...
@@ -15,7 +15,7 @@ class XlogX(scalar.UnaryScalarOp):
def
st_impl
(
x
):
def
st_impl
(
x
):
if
x
==
0.0
:
if
x
==
0.0
:
return
0.0
return
0.0
return
x
*
n
umpy
.
log
(
x
)
return
x
*
n
p
.
log
(
x
)
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
return
XlogX
.
st_impl
(
x
)
return
XlogX
.
st_impl
(
x
)
...
@@ -48,7 +48,7 @@ class XlogY0(scalar.BinaryScalarOp):
...
@@ -48,7 +48,7 @@ class XlogY0(scalar.BinaryScalarOp):
def
st_impl
(
x
,
y
):
def
st_impl
(
x
,
y
):
if
x
==
0.0
:
if
x
==
0.0
:
return
0.0
return
0.0
return
x
*
n
umpy
.
log
(
y
)
return
x
*
n
p
.
log
(
y
)
def
impl
(
self
,
x
,
y
):
def
impl
(
self
,
x
,
y
):
return
XlogY0
.
st_impl
(
x
,
y
)
return
XlogY0
.
st_impl
(
x
,
y
)
...
...
theano/typed_list/basic.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
numpy
import
numpy
as
np
from
.type
import
TypedListType
from
.type
import
TypedListType
import
theano
import
theano
...
@@ -500,7 +500,7 @@ class Index(Op):
...
@@ -500,7 +500,7 @@ class Index(Op):
(
out
,)
=
outputs
(
out
,)
=
outputs
for
y
in
range
(
len
(
x
)):
for
y
in
range
(
len
(
x
)):
if
node
.
inputs
[
0
]
.
ttype
.
values_eq
(
x
[
y
],
elem
):
if
node
.
inputs
[
0
]
.
ttype
.
values_eq
(
x
[
y
],
elem
):
out
[
0
]
=
n
umpy
.
asarray
(
y
,
dtype
=
theano
.
config
.
floatX
)
out
[
0
]
=
n
p
.
asarray
(
y
,
dtype
=
theano
.
config
.
floatX
)
break
break
def
__str__
(
self
):
def
__str__
(
self
):
...
@@ -530,7 +530,7 @@ class Count(Op):
...
@@ -530,7 +530,7 @@ class Count(Op):
for
y
in
range
(
len
(
x
)):
for
y
in
range
(
len
(
x
)):
if
node
.
inputs
[
0
]
.
ttype
.
values_eq
(
x
[
y
],
elem
):
if
node
.
inputs
[
0
]
.
ttype
.
values_eq
(
x
[
y
],
elem
):
out
[
0
]
+=
1
out
[
0
]
+=
1
out
[
0
]
=
n
umpy
.
asarray
(
out
[
0
],
dtype
=
theano
.
config
.
floatX
)
out
[
0
]
=
n
p
.
asarray
(
out
[
0
],
dtype
=
theano
.
config
.
floatX
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
...
@@ -565,7 +565,7 @@ class Length(Op):
...
@@ -565,7 +565,7 @@ class Length(Op):
def
perform
(
self
,
node
,
x
,
outputs
):
def
perform
(
self
,
node
,
x
,
outputs
):
(
out
,)
=
outputs
(
out
,)
=
outputs
out
[
0
]
=
n
umpy
.
asarray
(
len
(
x
[
0
]),
'int64'
)
out
[
0
]
=
n
p
.
asarray
(
len
(
x
[
0
]),
'int64'
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
...
...
theano/typed_list/tests/test_basic.py
浏览文件 @
ef9f6efc
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
import
unittest
from
nose.plugins.skip
import
SkipTest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
import
theano
import
theano
import
theano.typed_list
import
theano.typed_list
...
@@ -24,8 +24,8 @@ except ImportError:
...
@@ -24,8 +24,8 @@ except ImportError:
# took from tensors/tests/test_basic.py
# took from tensors/tests/test_basic.py
def
rand_ranged_matrix
(
minimum
,
maximum
,
shape
):
def
rand_ranged_matrix
(
minimum
,
maximum
,
shape
):
return
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
)
*
(
maximum
-
minimum
)
+
return
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
)
*
(
maximum
-
minimum
)
+
minimum
,
dtype
=
theano
.
config
.
floatX
)
minimum
,
dtype
=
theano
.
config
.
floatX
)
# took from sparse/tests/test_basic.py
# took from sparse/tests/test_basic.py
...
@@ -34,8 +34,8 @@ def random_lil(shape, dtype, nnz):
...
@@ -34,8 +34,8 @@ def random_lil(shape, dtype, nnz):
huge
=
2
**
30
huge
=
2
**
30
for
k
in
range
(
nnz
):
for
k
in
range
(
nnz
):
# set non-zeros in random locations (row x, col y)
# set non-zeros in random locations (row x, col y)
idx
=
n
umpy
.
random
.
randint
(
1
,
huge
+
1
,
size
=
2
)
%
shape
idx
=
n
p
.
random
.
randint
(
1
,
huge
+
1
,
size
=
2
)
%
shape
value
=
n
umpy
.
random
.
rand
()
value
=
n
p
.
random
.
rand
()
# if dtype *int*, value will always be zeros!
# if dtype *int*, value will always be zeros!
if
dtype
in
theano
.
tensor
.
integer_dtypes
:
if
dtype
in
theano
.
tensor
.
integer_dtypes
:
value
=
int
(
value
*
100
)
value
=
int
(
value
*
100
)
...
@@ -68,7 +68,7 @@ class test_get_item(unittest.TestCase):
...
@@ -68,7 +68,7 @@ class test_get_item(unittest.TestCase):
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
slice
(
0
,
1
,
1
)),
[
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
slice
(
0
,
1
,
1
)),
[
x
]))
def
test_sanity_check_single
(
self
):
def
test_sanity_check_single
(
self
):
...
@@ -84,9 +84,9 @@ class test_get_item(unittest.TestCase):
...
@@ -84,9 +84,9 @@ class test_get_item(unittest.TestCase):
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
numpy
.
asarray
(
0
,
dtype
=
'int64'
)),
np
.
asarray
(
0
,
dtype
=
'int64'
)),
x
))
x
))
def
test_interface
(
self
):
def
test_interface
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -100,16 +100,16 @@ class test_get_item(unittest.TestCase):
...
@@ -100,16 +100,16 @@ class test_get_item(unittest.TestCase):
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
numpy
.
asarray
(
0
,
dtype
=
'int64'
)),
np
.
asarray
(
0
,
dtype
=
'int64'
)),
x
))
x
))
z
=
mySymbolicMatricesList
[
0
]
z
=
mySymbolicMatricesList
[
0
]
f
=
theano
.
function
([
mySymbolicMatricesList
],
f
=
theano
.
function
([
mySymbolicMatricesList
],
z
)
z
)
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
]),
x
))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
]),
x
))
def
test_wrong_input
(
self
):
def
test_wrong_input
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -130,14 +130,14 @@ class test_get_item(unittest.TestCase):
...
@@ -130,14 +130,14 @@ class test_get_item(unittest.TestCase):
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
]),
x
))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
]),
x
))
z
=
GetItem
()(
mySymbolicMatricesList
,
slice
(
0
,
1
,
1
))
z
=
GetItem
()(
mySymbolicMatricesList
,
slice
(
0
,
1
,
1
))
f
=
theano
.
function
([
mySymbolicMatricesList
],
f
=
theano
.
function
([
mySymbolicMatricesList
],
z
)
z
)
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
]),
[
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
]),
[
x
]))
class
test_append
(
unittest
.
TestCase
):
class
test_append
(
unittest
.
TestCase
):
...
@@ -156,7 +156,7 @@ class test_append(unittest.TestCase):
...
@@ -156,7 +156,7 @@ class test_append(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
y
),
[
x
,
y
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
y
),
[
x
,
y
]))
def
test_sanity_check
(
self
):
def
test_sanity_check
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -171,7 +171,7 @@ class test_append(unittest.TestCase):
...
@@ -171,7 +171,7 @@ class test_append(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
y
),
[
x
,
y
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
y
),
[
x
,
y
]))
def
test_interfaces
(
self
):
def
test_interfaces
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -186,7 +186,7 @@ class test_append(unittest.TestCase):
...
@@ -186,7 +186,7 @@ class test_append(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
y
),
[
x
,
y
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
y
),
[
x
,
y
]))
class
test_extend
(
unittest
.
TestCase
):
class
test_extend
(
unittest
.
TestCase
):
...
@@ -206,7 +206,7 @@ class test_extend(unittest.TestCase):
...
@@ -206,7 +206,7 @@ class test_extend(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
[
y
]),
[
x
,
y
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
[
y
]),
[
x
,
y
]))
def
test_sanity_check
(
self
):
def
test_sanity_check
(
self
):
mySymbolicMatricesList1
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList1
=
TypedListType
(
T
.
TensorType
(
...
@@ -223,7 +223,7 @@ class test_extend(unittest.TestCase):
...
@@ -223,7 +223,7 @@ class test_extend(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
[
y
]),
[
x
,
y
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
[
y
]),
[
x
,
y
]))
def
test_interface
(
self
):
def
test_interface
(
self
):
mySymbolicMatricesList1
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList1
=
TypedListType
(
T
.
TensorType
(
...
@@ -240,7 +240,7 @@ class test_extend(unittest.TestCase):
...
@@ -240,7 +240,7 @@ class test_extend(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
[
y
]),
[
x
,
y
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
[
y
]),
[
x
,
y
]))
class
test_insert
(
unittest
.
TestCase
):
class
test_insert
(
unittest
.
TestCase
):
...
@@ -260,10 +260,10 @@ class test_insert(unittest.TestCase):
...
@@ -260,10 +260,10 @@ class test_insert(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
numpy
.
asarray
(
1
,
dtype
=
'int64'
),
np
.
asarray
(
1
,
dtype
=
'int64'
),
y
),
y
),
[
x
,
y
]))
[
x
,
y
]))
def
test_sanity_check
(
self
):
def
test_sanity_check
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -279,7 +279,7 @@ class test_insert(unittest.TestCase):
...
@@ -279,7 +279,7 @@ class test_insert(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
numpy
.
asarray
(
1
,
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
np
.
asarray
(
1
,
dtype
=
'int64'
),
y
),
[
x
,
y
]))
dtype
=
'int64'
),
y
),
[
x
,
y
]))
def
test_interface
(
self
):
def
test_interface
(
self
):
...
@@ -296,10 +296,10 @@ class test_insert(unittest.TestCase):
...
@@ -296,10 +296,10 @@ class test_insert(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
numpy
.
asarray
(
1
,
dtype
=
'int64'
),
np
.
asarray
(
1
,
dtype
=
'int64'
),
y
),
y
),
[
x
,
y
]))
[
x
,
y
]))
class
test_remove
(
unittest
.
TestCase
):
class
test_remove
(
unittest
.
TestCase
):
...
@@ -318,7 +318,7 @@ class test_remove(unittest.TestCase):
...
@@ -318,7 +318,7 @@ class test_remove(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
,
y
],
y
),
[
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
,
y
],
y
),
[
x
]))
def
test_sanity_check
(
self
):
def
test_sanity_check
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -333,7 +333,7 @@ class test_remove(unittest.TestCase):
...
@@ -333,7 +333,7 @@ class test_remove(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
,
y
],
y
),
[
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
,
y
],
y
),
[
x
]))
def
test_interface
(
self
):
def
test_interface
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -348,7 +348,7 @@ class test_remove(unittest.TestCase):
...
@@ -348,7 +348,7 @@ class test_remove(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
,
y
],
y
),
[
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
,
y
],
y
),
[
x
]))
class
test_reverse
(
unittest
.
TestCase
):
class
test_reverse
(
unittest
.
TestCase
):
...
@@ -366,7 +366,7 @@ class test_reverse(unittest.TestCase):
...
@@ -366,7 +366,7 @@ class test_reverse(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
,
y
]),
[
y
,
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
,
y
]),
[
y
,
x
]))
def
test_sanity_check
(
self
):
def
test_sanity_check
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -380,7 +380,7 @@ class test_reverse(unittest.TestCase):
...
@@ -380,7 +380,7 @@ class test_reverse(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
,
y
]),
[
y
,
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
,
y
]),
[
y
,
x
]))
def
test_interface
(
self
):
def
test_interface
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -394,7 +394,7 @@ class test_reverse(unittest.TestCase):
...
@@ -394,7 +394,7 @@ class test_reverse(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
,
y
]),
[
y
,
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
,
y
]),
[
y
,
x
]))
class
test_index
(
unittest
.
TestCase
):
class
test_index
(
unittest
.
TestCase
):
...
@@ -570,10 +570,10 @@ class TestMakeList(unittest.TestCase):
...
@@ -570,10 +570,10 @@ class TestMakeList(unittest.TestCase):
x
=
T
.
tensor3
()
x
=
T
.
tensor3
()
y
=
T
.
tensor3
()
y
=
T
.
tensor3
()
A
=
n
umpy
.
cast
[
theano
.
config
.
floatX
](
numpy
.
random
.
rand
(
5
,
3
))
A
=
n
p
.
cast
[
theano
.
config
.
floatX
](
np
.
random
.
rand
(
5
,
3
))
B
=
n
umpy
.
cast
[
theano
.
config
.
floatX
](
numpy
.
random
.
rand
(
7
,
2
))
B
=
n
p
.
cast
[
theano
.
config
.
floatX
](
np
.
random
.
rand
(
7
,
2
))
X
=
n
umpy
.
cast
[
theano
.
config
.
floatX
](
numpy
.
random
.
rand
(
5
,
6
,
1
))
X
=
n
p
.
cast
[
theano
.
config
.
floatX
](
np
.
random
.
rand
(
5
,
6
,
1
))
Y
=
n
umpy
.
cast
[
theano
.
config
.
floatX
](
numpy
.
random
.
rand
(
1
,
9
,
3
))
Y
=
n
p
.
cast
[
theano
.
config
.
floatX
](
np
.
random
.
rand
(
1
,
9
,
3
))
make_list
((
3.
,
4.
))
make_list
((
3.
,
4.
))
c
=
make_list
((
a
,
b
))
c
=
make_list
((
a
,
b
))
...
...
theano/typed_list/tests/test_opt.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
unittest
import
unittest
import
numpy
import
numpy
as
np
import
theano
import
theano
import
theano.typed_list
import
theano.typed_list
...
@@ -14,8 +14,8 @@ from theano import In
...
@@ -14,8 +14,8 @@ from theano import In
# took from tensors/tests/test_basic.py
# took from tensors/tests/test_basic.py
def
rand_ranged_matrix
(
minimum
,
maximum
,
shape
):
def
rand_ranged_matrix
(
minimum
,
maximum
,
shape
):
return
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
)
*
(
maximum
-
minimum
)
+
return
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
)
*
(
maximum
-
minimum
)
+
minimum
,
dtype
=
theano
.
config
.
floatX
)
minimum
,
dtype
=
theano
.
config
.
floatX
)
class
test_inplace
(
unittest
.
TestCase
):
class
test_inplace
(
unittest
.
TestCase
):
...
@@ -34,7 +34,7 @@ class test_inplace(unittest.TestCase):
...
@@ -34,7 +34,7 @@ class test_inplace(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
,
y
]),
[
y
,
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
,
y
]),
[
y
,
x
]))
def
test_append_inplace
(
self
):
def
test_append_inplace
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -52,7 +52,7 @@ class test_inplace(unittest.TestCase):
...
@@ -52,7 +52,7 @@ class test_inplace(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
y
),
[
x
,
y
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
y
),
[
x
,
y
]))
def
test_extend_inplace
(
self
):
def
test_extend_inplace
(
self
):
mySymbolicMatricesList1
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList1
=
TypedListType
(
T
.
TensorType
(
...
@@ -72,7 +72,7 @@ class test_inplace(unittest.TestCase):
...
@@ -72,7 +72,7 @@ class test_inplace(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
[
y
]),
[
x
,
y
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
[
y
]),
[
x
,
y
]))
def
test_insert_inplace
(
self
):
def
test_insert_inplace
(
self
):
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
mySymbolicMatricesList
=
TypedListType
(
T
.
TensorType
(
...
@@ -92,7 +92,7 @@ class test_inplace(unittest.TestCase):
...
@@ -92,7 +92,7 @@ class test_inplace(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
],
numpy
.
asarray
(
1
,
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
],
np
.
asarray
(
1
,
dtype
=
'int64'
),
y
),
[
x
,
y
]))
dtype
=
'int64'
),
y
),
[
x
,
y
]))
def
test_remove_inplace
(
self
):
def
test_remove_inplace
(
self
):
...
@@ -110,7 +110,7 @@ class test_inplace(unittest.TestCase):
...
@@ -110,7 +110,7 @@ class test_inplace(unittest.TestCase):
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
y
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
101
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
f
([
x
,
y
],
y
),
[
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
f
([
x
,
y
],
y
),
[
x
]))
def
test_constant_folding
():
def
test_constant_folding
():
...
...
theano/typed_list/tests/test_type.py
浏览文件 @
ef9f6efc
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
unittest
import
unittest
import
numpy
import
numpy
as
np
import
theano
import
theano
import
theano.typed_list
import
theano.typed_list
...
@@ -12,8 +12,8 @@ from theano.tests import unittest_tools as utt
...
@@ -12,8 +12,8 @@ from theano.tests import unittest_tools as utt
# took from tensors/tests/test_basic.py
# took from tensors/tests/test_basic.py
def
rand_ranged_matrix
(
minimum
,
maximum
,
shape
):
def
rand_ranged_matrix
(
minimum
,
maximum
,
shape
):
return
n
umpy
.
asarray
(
numpy
.
random
.
rand
(
*
shape
)
*
(
maximum
-
minimum
)
+
return
n
p
.
asarray
(
np
.
random
.
rand
(
*
shape
)
*
(
maximum
-
minimum
)
+
minimum
,
dtype
=
theano
.
config
.
floatX
)
minimum
,
dtype
=
theano
.
config
.
floatX
)
class
test_typed_list_type
(
unittest
.
TestCase
):
class
test_typed_list_type
(
unittest
.
TestCase
):
...
@@ -84,7 +84,7 @@ class test_typed_list_type(unittest.TestCase):
...
@@ -84,7 +84,7 @@ class test_typed_list_type(unittest.TestCase):
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
100
])
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
100
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
myType
.
filter
([
x
]),
[
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
myType
.
filter
([
x
]),
[
x
]))
def
test_intern_filter
(
self
):
def
test_intern_filter
(
self
):
"""
"""
...
@@ -95,9 +95,9 @@ class test_typed_list_type(unittest.TestCase):
...
@@ -95,9 +95,9 @@ class test_typed_list_type(unittest.TestCase):
myType
=
TypedListType
(
T
.
TensorType
(
'float64'
,
myType
=
TypedListType
(
T
.
TensorType
(
'float64'
,
(
False
,
False
)))
(
False
,
False
)))
x
=
n
umpy
.
asarray
([[
4
,
5
],
[
4
,
5
]],
dtype
=
'float32'
)
x
=
n
p
.
asarray
([[
4
,
5
],
[
4
,
5
]],
dtype
=
'float32'
)
self
.
assertTrue
(
n
umpy
.
array_equal
(
myType
.
filter
([
x
]),
[
x
]))
self
.
assertTrue
(
n
p
.
array_equal
(
myType
.
filter
([
x
]),
[
x
]))
# Will fail for unknown reasons
# Will fail for unknown reasons
# under search
# under search
...
@@ -125,7 +125,7 @@ class test_typed_list_type(unittest.TestCase):
...
@@ -125,7 +125,7 @@ class test_typed_list_type(unittest.TestCase):
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
100
])
x
=
rand_ranged_matrix
(
-
1000
,
1000
,
[
100
,
100
])
self
.
assertTrue
(
n
umpy
.
array_equal
(
myType
.
filter
([[
x
]]),
[[
x
]]))
self
.
assertTrue
(
n
p
.
array_equal
(
myType
.
filter
([[
x
]]),
[[
x
]]))
def
test_comparison_different_depth
(
self
):
def
test_comparison_different_depth
(
self
):
"""
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论