pytorch中张量torch.Size([])和torch.Size([1])的形状差异 [英] Difference in shape of tensor torch.Size([]) and torch.Size([1]) in pytorch

查看:216
本文介绍了pytorch中张量torch.Size([])和torch.Size([1])的形状差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 pytorch 的新手.在玩张量时,我观察到了两种类型的张量-

I am new to pytorch. While playing around with tensors I observed 2 types of tensors-

tensor(58)
tensor([57.3895])

我打印了它们的形状,输出分别是 -

I printed their shape and the output was respectively -

torch.Size([])
torch.Size([1])

两者有什么区别?

推荐答案

第一个有 0 尺寸维度,第二个有 1 维度,PyTorch 试图使两者兼容(0 大小可以被认为类似于 float 或类似的,尽管我还没有真正遇到过明确需要它的情况,除了 @javadr 显示在他下面的回答中).

First one has 0 size dimension, second one has 1 dimension, PyTorch tries to make both compatible (0 size can be regarded similarly to float or a-like although I haven't really met the case where it's explicitly needed, except what @javadr shown in his answer below).

通常你会使用 list 来初始化它,见这里 了解更多信息.

Usually you would use list to initialize it though, see here for more information.

这篇关于pytorch中张量torch.Size([])和torch.Size([1])的形状差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