Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2730024a
提交
2730024a
authored
3月 25, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
5e5d1bb2
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
31 行增加
和
31 行删除
+31
-31
elemwise.py
theano/tensor/elemwise.py
+31
-31
没有找到文件。
theano/tensor/elemwise.py
浏览文件 @
2730024a
...
@@ -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,9 +504,9 @@ second dimension
...
@@ -504,9 +504,9 @@ 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
)
...
@@ -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,7 +750,7 @@ second dimension
...
@@ -750,7 +750,7 @@ 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
:
...
@@ -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,7 +1415,7 @@ class CAReduce(Op):
...
@@ -1415,7 +1415,7 @@ 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
:
...
@@ -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,7 +1436,7 @@ class CAReduce(Op):
...
@@ -1436,7 +1436,7 @@ 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
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论