1. Tensor Info

Entry Memo

shape

张量形状,有些操作需特定的形状规则

dtype

张量元素以何种数据类型存储

device

张量存储在何种设备,通常为CPU或GPU

tensor_info = torch.rand(3,4)

print(tensor_info)
print(f"Tensor Shape: {tensor_info.shape}")
print(f"Tensor Datatype: {tensor_info.dtype}")
print(f"Tensor Device: {tensor_info.device}")
tensor([[0.6740, 0.2000, 0.3854, 0.1561],
        [0.6748, 0.1559, 0.0782, 0.6290],
        [0.2350, 0.4629, 0.6631, 0.8041]])

Tensor Shape: torch.Size([3, 4])
Tensor Datatype: torch.float32
Tensor Device: cpu(默认CPU)
  • 当遇到PyTorch问题时,通常会询问问题:张量是什么形状,什么数据类型,存储在哪?