Skip to main content

Print shape of pytorch tensor inside list, tuple, dict, generator

Project description

Intro

This package's main propose is to uppack those tensors inside list, tuple, dict, generator, then print their tensor shape, but it can also print a single tensor shape and normal variable. If the tensor has only one value, it will directly print this value.

Currently it only support print the tensor shape of pytorch and numpy. If you are using other deep learning framework, you can go the source code, and modify it locally to fit your needs. Also, it is hard to cover all of cases, if you find this package has bugs, you can directly go the source code to modify it, because the source code is very short and easy to modify...

I also developed an VSCode extension called Quick-Python-Print to handle insert, comment, uncomment, delete of print statement, when you can not print nested tentor shape with "Quick-Python-Print", then you can integrate it with "printensor", you just need to replace print with prints, and remove the .shape attribute if it has.

Usage

  1. install
pip install printensor
  1. import
(Due to some reason, the installed package name is not same with imported package name, but it is OK.)
from print_tensor import print_shape as prints
  1. example
import torch
import numpy as np
from print_tensor import print_shape as prints

a = torch.tensor([1,2,3])  # single tensor
b = [torch.rand(2,3) for i in range(5)]  # list of tensor
c = (torch.rand(3,4) for i in range(5))  # generator of tensor
d = {1:torch.randn(4,5), 2:torch.rand(5,6)}  # dict of tensor
e = (np.array([6,7]) for i in range(5))  # generator of numpy ndarray
f = (0,1,2,3,4)  # normal variable
g = torch.tensor([100000])  # one value tensor

# support following recognizable prefix string
prints("==>> a.shape: ", a)
prints("==>> b.shape: ", b)
prints("==>> c.shape: ", c)
prints("==>> d.shape: ", d)
prints("==>> e.shape: ", e)
prints("==>> f.shape: ", f)
prints("==>> g.shape: ", g)
print("".center(50, "-"))
prints("==>> a: ", a)
prints("==>> b: ", b)
prints("==>> c: ", c)
prints("==>> d: ", d)
prints("==>> e: ", e)
prints("==>> f: ", f)
prints("==>> g: ", g)
print("".center(50, "-"))
prints("a:", a)
prints("b:", b)
prints("c:", c)
prints("d:", d)
prints("e:", e)
prints("f:", f)
prints("g:", g)
print("".center(50, "-"))
prints("a", a)
prints("b", b)
prints("c", c)
prints("d", d)
prints("e", e)
prints("f", f)
prints("g", g)
print("".center(50, "-"))
# You can also just pass in variable
prints(a)
prints(b)
prints(c)
prints(d)
prints(e)
prints(f)
prints(g)

It will output:

==>> a shape: torch.Size([3])
==>> b[0] shape: torch.Size([2, 3])    
==>> b[1] shape: torch.Size([2, 3])    
==>> b[2] shape: torch.Size([2, 3])    
==>> b[3] shape: torch.Size([2, 3])    
==>> b[4] shape: torch.Size([2, 3])    
==>> c shape: torch.Size([3, 4])       
==>> c shape: torch.Size([3, 4])       
==>> c shape: torch.Size([3, 4])       
==>> c shape: torch.Size([3, 4])       
==>> c shape: torch.Size([3, 4])       
==>> d(key:1) shape: torch.Size([4, 5])
==>> d(key:2) shape: torch.Size([5, 6])
==>> e shape: (2,)
==>> e shape: (2,)
==>> e shape: (2,)
==>> e shape: (2,)
==>> e shape: (2,)
==>> f: (0, 1, 2, 3, 4)
==>> g: tensor([100000])
--------------------------------------------------
==>> a shape: torch.Size([3])
==>> b[0] shape: torch.Size([2, 3])
==>> b[1] shape: torch.Size([2, 3])
==>> b[2] shape: torch.Size([2, 3])
==>> b[3] shape: torch.Size([2, 3])
==>> b[4] shape: torch.Size([2, 3])
==>> d(key:1) shape: torch.Size([4, 5])
==>> d(key:2) shape: torch.Size([5, 6])
==>> f: (0, 1, 2, 3, 4)
==>> g: tensor([100000])
--------------------------------------------------
==>> a shape: torch.Size([3])
==>> b[0] shape: torch.Size([2, 3])
==>> b[1] shape: torch.Size([2, 3])
==>> b[2] shape: torch.Size([2, 3])
==>> b[3] shape: torch.Size([2, 3])
==>> b[4] shape: torch.Size([2, 3])
==>> d(key:1) shape: torch.Size([4, 5])
==>> d(key:2) shape: torch.Size([5, 6])
==>> g: tensor([100000])
--------------------------------------------------
==>> a shape: torch.Size([3])
==>> b[0] shape: torch.Size([2, 3])
==>> b[1] shape: torch.Size([2, 3])
==>> b[2] shape: torch.Size([2, 3])
==>> b[3] shape: torch.Size([2, 3])
==>> b[4] shape: torch.Size([2, 3])
==>> d(key:1) shape: torch.Size([4, 5])
==>> d(key:2) shape: torch.Size([5, 6])
==>> f: (0, 1, 2, 3, 4)
==>> g: tensor([100000])
--------------------------------------------------
==>>  shape: torch.Size([3])
==>> [0] shape: torch.Size([2, 3])
==>> [1] shape: torch.Size([2, 3])
==>> [2] shape: torch.Size([2, 3])
==>> [3] shape: torch.Size([2, 3])
==>> [4] shape: torch.Size([2, 3])
==>> (key:1) shape: torch.Size([4, 5])
==>> (key:2) shape: torch.Size([5, 6])
==>> : (0, 1, 2, 3, 4)
==>> : tensor([100000])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

printensor-0.0.7.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

printensor-0.0.7-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file printensor-0.0.7.tar.gz.

File metadata

  • Download URL: printensor-0.0.7.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.8.5

File hashes

Hashes for printensor-0.0.7.tar.gz
Algorithm Hash digest
SHA256 b938e284f0da3e50458b2a5c847c80f3e0b2486d5076e1c1671e2425c0e87ba2
MD5 4c76acac508e58696cde115448166e2a
BLAKE2b-256 63cc2bc0da672633e91099c3479efb818234682dae8e48be76dd276149275fc5

See more details on using hashes here.

File details

Details for the file printensor-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: printensor-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.8.5

File hashes

Hashes for printensor-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 797edb6041210ec549c6d50a7d40bc910d997e2a07fe1efdb5e85e304d2582e5
MD5 034b509732f09af4ccb3839678e76044
BLAKE2b-256 056df00229e7c96c8aed2265997f6bcb1ce0bd11b995bc26e2402c44b243db18

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page