Introduction
Tensors hold numbers in PyTorch. Choosing the right data type helps your model work well and fast.
torch.tensor(data, dtype=torch.<data_type>)
import torch x = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32)
y = torch.tensor([1, 2, 3], dtype=torch.int64)
z = torch.tensor([True, False, True], dtype=torch.bool)
import torch # Create tensors with different data types float_tensor = torch.tensor([1.5, 2.5, 3.5], dtype=torch.float32) int_tensor = torch.tensor([1, 2, 3], dtype=torch.int64) bool_tensor = torch.tensor([True, False, True], dtype=torch.bool) # Print tensor details print(f"Float tensor: {float_tensor}, dtype: {float_tensor.dtype}") print(f"Int tensor: {int_tensor}, dtype: {int_tensor.dtype}") print(f"Bool tensor: {bool_tensor}, dtype: {bool_tensor.dtype}")
torch.tensor([1.0, 2.0, 3.0]) in PyTorch?import torch t = torch.tensor([0, 1, 2], dtype=torch.bool) t = t.to(torch.float32) print(t.dtype)
import torch t = torch.tensor([1, 0, 1], dtype=torch.bool) print(t + 1)
Pass the test and this pattern moves unknown