Skip to content

convert

Utility functions for converting between different data types.

to_numpy

Convert a tensor or tensor subclasses like torch_geometric.Edge_Index to numpy.

Parameters:

Name Type Description Default
tensor torch.Tensor

Tensor or tensor subclass.

required

Returns:

Type Description
numpy.ndarray

Numpy array.

Source code in src/pathpyG/utils/convert.py
def to_numpy(tensor: torch.Tensor) -> np.ndarray:
    """
    Convert a tensor or tensor subclasses like `torch_geometric.Edge_Index` to numpy.

    Args:
        tensor: Tensor or tensor subclass.

    Returns:
        Numpy array.
    """
    if isinstance(tensor, (EdgeIndex, Index)):
        return tensor.as_tensor().numpy()
    return tensor.numpy()