Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ed79287a
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ed79287a
编写于
10月 14, 2019
作者:
Z
zhupengyang
提交者:
Tao Luo
10月 14, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add input type and dtype check for cast_op (#20070) (#20616)
上级
40823167
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
51 addition
and
23 deletion
+51
-23
python/paddle/fluid/data_feeder.py
python/paddle/fluid/data_feeder.py
+23
-22
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+11
-0
python/paddle/fluid/tests/unittests/test_cast_op.py
python/paddle/fluid/tests/unittests/test_cast_op.py
+16
-0
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
+1
-1
未找到文件。
python/paddle/fluid/data_feeder.py
浏览文件 @
ed79287a
...
...
@@ -29,31 +29,32 @@ __all__ = ['DataFeeder']
def
convert_dtype
(
dtype
):
if
isinstance
(
dtype
,
str
):
if
dtype
in
[
'
float32'
,
'int64'
,
'float64'
,
'float16'
,
'int32'
,
'uint8
'
,
'
bool
'
'
bool'
,
'float16'
,
'float32'
,
'float64'
,
'int8'
,
'int16
'
,
'
int32'
,
'int64'
,
'uint8
'
]:
return
dtype
else
:
raise
ValueError
(
"dtype must be any of [bool, int32, float32, int64, "
"float64, uint8]"
)
elif
dtype
==
core
.
VarDesc
.
VarType
.
BOOL
:
return
'bool'
elif
dtype
==
core
.
VarDesc
.
VarType
.
FP32
:
return
'float32'
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT64
:
return
'int64'
elif
dtype
==
core
.
VarDesc
.
VarType
.
FP64
:
return
'float64'
elif
dtype
==
core
.
VarDesc
.
VarType
.
FP16
:
return
'float16'
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT32
:
return
'int32'
elif
dtype
==
core
.
VarDesc
.
VarType
.
UINT8
:
return
'uint8'
else
:
raise
ValueError
(
"dtype must be any of [bool,int32, float32, int64, "
"float64, uint8]"
)
if
dtype
==
core
.
VarDesc
.
VarType
.
BOOL
:
return
'bool'
elif
dtype
==
core
.
VarDesc
.
VarType
.
FP16
:
return
'float16'
elif
dtype
==
core
.
VarDesc
.
VarType
.
FP32
:
return
'float32'
elif
dtype
==
core
.
VarDesc
.
VarType
.
FP64
:
return
'float64'
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT8
:
return
'int8'
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT16
:
return
'int16'
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT32
:
return
'int32'
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT64
:
return
'int64'
elif
dtype
==
core
.
VarDesc
.
VarType
.
UINT8
:
return
'uint8'
raise
ValueError
(
"dtype must be any of [bool, float16, float32, float64, int8, int16, "
"int32, int64, uint8]"
)
class
DataToLoDTensorConverter
(
object
):
...
...
python/paddle/fluid/layers/tensor.py
浏览文件 @
ed79287a
...
...
@@ -192,6 +192,17 @@ def cast(x, dtype):
# [ 0 4]] int32
"""
helper
=
LayerHelper
(
'cast'
,
**
locals
())
if
not
isinstance
(
x
,
Variable
):
raise
TypeError
(
"The type of 'x' in cast must be Variable, but received %s"
%
(
type
(
x
)))
if
convert_dtype
(
x
.
dtype
)
not
in
[
'bool'
,
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
,
'uint8'
]:
raise
TypeError
(
"The data type of 'x' in cast must be one of [bool, float16, float32, float64, int32, int64, uint8], but received %s."
%
(
convert_dtype
(
x
.
dtype
)))
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
dtype
)
helper
.
append_op
(
type
=
'cast'
,
...
...
python/paddle/fluid/tests/unittests/test_cast_op.py
浏览文件 @
ed79287a
...
...
@@ -18,6 +18,8 @@ import op_test
import
unittest
import
numpy
as
np
import
paddle.fluid.core
as
core
import
paddle.fluid
as
fluid
from
paddle.fluid
import
compiler
,
Program
,
program_guard
class
TestCastOp1
(
op_test
.
OpTest
):
...
...
@@ -69,5 +71,19 @@ class TestCastOp3(op_test.OpTest):
self
.
check_output
(
atol
=
1e-3
)
class
TestCastOpError
(
op_test
.
OpTest
):
def
test_errors
(
self
):
with
program_guard
(
Program
(),
Program
()):
# The input type of cast_op must be Variable.
x1
=
fluid
.
create_lod_tensor
(
np
.
array
([[
-
1
]]),
[[
1
]],
fluid
.
CPUPlace
())
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
cast
,
x1
,
'int32'
)
# The input dtype of cast_op must be bool, float16, float32, float64, int32, int64, uint8.
x2
=
fluid
.
layers
.
data
(
name
=
'x2'
,
shape
=
[
4
],
dtype
=
'int8'
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
cast
,
x2
,
'int32'
)
x3
=
fluid
.
layers
.
data
(
name
=
'x3'
,
shape
=
[
4
],
dtype
=
'int16'
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
cast
,
x3
,
'int32'
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
浏览文件 @
ed79287a
...
...
@@ -230,7 +230,7 @@ class TestFillConstantOpError(OpTest):
value
=
5
,
dtype
=
'uint4'
)
self
.
assertRaises
(
Valu
eError
,
Typ
eError
,
fluid
.
layers
.
fill_constant
,
shape
=
[
1
],
value
=
5
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录