Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
61c1b955
提交
61c1b955
authored
6月 17, 2011
作者:
Olivier Delalleau
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added tensor.isnan and tensor.isinf. Also:
- Added some doc about the identity, commutative and associative fields - Fixed crash in filter function when using NaN values
上级
ef5ce2e0
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
94 行增加
和
3 行删除
+94
-3
basic.py
theano/scalar/basic.py
+40
-0
basic.py
theano/tensor/basic.py
+14
-3
test_elemwise.py
theano/tensor/tests/test_elemwise.py
+40
-0
没有找到文件。
theano/scalar/basic.py
浏览文件 @
61c1b955
...
@@ -672,6 +672,12 @@ class UnaryScalarOp(ScalarOp):
...
@@ -672,6 +672,12 @@ class UnaryScalarOp(ScalarOp):
nin
=
1
nin
=
1
class
BinaryScalarOp
(
ScalarOp
):
class
BinaryScalarOp
(
ScalarOp
):
# One may define in subclasses the following fields:
# - `identity`: for an associative operation, identity corresponds to
# the neutral element. For instance, it will be 0 for addition, 1 for
# multiplication, True for "and", False for "or".
# - `commutative`: whether op(a, b) == op(b, a)
# - `associative`: whether op(op(a, b), c) == op(a, op(b, c))
nin
=
2
nin
=
2
...
@@ -685,6 +691,15 @@ class LogicalComparison(BinaryScalarOp):
...
@@ -685,6 +691,15 @@ class LogicalComparison(BinaryScalarOp):
def
grad
(
self
,
inputs
,
output_gradients
):
def
grad
(
self
,
inputs
,
output_gradients
):
return
[
None
,
None
]
return
[
None
,
None
]
class
FixedLogicalComparison
(
UnaryScalarOp
):
"""
Comparison to a fixed value.
"""
def
output_types
(
self
,
*
input_dtypes
):
return
[
int8
]
def
grad
(
self
,
inputs
,
output_gradients
):
return
[
None
]
class
LT
(
LogicalComparison
):
class
LT
(
LogicalComparison
):
identity
=
False
identity
=
False
commutative
=
False
commutative
=
False
...
@@ -749,6 +764,7 @@ class EQ(LogicalComparison):
...
@@ -749,6 +764,7 @@ class EQ(LogicalComparison):
return
"
%(z)
s = (
%(x)
s ==
%(y)
s);"
%
locals
()
return
"
%(z)
s = (
%(x)
s ==
%(y)
s);"
%
locals
()
eq
=
EQ
()
eq
=
EQ
()
class
NEQ
(
LogicalComparison
):
class
NEQ
(
LogicalComparison
):
identity
=
False
identity
=
False
commutative
=
True
commutative
=
True
...
@@ -761,6 +777,30 @@ class NEQ(LogicalComparison):
...
@@ -761,6 +777,30 @@ class NEQ(LogicalComparison):
return
"
%(z)
s = (
%(x)
s !=
%(y)
s);"
%
locals
()
return
"
%(z)
s = (
%(x)
s !=
%(y)
s);"
%
locals
()
neq
=
NEQ
()
neq
=
NEQ
()
class
IsNan
(
FixedLogicalComparison
):
def
impl
(
self
,
x
):
return
theano
.
_asarray
(
numpy
.
isnan
(
x
),
dtype
=
'int8'
)
def
c_code
(
self
,
node
,
name
,
(
x
,
),
(
z
,
),
sub
):
if
node
.
inputs
[
0
]
.
type
in
complex_types
:
raise
NotImplementedError
()
return
"
%(z)
s = isnan(
%(x)
s);"
%
locals
()
isnan
=
IsNan
()
class
IsInf
(
FixedLogicalComparison
):
def
impl
(
self
,
x
):
return
theano
.
_asarray
(
numpy
.
isinf
(
x
),
dtype
=
'int8'
)
def
c_code
(
self
,
node
,
name
,
(
x
,
),
(
z
,
),
sub
):
if
node
.
inputs
[
0
]
.
type
in
complex_types
:
raise
NotImplementedError
()
# Note that the C isinf returns -1 for -Inf and +1 for +Inf, while
# numpy simply returns True: we mimic numpy's behavior here, thus
# the absolute value.
return
"
%(z)
s = abs(isinf(
%(x)
s));"
%
locals
()
isinf
=
IsInf
()
class
InRange
(
LogicalComparison
):
class
InRange
(
LogicalComparison
):
nin
=
3
nin
=
3
def
__init__
(
self
,
openlow
,
openhi
):
def
__init__
(
self
,
openlow
,
openhi
):
...
...
theano/tensor/basic.py
浏览文件 @
61c1b955
...
@@ -581,7 +581,10 @@ class TensorType(Type):
...
@@ -581,7 +581,10 @@ class TensorType(Type):
# data has to be converted.
# data has to be converted.
# Check that this conversion is lossless
# Check that this conversion is lossless
converted_data
=
theano
.
_asarray
(
data
,
self
.
dtype
)
converted_data
=
theano
.
_asarray
(
data
,
self
.
dtype
)
if
numpy
.
all
(
data
==
converted_data
):
# We use the `values_eq` static function from TensorType
# to handle NaN values.
if
TensorType
.
values_eq
(
data
,
converted_data
,
force_same_dtype
=
False
):
data
=
converted_data
data
=
converted_data
else
:
else
:
# Do not print a too long description of data
# Do not print a too long description of data
...
@@ -661,12 +664,12 @@ class TensorType(Type):
...
@@ -661,12 +664,12 @@ class TensorType(Type):
return
False
return
False
@staticmethod
@staticmethod
def
values_eq
(
a
,
b
):
def
values_eq
(
a
,
b
,
force_same_dtype
=
True
):
#TODO: check to see if the dtype and shapes must match
#TODO: check to see if the dtype and shapes must match
# for now, we err on safe side...
# for now, we err on safe side...
if
a
.
shape
!=
b
.
shape
:
if
a
.
shape
!=
b
.
shape
:
return
False
return
False
if
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
=
numpy
.
all
(
a_eq_b
)
r
=
numpy
.
all
(
a_eq_b
)
...
@@ -2050,6 +2053,14 @@ def eq(a, b):
...
@@ -2050,6 +2053,14 @@ def eq(a, b):
def
neq
(
a
,
b
):
def
neq
(
a
,
b
):
"""a != b"""
"""a != b"""
@_scal_elemwise_with_nfunc
(
'isnan'
,
1
,
1
)
def
isnan
(
a
):
"""isnan(a)"""
@_scal_elemwise_with_nfunc
(
'isinf'
,
1
,
1
)
def
isinf
(
a
):
"""isinf(a)"""
##########################
##########################
# Condition
# Condition
...
...
theano/tensor/tests/test_elemwise.py
浏览文件 @
61c1b955
...
@@ -6,6 +6,7 @@ from theano import gof
...
@@ -6,6 +6,7 @@ from theano import gof
from
theano.scalar
import
*
from
theano.scalar
import
*
from
theano
import
tensor
from
theano
import
tensor
from
theano.compile.mode
import
get_default_mode
from
theano.tensor.elemwise
import
*
from
theano.tensor.elemwise
import
*
from
theano.tests
import
unittest_tools
from
theano.tests
import
unittest_tools
...
@@ -405,6 +406,45 @@ class test_Prod(unittest.TestCase):
...
@@ -405,6 +406,45 @@ class test_Prod(unittest.TestCase):
cPickle
.
dumps
(
o
)
cPickle
.
dumps
(
o
)
class
test_IsInf_IsNan
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
test_vals
=
map
(
numpy
.
array
,
[
0
,
1
,
numpy
.
nan
,
numpy
.
inf
,
-
numpy
.
inf
,
[
numpy
.
nan
,
numpy
.
inf
,
-
numpy
.
inf
,
0
,
1
,
-
1
],
])
self
.
scalar
=
tensor
.
scalar
()
self
.
vector
=
tensor
.
vector
()
self
.
mode
=
get_default_mode
()
if
isinstance
(
self
.
mode
,
theano
.
compile
.
debugmode
.
DebugMode
):
# Disable the check preventing usage of NaN / Inf values.
self
.
mode
=
copy
(
self
.
mode
)
self
.
mode
.
check_isfinite
=
False
def
run_test
(
self
,
isfunc
):
for
input
in
(
self
.
scalar
,
self
.
vector
):
theano_isfunc
=
theano
.
function
([
input
],
getattr
(
tensor
,
isfunc
)(
input
),
mode
=
self
.
mode
)
numpy_isfunc
=
getattr
(
numpy
,
isfunc
)
for
x
in
self
.
test_vals
:
if
((
x
.
ndim
==
0
and
input
is
not
self
.
scalar
)
or
(
x
.
ndim
==
1
and
input
is
not
self
.
vector
)):
# We only test with the appropriate input type.
continue
assert
(
theano_isfunc
(
x
)
==
numpy_isfunc
(
x
))
.
all
()
def
test_isinf
(
self
):
return
self
.
run_test
(
'isinf'
)
def
test_isnan
(
self
):
return
self
.
run_test
(
'isnan'
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
#unittest.main()
#unittest.main()
suite
=
unittest
.
TestSuite
([
test_Prod
(
'test_mul_without_zeros_zeros'
)])
suite
=
unittest
.
TestSuite
([
test_Prod
(
'test_mul_without_zeros_zeros'
)])
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论