Converts `x` to Tensor recursively. Args: x: a jnp array, numpy array, TF/PyTorch Tensor, or a nested structure of arrays or Tensors. Returns: A nested structure with the same structure as `x` but with values converted to Tensors. Raises: NotImplementedError: I
(x: Any)
| 585 | |
| 586 | |
| 587 | def as_tensor(x: Any): |
| 588 | """Converts `x` to Tensor recursively. |
| 589 | |
| 590 | Args: |
| 591 | x: a jnp array, numpy array, TF/PyTorch Tensor, or a nested structure of arrays or Tensors. |
| 592 | |
| 593 | Returns: |
| 594 | A nested structure with the same structure as `x` but with values converted to Tensors. |
| 595 | |
| 596 | Raises: |
| 597 | NotImplementedError: If conversion for the input type is unsupported. |
| 598 | """ |
| 599 | if isinstance(x, Tensor): |
| 600 | return x |
| 601 | if isinstance(x, (numbers.Number, np.ndarray)): |
| 602 | return jnp.asarray(x) |
| 603 | if hasattr(x, "detach"): |
| 604 | x = x.detach() |
| 605 | if hasattr(x, "numpy"): |
| 606 | return jnp.asarray(x.numpy()) |
| 607 | if isinstance(x, (Mapping, Sequence)): |
| 608 | return jax.tree.map(as_tensor, x) |
| 609 | raise NotImplementedError(f"{type(x)}: {x}") |
| 610 | |
| 611 | |
| 612 | def as_numpy_array(x: Any): |