How to discriminate the type of a data(a numpy or tensor)

def to_array_as(x, y):    
if isinstance(x, torch.Tensor) and isinstance(y, np.ndarray):
return x.detach().cpu().numpy().astype(y.dtype)
elif isinstance(x, np.ndarray) and isinstance(y, torch.Tensor):
return torch.as_tensor(x).to(y)
else:
return x
posted @ 2022-04-30 21:33  呦呦南山  阅读(21)  评论(0编辑  收藏  举报