Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
8897859b
提交
8897859b
authored
3月 25, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
c60f369f
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
95 行增加
和
95 行删除
+95
-95
basic.py
theano/tensor/basic.py
+95
-95
没有找到文件。
theano/tensor/basic.py
浏览文件 @
8897859b
...
...
@@ -5,7 +5,7 @@ from six.moves import builtins
import
sys
import
warnings
import
numpy
import
numpy
as
np
from
six
import
integer_types
from
six.moves
import
xrange
import
numbers
...
...
@@ -72,12 +72,12 @@ def check_equal_numpy(x, y):
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
n
umpy
.
all
(
abs
(
x
-
y
)
<
1e-10
))
elif
(
isinstance
(
x
,
n
umpy
.
random
.
RandomState
)
and
isinstance
(
y
,
n
umpy
.
random
.
RandomState
)):
return
python_all
(
n
umpy
.
all
(
a
==
b
)
for
a
,
b
in
n
p
.
all
(
abs
(
x
-
y
)
<
1e-10
))
elif
(
isinstance
(
x
,
n
p
.
random
.
RandomState
)
and
isinstance
(
y
,
n
p
.
random
.
RandomState
)):
return
python_all
(
n
p
.
all
(
a
==
b
)
for
a
,
b
in
izip
(
x
.
__getstate__
(),
y
.
__getstate__
()))
else
:
return
x
==
y
...
...
@@ -348,15 +348,15 @@ def _get_atol_rtol(a, b):
def
_allclose
(
a
,
b
,
rtol
=
None
,
atol
=
None
):
a
=
n
umpy
.
asarray
(
a
)
b
=
n
umpy
.
asarray
(
b
)
a
=
n
p
.
asarray
(
a
)
b
=
n
p
.
asarray
(
b
)
atol_
,
rtol_
=
_get_atol_rtol
(
a
,
b
)
if
rtol
is
not
None
:
rtol_
=
rtol
if
atol
is
not
None
:
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
):
...
...
@@ -387,10 +387,10 @@ def numpy_scalar(data):
if
(
data
.
ndim
>
0
and
(
len
(
data
.
shape
)
==
0
or
builtins
.
max
(
data
.
shape
)
==
0
)):
assert
n
umpy
.
all
(
numpy
.
array
([])
==
data
)
assert
n
p
.
all
(
np
.
array
([])
==
data
)
raise
EmptyConstantError
()
try
:
n
umpy
.
complex
(
data
)
# works for all numeric scalars
n
p
.
complex
(
data
)
# works for all numeric scalars
return
data
except
Exception
:
raise
NotScalarConstantError
(
...
...
@@ -444,10 +444,10 @@ def get_scalar_constant_value(orig_v, elemwise=True,
# to depend on passing it None)
raise
NotScalarConstantError
()
if
isinstance
(
v
,
(
n
umpy
.
integer
,
integer_types
,
float
)):
return
n
umpy
.
asarray
(
v
)
if
isinstance
(
v
,
(
n
p
.
integer
,
integer_types
,
float
)):
return
n
p
.
asarray
(
v
)
if
isinstance
(
v
,
n
umpy
.
ndarray
):
if
isinstance
(
v
,
n
p
.
ndarray
):
return
numpy_scalar
(
v
)
.
copy
()
if
isinstance
(
v
,
Constant
):
...
...
@@ -470,11 +470,11 @@ def get_scalar_constant_value(orig_v, elemwise=True,
i
=
v
.
owner
.
op
.
i
inp
=
v
.
owner
.
inputs
[
0
]
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
if
(
hasattr
(
inp
.
type
,
'broadcastable'
)
and
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
# fct is used too early in the optimization phase. This would
...
...
@@ -639,7 +639,7 @@ def get_scalar_constant_value(orig_v, elemwise=True,
raise
ValueError
(
msg
)
if
gp_broadcastable
[
idx
]:
return
n
umpy
.
asarray
(
1
)
return
n
p
.
asarray
(
1
)
raise
NotScalarConstantError
(
v
)
...
...
@@ -1002,7 +1002,7 @@ class TensorFromScalar(Op):
def
perform
(
self
,
node
,
inp
,
out_
):
s
,
=
inp
out
,
=
out_
out
[
0
]
=
n
umpy
.
asarray
(
s
)
out
[
0
]
=
n
p
.
asarray
(
s
)
def
infer_shape
(
self
,
node
,
in_shapes
):
return
[()]
...
...
@@ -1216,23 +1216,23 @@ class MaxAndArgmax(Op):
axes
=
tuple
(
range
(
x
.
ndim
))
else
:
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
)
# Numpy does not support multiple axes for argmax
# 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'
)
# 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
)]
reduced_shape
=
transposed_x
.
shape
[
len
(
keep_axes
):]
# Numpy.prod returns 1.0 when arg is empty, so we cast it to int64
# 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
)
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'
)
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
...
...
@@ -1399,11 +1399,11 @@ class Argmax(Op):
def
make_node
(
self
,
x
,
axis
=
None
):
x
=
_as_tensor_variable
(
x
)
if
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
if
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
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
)]
elif
isinstance
(
axis
,
(
tuple
,
list
,
n
umpy
.
ndarray
)):
elif
isinstance
(
axis
,
(
tuple
,
list
,
n
p
.
ndarray
)):
axis
=
[
int
(
a
)
for
a
in
axis
]
if
axis
==
list
(
range
(
x
.
type
.
ndim
)):
axis
=
None
...
...
@@ -1415,11 +1415,11 @@ class Argmax(Op):
"Argmax needs a constant axis. Got
%
s"
%
axis
)
else
:
assert
axis
.
dtype
in
integer_dtypes
if
isinstance
(
axis
.
data
,
(
integer_types
,
n
umpy
.
integer
))
or
\
(
isinstance
(
axis
.
data
,
n
umpy
.
ndarray
)
and
if
isinstance
(
axis
.
data
,
(
integer_types
,
n
p
.
integer
))
or
\
(
isinstance
(
axis
.
data
,
n
p
.
ndarray
)
and
axis
.
data
.
ndim
==
0
):
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
]
# Make axis entries non-negative, and sort them
...
...
@@ -1466,17 +1466,17 @@ class Argmax(Op):
# Numpy does not support multiple axes for argmax
# 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'
)
# 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
)))
kept_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
)
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'
)
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
...
...
@@ -1562,9 +1562,9 @@ def makeKeepDims(x, y, axis):
if
axis
is
None
:
axis
=
list
(
range
(
x
.
type
.
ndim
))
elif
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
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
)]
else
:
axis
=
[
int
(
a
)
for
a
in
axis
]
...
...
@@ -1609,10 +1609,10 @@ def max_and_argmax(a, axis=None, keepdims=False):
a
=
as_tensor_variable
(
a
)
if
axis
is
None
:
axis
=
list
(
range
(
a
.
type
.
ndim
))
elif
(
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
))
or
(
isinstance
(
axis
,
n
umpy
.
ndarray
)
and
axis
.
ndim
==
0
)):
elif
(
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
))
or
(
isinstance
(
axis
,
n
p
.
ndarray
)
and
axis
.
ndim
==
0
)):
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
]
elif
isinstance
(
axis
,
Variable
):
if
NoneConst
.
equals
(
axis
):
...
...
@@ -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
)
else
:
assert
axis
.
dtype
in
integer_dtypes
if
(
isinstance
(
axis
.
data
,
(
integer_types
,
n
umpy
.
integer
))
or
(
isinstance
(
axis
.
data
,
n
umpy
.
ndarray
)
and
axis
.
data
.
ndim
==
0
)):
if
(
isinstance
(
axis
.
data
,
(
integer_types
,
n
p
.
integer
))
or
(
isinstance
(
axis
.
data
,
n
p
.
ndarray
)
and
axis
.
data
.
ndim
==
0
)):
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
]
if
len
(
axis
)
==
0
:
axis
=
list
(
range
(
a
.
type
.
ndim
))
...
...
@@ -1838,7 +1838,7 @@ def isnan(a):
"""isnan(a)"""
a
=
as_tensor_variable
(
a
)
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
)])
return
isnan_
(
a
)
...
...
@@ -1857,7 +1857,7 @@ def isinf(a):
"""isinf(a)"""
a
=
as_tensor_variable
(
a
)
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
)])
return
isinf_
(
a
)
...
...
@@ -2426,7 +2426,7 @@ def zeros(shape, dtype=None):
shape
=
[
shape
]
if
dtype
is
None
:
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
):
...
...
@@ -2437,7 +2437,7 @@ def ones(shape, dtype=None):
shape
=
[
shape
]
if
dtype
is
None
:
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
):
...
...
@@ -2481,11 +2481,11 @@ class Nonzero(gof.Op):
a
=
inp
[
0
]
out
,
=
out_
result_tuple
=
n
umpy
.
nonzero
(
a
)
result_tuple
=
n
p
.
nonzero
(
a
)
if
len
(
result_tuple
[
0
])
>
0
:
result
=
n
umpy
.
vstack
(
result_tuple
)
result
=
n
p
.
vstack
(
result_tuple
)
else
:
result
=
n
umpy
.
zeros
((
len
(
result_tuple
),
0
))
result
=
n
p
.
zeros
((
len
(
result_tuple
),
0
))
out
[
0
]
=
result
.
astype
(
'int64'
)
...
...
@@ -2627,7 +2627,7 @@ class Tri(gof.Op):
def
perform
(
self
,
node
,
inp
,
out_
):
N
,
M
,
k
=
inp
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
):
out_shape
=
[
node
.
inputs
[
0
],
node
.
inputs
[
1
]]
...
...
@@ -2738,7 +2738,7 @@ class Eye(gof.Op):
def
perform
(
self
,
node
,
inp
,
out_
):
n
,
m
,
k
=
inp
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
):
out_shape
=
[
node
.
inputs
[
0
],
node
.
inputs
[
1
]]
...
...
@@ -2853,9 +2853,9 @@ class Alloc(gof.Op):
sh
=
tuple
([
int
(
i
)
for
i
in
inputs
[
1
:]])
if
out
[
0
]
is
None
or
out
[
0
]
.
shape
!=
sh
:
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
:
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
else
:
# reuse the allocated memory.
...
...
@@ -3139,7 +3139,7 @@ class Mean(elemwise.CAReduce):
axis
=
self
.
axis
[
0
]
# numpy.asarray is needed as otherwise we can end up with a
# numpy scalar.
output
[
0
]
=
n
umpy
.
asarray
(
numpy
.
mean
(
input
,
dtype
=
'float64'
,
output
[
0
]
=
n
p
.
asarray
(
np
.
mean
(
input
,
dtype
=
'float64'
,
axis
=
axis
))
def
c_code
(
self
,
node
,
name
,
inames
,
onames
,
sub
):
...
...
@@ -3232,9 +3232,9 @@ def mean(input, axis=None, dtype=None, op=False, keepdims=False,
if
axis
is
None
:
axis
=
list
(
range
(
input
.
ndim
))
elif
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
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
)]
else
:
axis
=
[
int
(
a
)
for
a
in
axis
]
...
...
@@ -3291,9 +3291,9 @@ def var(input, axis=None, ddof=0, keepdims=False, corrected=False):
input_ndim
=
input
.
type
.
ndim
if
axis
is
None
:
axis
=
list
(
range
(
input_ndim
))
elif
isinstance
(
axis
,
(
integer_types
,
n
umpy
.
integer
)):
elif
isinstance
(
axis
,
(
integer_types
,
n
p
.
integer
)):
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
)]
else
:
axis
=
[
int
(
a
)
for
a
in
axis
]
...
...
@@ -3617,7 +3617,7 @@ def batched_dot(a, b):
return
a
*
b
.
dimshuffle
(
*
([
0
]
+
[
"x"
]
*
(
a
.
ndim
-
1
)))
elif
a
.
ndim
>
3
or
b
.
ndim
>
3
:
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
:
# avoid circular import
return
theano
.
tensor
.
blas
.
BatchedDot
()(
a
,
b
)
...
...
@@ -3736,9 +3736,9 @@ class Split(Op):
raise
ValueError
(
'In Split.perform(), len(splits) != 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'
%
(
n
umpy
.
sum
(
splits
),
len_along_axis
))
(
n
p
.
sum
(
splits
),
len_along_axis
))
if
python_any
([
nb
<
0
for
nb
in
splits
]):
raise
ValueError
(
'Split: you tried to make an ndarray with a '
'negative number of elements.'
)
...
...
@@ -3828,8 +3828,8 @@ class Split(Op):
outputs_pointers
=
'&'
+
(
', &'
.
join
(
outputs
))
x
,
axis
,
splits
=
inputs
fail
=
sub
[
'fail'
]
x_typenum
=
n
umpy
.
dtype
(
node
.
inputs
[
0
]
.
dtype
)
.
num
x_itemsize
=
n
umpy
.
dtype
(
node
.
inputs
[
0
]
.
dtype
)
.
itemsize
x_typenum
=
n
p
.
dtype
(
node
.
inputs
[
0
]
.
dtype
)
.
num
x_itemsize
=
n
p
.
dtype
(
node
.
inputs
[
0
]
.
dtype
)
.
itemsize
axis_dtype
=
node
.
inputs
[
1
]
.
type
.
dtype_specs
()[
1
]
splits_dtype
=
node
.
inputs
[
2
]
.
type
.
dtype_specs
()[
1
]
expected_splits_count
=
self
.
len_splits
...
...
@@ -4187,7 +4187,7 @@ class Join(Op):
view
=
self
.
view
axis
,
tensors
=
axis_and_tensors
[
0
],
axis_and_tensors
[
1
:]
# 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
tensors
[
0
:
view
]
+
tensors
[
view
+
1
:]]):
out
[
0
]
=
tensors
[
view
]
...
...
@@ -4198,7 +4198,7 @@ class Join(Op):
raise
IndexError
(
"Join axis
%
d out of bounds [0,
%
d)"
%
(
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
)
def
c_code_cache_version
(
self
):
...
...
@@ -4584,9 +4584,9 @@ def stack(*tensors, **kwargs):
# And DebugMode can't detect error in this code as it is not in an
# optimization.
# See ticket #660
if
n
umpy
.
all
(
if
n
p
.
all
(
[
# 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
(
isinstance
(
t
,
Variable
)
and
isinstance
(
t
.
type
,
TensorType
)
and
...
...
@@ -4669,7 +4669,7 @@ def get_vector_length(v):
v
.
owner
.
inputs
,
v
.
owner
.
op
.
idx_list
)[
0
]
.
step
)
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
:
start
=
0
elif
isinstance
(
start
,
types
)
and
start
<
0
:
...
...
@@ -4790,7 +4790,7 @@ class Reshape(Op):
' length
%
i'
', should be
%
i'
%
(
len
(
shp
),
self
.
ndim
),
shp
)
try
:
out
[
0
]
=
n
umpy
.
reshape
(
x
,
shp
)
out
[
0
]
=
n
p
.
reshape
(
x
,
shp
)
except
Exception
:
raise
ValueError
(
'Cannot reshape input of shape
%
s to shape
%
s'
%
(
x
.
shape
,
shp
))
...
...
@@ -4976,12 +4976,12 @@ class Flatten(Op):
try
:
out
[
0
]
=
x
.
reshape
(
x
.
size
)
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
):
out
[
0
]
=
x
else
:
newshape
=
(
x
.
shape
[:
outdim
-
1
]
+
(
n
umpy
.
prod
(
x
.
shape
[
outdim
-
1
:]),))
(
n
p
.
prod
(
x
.
shape
[
outdim
-
1
:]),))
out
[
0
]
=
x
.
reshape
(
newshape
)
def
infer_shape
(
self
,
node
,
in_shapes
):
...
...
@@ -5196,16 +5196,16 @@ class Tile(Op):
def
perform
(
self
,
node
,
inp
,
out_
):
x
,
reps
=
inp
out
,
=
out_
res
=
n
umpy
.
tile
(
x
,
reps
)
res
=
n
p
.
tile
(
x
,
reps
)
if
res
.
ndim
!=
self
.
ndim
:
raise
ValueError
(
'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
# op isn't declared as inplace, we need to check that and
# copy the data.
if
n
umpy
.
may_share_memory
(
res
,
x
):
if
n
p
.
may_share_memory
(
res
,
x
):
res
=
res
.
copy
()
out
[
0
]
=
res
...
...
@@ -5289,7 +5289,7 @@ def tile(x, reps, ndim=None):
else
:
if
ndim
is
not
None
and
len
(
reps
)
>
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
r
.
dtype
in
theano
.
tensor
.
discrete_dtypes
)
for
r
in
reps
]):
raise
ValueError
(
"elements of reps must be scalars of integer dtype"
)
...
...
@@ -5305,7 +5305,7 @@ def tile(x, reps, ndim=None):
shape
=
[
1
]
*
(
ndim
-
x
.
ndim
)
+
[
x
.
shape
[
i
]
for
i
in
xrange
(
x
.
ndim
)]
alloc_shape
=
reps
+
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
()
y
=
y
.
dimshuffle
(
*
shuffle_ind
)
new_shapes
=
[
sh
*
reps
[
i
]
for
i
,
sh
in
enumerate
(
shape
)]
...
...
@@ -5343,7 +5343,7 @@ class ARange(Op):
def
is_constant_value
(
var
,
value
):
try
:
v
=
get_scalar_constant_value
(
var
)
return
n
umpy
.
all
(
v
==
value
)
return
n
p
.
all
(
v
==
value
)
except
NotScalarConstantError
:
pass
return
False
...
...
@@ -5378,7 +5378,7 @@ class ARange(Op):
start
=
start
.
item
()
stop
=
stop
.
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
):
...
...
@@ -5424,10 +5424,10 @@ def arange(start, stop=None, step=1, dtype=None):
# As an example, if `start`, `stop` and `step` are all int32,
# `numpy.arange` returns an int64 array (on 64-bit platforms),
# while the upcast above returns int32.
numpy_dtype
=
n
umpy
.
arange
(
start
=
n
umpy
.
array
(
0
,
dtype
=
start
.
dtype
),
stop
=
n
umpy
.
array
(
1
,
dtype
=
stop
.
dtype
),
step
=
n
umpy
.
array
(
1
,
dtype
=
step
.
dtype
))
.
dtype
numpy_dtype
=
n
p
.
arange
(
start
=
n
p
.
array
(
0
,
dtype
=
start
.
dtype
),
stop
=
n
p
.
array
(
1
,
dtype
=
stop
.
dtype
),
step
=
n
p
.
array
(
1
,
dtype
=
step
.
dtype
))
.
dtype
if
numpy_dtype
!=
dtype
:
if
(
config
.
cast_policy
==
'numpy+floatX'
and
config
.
floatX
==
'float32'
and
...
...
@@ -5653,7 +5653,7 @@ class PermuteRowElements(Op):
out_s
.
append
(
outdim
)
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
)
...
...
@@ -5796,7 +5796,7 @@ class Dot(Op):
# the asarray is here because dot between two vectors
# gives a numpy float object but we need to return a 0d
# ndarray
z
[
0
]
=
n
umpy
.
asarray
(
numpy
.
dot
(
x
,
y
))
z
[
0
]
=
n
p
.
asarray
(
np
.
dot
(
x
,
y
))
def
grad
(
self
,
inp
,
grads
):
...
...
@@ -5976,7 +5976,7 @@ def dot(a, b):
if
a
.
ndim
==
0
or
b
.
ndim
==
0
:
return
a
*
b
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
:
return
_dot
(
a
,
b
)
...
...
@@ -6012,14 +6012,14 @@ def _tensordot_as_dot(a, b, axes, dot, batched):
"""
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 '
'list/tuple of len 2 (
%
s was provided)'
%
str
(
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.
elif
n
umpy
.
isscalar
(
axes
):
elif
n
p
.
isscalar
(
axes
):
axes
=
int
(
axes
)
for
operand_name
,
operand
in
((
"a"
,
a
),
(
"b"
,
b
)):
...
...
@@ -6083,12 +6083,12 @@ def _tensordot_as_dot(a, b, axes, dot, batched):
'the dimensions of
%
s (
%
s.ndim=
%
i, len(axes[0])=
%
i).'
%
(
i
,
operand_name
,
operand_name
,
operand
.
ndim
,
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
(
'axes[
%
i] contains dimensions greater than or equal '
'to
%
s.ndim (
%
s.ndim=
%
i, max(axes[0])=
%
i).'
%
(
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
]:
raise
ValueError
(
'axes to sum over must not contain the batch axis '
...
...
@@ -6243,8 +6243,8 @@ def all(x, axis=None, keepdims=False):
# Some NumPy version like 1.9.2 return a view for numpy.diagonal
x
=
n
umpy
.
zeros
((
4
,
4
))
numpy_diagonal_return_view
=
n
umpy
.
may_share_memory
(
numpy
.
diagonal
(
x
),
x
)
x
=
n
p
.
zeros
((
4
,
4
))
numpy_diagonal_return_view
=
n
p
.
may_share_memory
(
np
.
diagonal
(
x
),
x
)
del
x
...
...
@@ -6271,7 +6271,7 @@ class ExtractDiag(Op):
"set to True but numpy version
%
s and prior versions of "
"numpy.diagonal() do not return a view. Update "
"numpy to use ExtractDiag(view=True)"
%
n
umpy
.
version
.
version
)
n
p
.
version
.
version
)
self
.
view
=
False
if
self
.
view
:
self
.
view_map
=
{
0
:
[
0
]}
...
...
@@ -6395,7 +6395,7 @@ class AllocDiag(Op):
def
perform
(
self
,
node
,
inputs
,
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
):
(
gz
,)
=
gout
...
...
@@ -6629,7 +6629,7 @@ class Choose(Op):
choice
=
as_tensor_variable
(
choices
)
choice_ndim
=
choice
.
ndim
-
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.
a
=
shape_padleft
(
a
,
out_ndim
-
a
.
ndim
)
...
...
@@ -6660,7 +6660,7 @@ class Choose(Op):
a
=
inputs
[
0
]
choice
=
inputs
[
1
]
# 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
):
...
...
@@ -6699,7 +6699,7 @@ class AllocEmpty(gof.Op):
out
,
=
out_
sh
=
tuple
([
int
(
i
)
for
i
in
inputs
])
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
):
dtype
=
"NPY_"
+
self
.
dtype
.
upper
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论