convinence-torch


Nameconvinence-torch JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/NISCHALPI/ConvinenceTorch
Summaryminimalistic convenience utilities for using PyTorch
upload_time2023-07-23 01:52:26
maintainer
docs_urlNone
authorNischal Bhattarai
requires_python>=3.8
licenseMIT License Copyright (c) 2023 HadesX Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords torch utilities trainer neural network
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Static Badge](https://img.shields.io/badge/pytorch-v2.x-red)](https://pytorch.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) 



# ConvinenceTorch - A Lightweight and Minimalistic Convenience Library for PyTorch
<img src="./Logo.jpeg" alt="Logo" width="300">

ConvinenceTorch is a Python package that serves as a lightweight and minimalistic convenience library for PyTorch, a popular deep learning framework. It provides a collection of utility modules and classes to simplify and streamline the training process of neural networks in PyTorch.

## Features

- **NNtrainer**: ConvinenceTorch offers a flexible and extensible `NNtrainer` class designed for training neural networks in PyTorch. The `NNtrainer` handles various common training tasks, such as model initialization, optimization, loss computation, device management, and training/validation loops. By utilizing the `NNtrainer` class, developers can focus on defining their models and customizing the training process without being burdened by boilerplate code.

- **Memory Usage Report**: ConvinenceTorch provides a memory usage report feature that allows users to monitor the memory consumption during the training process. The mem_report() function in NNtrainer provides valuable insights into the memory usage, helping users optimize their models and data loaders efficiently.

- **Metrics and Loss capture**: ConvinenceTorch offers a comprehensive and flexible mechanism for capturing and tracking metrics and losses during the training process. The NNtrainer class allows users to easily specify the metrics they wish to monitor, such as accuracy, F1 score, or any custom evaluation metric. Additionally, users can choose to record the training and validation losses for further analysis.
- **Stochastic Weight Averaging (SWA) Support**: NNtrainer supports Stochastic Weight Averaging, a technique that can improve model generalization. The class includes functionalities for setting SWA models, starting epochs, and SWA-specific validation.
- **Model Checkpointing**: ConvinenceTorch includes model checkpointing functionality, allowing users to save the model's state at specified intervals during training or after every epoch. This feature facilitates easy resumption of training from the last saved checkpoint, enhancing training reliability and fault tolerance.
- **Inference Support** : With NNtrainer, users can make predictions using the trained model on new data, enabling them to leverage their models for inference tasks.

## Installation
This library is designed with a minimalistic approach to cater to educational applications and general research purposes. It boasts well-documented docstrings for every component, ensuring clarity and ease of understanding throughout its usage.

For those seeking to extend its functionalities, performing an editable install and making necessary tweaks to the library is highly recommended. Consequently, an editable install is the preferred method, allowing users to seamlessly customize the library to suit their specific requirements and leverage its full potential.
```bash
$ pip install convinence-torch
```

## Usage

```python
import torch
import torch.nn as nn
import torchvision
from torch.utils.data import DataLoader

# Enable debugging 
import os
os.environ['LOG'] = '10'

# Import trainer 
from torchutils.trainer import NNtrainer

# Setup your data loaders
train_data = torchvision.datasets.FashionMNIST('./data/', train=True, transform=torchvision.transforms.ToTensor(), download=True)
test_data = torchvision.datasets.FashionMNIST('./data/', train=False, transform=torchvision.transforms.ToTensor(), download=True)
trainloader = DataLoader(train_data, batch_size=B, shuffle=True, num_workers=5)
test_loader = DataLoader(test_data, batch_size=B)

# Constants
D = 28 * 28
n = 32
C = 1
classes = 10
eta_0 = 0.005
eta_1 = 0.001

# Define your PyTorch model, optimizer, and loss function
fc_model = nn.Sequential(
    nn.Conv2d(1, n, 3, padding=1),  # 28 * 28
    nn.LeakyReLU(0.1),
    nn.Conv2d(n, n, 3, padding=1),  # 28 * 28
    nn.LeakyReLU(0.1),
    nn.MaxPool2d(2),  # 14 * 14
    nn.Conv2d(n, n // 2, 3, padding=1),
    nn.LeakyReLU(0.1),
    nn.Conv2d(n // 2, n // 2, 3, padding=1),
    nn.LeakyReLU(0.1),
    nn.MaxPool2d(2),  # 7 * 7
    nn.Conv2d(n // 2, n // 4, 3, padding=1),
    nn.LeakyReLU(0.1),
    nn.Flatten(),
    nn.Linear(7 * 7 * n // 4, 64),
    nn.Tanh(),
    nn.Linear(64, 64),
    nn.Tanh(),
    nn.Linear(64, 10)
)

# Create an instance of NNtrainer
trainer = NNtrainer(model, optimizer, loss)

# See the memory usage report
trainer.mem_report(batch_shape=(33, 1, 28, 28))

# Add Cosine Annealing
trainer.add_cosineannealing_lrs(lr_initial=eta_0, lr_final=eta_1, dips=5, epoch=20, verbose=True)

# Add Gradient Clipping
trainer.add_gradient_clipping(min=-100, max=100)

# Compile the model before training
trainer.compile()

# Train the model
trainer.train(trainloader=trainloader, valloader=valloader, epoch=20, metrics=['accuracy', 'f1'], record_loss=True, checkpoint_file='train')

# Plot the training and validation metric curves
trainer.plot_train_validation_metric_curve('accuracy')
```

## Contributing
Contributions are welcome! If you find any issues or have suggestions for new features, please open an issue or submit a pull request.

## License
This project is licensed under the MIT License.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/NISCHALPI/ConvinenceTorch",
    "name": "convinence-torch",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "torch,utilities,trainer,neural network",
    "author": "Nischal Bhattarai",
    "author_email": "Nischal bhattarai <nischalbhattaraipi@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/a4/cb/ea36b7ce96e2a6785a5a0e9e63f730a3b7142ccf47c7c3c09bddc0fd1f99/convinence-torch-1.0.0.tar.gz",
    "platform": null,
    "description": "[![Static Badge](https://img.shields.io/badge/pytorch-v2.x-red)](https://pytorch.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) \n\n\n\n# ConvinenceTorch - A Lightweight and Minimalistic Convenience Library for PyTorch\n<img src=\"./Logo.jpeg\" alt=\"Logo\" width=\"300\">\n\nConvinenceTorch is a Python package that serves as a lightweight and minimalistic convenience library for PyTorch, a popular deep learning framework. It provides a collection of utility modules and classes to simplify and streamline the training process of neural networks in PyTorch.\n\n## Features\n\n- **NNtrainer**: ConvinenceTorch offers a flexible and extensible `NNtrainer` class designed for training neural networks in PyTorch. The `NNtrainer` handles various common training tasks, such as model initialization, optimization, loss computation, device management, and training/validation loops. By utilizing the `NNtrainer` class, developers can focus on defining their models and customizing the training process without being burdened by boilerplate code.\n\n- **Memory Usage Report**: ConvinenceTorch provides a memory usage report feature that allows users to monitor the memory consumption during the training process. The mem_report() function in NNtrainer provides valuable insights into the memory usage, helping users optimize their models and data loaders efficiently.\n\n- **Metrics and Loss capture**: ConvinenceTorch offers a comprehensive and flexible mechanism for capturing and tracking metrics and losses during the training process. The NNtrainer class allows users to easily specify the metrics they wish to monitor, such as accuracy, F1 score, or any custom evaluation metric. Additionally, users can choose to record the training and validation losses for further analysis.\n- **Stochastic Weight Averaging (SWA) Support**: NNtrainer supports Stochastic Weight Averaging, a technique that can improve model generalization. The class includes functionalities for setting SWA models, starting epochs, and SWA-specific validation.\n- **Model Checkpointing**: ConvinenceTorch includes model checkpointing functionality, allowing users to save the model's state at specified intervals during training or after every epoch. This feature facilitates easy resumption of training from the last saved checkpoint, enhancing training reliability and fault tolerance.\n- **Inference Support** : With NNtrainer, users can make predictions using the trained model on new data, enabling them to leverage their models for inference tasks.\n\n## Installation\nThis library is designed with a minimalistic approach to cater to educational applications and general research purposes. It boasts well-documented docstrings for every component, ensuring clarity and ease of understanding throughout its usage.\n\nFor those seeking to extend its functionalities, performing an editable install and making necessary tweaks to the library is highly recommended. Consequently, an editable install is the preferred method, allowing users to seamlessly customize the library to suit their specific requirements and leverage its full potential.\n```bash\n$ pip install convinence-torch\n```\n\n## Usage\n\n```python\nimport torch\nimport torch.nn as nn\nimport torchvision\nfrom torch.utils.data import DataLoader\n\n# Enable debugging \nimport os\nos.environ['LOG'] = '10'\n\n# Import trainer \nfrom torchutils.trainer import NNtrainer\n\n# Setup your data loaders\ntrain_data = torchvision.datasets.FashionMNIST('./data/', train=True, transform=torchvision.transforms.ToTensor(), download=True)\ntest_data = torchvision.datasets.FashionMNIST('./data/', train=False, transform=torchvision.transforms.ToTensor(), download=True)\ntrainloader = DataLoader(train_data, batch_size=B, shuffle=True, num_workers=5)\ntest_loader = DataLoader(test_data, batch_size=B)\n\n# Constants\nD = 28 * 28\nn = 32\nC = 1\nclasses = 10\neta_0 = 0.005\neta_1 = 0.001\n\n# Define your PyTorch model, optimizer, and loss function\nfc_model = nn.Sequential(\n    nn.Conv2d(1, n, 3, padding=1),  # 28 * 28\n    nn.LeakyReLU(0.1),\n    nn.Conv2d(n, n, 3, padding=1),  # 28 * 28\n    nn.LeakyReLU(0.1),\n    nn.MaxPool2d(2),  # 14 * 14\n    nn.Conv2d(n, n // 2, 3, padding=1),\n    nn.LeakyReLU(0.1),\n    nn.Conv2d(n // 2, n // 2, 3, padding=1),\n    nn.LeakyReLU(0.1),\n    nn.MaxPool2d(2),  # 7 * 7\n    nn.Conv2d(n // 2, n // 4, 3, padding=1),\n    nn.LeakyReLU(0.1),\n    nn.Flatten(),\n    nn.Linear(7 * 7 * n // 4, 64),\n    nn.Tanh(),\n    nn.Linear(64, 64),\n    nn.Tanh(),\n    nn.Linear(64, 10)\n)\n\n# Create an instance of NNtrainer\ntrainer = NNtrainer(model, optimizer, loss)\n\n# See the memory usage report\ntrainer.mem_report(batch_shape=(33, 1, 28, 28))\n\n# Add Cosine Annealing\ntrainer.add_cosineannealing_lrs(lr_initial=eta_0, lr_final=eta_1, dips=5, epoch=20, verbose=True)\n\n# Add Gradient Clipping\ntrainer.add_gradient_clipping(min=-100, max=100)\n\n# Compile the model before training\ntrainer.compile()\n\n# Train the model\ntrainer.train(trainloader=trainloader, valloader=valloader, epoch=20, metrics=['accuracy', 'f1'], record_loss=True, checkpoint_file='train')\n\n# Plot the training and validation metric curves\ntrainer.plot_train_validation_metric_curve('accuracy')\n```\n\n## Contributing\nContributions are welcome! If you find any issues or have suggestions for new features, please open an issue or submit a pull request.\n\n## License\nThis project is licensed under the MIT License.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2023 HadesX  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "minimalistic convenience utilities for using PyTorch",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/NISCHALPI/ConvinenceTorch"
    },
    "split_keywords": [
        "torch",
        "utilities",
        "trainer",
        "neural network"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a59e25646a8abc1074d2a2b2b126c81ca1bb092d2e0f999b4ae11b29ebfa5169",
                "md5": "aac0c266aa1684c573d2f91886e7269a",
                "sha256": "c96984bf721a080c419f8d6f736c9c76e6ca55bb3ab57ab82ece9734a9f5345e"
            },
            "downloads": -1,
            "filename": "convinence_torch-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "aac0c266aa1684c573d2f91886e7269a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 27325,
            "upload_time": "2023-07-23T01:52:24",
            "upload_time_iso_8601": "2023-07-23T01:52:24.470749Z",
            "url": "https://files.pythonhosted.org/packages/a5/9e/25646a8abc1074d2a2b2b126c81ca1bb092d2e0f999b4ae11b29ebfa5169/convinence_torch-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a4cbea36b7ce96e2a6785a5a0e9e63f730a3b7142ccf47c7c3c09bddc0fd1f99",
                "md5": "b900b0271c8c65a0179d3d7a3686c873",
                "sha256": "e9af473d47a0bda635290b84d51b54657b7b9c189bf170c5d21cf601c94ccbe3"
            },
            "downloads": -1,
            "filename": "convinence-torch-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b900b0271c8c65a0179d3d7a3686c873",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 969542,
            "upload_time": "2023-07-23T01:52:26",
            "upload_time_iso_8601": "2023-07-23T01:52:26.527050Z",
            "url": "https://files.pythonhosted.org/packages/a4/cb/ea36b7ce96e2a6785a5a0e9e63f730a3b7142ccf47c7c3c09bddc0fd1f99/convinence-torch-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-23 01:52:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "NISCHALPI",
    "github_project": "ConvinenceTorch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "convinence-torch"
}
        
Elapsed time: 2.25915s