Rate this Page
★ ★ ★ ★ ★

TensorDictBase.is_non_tensor#

TensorDictBase.is_non_tensor(key: NestedKey) → bool#

Checks if the value associated with a key is a non-tensor (NonTensorData).

Unlike the standalone is_non_tensor() function which checks a value directly, this method checks the internal representation of the stored value, making it reliable for both TensorDict and TensorClass instances.

This is useful because TensorClass get() unwraps NonTensorData to plain values, so calling is_non_tensor(tc.get(key)) on the result would return False. This method always checks the wrapped storage.

Parameters:

key (NestedKey) – the key to check.

Returns:

True if the value stored at key is a NonTensorData or NonTensorStack, False otherwise.

Examples

>>> from tensordict import TensorDict
>>> import torch
>>> td = TensorDict({"obs": torch.randn(4)}, [])
>>> td = td.set_non_tensor("label", "cat")
>>> td.is_non_tensor("label")
True
>>> td.is_non_tensor("obs")
False

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.