>>> import torch>>> a = torch.Tensor((1,2))>>> b = torch.Tensor((3,2))>>> a*btensor([3., 4.])>>> a/btensor([0.3333, 1.0000])>>> a==btensor([False, True])>>> a==1tensor([ True, False])>>> c = torch.Tensor((3,2,1)) >>> a==cTraceback (most recent call last): File "<stdin>", line 1, in <module>RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 0
>>> a = torch.Tensor((1,2))>>> print("yes" if a else "no")Traceback (most recent call last): File "<stdin>", line 1, in <module>RuntimeError: Boolean value of Tensor with more than one value is ambiguous>>> a.all()tensor(False)>>> a.any()tensor(True)
这也意味着你需要用 if a is not None: 而不是 if a:
来判断一个张量变量是否已被赋值。