torch.is_tensor(obj):判断obj是否是tensor类型
import torch
x = torch.randn(1,2)
y = x.numpy()
print(torch.is_tensor(x))
print(torch.is_tensor(y))
True
False
torch.is_storage(obj): 判断obj是否是pytorch storage 对象
从源码来看,storage对象有以下几种,具体什么含义,暂不清楚。有需求再更新。
_storage_classes = {
DoubleStorage, FloatStorage, LongStorage, IntStorage, ShortStorage,
CharStorage, ByteStorage, HalfStorage, BoolStorage
}
torch.is_floating_point(tensor) -> bool :判断是否为浮点类型tensor
浮点类型tensor主要包括:torch.float64, torch.float32, torch.float16
import torch
x = torch.FloatTensor([1,2,3,4])
print(x.dtype)
print(torch.is_floating_point(x))
torch.float32
True
torch.set_default_dtype(d): 设置默认tensor类型为d。设置之后,torch.tensor初始化的tensor类型将为d。
import torch
x = torch.tensor([1.0,2,3,4])
print(x.dtype)
torch.set_default_dtype(torch.float32)
y = torch.tensor([1.0,2,3,4])
print(y.dtype)
z = torch.tensor([1,2,3,4])
print(z.dtype)
torch.float32
torch.float32
torch.int64
torch.get_default_dtype() ->torch.dtype :返回默认的floating dtype
import torch
print(torch.get_default_dtype())
torch.float32
torch.set_default_tensor_type(t): 这个好像和torch.set_default_dtype()没啥区别。待补充。
torch.numel(input)-> int :返回input元素的数量
import torch
x = torch.randn(2,3)
print(torch.numel(x))
6