Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
fe6a777b
提交
fe6a777b
authored
9月 23, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modified casting to work correctly with complex-valued types. Casting from
complex->real is forbidden by the cast() function.
上级
5d44e7db
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
55 行增加
和
21 行删除
+55
-21
basic.py
theano/tensor/basic.py
+30
-21
test_casting.py
theano/tensor/tests/test_casting.py
+25
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
fe6a777b
...
@@ -882,18 +882,6 @@ class ScalarFromTensor(Op):
...
@@ -882,18 +882,6 @@ class ScalarFromTensor(Op):
scalar_from_tensor
=
ScalarFromTensor
()
scalar_from_tensor
=
ScalarFromTensor
()
@constructor
def
cast
(
t
,
dtype
):
mapping
=
{
'int8'
:
convert_to_int8
,
'int16'
:
convert_to_int16
,
'int32'
:
convert_to_int32
,
'int64'
:
convert_to_int64
,
'float32'
:
convert_to_float32
,
'float64'
:
convert_to_float64
,
'complex64'
:
convert_to_complex64
,
'complex128'
:
convert_to_complex128
}
return
mapping
[
dtype
](
t
)
#to be removed as we get the epydoc routine-documenting thing going -JB 20080924
#to be removed as we get the epydoc routine-documenting thing going -JB 20080924
def
_conversion
(
real_value
,
name
):
def
_conversion
(
real_value
,
name
):
__oplist_tag
(
real_value
,
'casting'
)
__oplist_tag
(
real_value
,
'casting'
)
...
@@ -901,30 +889,52 @@ def _conversion(real_value, name):
...
@@ -901,30 +889,52 @@ def _conversion(real_value, name):
pprint
.
assign
(
real_value
,
printing
.
FunctionPrinter
(
name
))
pprint
.
assign
(
real_value
,
printing
.
FunctionPrinter
(
name
))
return
real_value
return
real_value
convert_to_int8
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_int8
),
'int8'
)
#
# These _conver_to_<type> functions have leading underscores to indicate that they should not
# be called directly. They do not perform sanity checks about what types you are casting to
# what. That logic is implemented by the `cast()` function below.
#
_convert_to_int8
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_int8
),
'int8'
)
"""Cast to 8-bit integer"""
"""Cast to 8-bit integer"""
convert_to_int16
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_int16
),
'int16'
)
_
convert_to_int16
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_int16
),
'int16'
)
"""Cast to 16-bit integer"""
"""Cast to 16-bit integer"""
convert_to_int32
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_int32
),
'int32'
)
_
convert_to_int32
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_int32
),
'int32'
)
"""Cast to 32-bit integer"""
"""Cast to 32-bit integer"""
convert_to_int64
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_int64
),
'int64'
)
_
convert_to_int64
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_int64
),
'int64'
)
"""Cast to 64-bit integer"""
"""Cast to 64-bit integer"""
convert_to_float32
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_float32
),
'float32'
)
_
convert_to_float32
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_float32
),
'float32'
)
"""Cast to single-precision floating point"""
"""Cast to single-precision floating point"""
convert_to_float64
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_float64
),
'float64'
)
_
convert_to_float64
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_float64
),
'float64'
)
"""Cast to double-precision floating point"""
"""Cast to double-precision floating point"""
convert_to_complex64
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_complex64
),
'complex64'
)
_
convert_to_complex64
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_complex64
),
'complex64'
)
"""Cast to single-precision complex"""
"""Cast to single-precision complex"""
convert_to_complex128
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_complex128
),
'complex128'
)
_
convert_to_complex128
=
_conversion
(
elemwise
.
Elemwise
(
scal
.
convert_to_complex128
),
'complex128'
)
"""Cast to double-precision complex"""
"""Cast to double-precision complex"""
_cast_mapping
=
{
'int8'
:
_convert_to_int8
,
'int16'
:
_convert_to_int16
,
'int32'
:
_convert_to_int32
,
'int64'
:
_convert_to_int64
,
'float32'
:
_convert_to_float32
,
'float64'
:
_convert_to_float64
,
'complex64'
:
_convert_to_complex64
,
'complex128'
:
_convert_to_complex128
}
@constructor
def
cast
(
x
,
dtype
):
"""Symbolically cast `x` to a Tensor of type `dtype`."""
if
x
.
type
.
dtype
.
startswith
(
'complex'
)
and
not
dtype
.
startswith
(
'complex'
):
raise
TypeError
(
'Casting from complex to real is ambiguous: consider real(), imag(), angle() or abs()'
)
return
_cast_mapping
[
dtype
](
x
)
##########################
##########################
...
@@ -1140,7 +1150,6 @@ def abs_(a):
...
@@ -1140,7 +1150,6 @@ def abs_(a):
pprint
.
assign
(
abs_
,
printing
.
PatternPrinter
((
'|
%(0)
s|'
,
-
1000
)))
pprint
.
assign
(
abs_
,
printing
.
PatternPrinter
((
'|
%(0)
s|'
,
-
1000
)))
@_scal_elemwise
@_scal_elemwise
def
exp
(
a
):
def
exp
(
a
):
"""e^`a`"""
"""e^`a`"""
...
...
theano/tensor/tests/test_casting.py
0 → 100644
浏览文件 @
fe6a777b
import
unittest
from
theano
import
function
from
theano.tensor
import
*
class
test_casting
(
unittest
.
TestCase
):
def
test_0
(
self
):
for
op_fn
in
convert_to_int32
,
convert_to_float32
,
convert_to_float64
:
for
type_fn
in
bvector
,
ivector
,
fvector
,
dvector
:
x
=
type_fn
()
f
=
function
([
x
],
op_fn
(
x
))
xval
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
10
)
*
10
,
dtype
=
type_fn
.
dtype
)
yval
=
f
(
xval
)
assert
str
(
yval
.
dtype
)
==
op_fn
.
scalar_op
.
output_types_preference
.
spec
[
0
]
.
dtype
def
test_illegal
(
self
):
try
:
x
=
zmatrix
()
function
([
x
],
convert_to_float64
(
x
))(
numpy
.
ones
((
2
,
3
),
dtype
=
'complex128'
))
except
TypeError
:
return
assert
0
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论