Reputation: 598
I am facing a very weird error when I am trying to run the following simple line :
a = torch.Tensor([0,0,0],dtype = torch.int64)
TypeError: new() received an invalid combination of arguments - got (list, dtype=torch.dtype), but expected one of:
* (*, torch.device device)
didn't match because some of the keywords were incorrect: dtype
* (torch.Storage storage)
* (Tensor other)
* (tuple of ints size, *, torch.device device)
* (object data, *, torch.device device)
whereas if we look at official documentation
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor
Parameters
data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.
dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, infers data type from data.
Why is the code snippet not working even though the official documentation supports it?
Upvotes: 10
Views: 25094
Reputation: 1524
As Jan's answer said but here I will explain why Tensor different from tensor
torch.Tensor is an alias for the default tensor type (torch.FloatTensor). which is a CPU tensor, 32-bit floating-point so the type and device are predetermined.
#This will cause an Error, because Tensor has no dtype and device properties on it's constructor
example_tensor = torch.Tensor( [ [[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 0], [1, 2]] ] , dtype=torch.float32, device=device)
To move a tensor to a new device, you can write new_tensor = example_tensor.to(device) where the device will be either CPU or Cuda. and to set new type you can use new_tensor.dtype=torch.float64 for example
example_tensor = torch.Tensor(
[
[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
[[9, 0], [1, 2]]
]
)
new_tensor = example_tensor.to(device)
new_tensor.dtype #set or get dtype
A tensor (case senstive tensor not Tensor) of specific data type can be constructed by passing a torch.dtype and/or a torch.device to a constructor or tensor creation op:
cuda0 = torch.device('cuda:0')
torch.ones([2, 4], dtype=torch.float64, device=cuda0)
.
Upvotes: 4
Reputation: 1868
Capitalization matters -- in your top example, you have Tensor
with uppercase T, but the documentation excerpt is talking about tensor
with lowercase t.
Upvotes: 15