Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
6f0ae156
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
6f0ae156
编写于
2月 01, 2023
作者:
P
PuQing
提交者:
GitHub
2月 01, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Numpy]Fix NumpyScaler2Tensor dtype error (#50018)
* fix numpyScaler2Tensor type error * fix to_tensor docs, test=document_fix
上级
03619037
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
112 addition
and
0 deletion
+112
-0
python/paddle/fluid/tests/unittests/test_npscaler_to_tensor.py
...n/paddle/fluid/tests/unittests/test_npscaler_to_tensor.py
+95
-0
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+17
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_npscaler_to_tensor.py
0 → 100644
浏览文件 @
6f0ae156
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
unittest
import
numpy
as
np
import
paddle
DTYPE_MAP
=
{
paddle
.
bool
:
np
.
bool_
,
paddle
.
int32
:
np
.
int32
,
paddle
.
int64
:
np
.
int64
,
paddle
.
float16
:
np
.
float16
,
paddle
.
float32
:
np
.
float32
,
paddle
.
float64
:
np
.
float64
,
paddle
.
complex64
:
np
.
complex64
,
}
class
NumpyScaler2Tensor
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
dtype
=
np
.
float32
self
.
x_np
=
np
.
array
([
1
],
dtype
=
self
.
dtype
)[
0
]
def
test_dynamic_scaler2tensor
(
self
):
paddle
.
disable_static
()
x
=
paddle
.
to_tensor
(
self
.
x_np
)
self
.
assertEqual
(
DTYPE_MAP
[
x
.
dtype
],
self
.
dtype
)
self
.
assertEqual
(
x
.
numpy
(),
self
.
x_np
)
if
self
.
dtype
in
[
np
.
bool_
]:
# bool is not supported convert to 0D-Tensor
return
self
.
assertEqual
(
len
(
x
.
shape
),
0
)
def
test_static_scaler2tensor
(
self
):
if
self
.
dtype
in
[
np
.
float16
,
np
.
complex64
]:
return
paddle
.
enable_static
()
x
=
paddle
.
to_tensor
(
self
.
x_np
)
self
.
assertEqual
(
DTYPE_MAP
[
x
.
dtype
],
self
.
dtype
)
if
self
.
dtype
in
[
np
.
bool_
,
np
.
float64
,
]:
# bool is not supported convert to 0D-Tensor and float64 not supported in static mode
return
self
.
assertEqual
(
len
(
x
.
shape
),
0
)
class
NumpyScaler2TensorBool
(
NumpyScaler2Tensor
):
def
setUp
(
self
):
self
.
dtype
=
np
.
bool_
self
.
x_np
=
np
.
array
([
1
],
dtype
=
self
.
dtype
)[
0
]
class
NumpyScaler2TensorFloat16
(
NumpyScaler2Tensor
):
def
setUp
(
self
):
self
.
dtype
=
np
.
float16
self
.
x_np
=
np
.
array
([
1
],
dtype
=
self
.
dtype
)[
0
]
class
NumpyScaler2TensorFloat64
(
NumpyScaler2Tensor
):
def
setUp
(
self
):
self
.
dtype
=
np
.
float64
self
.
x_np
=
np
.
array
([
1
],
dtype
=
self
.
dtype
)[
0
]
class
NumpyScaler2TensorInt32
(
NumpyScaler2Tensor
):
def
setUp
(
self
):
self
.
dtype
=
np
.
int32
self
.
x_np
=
np
.
array
([
1
],
dtype
=
self
.
dtype
)[
0
]
class
NumpyScaler2TensorInt64
(
NumpyScaler2Tensor
):
def
setUp
(
self
):
self
.
dtype
=
np
.
int64
self
.
x_np
=
np
.
array
([
1
],
dtype
=
self
.
dtype
)[
0
]
class
NumpyScaler2TensorComplex64
(
NumpyScaler2Tensor
):
def
setUp
(
self
):
self
.
dtype
=
np
.
complex64
self
.
x_np
=
np
.
array
([
1
],
dtype
=
self
.
dtype
)[
0
]
python/paddle/tensor/creation.py
浏览文件 @
6f0ae156
...
...
@@ -533,6 +533,9 @@ def logspace(start, stop, num, base=10.0, dtype=None, name=None):
def
_to_tensor_non_static
(
data
,
dtype
=
None
,
place
=
None
,
stop_gradient
=
True
):
if
isinstance
(
data
,
np
.
number
):
# Special case for numpy scalars
data
=
np
.
array
(
data
)
if
not
isinstance
(
data
,
np
.
ndarray
):
def
_handle_dtype
(
data
,
dtype
):
...
...
@@ -627,6 +630,8 @@ def _to_tensor_static(data, dtype=None, stop_gradient=None):
if
isinstance
(
data
,
Variable
)
and
(
dtype
is
None
or
dtype
==
data
.
dtype
):
output
=
data
else
:
if
isinstance
(
data
,
np
.
number
):
# Special case for numpy scalars
data
=
np
.
array
(
data
)
if
not
isinstance
(
data
,
np
.
ndarray
):
if
np
.
isscalar
(
data
)
and
not
isinstance
(
data
,
str
):
...
...
@@ -690,6 +695,18 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
If the ``data`` is already a Tensor, copy will be performed and return a new tensor.
If you only want to change stop_gradient property, please call ``Tensor.stop_gradient = stop_gradient`` directly.
.. code-block:: text
We use the dtype conversion rules following this:
Keep dtype
np.number ───────────► paddle.Tensor
(0D-Tensor)
default_dtype
Python Number ───────────────► paddle.Tensor
(1D-Tensor)
Keep dtype
np.ndarray ───────────► paddle.Tensor
Args:
data(scalar|tuple|list|ndarray|Tensor): Initial data for the tensor.
Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录