Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
b7a54fc1
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
b7a54fc1
编写于
6月 15, 2021
作者:
Z
Zhou Wei
提交者:
GitHub
6月 15, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support convert core.Tensor to paddle.Tensor (#33430)
上级
e47c3f04
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
23 addition
and
5 deletion
+23
-5
paddle/fluid/pybind/imperative.cc
paddle/fluid/pybind/imperative.cc
+1
-1
python/paddle/fluid/tests/unittests/test_var_base.py
python/paddle/fluid/tests/unittests/test_var_base.py
+18
-1
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+4
-3
未找到文件。
paddle/fluid/pybind/imperative.cc
浏览文件 @
b7a54fc1
...
...
@@ -245,7 +245,7 @@ static void InitVarBaseFromNumpyWithArgDefault(imperative::VarBase *self,
}
static
void
InitVarBaseFromTensorWithArgDefault
(
imperative
::
VarBase
*
self
,
const
framework
::
LoD
Tensor
&
tensor
)
{
imperative
::
VarBase
*
self
,
const
framework
::
Tensor
&
tensor
)
{
VLOG
(
4
)
<<
"Init VarBase"
;
auto
place
=
imperative
::
GetCurrentTracer
()
->
ExpectedPlace
();
new
(
self
)
imperative
::
VarBase
(
...
...
python/paddle/fluid/tests/unittests/test_var_base.py
浏览文件 @
b7a54fc1
...
...
@@ -176,7 +176,6 @@ class TestVarBase(unittest.TestCase):
x
=
paddle
.
to_tensor
(
1
,
dtype
=
'uint8'
)
self
.
assertEqual
(
x
.
item
(),
1
)
print
(
type
(
x
.
item
()))
self
.
assertTrue
(
isinstance
(
x
.
item
(),
int
))
x
=
paddle
.
to_tensor
(
1
,
dtype
=
'int8'
)
...
...
@@ -203,6 +202,24 @@ class TestVarBase(unittest.TestCase):
self
.
assertEqual
(
x
.
item
(),
1
+
1j
)
self
.
assertTrue
(
isinstance
(
x
.
item
(),
complex
))
numpy_array
=
np
.
random
.
randn
(
3
,
4
)
# covert core.LoDTensor to paddle.Tensor
lod_tensor
=
paddle
.
fluid
.
core
.
LoDTensor
()
place
=
paddle
.
fluid
.
framework
.
_current_expected_place
()
lod_tensor
.
set
(
numpy_array
,
place
)
x
=
paddle
.
to_tensor
(
lod_tensor
)
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
numpy_array
))
self
.
assertEqual
(
x
.
type
,
core
.
VarDesc
.
VarType
.
LOD_TENSOR
)
self
.
assertEqual
(
str
(
x
.
place
),
str
(
place
))
# covert core.Tensor to paddle.Tensor
x
=
paddle
.
to_tensor
(
numpy_array
)
dlpack
=
x
.
value
().
get_tensor
().
_to_dlpack
()
tensor_from_dlpack
=
paddle
.
fluid
.
core
.
from_dlpack
(
dlpack
)
x
=
paddle
.
to_tensor
(
tensor_from_dlpack
)
self
.
assertTrue
(
np
.
array_equal
(
x
.
numpy
(),
numpy_array
))
self
.
assertEqual
(
x
.
type
,
core
.
VarDesc
.
VarType
.
LOD_TENSOR
)
with
self
.
assertRaises
(
ValueError
):
paddle
.
randn
([
3
,
2
,
2
]).
item
()
with
self
.
assertRaises
(
ValueError
):
...
...
python/paddle/tensor/creation.py
浏览文件 @
b7a54fc1
...
...
@@ -136,9 +136,10 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
data
=
data
.
_copy_to
(
place
,
False
)
ata
=
_handle_dtype
(
data
,
dtype
)
data
.
stop_gradient
=
stop_gradient
elif
isinstance
(
data
,
core
.
LoDTensor
):
# convert LoDTensor to VarBase first
# Currenly, LoDTensor does no copy when places are same
elif
isinstance
(
data
,
(
core
.
LoDTensor
,
core
.
Tensor
)):
# Note(zhouwei25): should't expose it to users, just for internal use.
# convert core.Tensor/core.LoDTensor to VarBase first
# Currenly, there is no copy when places are same
data
=
paddle
.
Tensor
(
data
)
if
not
data
.
place
.
_equals
(
place
):
data
=
data
.
_copy_to
(
place
,
False
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录