Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b17f6ef6
提交
b17f6ef6
authored
4月 14, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
420eb37b
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
119 行增加
和
119 行删除
+119
-119
basic.py
theano/scalar/basic.py
+119
-119
没有找到文件。
theano/scalar/basic.py
浏览文件 @
b17f6ef6
...
...
@@ -17,7 +17,7 @@ import warnings
from
copy
import
copy
from
textwrap
import
dedent
import
numpy
import
numpy
as
np
import
six
from
six.moves
import
xrange
...
...
@@ -72,7 +72,7 @@ def upcast(dtype, *dtypes):
keep_float16
[
0
]
=
False
if
dt
==
'float32'
:
keep_float16
[
0
]
=
False
return
n
umpy
.
zeros
((),
dtype
=
dt
)
return
n
p
.
zeros
((),
dtype
=
dt
)
z
=
make_array
(
dtype
)
for
dt
in
dtypes
:
z
=
z
+
make_array
(
dt
=
dt
)
...
...
@@ -168,12 +168,12 @@ class NumpyAutocaster(object):
# Make sure we only deal with scalars.
assert
(
isinstance
(
x
,
six
.
integer_types
)
or
isinstance
(
x
,
builtin_float
)
or
(
isinstance
(
x
,
n
umpy
.
ndarray
)
and
x
.
ndim
==
0
))
(
isinstance
(
x
,
n
p
.
ndarray
)
and
x
.
ndim
==
0
))
if
config
.
cast_policy
==
'numpy'
:
return
n
umpy
.
asarray
(
x
)
return
n
p
.
asarray
(
x
)
elif
config
.
cast_policy
==
'numpy+floatX'
:
rval
=
n
umpy
.
asarray
(
x
)
rval
=
n
p
.
asarray
(
x
)
if
((
not
hasattr
(
x
,
'dtype'
)
and
rval
.
dtype
in
(
'float64'
,
'float32'
)
and
rval
.
dtype
!=
config
.
floatX
)):
...
...
@@ -191,7 +191,7 @@ class NumpyAutocaster(object):
# No need to cast `x` into a new dtype. Note that we still
# need to convert it into an array, because it may not be
# one already (e.g. if x == numpy.float64(1.1)).
return
n
umpy
.
asarray
(
x
)
return
n
p
.
asarray
(
x
)
except
AttributeError
:
# Means `x` has no 'dtype' attribute.
pass
...
...
@@ -209,7 +209,7 @@ class NumpyAutocaster(object):
for
dtype
in
try_dtypes
:
x_
=
theano
.
_asarray
(
x
,
dtype
=
dtype
)
if
n
umpy
.
all
(
x
==
x_
):
if
n
p
.
all
(
x
==
x_
):
break
# returns either an exact x_==x, or the last cast x_
return
x_
...
...
@@ -272,16 +272,16 @@ def convert(x, dtype=None):
x_
=
theano
.
_asarray
(
x
,
dtype
=
'uint64'
)
elif
isinstance
(
x
,
builtin_float
):
x_
=
autocast_float
(
x
)
elif
isinstance
(
x
,
n
umpy
.
ndarray
):
elif
isinstance
(
x
,
n
p
.
ndarray
):
x_
=
x
else
:
# Here x is probably a list or a tuple. If it contains a
# long, we will behave like the current NumPy version: it
# will work if the long fits in int64 or uint64.
x_
=
n
umpy
.
asarray
(
x
)
x_
=
n
p
.
asarray
(
x
)
if
x_
.
size
==
0
and
not
hasattr
(
x
,
'dtype'
):
x_
=
n
umpy
.
asarray
(
x
,
dtype
=
config
.
floatX
)
assert
type
(
x_
)
in
[
n
umpy
.
ndarray
,
numpy
.
memmap
]
x_
=
n
p
.
asarray
(
x
,
dtype
=
config
.
floatX
)
assert
type
(
x_
)
in
[
n
p
.
ndarray
,
np
.
memmap
]
return
x_
...
...
@@ -396,21 +396,21 @@ class Scalar(Type):
print(dtype, np.zeros(1, dtype=dtype).dtype.num)
"""
return
{
# dtype: (py_type, c_type, cls_name)
'float16'
:
(
n
umpy
.
float16
,
'npy_float16'
,
'Float16'
),
'float32'
:
(
n
umpy
.
float32
,
'npy_float32'
,
'Float32'
),
'float64'
:
(
n
umpy
.
float64
,
'npy_float64'
,
'Float64'
),
'complex128'
:
(
n
umpy
.
complex128
,
'theano_complex128'
,
'float16'
:
(
n
p
.
float16
,
'npy_float16'
,
'Float16'
),
'float32'
:
(
n
p
.
float32
,
'npy_float32'
,
'Float32'
),
'float64'
:
(
n
p
.
float64
,
'npy_float64'
,
'Float64'
),
'complex128'
:
(
n
p
.
complex128
,
'theano_complex128'
,
'Complex128'
),
'complex64'
:
(
n
umpy
.
complex64
,
'theano_complex64'
,
'Complex64'
),
'bool'
:
(
n
umpy
.
bool_
,
'npy_bool'
,
'Bool'
),
'uint8'
:
(
n
umpy
.
uint8
,
'npy_uint8'
,
'UInt8'
),
'int8'
:
(
n
umpy
.
int8
,
'npy_int8'
,
'Int8'
),
'uint16'
:
(
n
umpy
.
uint16
,
'npy_uint16'
,
'UInt16'
),
'int16'
:
(
n
umpy
.
int16
,
'npy_int16'
,
'Int16'
),
'uint32'
:
(
n
umpy
.
uint32
,
'npy_uint32'
,
'UInt32'
),
'int32'
:
(
n
umpy
.
int32
,
'npy_int32'
,
'Int32'
),
'uint64'
:
(
n
umpy
.
uint64
,
'npy_uint64'
,
'UInt64'
),
'int64'
:
(
n
umpy
.
int64
,
'npy_int64'
,
'Int64'
)
'complex64'
:
(
n
p
.
complex64
,
'theano_complex64'
,
'Complex64'
),
'bool'
:
(
n
p
.
bool_
,
'npy_bool'
,
'Bool'
),
'uint8'
:
(
n
p
.
uint8
,
'npy_uint8'
,
'UInt8'
),
'int8'
:
(
n
p
.
int8
,
'npy_int8'
,
'Int8'
),
'uint16'
:
(
n
p
.
uint16
,
'npy_uint16'
,
'UInt16'
),
'int16'
:
(
n
p
.
int16
,
'npy_int16'
,
'Int16'
),
'uint32'
:
(
n
p
.
uint32
,
'npy_uint32'
,
'UInt32'
),
'int32'
:
(
n
p
.
int32
,
'npy_int32'
,
'Int32'
),
'uint64'
:
(
n
p
.
uint64
,
'npy_uint64'
,
'UInt64'
),
'int64'
:
(
n
p
.
int64
,
'npy_int64'
,
'Int64'
)
}[
self
.
dtype
]
except
KeyError
:
raise
TypeError
(
"Unsupported dtype for
%
s:
%
s"
%
(
...
...
@@ -505,8 +505,8 @@ class Scalar(Type):
# If the 'int' C type is not exactly the same as an existing
# 'npy_intX', some C code may not compile, e.g. when assigning
# the value 0 (cast to 'int' in C) to a theano_complex64.
if
(
n
umpy
.
dtype
(
'intc'
)
.
num
not
in
[
n
umpy
.
dtype
(
d
[
4
:])
.
num
for
d
in
real_types
]):
if
(
n
p
.
dtype
(
'intc'
)
.
num
not
in
[
n
p
.
dtype
(
d
[
4
:])
.
num
for
d
in
real_types
]):
# In that case we add the 'int' type to the real types.
real_types
.
append
(
'int'
)
...
...
@@ -645,7 +645,7 @@ class Scalar(Type):
return
[
"import_array();"
]
def
c_code_cache_version
(
self
):
return
(
13
,
n
umpy
.
__version__
)
return
(
13
,
n
p
.
__version__
)
def
get_shape_info
(
self
,
obj
):
return
obj
.
itemsize
...
...
@@ -1247,7 +1247,7 @@ class LT(LogicalComparison):
def
impl
(
self
,
x
,
y
):
# built-in < don't support complex
return
n
umpy
.
less
(
x
,
y
)
return
n
p
.
less
(
x
,
y
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,
y
)
=
inputs
...
...
@@ -1266,7 +1266,7 @@ class GT(LogicalComparison):
def
impl
(
self
,
x
,
y
):
# built-in > don't support complex
return
n
umpy
.
greater
(
x
,
y
)
return
n
p
.
greater
(
x
,
y
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,
y
)
=
inputs
...
...
@@ -1285,7 +1285,7 @@ class LE(LogicalComparison):
def
impl
(
self
,
x
,
y
):
# built-in <= don't support complex
return
n
umpy
.
less_equal
(
x
,
y
)
return
n
p
.
less_equal
(
x
,
y
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,
y
)
=
inputs
...
...
@@ -1304,7 +1304,7 @@ class GE(LogicalComparison):
def
impl
(
self
,
x
,
y
):
# built-in >= don't support complex
return
n
umpy
.
greater_equal
(
x
,
y
)
return
n
p
.
greater_equal
(
x
,
y
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,
y
)
=
inputs
...
...
@@ -1353,7 +1353,7 @@ class IsNan(FixedLogicalComparison):
nfunc_spec
=
(
'isnan'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
isnan
(
x
)
return
n
p
.
isnan
(
x
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -1378,7 +1378,7 @@ class IsInf(FixedLogicalComparison):
nfunc_spec
=
(
'isinf'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
isinf
(
x
)
return
n
p
.
isinf
(
x
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -1596,7 +1596,7 @@ class Maximum(BinaryScalarOp):
def
impl
(
self
,
*
inputs
):
# The built-in max function don't support complex type
return
n
umpy
.
maximum
(
*
inputs
)
return
n
p
.
maximum
(
*
inputs
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,
y
)
=
inputs
...
...
@@ -1633,7 +1633,7 @@ class Minimum(BinaryScalarOp):
def
impl
(
self
,
*
inputs
):
# The built-in min function don't support complex type
return
n
umpy
.
minimum
(
*
inputs
)
return
n
p
.
minimum
(
*
inputs
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,
y
)
=
inputs
...
...
@@ -1709,7 +1709,7 @@ class Mul(ScalarOp):
nfunc_spec
=
(
'multiply'
,
2
,
1
)
def
impl
(
self
,
*
inputs
):
return
n
umpy
.
product
(
inputs
)
return
n
p
.
product
(
inputs
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
z
,)
=
outputs
...
...
@@ -1859,10 +1859,10 @@ class TrueDiv(BinaryScalarOp):
return
super
(
TrueDiv
,
self
)
.
output_types
(
types
)
def
impl
(
self
,
x
,
y
):
x
=
n
umpy
.
asarray
(
x
)
y
=
n
umpy
.
asarray
(
y
)
x
=
n
p
.
asarray
(
x
)
y
=
n
p
.
asarray
(
y
)
if
all
(
a
.
dtype
in
discrete_types
for
a
in
(
x
,
y
)):
return
n
umpy
.
sctypeDict
[
config
.
floatX
](
float
(
x
)
/
y
)
return
n
p
.
sctypeDict
[
config
.
floatX
](
float
(
x
)
/
y
)
else
:
return
x
/
y
...
...
@@ -1999,7 +1999,7 @@ class Mod(BinaryScalarOp):
"complex numbers, since numpy deprecated it."
)
def
impl
(
self
,
x
,
y
):
if
isinstance
(
x
,
n
umpy
.
complex
)
or
isinstance
(
y
,
numpy
.
complex
):
if
isinstance
(
x
,
n
p
.
complex
)
or
isinstance
(
y
,
np
.
complex
):
raise
self
.
complex_error
return
x
%
y
...
...
@@ -2360,7 +2360,7 @@ class Abs(UnaryScalarOp):
return
Apply
(
self
,
inputs
,
outputs
)
def
impl
(
self
,
x
):
return
n
umpy
.
abs
(
x
)
return
n
p
.
abs
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2400,7 +2400,7 @@ class Sgn(UnaryScalarOp):
def
impl
(
self
,
x
):
# casting to output type is handled by filter
return
n
umpy
.
sign
(
x
)
return
n
p
.
sign
(
x
)
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2437,7 +2437,7 @@ class Ceil(UnaryScalarOp):
nfunc_spec
=
(
'ceil'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
ceil
(
x
)
return
n
p
.
ceil
(
x
)
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2460,7 +2460,7 @@ class Floor(UnaryScalarOp):
nfunc_spec
=
(
'floor'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
floor
(
x
)
return
n
p
.
floor
(
x
)
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2483,7 +2483,7 @@ class Trunc(UnaryScalarOp):
nfunc_spec
=
(
'trunc'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
trunc
(
x
)
return
n
p
.
trunc
(
x
)
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2508,7 +2508,7 @@ class RoundHalfToEven(UnaryScalarOp):
nfunc_spec
=
(
'around'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
round
(
x
)
return
n
p
.
round
(
x
)
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2561,21 +2561,21 @@ round_half_to_even = RoundHalfToEven(same_out_float_only)
def
round_half_away_from_zero_
(
a
):
if
a
>
0
:
return
n
umpy
.
floor
(
a
+
0.5
)
return
n
p
.
floor
(
a
+
0.5
)
else
:
return
n
umpy
.
ceil
(
a
-
0.5
)
return
n
p
.
ceil
(
a
-
0.5
)
round_half_away_from_zero_vec64
=
n
umpy
.
vectorize
(
round_half_away_from_zero_vec64
=
n
p
.
vectorize
(
round_half_away_from_zero_
,
doc
=
'round_half_away_from_zero_vec64'
)
round_half_away_from_zero_vec32
=
n
umpy
.
vectorize
(
round_half_away_from_zero_vec32
=
n
p
.
vectorize
(
round_half_away_from_zero_
,
doc
=
'round_half_away_from_zero_vec32'
,
otypes
=
[
'float32'
])
def
round_half_away_from_zero_vec
(
a
):
if
getattr
(
a
,
'dtype'
,
None
)
==
n
umpy
.
float32
:
if
getattr
(
a
,
'dtype'
,
None
)
==
n
p
.
float32
:
return
round_half_away_from_zero_vec32
(
a
)
return
round_half_away_from_zero_vec64
(
a
)
...
...
@@ -2653,7 +2653,7 @@ class Inv(UnaryScalarOp):
"""
def
impl
(
self
,
x
):
return
n
umpy
.
float32
(
1.0
)
/
x
return
n
p
.
float32
(
1.0
)
/
x
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2691,8 +2691,8 @@ class Log(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
log
(
x
,
sig
=
'f'
)
return
n
umpy
.
log
(
x
)
return
n
p
.
log
(
x
,
sig
=
'f'
)
return
n
p
.
log
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2733,8 +2733,8 @@ class Log2(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
log2
(
x
,
sig
=
'f'
)
return
n
umpy
.
log2
(
x
)
return
n
p
.
log2
(
x
,
sig
=
'f'
)
return
n
p
.
log2
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2747,7 +2747,7 @@ class Log2(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
/
(
x
*
n
umpy
.
asarray
(
math
.
log
(
2.0
))
.
astype
(
x
.
dtype
)),
return
gz
/
(
x
*
n
p
.
asarray
(
math
.
log
(
2.0
))
.
astype
(
x
.
dtype
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -2772,8 +2772,8 @@ class Log10(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
log10
(
x
,
sig
=
'f'
)
return
n
umpy
.
log10
(
x
)
return
n
p
.
log10
(
x
,
sig
=
'f'
)
return
n
p
.
log10
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2786,7 +2786,7 @@ class Log10(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
/
(
x
*
n
umpy
.
log
(
10.0
)),
return
gz
/
(
x
*
n
p
.
log
(
10.0
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -2809,8 +2809,8 @@ class Log1p(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
log1p
(
x
,
sig
=
'f'
)
return
n
umpy
.
log1p
(
x
)
return
n
p
.
log1p
(
x
,
sig
=
'f'
)
return
n
p
.
log1p
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2844,8 +2844,8 @@ class Exp(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
exp
(
x
,
sig
=
'f'
)
return
n
umpy
.
exp
(
x
)
return
n
p
.
exp
(
x
,
sig
=
'f'
)
return
n
p
.
exp
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2877,8 +2877,8 @@ class Exp2(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
exp2
(
x
,
sig
=
'f'
)
return
n
umpy
.
exp2
(
x
)
return
n
p
.
exp2
(
x
,
sig
=
'f'
)
return
n
p
.
exp2
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2891,7 +2891,7 @@ class Exp2(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
*
exp2
(
x
)
*
log
(
n
umpy
.
cast
[
x
.
type
](
2
)),
return
gz
*
exp2
(
x
)
*
log
(
n
p
.
cast
[
x
.
type
](
2
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -2910,8 +2910,8 @@ class Expm1(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
expm1
(
x
,
sig
=
'f'
)
return
n
umpy
.
expm1
(
x
)
return
n
p
.
expm1
(
x
,
sig
=
'f'
)
return
n
p
.
expm1
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -2972,8 +2972,8 @@ class Sqrt(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
sqrt
(
x
,
sig
=
'f'
)
return
n
umpy
.
sqrt
(
x
)
return
n
p
.
sqrt
(
x
,
sig
=
'f'
)
return
n
p
.
sqrt
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3005,8 +3005,8 @@ class Deg2Rad(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
deg2rad
(
x
,
sig
=
'f'
)
return
n
umpy
.
deg2rad
(
x
)
return
n
p
.
deg2rad
(
x
,
sig
=
'f'
)
return
n
p
.
deg2rad
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3019,7 +3019,7 @@ class Deg2Rad(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
*
n
umpy
.
asarray
(
numpy
.
pi
/
180
,
gz
.
type
),
return
gz
*
n
p
.
asarray
(
np
.
pi
/
180
,
gz
.
type
),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3038,8 +3038,8 @@ class Rad2Deg(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
rad2deg
(
x
,
sig
=
'f'
)
return
n
umpy
.
rad2deg
(
x
)
return
n
p
.
rad2deg
(
x
,
sig
=
'f'
)
return
n
p
.
rad2deg
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3052,7 +3052,7 @@ class Rad2Deg(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
*
n
umpy
.
asarray
(
180.
/
numpy
.
pi
,
gz
.
type
),
return
gz
*
n
p
.
asarray
(
180.
/
np
.
pi
,
gz
.
type
),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3073,8 +3073,8 @@ class Cos(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
cos
(
x
,
sig
=
'f'
)
return
n
umpy
.
cos
(
x
)
return
n
p
.
cos
(
x
,
sig
=
'f'
)
return
n
p
.
cos
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3106,8 +3106,8 @@ class ArcCos(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
arccos
(
x
,
sig
=
'f'
)
return
n
umpy
.
arccos
(
x
)
return
n
p
.
arccos
(
x
,
sig
=
'f'
)
return
n
p
.
arccos
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3120,7 +3120,7 @@ class ArcCos(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
-
gz
/
sqrt
(
n
umpy
.
cast
[
x
.
type
](
1
)
-
sqr
(
x
)),
return
-
gz
/
sqrt
(
n
p
.
cast
[
x
.
type
](
1
)
-
sqr
(
x
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3141,8 +3141,8 @@ class Sin(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
sin
(
x
,
sig
=
'f'
)
return
n
umpy
.
sin
(
x
)
return
n
p
.
sin
(
x
,
sig
=
'f'
)
return
n
p
.
sin
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3174,8 +3174,8 @@ class ArcSin(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
arcsin
(
x
,
sig
=
'f'
)
return
n
umpy
.
arcsin
(
x
)
return
n
p
.
arcsin
(
x
,
sig
=
'f'
)
return
n
p
.
arcsin
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3188,7 +3188,7 @@ class ArcSin(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
/
sqrt
(
n
umpy
.
cast
[
x
.
type
](
1
)
-
sqr
(
x
)),
return
gz
/
sqrt
(
n
p
.
cast
[
x
.
type
](
1
)
-
sqr
(
x
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3207,8 +3207,8 @@ class Tan(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
tan
(
x
,
sig
=
'f'
)
return
n
umpy
.
tan
(
x
)
return
n
p
.
tan
(
x
,
sig
=
'f'
)
return
n
p
.
tan
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3240,8 +3240,8 @@ class ArcTan(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
arctan
(
x
,
sig
=
'f'
)
return
n
umpy
.
arctan
(
x
)
return
n
p
.
arctan
(
x
,
sig
=
'f'
)
return
n
p
.
arctan
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3254,7 +3254,7 @@ class ArcTan(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
/
(
n
umpy
.
cast
[
x
.
type
](
1
)
+
sqr
(
x
)),
return
gz
/
(
n
p
.
cast
[
x
.
type
](
1
)
+
sqr
(
x
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3275,8 +3275,8 @@ class ArcTan2(BinaryScalarOp):
if
x_dtype
in
(
'int8'
,
'uint8'
):
y_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
y_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
arctan2
(
y
,
x
,
sig
=
'f'
)
return
n
umpy
.
arctan2
(
y
,
x
)
return
n
p
.
arctan2
(
y
,
x
,
sig
=
'f'
)
return
n
p
.
arctan2
(
y
,
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
y
,
x
)
=
inputs
...
...
@@ -3322,8 +3322,8 @@ class Cosh(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
cosh
(
x
,
sig
=
'f'
)
return
n
umpy
.
cosh
(
x
)
return
n
p
.
cosh
(
x
,
sig
=
'f'
)
return
n
p
.
cosh
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3355,8 +3355,8 @@ class ArcCosh(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
arccosh
(
x
,
sig
=
'f'
)
return
n
umpy
.
arccosh
(
x
)
return
n
p
.
arccosh
(
x
,
sig
=
'f'
)
return
n
p
.
arccosh
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3369,7 +3369,7 @@ class ArcCosh(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
/
sqrt
(
sqr
(
x
)
-
n
umpy
.
cast
[
x
.
type
](
1
)),
return
gz
/
sqrt
(
sqr
(
x
)
-
n
p
.
cast
[
x
.
type
](
1
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3392,8 +3392,8 @@ class Sinh(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
sinh
(
x
,
sig
=
'f'
)
return
n
umpy
.
sinh
(
x
)
return
n
p
.
sinh
(
x
,
sig
=
'f'
)
return
n
p
.
sinh
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3425,8 +3425,8 @@ class ArcSinh(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
arcsinh
(
x
,
sig
=
'f'
)
return
n
umpy
.
arcsinh
(
x
)
return
n
p
.
arcsinh
(
x
,
sig
=
'f'
)
return
n
p
.
arcsinh
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3439,7 +3439,7 @@ class ArcSinh(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
/
sqrt
(
sqr
(
x
)
+
n
umpy
.
cast
[
x
.
type
](
1
)),
return
gz
/
sqrt
(
sqr
(
x
)
+
n
p
.
cast
[
x
.
type
](
1
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3463,8 +3463,8 @@ class Tanh(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
tanh
(
x
,
sig
=
'f'
)
return
n
umpy
.
tanh
(
x
)
return
n
p
.
tanh
(
x
,
sig
=
'f'
)
return
n
p
.
tanh
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3496,8 +3496,8 @@ class ArcTanh(UnaryScalarOp):
# half-precision (float16), where we want float32.
x_dtype
=
str
(
getattr
(
x
,
'dtype'
,
''
))
if
x_dtype
in
(
'int8'
,
'uint8'
):
return
n
umpy
.
arctanh
(
x
,
sig
=
'f'
)
return
n
umpy
.
arctanh
(
x
)
return
n
p
.
arctanh
(
x
,
sig
=
'f'
)
return
n
p
.
arctanh
(
x
)
def
L_op
(
self
,
inputs
,
outputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3510,7 +3510,7 @@ class ArcTanh(UnaryScalarOp):
else
:
return
[
x
.
zeros_like
()]
return
gz
/
(
n
umpy
.
cast
[
x
.
type
](
1
)
-
sqr
(
x
)),
return
gz
/
(
n
p
.
cast
[
x
.
type
](
1
)
-
sqr
(
x
)),
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3530,7 +3530,7 @@ class Real(UnaryScalarOp):
# nfunc_spec = ('real', 1, 1)
def
impl
(
self
,
x
):
return
n
umpy
.
real
(
x
)
return
n
p
.
real
(
x
)
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3544,7 +3544,7 @@ class Imag(UnaryScalarOp):
nfunc_spec
=
(
'imag'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
imag
(
x
)
return
n
p
.
imag
(
x
)
def
grad
(
self
,
inputs
,
gout
):
(
x
,)
=
inputs
...
...
@@ -3563,7 +3563,7 @@ class Angle(UnaryScalarOp):
nfunc_spec
=
(
'angle'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
angle
(
x
)
return
n
p
.
angle
(
x
)
def
grad
(
self
,
inputs
,
gout
):
# y = x.imag
...
...
@@ -3610,7 +3610,7 @@ class Complex(BinaryScalarOp):
return
[
complex64
]
def
impl
(
self
,
x
,
y
):
return
n
umpy
.
complex
(
x
,
y
)
return
n
p
.
complex
(
x
,
y
)
def
grad
(
self
,
inputs
,
gout
):
(
x
,
y
)
=
inputs
...
...
@@ -3624,7 +3624,7 @@ class Conj(UnaryScalarOp):
nfunc_spec
=
(
'conj'
,
1
,
1
)
def
impl
(
self
,
x
):
return
n
umpy
.
conj
(
x
)
return
n
p
.
conj
(
x
)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
...
...
@@ -3646,12 +3646,12 @@ class ComplexFromPolar(BinaryScalarOp):
def
impl
(
self
,
r
,
theta
):
if
r
<
0
:
raise
ValueError
(
'polar radius must be non-negative'
,
r
)
x
=
r
*
n
umpy
.
cos
(
theta
)
y
=
r
*
n
umpy
.
sin
(
theta
)
x
=
r
*
n
p
.
cos
(
theta
)
y
=
r
*
n
p
.
sin
(
theta
)
if
x
.
dtype
==
'float32'
:
return
n
umpy
.
complex64
(
numpy
.
complex
(
x
,
y
))
return
n
p
.
complex64
(
np
.
complex
(
x
,
y
))
else
:
return
n
umpy
.
complex128
(
numpy
.
complex
(
x
,
y
))
return
n
p
.
complex128
(
np
.
complex
(
x
,
y
))
def
grad
(
self
,
inputs
,
gout
):
(
r
,
theta
)
=
inputs
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论