Created
April 16, 2021 13:47
-
-
Save kretes/5c4642a9842d2fbe31269563c2ab8d11 to your computer and use it in GitHub Desktop.
Explains structure with collections/numpy/tensorflow tensors in it
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
import tensorflow as tf | |
def explain_structure(obj, how_many=3): | |
if hasattr(obj, 'shape'): | |
repr = f"{type(obj)}, {obj.shape}" | |
if type(obj) == tf.Tensor: | |
repr = f"{repr} {obj.device}" | |
return repr | |
elif type(obj) in [type([]), type(())]: | |
inner = obj | |
if how_many > 0: | |
inner = inner[:how_many] | |
inner_explain = ",\n".join([f"[{i}]= ( {explain_structure(inner_obj)} )" for i, inner_obj in enumerate(inner)]) | |
return f"{type(obj)}, len({len(obj)}), {inner_explain}" | |
else: | |
return f"{type(obj)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment