Skip to content

_to_tensor

to_tensor

Convert tensors within a collection of data to a given target_type recursively.

This method can be used with Numpy data:

data = {"x": np.ones((10,15)), "y":[np.ones((4)), np.ones((5, 3))], "z":{"key":np.ones((2,2))}}
t = fe.backend.to_tensor(data, target_type='tf')
# {"x": <tf.Tensor>, "y":[<tf.Tensor>, <tf.Tensor>], "z": {"key": <tf.Tensor>}}
p = fe.backend.to_tensor(data, target_type='torch')
# {"x": <torch.Tensor>, "y":[<torch.Tensor>, <torch.Tensor>], "z": {"key": <torch.Tensor>}}

This method can be used with TensorFlow tensors:

data = {"x": tf.ones((10,15)), "y":[tf.ones((4)), tf.ones((5, 3))], "z":{"key":tf.ones((2,2))}}
p = fe.backend.to_tensor(data, target_type='torch')
# {"x": <torch.Tensor>, "y":[<torch.Tensor>, <torch.Tensor>], "z": {"key": <torch.Tensor>}}

This method can be used with PyTorch tensors:

data = {"x": torch.ones((10,15)), "y":[torch.ones((4)), torch.ones((5, 3))], "z":{"key":torch.ones((2,2))}}
t = fe.backend.to_tensor(data, target_type='tf')
# {"x": <tf.Tensor>, "y":[<tf.Tensor>, <tf.Tensor>], "z": {"key": <tf.Tensor>}}

Parameters:

Name Type Description Default
data Union[Collection, Array, float, int, None]

A tensor or possibly nested collection of tensors.

required
target_type str

What kind of tensor(s) to create, one of "tf", "torch", or "np".

required
shared_memory bool

Whether to put the tensor(s) in shared memory (only applicable when target_type is 'torch').

False

Returns:

Type Description
Union[Collection, ArrayT, Array, None]

A collection with the same structure as data, but with any tensors converted to the target_type.

Source code in fastestimator/fastestimator/backend/_to_tensor.py
def to_tensor(data: Union[Collection, Array, float, int, None], target_type: str,
              shared_memory: bool = False) -> Union[Collection, ArrayT, Array, None]:
    """Convert tensors within a collection of `data` to a given `target_type` recursively.

    This method can be used with Numpy data:
    ```python
    data = {"x": np.ones((10,15)), "y":[np.ones((4)), np.ones((5, 3))], "z":{"key":np.ones((2,2))}}
    t = fe.backend.to_tensor(data, target_type='tf')
    # {"x": <tf.Tensor>, "y":[<tf.Tensor>, <tf.Tensor>], "z": {"key": <tf.Tensor>}}
    p = fe.backend.to_tensor(data, target_type='torch')
    # {"x": <torch.Tensor>, "y":[<torch.Tensor>, <torch.Tensor>], "z": {"key": <torch.Tensor>}}
    ```

    This method can be used with TensorFlow tensors:
    ```python
    data = {"x": tf.ones((10,15)), "y":[tf.ones((4)), tf.ones((5, 3))], "z":{"key":tf.ones((2,2))}}
    p = fe.backend.to_tensor(data, target_type='torch')
    # {"x": <torch.Tensor>, "y":[<torch.Tensor>, <torch.Tensor>], "z": {"key": <torch.Tensor>}}
    ```

    This method can be used with PyTorch tensors:
    ```python
    data = {"x": torch.ones((10,15)), "y":[torch.ones((4)), torch.ones((5, 3))], "z":{"key":torch.ones((2,2))}}
    t = fe.backend.to_tensor(data, target_type='tf')
    # {"x": <tf.Tensor>, "y":[<tf.Tensor>, <tf.Tensor>], "z": {"key": <tf.Tensor>}}
    ```

    Args:
        data: A tensor or possibly nested collection of tensors.
        target_type: What kind of tensor(s) to create, one of "tf", "torch", or "np".
        shared_memory: Whether to put the tensor(s) in shared memory (only applicable when `target_type` is 'torch').

    Returns:
        A collection with the same structure as `data`, but with any tensors converted to the `target_type`.
    """
    target_instance = {
        "tf": (tf.Tensor, tf.Variable, tf.distribute.DistributedValues), "torch": torch.Tensor, "np": np.ndarray
    }
    conversion_function = {"tf": tf.convert_to_tensor, "torch": torch.from_numpy, "np": np.array}
    if isinstance(data, target_instance[target_type]):
        if shared_memory and target_type == "torch":
            data.share_memory_()
        return data
    elif data is None:
        return None
    elif isinstance(data, str):
        # We don't convert strings to tensors because torch collate behavior is just to wrap strings into a list
        return data
    elif isinstance(data, dict):
        return {key: to_tensor(value, target_type) for (key, value) in data.items()}
    elif isinstance(data, list):
        return [to_tensor(val, target_type) for val in data]
    elif isinstance(data, tuple) and hasattr(data, '_fields'):  # Named tuple
        return type(data)([to_tensor(val, target_type) for val in data])
    elif isinstance(data, tuple):
        return tuple([to_tensor(val, target_type) for val in data])
    elif isinstance(data, set):
        return set([to_tensor(val, target_type) for val in data])
    else:
        data = conversion_function[target_type](np.array(data))
        if shared_memory and target_type == "torch":
            data.share_memory_()
        return data