एक PyTorch टेंसर समरूप है, अर्थात, एक टेंसर के सभी तत्व एक ही डेटा प्रकार के होते हैं। हम .dtype" . का उपयोग करके एक टेंसर के डेटा प्रकार तक पहुंच सकते हैं टेंसर की विशेषता। यह टेंसर का डेटा प्रकार लौटाता है।
कदम
-
आवश्यक पुस्तकालय आयात करें। निम्नलिखित सभी पायथन उदाहरणों में, आवश्यक पायथन पुस्तकालय है टॉर्च . सुनिश्चित करें कि आपने इसे पहले ही इंस्टॉल कर लिया है।
-
एक टेंसर बनाएं और उसे प्रिंट करें।
-
गणना T.dtype . यहाँ T वह टेंसर है जिसका हम डेटा प्रकार प्राप्त करना चाहते हैं।
-
टेंसर के डेटा प्रकार को प्रिंट करें।
उदाहरण 1
निम्न पायथन प्रोग्राम दिखाता है कि एक टेंसर का डेटा प्रकार कैसे प्राप्त करें।
# 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