PyTorch 텐서는 동질적입니다. 즉, 텐서의 모든 요소는 동일한 데이터 유형입니다. ".dtype"을 사용하여 텐서의 데이터 유형에 액세스할 수 있습니다. 텐서의 속성. 텐서의 데이터 유형을 반환합니다.
단계
-
필요한 라이브러리를 가져옵니다. 다음 모든 Python 예제에서 필수 Python 라이브러리는 torch입니다. . 이미 설치했는지 확인하십시오.
-
텐서를 만들고 인쇄하세요.
-
T.dtype 계산 . 여기서 T는 데이터 유형을 얻으려는 텐서입니다.
-
텐서의 데이터 유형을 인쇄합니다.
예시 1
다음 Python 프로그램은 텐서의 데이터 유형을 가져오는 방법을 보여줍니다.
# Import the library
import torch
# Create a tensor of random numbers of size 3x4
T = torch.randn(3,4)
print("Original Tensor T:\n", T)
# Get the data type of above tensor
data_type = T.dtype
# Print the data type of the tensor
print("Data type of tensor T:\n", data_type) 출력
Original Tensor T: tensor([[ 2.1768, -0.1328, 0.8155, -0.7967], [ 0.1194, 1.0465, 0.0779, 0.9103], [-0.1809, 1.8085, 0.8393, -0.2463]]) Data type of tensor T: torch.float32
예시 2
# Python program to get data type of a tensor
# Import the library
import torch
# Create a tensor of random numbers of size 3x4
T = torch.Tensor([1,2,3,4])
print("Original Tensor T:\n", T)
# Get the data type of above tensor
data_type = T.dtype
# Print the data type of the tensor
print("Data type of tensor T:\n", data_type) 출력
Original Tensor T: tensor([1., 2., 3., 4.]) Data type of tensor T: torch.float32