Skip to main content

Lovely Tensors

Project description

Lovely Tensors

Install

pip install lovely-tensors

How to use

How often do you find yourself debugging PyTorch code? You dump a tensor to the cell output, and see this:

numbers
tensor([[[-0.3541, -0.3369, -0.4054,  ..., -0.5596, -0.4739,  2.2489],
         [-0.4054, -0.4226, -0.4911,  ..., -0.9192, -0.8507,  2.1633],
         [-0.4739, -0.4739, -0.5424,  ..., -1.0390, -1.0390,  2.1975],
         ...,
         [-0.9020, -0.8335, -0.9363,  ..., -1.4672, -1.2959,  2.2318],
         [-0.8507, -0.7822, -0.9363,  ..., -1.6042, -1.5014,  2.1804],
         [-0.8335, -0.8164, -0.9705,  ..., -1.6555, -1.5528,  2.1119]],

        [[-0.1975, -0.1975, -0.3025,  ..., -0.4776, -0.3725,  2.4111],
         [-0.2500, -0.2325, -0.3375,  ..., -0.7052, -0.6702,  2.3585],
         [-0.3025, -0.2850, -0.3901,  ..., -0.7402, -0.8102,  2.3761],
         ...,
         [-0.4251, -0.2325, -0.3725,  ..., -1.0903, -1.0203,  2.4286],
         [-0.3901, -0.2325, -0.4251,  ..., -1.2304, -1.2304,  2.4111],
         [-0.4076, -0.2850, -0.4776,  ..., -1.2829, -1.2829,  2.3410]],

        [[-0.6715, -0.9853, -0.8807,  ..., -0.9678, -0.6890,  2.3960],
         [-0.7238, -1.0724, -0.9678,  ..., -1.2467, -1.0201,  2.3263],
         [-0.8284, -1.1247, -1.0201,  ..., -1.2641, -1.1596,  2.3786],
         ...,
         [-1.2293, -1.4733, -1.3861,  ..., -1.5081, -1.2641,  2.5180],
         [-1.1944, -1.4559, -1.4210,  ..., -1.6476, -1.4733,  2.4308],
         [-1.2293, -1.5256, -1.5081,  ..., -1.6824, -1.5256,  2.3611]]])

Was it really useful?

What is the shape?
What are the statistics?
Are any of the values nan or inf?
Is it an image of a man holding a tench?

import lovely_tensors as lt
lt.monkey_patch()

__repr__()

# A very short tensor - no min/max
numbers.flatten()[:2]
tensor[2] μ=-0.345 σ=0.012 [-0.354, -0.337]
# A slightly longer one
numbers.flatten()[:6].view(2,3)
tensor[2, 3] n=6 x∈[-0.440, -0.337] μ=-0.388 σ=0.038 [[-0.354, -0.337, -0.405], [-0.440, -0.388, -0.405]]
# Too long to show the values
numbers
tensor[3, 196, 196] n=115248 x∈[-2.118, 2.640] μ=-0.388 σ=1.073
spicy = numbers.flatten()[:12].clone()

spicy[0] *= 10000
spicy[1] /= 10000
spicy[2] = float('inf')
spicy[3] = float('-inf')
spicy[4] = float('nan')

spicy = spicy.reshape((2,6))
spicy
tensor[2, 6] n=12 x∈[-3.541e+03, -3.369e-05] μ=-393.776 σ=1.180e+03 +inf! -inf! nan!
# A zero tensor
torch.zeros(10, 10)
tensor[10, 10] n=100 all_zeros
spicy.verbose
tensor[2, 6] n=12 x∈[-3.541e+03, -3.369e-05] μ=-393.776 σ=1.180e+03 +inf! -inf! nan!
[[-3.5405e+03, -3.3693e-05,         inf,        -inf,         nan, -4.0543e-01],
 [-4.2255e-01, -4.9105e-01, -5.0818e-01, -5.5955e-01, -5.4243e-01, -5.0818e-01]]
spicy.plain
[[-3.5405e+03, -3.3693e-05,         inf,        -inf,         nan, -4.0543e-01],
 [-4.2255e-01, -4.9105e-01, -5.0818e-01, -5.5955e-01, -5.4243e-01, -5.0818e-01]]

Going .deeper

numbers.deeper
tensor[3, 196, 196] n=115248 x∈[-2.118, 2.640] μ=-0.388 σ=1.073
  tensor[196, 196] n=38416 x∈[-2.118, 2.249] μ=-0.324 σ=1.036
  tensor[196, 196] n=38416 x∈[-1.966, 2.429] μ=-0.274 σ=0.973
  tensor[196, 196] n=38416 x∈[-1.804, 2.640] μ=-0.567 σ=1.178
