Skip to content

Instantly share code, notes, and snippets.

@kretes
Created April 16, 2021 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kretes/5c4642a9842d2fbe31269563c2ab8d11 to your computer and use it in GitHub Desktop.
Save kretes/5c4642a9842d2fbe31269563c2ab8d11 to your computer and use it in GitHub Desktop.
Explains structure with collections/numpy/tensorflow tensors in it
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