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

paddle中tensor维度的问题 #39825

Closed
Shelly111111 opened this issue Feb 22, 2022 · 9 comments
Closed

paddle中tensor维度的问题 #39825

Shelly111111 opened this issue Feb 22, 2022 · 9 comments
Assignees
Labels
status/close 已关闭

Comments

@Shelly111111
Copy link

’‘’代码’‘’
import paddle

a = paddle.arange(5)
print(a)
ls = []
for i in a:
ls.append(i)
print(paddle.to_tensor(ls))

’‘’输出‘’‘
Tensor(shape=[5], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
[0, 1, 2, 3, 4])
Tensor(shape=[5, 1], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
[[0],
[1],
[2],
[3],
[4]])

如上,生成了一个尺寸为[5]的tensor,然后将其依次取出并加入一个列表中,再将列表转为tensor后,尺寸便会增加一个维度。对照pytorch以及numpy时并不会如此,如果可以,建议更新一下tensor的数组操作。

@paddle-bot-old
Copy link

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

@Shelly111111
Copy link
Author

Shelly111111 commented Feb 22, 2022

import paddle

a = paddle.arange(5)
print(a)
# Tensor(shape=[5], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#       [0, 1, 2, 3, 4])
a[3].set_value(paddle.to_tensor(7, dtype=paddle.int64))
print(a)
# Tensor(shape=[5], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#       [0, 1, 2, 3, 4])
a[3].set_value(paddle.to_tensor([7], dtype=paddle.int64))
print(a)
# Tensor(shape=[5], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#       [0, 1, 2, 3, 4])
b = paddle.arange(1,6)
a.set_value(b)
print(a)
# Tensor(shape=[5], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#        [1, 2, 3, 4, 5])

如上,tensor也不能只将某段数据更改为我们需要的数据,使用set_value是由于我在某个程序运行时,Leaf Tensor (auto_6208350_) that doesn't stop gradient can't use inplace strategy.这样的错误,由于不能stop_gradient不能直接使用‘=’等号赋值.

我希望我可以对有梯度的tensor的其中一部分进行更改。
我尝试了a[3].set_value(7),这样,它会报错Variable set_value function, arguments type only support Variable, numpy, VarBase, dict, string.

@littletomatodonkey
Copy link
Contributor

你好,感谢反馈,这个需求我们记录下哈~

@zhwesky2010
Copy link
Contributor

这个因为 __getitem__从中取值时,将遍历的scalar元素转为 shape为1的Tensor,目前框架还没支持空维Tensor。如需更改,则要修改__getitem__中的返回值,针对遍历的单个元素返回scalar

@GT-ZhangAcer
Copy link
Member

0维Tensor在EinOps这个开源项目中也有需求,项目作者也期待咱们框架可以尽早支持0维Tensor~

@phoenixsfly
Copy link

0维Tensor在EinOps这个开源项目中也有需求,项目作者也期待咱们框架可以尽早支持0维Tensor~

啥时候会支持0维tensor有具体消息吗?感觉这个太重要了...

@GT-ZhangAcer
Copy link
Member

0维Tensor在EinOps这个开源项目中也有需求,项目作者也期待咱们框架可以尽早支持0维Tensor~

啥时候会支持0维tensor有具体消息吗?感觉这个太重要了...

我这边对这方面认知有些生疏,可以说说有哪些应用场景吗?如果能有个比较常见的场景或者玩法,我也好去催一下他们(哈哈哈)

@phoenixsfly
Copy link

0维Tensor在EinOps这个开源项目中也有需求,项目作者也期待咱们框架可以尽早支持0维Tensor~

啥时候会支持0维tensor有具体消息吗?感觉这个太重要了...

我这边对这方面认知有些生疏,可以说说有哪些应用场景吗?如果能有个比较常见的场景或者玩法,我也好去催一下他们(哈哈哈)

我举一个简单的场景,现在有2个tensor,我希望将它们的首元素合并为一个tensor,按照正常思维,合并得到的元素应该是一个shape为(2,)的1阶tensor对吧

x=paddle.rand([2])
y=paddle.rand([2])

paddle.stack([x[0],y[0]])

但是得到的结果却是一个shape为(2,1)的2阶tensor

Tensor(shape=[2, 1], dtype=float32, place=CPUPlace, stop_gradient=True,
       [[0.10902353],
        [0.77193171]])

那么为了得到预期结果,我不得不对输出再进行一次reshape。
但这还没完,更要命的是假如x,y含有Batch维度,也即:

BATCH=8
x=paddle.rand([BATCH,1])
y=paddle.rand([BATCH,1])

paddle.stack([x[0],y[0]])

得到的结果是也一个shape为(2,1)的2阶tensor,那么这个时候就会和上面引起严重歧义,不得不增加很多代码来判断此时到底应不应该reshape

@zhwesky2010
Copy link
Contributor

zhwesky2010 commented May 15, 2023

@Shelly111111 @GT-ZhangAcer @phoenixsfly

你好,飞桨将于2.5版本全面支持0维Tensor,上述反馈的0D问题也将同时解决。(索引API需要设置 export FLAGS_set_to_1d=0,其他API无需设置)

欢迎试用并安装develop 版本的paddle whl包,将体验最新的0维特性:https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux-pip.html
安装命令:
pip install paddlepaddle-gpu==0.0.0.post112 -f https://www.paddlepaddle.org.cn/whl/linux/gpu/develop.html


Hello, Paddle will fully support the 0-dimensional Tensor in version 2.5, and the 0D issue will also be resolved at the same time.
Welcome to try and install the develop version of the paddle whl package, and you will experience the latest 0-dimensional features: https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux -pip.html
Install Command:
pip install paddlepaddle-gpu==0.0.0.post112 -f https://www.paddlepaddle.org.cn/whl/linux/gpu/develop.html

@paddle-bot paddle-bot bot added the status/close 已关闭 label May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/close 已关闭
Projects
None yet
Development

No branches or pull requests

6 participants