What is the difference between torch.tensor and torch.Tensor?(torch.tensor 和 torch.Tensor 有什么区别?)
问题描述
从 0.4.0 版本开始,可以使用 torch.tensor 和 torch.Tensor
有什么区别?提供这两个非常相似且令人困惑的替代方案的原因是什么?
在 PyTorch 中 torch.Tensor 是主要的张量类.所以所有的张量都只是 torch.Tensor 的实例.
当你调用 torch.Tensor() 时,你会得到一个没有任何 data 的空张量.
相比之下 torch.tensor 是一个返回张量的函数.在 文档 中,它说:
torch.tensor(data, dtype=None, device=None, requires_grad=False) → Tensor用data构造一个张量.
<小时>这也解释了为什么通过调用创建一个没有 `data` 的 `torch.Tensor` 的空张量实例是没有问题的:
tensor_without_data = torch.Tensor()但另一方面:
tensor_without_data = torch.tensor()会导致错误:
---------------------------------------------------------------------------TypeError Traceback(最近一次调用最后一次)<ipython-input-12-ebc3ceaa76d2>在 <module>()---->1 火炬张量()类型错误:tensor() 缺少 1 个必需的位置参数:数据";<小时>但总的来说,没有理由选择 `torch.Tensor` 而不是 `torch.tensor`.`torch.Tensor` 也缺少文档字符串.
在没有 data 的情况下创建张量的类似行为,例如:torch.Tensor() 可以使用:
torch.tensor(())输出:
张量([])Since version 0.4.0, it is possible to use torch.tensor and torch.Tensor
What is the difference? What was the reasoning for providing these two very similar and confusing alternatives?
In PyTorch torch.Tensor is the main tensor class. So all tensors are just instances of torch.Tensor.
When you call torch.Tensor() you will get an empty tensor without any data.
In contrast torch.tensor is a function which returns a tensor. In the documentation it says:
torch.tensor(data, dtype=None, device=None, requires_grad=False) → TensorConstructs a tensor with
data.
This also also explains why it is no problem creating an empty tensor instance of `torch.Tensor` without `data` by calling:
tensor_without_data = torch.Tensor()
But on the other side:
tensor_without_data = torch.tensor()
Will lead to an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-ebc3ceaa76d2> in <module>()
----> 1 torch.tensor()
TypeError: tensor() missing 1 required positional arguments: "data"
But in general there is no reason to choose `torch.Tensor` over `torch.tensor`. Also `torch.Tensor` lacks a docstring.
Similar behaviour for creating a tensor without data like with: torch.Tensor() can be achieved using:
torch.tensor(())
Output:
tensor([])
这篇关于torch.tensor 和 torch.Tensor 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:torch.tensor 和 torch.Tensor 有什么区别?
基础教程推荐
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
