判断数组类型是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 @   呦呦南山  阅读(2131)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示