Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add API:Tensor.item(*args) and API:np.array(Tensor) #32561

Merged
merged 1 commit into from
Apr 30, 2021

Conversation

zhwesky2010
Copy link
Contributor

@zhwesky2010 zhwesky2010 commented Apr 26, 2021

PR types

New features

PR changes

APIs

Describe


1. Add API: Tensor.item(*args) to convert one element of Tensor to a Python scalar and return to user.

import paddle

x = paddle.to_tensor(1)
print(x.item())             #1
print(type(x.item()))      #<class 'int'>

x = paddle.to_tensor(1.0)
print(x.item())             #1.0
print(type(x.item()))      #<class 'float'>

x = paddle.to_tensor(True)
print(x.item())             #True
print(type(x.item()))       #<class 'bool'>

x = paddle.to_tensor(1+1j)
print(x.item())             #(1+1j)
print(type(x.item()))       #<class 'complex'>

x = paddle.to_tensor([[1.111111, 2.222222, 3.333333]])
print(x.item(2))            #3.333333
print(x.item(0, 2))         #3.333333

x = paddle.to_tensor([1, 2])
x.item()   #ValueError: only one element tensor can be converted to Python scalars when no input coordinates.

2. Fix API: np.array(Tensor) to convert the Tensor to numpy.ndarray.

import paddle
import numpy as np

x = paddle.randn([2, 2])
x_array = np.array(x)

print(type(x_array))            #<class 'numpy.ndarray'>
print(x_array.shape)            #(2, 2)

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@zhwesky2010 zhwesky2010 force-pushed the Tensor2 branch 3 times, most recently from c08f670 to 8040567 Compare April 26, 2021 05:18
@zhwesky2010 zhwesky2010 changed the title add API Tensor.item() and API np.array(Tensor) [2.1]add API Tensor.item() and API np.array(Tensor) Apr 26, 2021
@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Apr 26, 2021
@PaddlePaddle PaddlePaddle unlocked this conversation Apr 26, 2021
@zhwesky2010 zhwesky2010 force-pushed the Tensor2 branch 3 times, most recently from 2a6fff8 to 3ea171f Compare April 27, 2021 12:29
@zhwesky2010 zhwesky2010 changed the title [2.1]add API Tensor.item() and API np.array(Tensor) [2.1]add API Tensor.item(*args) and API np.array(Tensor) Apr 27, 2021
@zhwesky2010 zhwesky2010 changed the title [2.1]add API Tensor.item(*args) and API np.array(Tensor) [2.1]add API:Tensor.item(*args) and API:np.array(Tensor) Apr 27, 2021
@zhwesky2010 zhwesky2010 changed the title [2.1]add API:Tensor.item(*args) and API:np.array(Tensor) add API:Tensor.item(*args) and API:np.array(Tensor) Apr 29, 2021
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhwesky2010 zhwesky2010 merged commit 7e2b60a into PaddlePaddle:develop Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants