_to_shape
to_shape
¶
Compute the shape of tensors within a collection of data
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))}}
shape = fe.backend.to_shape(data) # {"x": (10, 15), "y":[(4), (5, 3)], "z": {"key": (2, 2)}}
shape = fe.backend.to_shape(data, add_batch=True)
# {"x": (None, 10, 15), "y":[(None, 4), (None, 5, 3)], "z": {"key": (None, 2, 2)}}
shape = fe.backend.to_shape(data, exact_shape=False)
# {"x": (None, None), "y":[(None), (None, None)], "z": {"key": (None, None)}}
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))}}
shape = fe.backend.to_shape(data) # {"x": (10, 15), "y":[(4), (5, 3)], "z": {"key": (2, 2)}}
shape = fe.backend.to_shape(data, add_batch=True)
# {"x": (None, 10, 15), "y":[(None, 4), (None, 5, 3)], "z": {"key": (None, 2, 2)}}
shape = fe.backend.to_shape(data, exact_shape=False)
# {"x": (None, None), "y":[(None), (None, None)], "z": {"key": (None, None)}}
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))}}
shape = fe.backend.to_shape(data) # {"x": (10, 15), "y":[(4), (5, 3)], "z": {"key": (2, 2)}}
shape = fe.backend.to_shape(data, add_batch=True)
# {"x": (None, 10, 15), "y":[(None, 4), (None, 5, 3)], "z": {"key": (None, 2, 2)}}
shape = fe.backend.to_shape(data, exact_shape=False)
# {"x": (None, None), "y":[(None), (None, None)], "z": {"key": (None, None)}}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[Collection, Tensor]
|
A tensor or possibly nested collection of tensors. |
required |
add_batch |
Whether to prepend a batch dimension to the shapes. |
False
|
|
exact_shape |
Whether to return the exact shapes, or if False to fill the shapes with None values. |
True
|
Returns:
Type | Description |
---|---|
Union[Collection, Tensor]
|
A collection with the same structure as |