Skip to main content

A plug-and-use python library to monitor learning of PyTorch neural networks

Project description

Monitorch

A plug-and-use python module to monitor learning of PyTorch neural networks. Provides easy to use interface to collect and display

  • Loss and custom metrics
  • Layer outputs (activation and norms)
  • Gradients (norms, oscilations and activation)
  • Neural Net's Parameters Evolution

Monitorch manages layer separation, data collection and vizualization with simple exposed methods and classes, so the code is concise and expressive without sacrificing informativness of the visualizations and broad scope of its application. It also allows user to choose between static matplotlib and dynamic tensorboard plotting to help investigate both small models and keep track of large machines.

Documentation

Documentation can be found here:

https://monitorch.readthedocs.io/en/latest/.

Usage

Installation

Install the module using pip dependency manager.

pip install monitorch

Code

Use PyTorchInspector from monitorch.inspector to hook your module and lenses (for example LossMetrics or ParameterGradientGeometry) from monitorch.lens to define vizualizations.

import torch

from monitorch.inspector import PyTorchInspector
from monitorch.lens import LossMetrics, ParameterGradientGeometry

mynet = MyNeuralNet() # torch.nn.Module subclass
loss_fn = MSELoss()
optimizer = torch.optim.Adam(module.parameters())

inspector = PyTorchInspector(
    lenses = [
        LossMetrics(loss_fn=loss_fn),
        ParameterGradientGeometry()
    ],
    module = mynet,
    vizualizer = "matplotlib"
)

for epoch in range(n_epochs):
    # No changes to your training loop
    # Passes through training and validation datasets remain the same

    ...

    # at the end of an epoch inspector must be ticked
    inspector.tick_epoch()

inspector.vizualizer.show_fig()

You can choose other vizualizers by passing "tensorboard", "print" or an instance of vizualizer's class from monitorch.vizualizers. Note that matplotlib vizualier requires show_fig() call to plot.

Currently module supports gradient and parameter collection for arbitrary PyTorch module and output collection for single output architectures (feedforward, convolution, non-transformer autoencoders etc).

Requirments

  • python>=3.10
  • torch>=2.0.0

Optional:

  • matplotlib>=3.10.0
  • tensorboard>=2.19.0

Development:

  • sphinx>=8.1.3
  • pydata-sphinx-theme>=0.16.1
  • pytest>=9.0.3

Tests

Tests can be run with pytest from root project directory. Lens test have no assertions or other critical functionality tests, but are rather smoke tests to catch unhandled exceptions. To run functionality tests run pytest -k "not smoke".

Other repositories

Case studies

Tensorflow

Malcolm Lett has created an excellent Tensorflow library with similar features. So if you prefer Tensorflow, please check it out:

training-instrumentation-toolkit

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

monitorch-0.1.0.4.tar.gz (43.1 kB view details)

Uploaded Source

Built Distribution

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

monitorch-0.1.0.4-py3-none-any.whl (74.0 kB view details)

Uploaded Python 3

File details

Details for the file monitorch-0.1.0.4.tar.gz.

File metadata

  • Download URL: monitorch-0.1.0.4.tar.gz
  • Upload date:
  • Size: 43.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for monitorch-0.1.0.4.tar.gz
Algorithm Hash digest
SHA256 179a9e3bd4e28af5879edda64c1f63a49f33254c3a57dff070fc74d07264d3ab
MD5 39abdc3a42e1821dff05cb381cc75b30
BLAKE2b-256 03ddae051080823058ee7045dd829d3ec19c2b9a3939900297295201ad235886

See more details on using hashes here.

File details

Details for the file monitorch-0.1.0.4-py3-none-any.whl.

File metadata

  • Download URL: monitorch-0.1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 74.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for monitorch-0.1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e90c5c4d2db590ac24504b7598d5e3181b0be53010c82efe09c23499c5c0c1c0
MD5 6fd76b432a153dc6479c6928a0e02fd5
BLAKE2b-256 52d04c4eb4d368c97ff513057aa18ba378d5207bafa057e8e25867aa48b9777b

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