# You can go deeper if you need to
dt = torch.randn(3, 3, 5)
dt.deeper(2)
tensor[3, 3, 5] n=45 x∈[-2.057, 2.357] μ=-0.182 σ=1.114
  tensor[3, 5] n=15 x∈[-2.057, 1.315] μ=-0.299 σ=1.197
    tensor[5] x∈[-1.703, 0.807] μ=-0.953 σ=1.061 [-1.703, -1.634, -1.519, -0.713, 0.807]
    tensor[5] x∈[-2.057, 1.287] μ=-0.448 σ=1.338 [1.287, -0.517, -1.358, -2.057, 0.408]
    tensor[5] x∈[-0.884, 1.315] μ=0.503 σ=0.857 [-0.884, 1.315, 0.296, 0.832, 0.955]
  tensor[3, 5] n=15 x∈[-1.947, 2.357] μ=-0.151 σ=1.211
    tensor[5] x∈[-1.947, 2.357] μ=-0.070 σ=1.747 [2.357, -1.947, -1.072, -0.766, 1.076]
    tensor[5] x∈[-1.502, 0.792] μ=-0.253 σ=0.842 [-1.502, -0.065, -0.516, 0.027, 0.792]
    tensor[5] x∈[-1.080, 1.276] μ=-0.130 σ=1.160 [-1.080, -1.056, 1.276, -0.773, 0.981]
  tensor[3, 5] n=15 x∈[-1.614, 1.811] μ=-0.095 σ=0.989
    tensor[5] x∈[-1.614, 0.926] μ=-0.646 σ=1.048 [-1.614, 0.926, -1.299, -1.150, -0.093]
    tensor[5] x∈[-0.600, 1.811] μ=0.484 σ=0.861 [0.357, 1.811, -0.600, 0.483, 0.368]
    tensor[5] x∈[-1.047, 1.235] μ=-0.124 σ=0.886 [1.235, -1.047, -0.634, -0.386, 0.213]

Now in .rgb colour

The important queston - is it our man?

numbers.rgb

Maaaaybe? Looks like someone normalized him.

in_stats = { "mean": (0.485, 0.456, 0.406),
             "std": (0.229, 0.224, 0.225) }
numbers.rgb(in_stats)

It’s indeed our hero, the Tenchman!

.plt the statistics

(numbers+3).plt

(numbers+3).plt(center="mean")

(numbers+3).plt(center="range")

Without .monkey_patch

lt.lovely(spicy)
tensor[2, 6] n=12 x∈[-3.541e+03, -3.369e-05] μ=-393.776 σ=1.180e+03 +inf! -inf! nan!
lt.lovely(spicy, verbose=True)
tensor[2, 6] n=12 x∈[-3.541e+03, -3.369e-05] μ=-393.776 σ=1.180e+03 +inf! -inf! nan!
[[-3.5405e+03, -3.3693e-05,         inf,        -inf,         nan, -4.0543e-01],
 [-4.2255e-01, -4.9105e-01, -5.0818e-01, -5.5955e-01, -5.4243e-01, -5.0818e-01]]
lt.lovely(numbers, depth=1)
tensor[3, 196, 196] n=115248 x∈[-2.118, 2.640] μ=-0.388 σ=1.073
  tensor[196, 196] n=38416 x∈[-2.118, 2.249] μ=-0.324 σ=1.036
  tensor[196, 196] n=38416 x∈[-1.966, 2.429] μ=-0.274 σ=0.973
  tensor[196, 196] n=38416 x∈[-1.804, 2.640] μ=-0.567 σ=1.178
lt.rgb(numbers, in_stats)

lt.plot(numbers, center="mean")

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

lovely-tensors-0.0.7.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

lovely_tensors-0.0.7-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file lovely-tensors-0.0.7.tar.gz.

File metadata

  • Download URL: lovely-tensors-0.0.7.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for lovely-tensors-0.0.7.tar.gz
Algorithm Hash digest
SHA256 438937dfbc63c77f90ea24683e8d8a8b65b232d16aa316caa93cfe336faba4f2
MD5 c2df2b0bc0e340ac028f52cac4cc647b
BLAKE2b-256 d4d00edc29b4febbd90d5b95e1de5962650772e9388c0a00fdd8a4156cb859be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lovely_tensors-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 41295034550c0970d9e76a6ca08d0d7053375cafa11aa55a7c734f30c5c7cdeb
MD5 7dfa5914091ff238c1b504df3c68d068
BLAKE2b-256 c10410a3db9b795c12c94e62e1151f57655270fd22da0552922f6f8c6de3f76b

See more details on using hashes here.

Supported by

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