Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
b3a291fa
提交
b3a291fa
authored
10月 19, 2016
作者:
Arnaud Bergeron
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make sure the scalar ops handle bool correctly and don't produce irregular booleans.
上级
33bd55ef
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
40 行增加
和
16 行删除
+40
-16
basic.py
theano/scalar/basic.py
+39
-15
elemwise.py
theano/tensor/elemwise.py
+1
-1
没有找到文件。
theano/scalar/basic.py
浏览文件 @
b3a291fa
...
@@ -689,6 +689,13 @@ def upcast_out(*types):
...
@@ -689,6 +689,13 @@ def upcast_out(*types):
return
get_scalar_type
(
dtype
),
return
get_scalar_type
(
dtype
),
def
upcast_out_nobool
(
*
types
):
type
=
upcast_out
(
*
types
)
if
type
[
0
]
==
bool
:
return
int8
,
return
type
def
upgrade_to_float
(
*
types
):
def
upgrade_to_float
(
*
types
):
"""
"""
Upgrade any int types to float32 or float64 to avoid losing precision.
Upgrade any int types to float32 or float64 to avoid losing precision.
...
@@ -710,6 +717,11 @@ def upgrade_to_float(*types):
...
@@ -710,6 +717,11 @@ def upgrade_to_float(*types):
def
same_out
(
type
):
def
same_out
(
type
):
return
type
,
return
type
,
def
same_out_nobool
(
type
):
if
type
==
bool
:
return
int8
,
return
type
,
def
upcast_out_no_complex
(
*
types
):
def
upcast_out_no_complex
(
*
types
):
if
any
([
type
in
complex_types
for
type
in
types
]):
if
any
([
type
in
complex_types
for
type
in
types
]):
...
@@ -1474,10 +1486,13 @@ class Add(ScalarOp):
...
@@ -1474,10 +1486,13 @@ class Add(ScalarOp):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
z
,)
=
outputs
(
z
,)
=
outputs
op
=
" + "
if
z
.
type
==
bool
:
op
=
" || "
if
not
inputs
:
if
not
inputs
:
return
z
+
" = 0;"
return
z
+
" = 0;"
else
:
else
:
return
z
+
" = "
+
" + "
.
join
(
inputs
)
+
";"
return
z
+
" = "
+
op
.
join
(
inputs
)
+
";"
def
grad
(
self
,
inputs
,
gout
):
def
grad
(
self
,
inputs
,
gout
):
(
gz
,)
=
gout
(
gz
,)
=
gout
...
@@ -1513,10 +1528,13 @@ class Mul(ScalarOp):
...
@@ -1513,10 +1528,13 @@ class Mul(ScalarOp):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
z
,)
=
outputs
(
z
,)
=
outputs
op
=
" * "
if
z
.
type
==
bool
:
op
=
" && "
if
not
inputs
:
if
not
inputs
:
return
z
+
" = 1;"
return
z
+
" = 1;"
else
:
else
:
return
z
+
" = "
+
" * "
.
join
(
inputs
)
+
";"
return
z
+
" = "
+
op
.
join
(
inputs
)
+
";"
def
grad
(
self
,
inputs
,
gout
):
def
grad
(
self
,
inputs
,
gout
):
(
gz
,)
=
gout
(
gz
,)
=
gout
...
@@ -1566,6 +1584,9 @@ class Sub(BinaryScalarOp):
...
@@ -1566,6 +1584,9 @@ class Sub(BinaryScalarOp):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,
y
)
=
inputs
(
x
,
y
)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
if
z
.
type
==
bool
:
# xor
return
"
%(z)
s = (
%(x)
s ||
%(y)
s) && !(
%(x)
s &&
%(y)
s);"
%
locals
()
return
"
%(z)
s =
%(x)
s -
%(y)
s;"
%
locals
()
return
"
%(z)
s =
%(x)
s -
%(y)
s;"
%
locals
()
def
grad
(
self
,
inputs
,
gout
):
def
grad
(
self
,
inputs
,
gout
):
...
@@ -1948,7 +1969,7 @@ class Pow(BinaryScalarOp):
...
@@ -1948,7 +1969,7 @@ class Pow(BinaryScalarOp):
raise
theano
.
gof
.
utils
.
MethodNotDefined
()
raise
theano
.
gof
.
utils
.
MethodNotDefined
()
pow
=
Pow
(
upcast_out
,
name
=
'pow'
)
pow
=
Pow
(
upcast_out
_nobool
,
name
=
'pow'
)
class
Clip
(
ScalarOp
):
class
Clip
(
ScalarOp
):
...
@@ -2073,6 +2094,8 @@ class Cast(UnaryScalarOp):
...
@@ -2073,6 +2094,8 @@ class Cast(UnaryScalarOp):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
if
z
.
type
==
bool
:
return
"
%
s = (
%
s) ? 1 : 0"
%
(
z
,
x
)
return
"
%
s = (
%
s)
%
s;"
%
(
z
,
node
.
outputs
[
0
]
.
type
.
dtype_specs
()[
1
],
x
)
return
"
%
s = (
%
s)
%
s;"
%
(
z
,
node
.
outputs
[
0
]
.
type
.
dtype_specs
()[
1
],
x
)
def
grad
(
self
,
inputs
,
gout
):
def
grad
(
self
,
inputs
,
gout
):
...
@@ -2186,6 +2209,12 @@ abs_ = Abs(same_out)
...
@@ -2186,6 +2209,12 @@ abs_ = Abs(same_out)
class
Sgn
(
UnaryScalarOp
):
class
Sgn
(
UnaryScalarOp
):
nfunc_spec
=
(
'sign'
,
1
,
1
)
nfunc_spec
=
(
'sign'
,
1
,
1
)
@staticmethod
def
output_types_preference
(
x
):
if
x
==
bool
:
raise
TypeError
(
x
)
return
same_out_nocomplex
(
x
)
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
# casting to output type is handled by filter
# casting to output type is handled by filter
return
numpy
.
sign
(
x
)
return
numpy
.
sign
(
x
)
...
@@ -2218,7 +2247,7 @@ class Sgn(UnaryScalarOp):
...
@@ -2218,7 +2247,7 @@ class Sgn(UnaryScalarOp):
return
(
4
,)
+
s
return
(
4
,)
+
s
else
:
# if parent is unversioned, we are too
else
:
# if parent is unversioned, we are too
return
s
return
s
sgn
=
Sgn
(
same_out_nocomplex
,
name
=
'sgn'
)
sgn
=
Sgn
(
name
=
'sgn'
)
class
Ceil
(
UnaryScalarOp
):
class
Ceil
(
UnaryScalarOp
):
...
@@ -2241,7 +2270,7 @@ class Ceil(UnaryScalarOp):
...
@@ -2241,7 +2270,7 @@ class Ceil(UnaryScalarOp):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
return
"
%(z)
s = ceil(
%(x)
s);"
%
locals
()
return
"
%(z)
s = ceil(
%(x)
s);"
%
locals
()
ceil
=
Ceil
(
same_out_no
complex
,
name
=
'ceil'
)
ceil
=
Ceil
(
upgrade_to_float_no_
complex
,
name
=
'ceil'
)
class
Floor
(
UnaryScalarOp
):
class
Floor
(
UnaryScalarOp
):
...
@@ -2264,7 +2293,7 @@ class Floor(UnaryScalarOp):
...
@@ -2264,7 +2293,7 @@ class Floor(UnaryScalarOp):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
return
"
%(z)
s = floor(
%(x)
s);"
%
locals
()
return
"
%(z)
s = floor(
%(x)
s);"
%
locals
()
floor
=
Floor
(
same_out_no
complex
,
name
=
'floor'
)
floor
=
Floor
(
upgrade_to_float_no_
complex
,
name
=
'floor'
)
class
Trunc
(
UnaryScalarOp
):
class
Trunc
(
UnaryScalarOp
):
...
@@ -2282,7 +2311,7 @@ class Trunc(UnaryScalarOp):
...
@@ -2282,7 +2311,7 @@ class Trunc(UnaryScalarOp):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
return
"
%(z)
s =
%(x)
s >= 0? floor(
%(x)
s): -floor(-
%(x)
s);"
%
locals
()
return
"
%(z)
s =
%(x)
s >= 0? floor(
%(x)
s): -floor(-
%(x)
s);"
%
locals
()
trunc
=
Trunc
(
same_out_no
complex
,
name
=
'trunc'
)
trunc
=
Trunc
(
upgrade_to_float_no_
complex
,
name
=
'trunc'
)
class
RoundHalfToEven
(
UnaryScalarOp
):
class
RoundHalfToEven
(
UnaryScalarOp
):
...
@@ -2422,13 +2451,6 @@ class Neg(UnaryScalarOp):
...
@@ -2422,13 +2451,6 @@ class Neg(UnaryScalarOp):
nfunc_spec
=
(
'negative'
,
1
,
1
)
nfunc_spec
=
(
'negative'
,
1
,
1
)
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
# We have to make sure x is not a numpy.bool_, because
# `-numpy.bool_(True)` is `False` (we want 0), and
# `-numpy.bool_(False)` is `True` (we want 1).
# This happens for Composite, as the intermediate results are not
# casted in the dtype of the intermediate variable in general.
if
isinstance
(
x
,
numpy
.
bool_
):
x
=
numpy
.
int8
(
x
)
return
-
x
return
-
x
def
grad
(
self
,
inputs
,
gout
):
def
grad
(
self
,
inputs
,
gout
):
...
@@ -2445,6 +2467,8 @@ class Neg(UnaryScalarOp):
...
@@ -2445,6 +2467,8 @@ class Neg(UnaryScalarOp):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
if
z
.
type
==
bool
:
return
"
%(z)
s = !
%(x)
s;"
%
locals
()
return
"
%(z)
s = -
%(x)
s;"
%
locals
()
return
"
%(z)
s = -
%(x)
s;"
%
locals
()
neg
=
Neg
(
same_out
,
name
=
'neg'
)
neg
=
Neg
(
same_out
,
name
=
'neg'
)
...
@@ -3436,7 +3460,7 @@ class Conj(UnaryScalarOp):
...
@@ -3436,7 +3460,7 @@ class Conj(UnaryScalarOp):
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
return
numpy
.
conj
(
x
)
return
numpy
.
conj
(
x
)
conj
=
Conj
(
same_out
,
name
=
'conj'
)
conj
=
Conj
(
same_out
_nobool
,
name
=
'conj'
)
class
ComplexFromPolar
(
BinaryScalarOp
):
class
ComplexFromPolar
(
BinaryScalarOp
):
...
...
theano/tensor/elemwise.py
浏览文件 @
b3a291fa
...
@@ -1065,7 +1065,7 @@ second dimension
...
@@ -1065,7 +1065,7 @@ second dimension
# We loop over the "aliased" outputs, i.e., those that are
# We loop over the "aliased" outputs, i.e., those that are
# inplace (overwrite the contents of one of the inputs) and
# inplace (overwrite the contents of one of the inputs) and
# make the output pointers point to the
u
r corresponding input
# make the output pointers point to the
i
r corresponding input
# pointers.
# pointers.
for
output
,
oname
in
izip
(
aliased_outputs
,
aliased_onames
):
for
output
,
oname
in
izip
(
aliased_outputs
,
aliased_onames
):
olv_index
=
inputs
.
index
(
dmap
[
output
][
0
])
olv_index
=
inputs
.
index
(
dmap
[
output
][
0
])
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论