Created
March 24, 2022 15:57
-
-
Save ritwikraha/cf0eaea52f0b87793cf7831f300b891e to your computer and use it in GitHub Desktop.
The shape_list function from HuggingFace/transformers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shape_list(tensor: Union[tf.Tensor, np.ndarray]) -> List[int]: | |
""" | |
Deal with dynamic shape in tensorflow cleanly. | |
Args: | |
tensor (`tf.Tensor` or `np.ndarray`): The tensor we want the shape of. | |
Returns: | |
`List[int]`: The shape of the tensor as a list. | |
""" | |
if isinstance(tensor, np.ndarray): | |
return list(tensor.shape) | |
dynamic = tf.shape(tensor) | |
if tensor.shape == tf.TensorShape(None): | |
return dynamic | |
static = tensor.shape.as_list() | |
return [dynamic[i] if s is None else s for i, s in enumerate(static)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment