判断数组类型是tensor还是numpy


import torch
import numpy as np



# 确认是否为 Tensor 类型
a = torch.constant([1.])
b = torch.constant([True, False])
c = torch.constant("hello, world.")
d = np.arange(4)

# 判断是否为Tensor类型
# ininstance
print(isinstance(a, torch.Tensor))
print(isinstance(b, torch.Tensor))
print(isinstance(c, torch.Tensor))
print(isinstance(d, torch.Tensor))

# is_tensor
print(torch.is_tensor(a))
print(torch.is_tensor(b))
print(torch.is_tensor(c))
print(torch.is_tensor(d))


# dtype
print(a.dtype)
print(b.dtype)
print(c.dtype)
print(d.dtype)

print(a.dtype == torch.float32)
print(c.dtype == torch.string)
print(a.dtype == float)
print(c.dtype == str)
print(d.dtype == int)

 

原文链接:https://blog.csdn.net/weixin_45875105/article/details/105030129

posted @ 2022-06-29 10:13  呦呦南山  阅读(2087)  评论(0编辑  收藏  举报