Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
977b8b75
提交
977b8b75
authored
7月 06, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
1f61b6c9
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
36 行增加
和
28 行删除
+36
-28
test_casting.py
theano/tensor/tests/test_casting.py
+36
-28
没有找到文件。
theano/tensor/tests/test_casting.py
浏览文件 @
977b8b75
import
unittest
from
theano
import
function
from
theano.tensor.basic
import
(
_convert_to_int32
,
_convert_to_int8
,
_convert_to_int16
,
_convert_to_int64
,
_convert_to_float32
,
_convert_to_float64
)
from
theano.tensor.basic
import
(
_convert_to_int32
,
_convert_to_int8
,
_convert_to_int16
,
_convert_to_int64
,
_convert_to_float32
,
_convert_to_float64
)
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
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
=
theano
.
_asarray
(
numpy
.
random
.
rand
(
10
)
*
10
,
dtype
=
type_fn
.
dtype
)
xval
=
theano
.
_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
assert
(
str
(
yval
.
dtype
)
==
op_fn
.
scalar_op
.
output_types_preference
.
spec
[
0
]
.
dtype
)
def
test_illegal
(
self
):
try
:
x
=
zmatrix
()
function
([
x
],
cast
(
x
,
'float64'
))(
numpy
.
ones
((
2
,
3
),
dtype
=
'complex128'
))
function
([
x
],
cast
(
x
,
'float64'
))(
numpy
.
ones
((
2
,
3
),
dtype
=
'complex128'
))
except
TypeError
:
return
assert
0
def
test_basic
(
self
):
for
type1
in
[
'uint8'
,
'uint16'
,
'uint32'
,
'uint64'
,
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
,
'float64'
]:
x
=
TensorType
(
dtype
=
type1
,
broadcastable
=
(
False
,
))
.
make_variable
()
for
type2
,
converter
in
zip
([
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
,
'float64'
],
for
type1
in
[
'uint8'
,
'uint16'
,
'uint32'
,
'uint64'
,
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
,
'float64'
]:
x
=
TensorType
(
dtype
=
type1
,
broadcastable
=
(
False
,
))
.
make_variable
()
for
type2
,
converter
in
zip
([
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
,
'float64'
],
[
_convert_to_int8
,
_convert_to_int16
,
_convert_to_int32
,
_convert_to_int64
,
_convert_to_float32
,
_convert_to_float64
]):
_convert_to_int32
,
_convert_to_int64
,
_convert_to_float32
,
_convert_to_float64
]):
y
=
converter
(
x
)
f
=
function
([
compile
.
In
(
x
,
strict
=
True
)],
y
)
a
=
numpy
.
arange
(
10
,
dtype
=
type1
)
f
=
function
([
compile
.
In
(
x
,
strict
=
True
)],
y
)
a
=
numpy
.
arange
(
10
,
dtype
=
type1
)
b
=
f
(
a
)
self
.
assertTrue
(
numpy
.
all
(
b
==
numpy
.
arange
(
10
,
dtype
=
type2
)))
self
.
assertTrue
(
numpy
.
all
(
b
==
numpy
.
arange
(
10
,
dtype
=
type2
)))
def
test_convert_to_complex
(
self
):
val64
=
numpy
.
ones
(
3
,
dtype
=
'complex64'
)
+
0.5
j
val128
=
numpy
.
ones
(
3
,
dtype
=
'complex128'
)
+
0.5
j
vec64
=
TensorType
(
'complex64'
,
(
False
,
))()
vec128
=
TensorType
(
'complex128'
,
(
False
,
))()
vec64
=
TensorType
(
'complex64'
,
(
False
,
))()
vec128
=
TensorType
(
'complex128'
,
(
False
,
))()
f
=
function
([
vec64
],
basic
.
_convert_to_complex128
(
vec64
))
f
=
function
([
vec64
],
basic
.
_convert_to_complex128
(
vec64
))
#we need to compare with the same type.
assert
vec64
.
type
.
values_eq_approx
(
val128
,
f
(
val64
))
f
=
function
([
vec128
],
basic
.
_convert_to_complex128
(
vec128
))
f
=
function
([
vec128
],
basic
.
_convert_to_complex128
(
vec128
))
assert
vec64
.
type
.
values_eq_approx
(
val128
,
f
(
val128
))
f
=
function
([
vec64
],
basic
.
_convert_to_complex64
(
vec64
))
f
=
function
([
vec64
],
basic
.
_convert_to_complex64
(
vec64
))
assert
vec64
.
type
.
values_eq_approx
(
val64
,
f
(
val64
))
f
=
function
([
vec128
],
basic
.
_convert_to_complex64
(
vec128
))
f
=
function
([
vec128
],
basic
.
_convert_to_complex64
(
vec128
))
assert
vec128
.
type
.
values_eq_approx
(
val64
,
f
(
val128
))
# upcasting to complex128
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
,
'float64'
]:
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
,
'float64'
]:
a
=
theano
.
shared
(
numpy
.
ones
(
3
,
dtype
=
t
))
b
=
theano
.
shared
(
numpy
.
ones
(
3
,
dtype
=
'complex128'
))
f
=
function
([],
basic
.
_convert_to_complex128
(
a
))
f
=
function
([],
basic
.
_convert_to_complex128
(
a
))
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
# upcasting to complex64
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
]:
for
t
in
[
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'float32'
]:
a
=
theano
.
shared
(
numpy
.
ones
(
3
,
dtype
=
t
))
b
=
theano
.
shared
(
numpy
.
ones
(
3
,
dtype
=
'complex64'
))
f
=
function
([],
basic
.
_convert_to_complex64
(
a
))
f
=
function
([],
basic
.
_convert_to_complex64
(
a
))
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
# downcast to complex64
for
t
in
[
'float64'
]:
a
=
theano
.
shared
(
numpy
.
ones
(
3
,
dtype
=
t
))
b
=
theano
.
shared
(
numpy
.
ones
(
3
,
dtype
=
'complex64'
))
f
=
function
([],
basic
.
_convert_to_complex64
(
a
))
f
=
function
([],
basic
.
_convert_to_complex64
(
a
))
assert
a
.
type
.
values_eq_approx
(
b
.
get_value
(),
f
())
def
test_bug_complext_10_august_09
(
self
):
v0
=
dmatrix
()
v1
=
basic
.
_convert_to_complex128
(
v0
)
...
...
@@ -87,5 +95,5 @@ class test_casting(unittest.TestCase):
inputs
=
[
v0
]
outputs
=
[
v1
]
f
=
function
(
inputs
,
outputs
)
i
=
numpy
.
zeros
((
2
,
2
))
assert
(
f
(
i
)
==
numpy
.
zeros
((
2
,
2
)))
.
all
()
i
=
numpy
.
zeros
((
2
,
2
))
assert
(
f
(
i
)
==
numpy
.
zeros
((
2
,
2
)))
.
all
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论