4 ms·
1-liner for viewing the shape of all local Tensors in Python
- extasia 3y agoUsing torch and numpy a lot I find myself perplexed about the shape of some torch.Tensors, particularly if they have a non-obvious set of dimensions (e.g a batch dimension or something). I reach for this one-liner pretty regularly so perhaps it can be of use to others:) Here's an example output: Array Shapes: ================================================== attention1 : Tensor : (8, 8921, 251) m : Tensor : (8, 8921, 50) ŷ0 : Tensor : (8, 8921, 50) ŷ1 : Tensor : (8, 8921) ŷ2 : Tensor : (8, 8921) random_np_arr : ndarray: (2, 3) And the code: print(f"\nArray Shapes:\n{'':=>50}"); [print(f"{k:<20}: {str(type(v).__name__):<7}:", tuple(v.shape)) for k, v in locals().items() if hasattr(v, "shape") and not hasattr(getattr(v, "shape", None), "__call__")] # Exclude the 'numpy' import, the `shape` func. isn't relevant (semi-golfed to be able to easily run in a `breakpoint()`)