How To Check If An Object Is A PyTorch Tensor
Learn how to create & access tensors with PyTorch! Use `is_tensor()` to check if an object is a tensor, `numel()` for element count, and `device()` for device representation.
Buy Me a Coffee☕ *My post explains how to create and acceess a tensor. is_tensor() can check if an object is a PyTorch tensor, getting the scalar of a boolean value as shown below: *Memos: is_tensor() can be used with torch but not with a tensor. The 1st argument with torch is obj(Required-Type:object). import torch import numpy as np pytorch_tensor = torch.tensor([0, 1, 2]) torch.is_tensor(obj=pytorch_tensor) # True numpy_tensor = np.array([0., 1., 2.]) torch.is_tensor(obj=numpy_tensor) # False torch.is_tensor(obj=7) # False torch.is_tensor(obj=7.) # False torch.is_tensor(obj=7.+0.j) # Fa...