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