def to_tensor(X, device, accept_sparse=False):
    """Turn input data to torch tensor.

    Parameters
    ----------
    X : input data
      Handles the cases:
        * PackedSequence
        * numpy array
        * torch Tensor
        * scipy sparse CSR matrix
        * list or tuple of one of the former
        * dict with values of one of the former

    device : str, torch.device
      The compute device to be used. If set to 'cuda', data in torch
      tensors will be pushed to cuda tensors before being sent to the
      module.

    accept_sparse : bool (default=False)
      Whether to accept scipy sparse matrices as input. If False,
      passing a sparse matrix raises an error. If True, it is
      converted to a torch COO tensor.

    Returns
    -------
    output : torch Tensor

    """
    to_tensor_ = partial(to_tensor, device=device)

    if is_torch_data_type(X):
        return X.to(device)
    if isinstance(X, dict):
        return {key: to_tensor_(val) for key, val in X.items()}
    if isinstance(X, (list, tuple)):
        return [to_tensor_(x) for x in X]
    if np.isscalar(X):
        return torch.as_tensor(X, device=device)
    if isinstance(X, Sequence):
        return torch.as_tensor(np.array(X), device=device)
    if isinstance(X, np.ndarray):
        return torch.as_tensor(X, device=device)
    if sparse.issparse(X):
        if accept_sparse:
            return torch.sparse_coo_tensor(
                X.nonzero(), X.data, size=X.shape).to(device)
        raise TypeError("Sparse matrices are not supported. Set "
                        "accept_sparse=True to allow sparse matrices.")

    raise TypeError("Cannot convert this data type to a torch tensor.")
def to_tensor ( X , device , accept_sparse = False ) : to_tensor_ = partial ( to_tensor , device = device ) if is_torch_data_type ( X ) : return X . to ( device ) if isinstance ( X , dict ) : return { key : to_tensor_ ( val ) for key , val in X . items ( ) } if isinstance ( X , ( list , tuple ) ) : return [ to_tensor_ ( x ) for x in X ] if np . isscalar ( X ) : return torch . as_tensor ( X , device = device ) if isinstance ( X , Sequence ) : return torch . as_tensor ( np . array ( X ) , device = device ) if isinstance ( X , np . ndarray ) : return torch . as_tensor ( X , device = device ) if sparse . issparse ( X ) : if accept_sparse : return torch . sparse_coo_tensor ( X . nonzero ( ) , X . data , size = X . shape ) . to ( device ) raise TypeError ( "Sparse matrices are not supported. Set " "accept_sparse=True to allow sparse matrices." ) raise TypeError ( "Cannot convert this data type to a torch tensor." )
Turn input data to torch tensor.

    Parameters
    ----------
    X : input data
      Handles the cases:
        * PackedSequence
        * numpy array
        * torch Tensor
        * scipy sparse CSR matrix
        * list or tuple of one of the former
        * dict with values of one of the former

    device : str, torch.device
      The compute device to be used. If set to 'cuda', data in torch
      tensors will be pushed to cuda tensors before being sent to the
      module.

    accept_sparse : bool (default=False)
      Whether to accept scipy sparse matrices as input. If False,
      passing a sparse matrix raises an error. If True, it is
      converted to a torch COO tensor.

    Returns
    -------
    output : torch Tensor