Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7b78bfc0
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看板
未验证
提交
7b78bfc0
编写于
8月 28, 2020
作者:
Z
Zhou Wei
提交者:
GitHub
8月 28, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[2.0API]support set_default_dtype for to_tensor (#26432)
* add set_default_type for tensor * fix doc * fix doc
上级
844583c8
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
46 addition
and
12 deletion
+46
-12
python/paddle/fluid/tests/unittests/test_var_base.py
python/paddle/fluid/tests/unittests/test_var_base.py
+24
-0
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+21
-12
tools/wlist.json
tools/wlist.json
+1
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_var_base.py
浏览文件 @
7b78bfc0
...
@@ -32,6 +32,30 @@ class TestVarBase(unittest.TestCase):
...
@@ -32,6 +32,30 @@ class TestVarBase(unittest.TestCase):
def
test_to_tensor
(
self
):
def
test_to_tensor
(
self
):
def
_test_place
(
place
):
def
_test_place
(
place
):
with
fluid
.
dygraph
.
guard
():
with
fluid
.
dygraph
.
guard
():
paddle
.
set_default_dtype
(
'float32'
)
x
=
paddle
.
to_tensor
(
1
,
place
=
place
,
stop_gradient
=
False
)
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
[
1
]))
self
.
assertNotEqual
(
x
.
dtype
,
core
.
VarDesc
.
VarType
.
FP32
)
x
=
paddle
.
to_tensor
(
1.2
,
place
=
place
,
stop_gradient
=
False
)
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
np
.
array
([
1.2
]).
astype
(
'float32'
)))
self
.
assertEqual
(
x
.
dtype
,
core
.
VarDesc
.
VarType
.
FP32
)
x
=
paddle
.
to_tensor
(
1
+
2j
,
place
=
place
,
stop_gradient
=
False
)
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
[
1
+
2j
]))
self
.
assertEqual
(
x
.
dtype
,
'complex64'
)
paddle
.
set_default_dtype
(
'float64'
)
x
=
paddle
.
to_tensor
(
1.2
,
place
=
place
,
stop_gradient
=
False
)
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
[
1.2
]))
self
.
assertEqual
(
x
.
dtype
,
core
.
VarDesc
.
VarType
.
FP64
)
x
=
paddle
.
to_tensor
(
1
+
2j
,
place
=
place
,
stop_gradient
=
False
)
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
[
1
+
2j
]))
self
.
assertEqual
(
x
.
dtype
,
'complex128'
)
x
=
paddle
.
to_tensor
(
x
=
paddle
.
to_tensor
(
1
,
dtype
=
'float32'
,
place
=
place
,
stop_gradient
=
False
)
1
,
dtype
=
'float32'
,
place
=
place
,
stop_gradient
=
False
)
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
[
1.
]))
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
[
1.
]))
...
...
python/paddle/tensor/creation.py
浏览文件 @
7b78bfc0
...
@@ -71,22 +71,22 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
...
@@ -71,22 +71,22 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
Args:
Args:
data(scalar|tuple|list|ndarray|Tensor|ComplexTensor): Initial data for the tensor.
data(scalar|tuple|list|ndarray|Tensor|ComplexTensor): Initial data for the tensor.
Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor, paddle\.ComplexTensor.
Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor, paddle\.ComplexTensor.
dtype(str, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
dtype(str
|np.dtype
, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
'float32' , 'float64' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8'. And
'float32' , 'float64' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8'. And
'complex64' , 'complex128' only for ComplexTensor.
'complex64' , 'complex128' only for ComplexTensor.
Default: None, for float point number,
Default: None, infers data type
from ``data`` .
get type from ``get_default_type``, for other type, infers
from ``data`` .
place(CPUPlace|CUDAPinnedPlace|CUDAPlace, optional): The place to allocate Tensor. Can be
place(CPUPlace|CUDAPinnedPlace|CUDAPlace, optional): The place to allocate Tensor. Can be
CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place.
CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place.
stop_gradient(bool, optional): Whether to block the gradient propagation of Autograd. Default: True.
stop_gradient(bool, optional): Whether to block the gradient propagation of Autograd. Default: True.
Returns:
Returns:
Tensor: A Tensor or ComplexTensor constructed from ``data``.
Tensor: A Tensor or ComplexTensor constructed from ``data``
.
Raises:
Raises:
TypeError: If the data type of ``data`` is not scalar, list, tuple, numpy.ndarray, paddle.Tensor, paddle.ComplexTensor
TypeError: If the data type of ``data`` is not scalar, list, tuple, numpy.ndarray, paddle.Tensor, paddle.ComplexTensor
ValueError: If ``data`` is tuple|list, it can't contain nested tuple|list with different lengths , such as: [[1, 2], [3, 4, 5]]
ValueError: If ``data`` is tuple|list, it can't contain nested tuple|list with different lengths , such as: [[1, 2], [3, 4, 5]]
TypeError: If ``dtype`` is not bool, float16, float32, float64, int8, int16, int32, int64, uint8, complex64, complex128
TypeError: If ``dtype`` is not bool, float16, float32, float64, int8, int16, int32, int64, uint8, complex64, complex128
ValueError: If ``place`` is not paddle.Place, paddle.CUDAPinnedPlace, paddle.CUDAPlace
ValueError: If ``place`` is not paddle.
CPU
Place, paddle.CUDAPinnedPlace, paddle.CUDAPlace
Examples:
Examples:
...
@@ -94,7 +94,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
...
@@ -94,7 +94,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
import paddle
import paddle
import numpy as np
import numpy as np
paddle.
enable_imperative
()
paddle.
disable_static
()
type(paddle.to_tensor(1))
type(paddle.to_tensor(1))
# <class 'paddle.Tensor'>
# <class 'paddle.Tensor'>
...
@@ -132,7 +132,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
...
@@ -132,7 +132,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
# - dtype: double
# - dtype: double
# - data: [0.1 0.2 0.3 0.4]
# - data: [0.1 0.2 0.3 0.4]
type(paddle.to_tensor([[1+1j, 2], [3+2j, 4]]),
,
dtype='complex64')
type(paddle.to_tensor([[1+1j, 2], [3+2j, 4]]), dtype='complex64')
# <class 'paddle.ComplexTensor'>
# <class 'paddle.ComplexTensor'>
paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64')
paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64')
...
@@ -189,12 +189,13 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
...
@@ -189,12 +189,13 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
"Can't constructs a 'paddle.Tensor' with data type {}, data type must be scalar|list|tuple|numpy.ndarray|paddle.Tensor|paddle.ComplexTensor"
.
"Can't constructs a 'paddle.Tensor' with data type {}, data type must be scalar|list|tuple|numpy.ndarray|paddle.Tensor|paddle.ComplexTensor"
.
format
(
type
(
data
)))
format
(
type
(
data
)))
if
not
np
.
iscomplexobj
(
data
):
if
dtype
:
if
dtype
:
dtype
=
convert_dtype
(
dtype
)
dtype
=
convert_dtype
(
dtype
)
if
dtype
!=
data
.
dtype
:
elif
data
.
dtype
in
[
'float16'
,
'float32'
,
'float64'
]:
dtype
=
paddle
.
framework
.
get_default_dtype
()
if
dtype
and
dtype
!=
data
.
dtype
:
data
=
data
.
astype
(
dtype
)
data
=
data
.
astype
(
dtype
)
if
not
np
.
iscomplexobj
(
data
):
return
paddle
.
Tensor
(
return
paddle
.
Tensor
(
value
=
data
,
value
=
data
,
place
=
place
,
place
=
place
,
...
@@ -202,6 +203,14 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
...
@@ -202,6 +203,14 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
zero_copy
=
True
,
zero_copy
=
True
,
stop_gradient
=
stop_gradient
)
stop_gradient
=
stop_gradient
)
else
:
else
:
if
dtype
:
dtype
=
convert_dtype
(
dtype
)
else
:
dtype
=
paddle
.
framework
.
get_default_dtype
()
dtype
=
'complex64'
if
dtype
in
[
'float16'
,
'float32'
]
else
'complex128'
if
dtype
!=
data
.
dtype
:
data
=
data
.
astype
(
dtype
)
name
=
unique_name
.
generate
(
'generated_tensor'
)
name
=
unique_name
.
generate
(
'generated_tensor'
)
real_tensor
=
paddle
.
Tensor
(
real_tensor
=
paddle
.
Tensor
(
value
=
data
.
real
,
value
=
data
.
real
,
...
...
tools/wlist.json
浏览文件 @
7b78bfc0
...
@@ -247,6 +247,7 @@
...
@@ -247,6 +247,7 @@
"prroi_pool"
"prroi_pool"
],
],
"wlist_temp"
:[
"wlist_temp"
:[
"to_tensor"
,
"ChunkEvaluator"
,
"ChunkEvaluator"
,
"EditDistance"
,
"EditDistance"
,
"ErrorClipByValue"
,
"ErrorClipByValue"
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录