Skip to main content

One line functions for common tasks

Project description

Pytorch utilities for simple pytorch needs

Logging and plotting metrics

from torch_snippets import Report

n_epochs = 5
log = Report(n_epochs)

for epoch in range(n_epochs):
    # No need to --> train_epoch_losses, train_epoch_accuracies = [], []
    N = len(trn_dl)
    for ix, batch in enumerate(iter(trn_dl)):
        ...
        pos = (epoch + (ix+1)/N) # a float between 0 - n_epochs
        # give any number of kwargs that need to be reported and stored.
        # args should be float
        log.record(pos=pos, train_acc=np.mean(is_correct), train_loss=batch_loss, end='\r') # impersistent log

    N = len(val_dl)
    for ix, batch in enumerate(iter(val_dl)):
        ...
        pos = (epoch + (ix+1)/N) # a float between 0 - n_epochs
        log.record(pos=pos, val_loss=batch_loss, end='\r') # impersistent log
    log.report_avgs(epoch+1) # persist the report

  • Auto calculates time remaining
  • No need to preinitialize empty lists
  • Automatically stores metrics as collection of key words
  • Persistent vs Transient logging (use end='\r')
>>> print(log.logged) # get logged metric names
# ['train_loss', 'train_acc', 'val_loss', 'val_acc']
>>> log.plot() # plot all the logged metrics
  • Auto calculates average of all metrics in an epoch

  • Plot entire training history with one command

  • selectively plot logged metrics using regex

>>> log.plot('*_loss')
# or you can list out the metrics that need plotting
# >>> log.plot(['train_acc','val_acc'])

Auxiliary Functions

There are simple functions that are overloaded to take inputs and perform repetitive tasks that usually take a few lines to write

Images

show, inspect, Glob, read

FilePaths

stem, Glob, parent, name

Dump and load python objects

loaddill,dumpdill

Misc

Tqdm, Timer, randint, unique, diff

Pytorch Modules

Reshape and Permute (nn.Modules)

and many more...

Install

pip install torch_snippets

Usage

import pytorch_snippets
dir(pytorch_snippets)

Project details


Release history Release notifications | RSS feed

This version

0.320

Download files

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

Source Distribution

torch_snippets-0.320.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

torch_snippets-0.320-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file torch_snippets-0.320.tar.gz.

File metadata

  • Download URL: torch_snippets-0.320.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.4

File hashes

Hashes for torch_snippets-0.320.tar.gz
Algorithm Hash digest
SHA256 fd16399bb3415c23296b2e4793b5530a69cf8ff6998db78f04a03b30f5dc2c61
MD5 b399902a98475bc02eb6fb9d45034e51
BLAKE2b-256 56ac561a75584003c8d3b9d1b9c18eaab0fc3405c53786d308771a53b9632796

See more details on using hashes here.

File details

Details for the file torch_snippets-0.320-py3-none-any.whl.

File metadata

  • Download URL: torch_snippets-0.320-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.4

File hashes

Hashes for torch_snippets-0.320-py3-none-any.whl
Algorithm Hash digest
SHA256 5453c33e4039f59e3b2ff35007890a090419b961cc9758ee4cfa252ea9fd9816
MD5 87f17e6e16314ded488d9ec3ee0aeb9c
BLAKE2b-256 d44d74754b781c73ecdf59e9d64ba8d3675a8a38b22fd14a412c61afdd03c1c0

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