_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 |
False
|
Returns:
Type | Description |
---|---|
Union[Collection, ArrayT, Array, None]
|
A collection with the same structure as |