简体   繁体   English

参数为require_grad的torch.Tensor()

[英]torch.Tensor() with requires_grad parameter

I can't use torch.Tensor() with requires_grad parameter (torch version : 0.4.1) 我无法将torch.Tensor()与required_grad参数一起使用(火炬版本:0.4.1)

without requires_grad : 没有require_grad:

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

with requires_grad=True or requires_grad=False : 使用require_grad = True或require_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

You are creating the tensor x by using the torch.Tensor class constructor which doesn't take the requires_grad flag. 您所创建的张x使用torch.Tensor类的构造函数不拿requires_grad标志。 Instead, you want to use torch.tensor() (lowercase 't') method 相反,您想使用torch.tensor() (小写的“ t”)方法

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

Edit: adding a link to docs: torch.Tensor 编辑:添加到文档的链接: torch.Tensor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM