python - 带有 requires_grad 参数的 torch.Tensor()

标签 python pytorch tensor

我不能使用带有 requires_grad 参数的 torch.Tensor()(torch 版本:0.4.1)

没有 requires_grad :

x = torch.Tensor([[.5, .3, 2.1]])
print(x)
> tensor([[0.5000, 0.3000, 2.1000]])

with requires_grad=True 或 requires_grad=False :

x = torch.Tensor([[.5, .3, 2.1]], requires_grad=False)
print(x)
Traceback (most recent call last):
  File "D:/_P/dev/ai/pytorch/notes/tensor01.py", line 4, in <module>
    x = torch.Tensor([[.5, .3, 2.1]], requires_grad=False)
TypeError: new() received an invalid combination of arguments - got (list, requires_grad=bool), but expected one of:
 * (torch.device device)
 * (torch.Storage storage)
 * (Tensor other)
 * (tuple of ints size, torch.device device)
      didn't match because some of the keywords were incorrect: requires_grad
 * (object data, torch.device device)
      didn't match because some of the keywords were incorrect: requires_grad

最佳答案

您正在使用不带 requires_grad 标志的 torch.Tensor 类构造函数创建张量 x。相反,您想使用 torch.tensor()(小写't')方法

x = torch.tensor([[.5, .3, 2.1]], requires_grad=False)

编辑:添加文档链接:torch.Tensor

关于python - 带有 requires_grad 参数的 torch.Tensor(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53201921/

相关文章:

python - 在 map 上绘制电流数据 : Too many value to unpack error

python - 无法将 Jupyter 笔记本导出到 Azure ML Studio 中的 Python 脚本

python - 我如何使用 torch.stack?

python - 如何在窗口中像 exe 一样为 Tkinter 构建一个包?

python - 使用 pybarcode ImageWriter 和 docx 模块将条形码图像附加到 docx 文件

python - pytorch中的高阶梯度

permissions - PyTorch 权限错误 : [Errno 13] Permission denied: '/.torch'

machine-learning - PyTorch 的 nn.Transformer 中的 src 和 tgt 有什么区别?

python - 访问张量的元素

tensorflow - 将不同等级的张量相乘