#Tensor Datatypes

Tensors can also be created with specific data types. This is useful when you need to work with different precision levels. By default, PyTorch tensors are created with the torch.float32 datatype unless specified otherwise. You can create tensors with other data types to control precision or memory usage.

import torch # Create a tensor of type float32 float_tensor = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32) print("Float Tensor:") print(float_tensor) # Create a tensor of type int64 int_tensor = torch.tensor([1, 2, 3], dtype=torch.int64) print("Integer Tensor:") print(int_tensor) print(float_tensor.dtype) # torch.float64 print(int_tensor.dtype) # torch.int32

Here, torch.tensor([...], dtype=...) allows you to specify the data type of the tensor elements.