torchplate


Nametorchplate JSON
Version 0.0.10 PyPI version JSON
download
home_page
SummaryA minimal and simple machine learning experiment module for PyTorch.
upload_time2023-06-04 06:04:22
maintainer
docs_urlNone
author
requires_python>=3.7
licenseCopyright (c) 2018 The Python Packaging Authority 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
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # `torchplate`: Minimal Experiment Workflows in PyTorch 

([Github](https://github.com/rosikand/torchplate) | [PyPI](https://pypi.org/project/torchplate) | [Documentation](https://rosikand.github.io/torchplate/))

[Installation](#installation) | [Example](#example) | [More examples](#more-examples) | [Starter project](#starter-project) | [Changelog](https://rosikand.github.io/torchplate/changelog/) 

An extremely minimal and simple experiment module for machine learning in PyTorch (PyTorch + boilerplate = `torchplate`).

In addition to abstracting away the training loop, we provide several abstractions to improve the efficiency of machine learning workflows with PyTorch. 

## Installation 

```
$ pip install torchplate
```

## Example 

To get started, create a child class of `torchplate.experiment.Experiment` and provide several key, experiment-unique items: model, optimizer, and a training set dataloader. Then, provide an implementation of the abstract method `evaluate`. This function takes in a batch from the `trainloader` and should return the loss (i.e., implement the forward pass + loss calculation). Add whatever custom methods you may want to this class. Then starting training! That's it! 

```python
import torchplate
from torchplate import experiment
from torchplate import utils
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
import requests
import cloudpickle as cp
from urllib.request import urlopen


class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(3*32*32, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 3)

    def forward(self, x):
        x = torch.flatten(x, 1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


class CifarExp(torchplate.experiment.Experiment):
    def __init__(self): 
        self.model = Net()
        self.optimizer = optim.Adam(self.model.parameters(), lr=0.001)
        self.criterion = nn.CrossEntropyLoss()
        dataset = cp.load(urlopen("https://stanford.edu/~rsikand/assets/datasets/mini_cifar.pkl")) 
        # use various torchplate.utils to improve efficiency of common workflows 
        self.trainloader, self.testloader = torchplate.utils.get_xy_loaders(dataset)

        # inherit from torchplate.experiment.Experiment and pass in
        # model, optimizer, and dataloader 
        super().__init__(
            model = self.model,
            optimizer = self.optimizer,
            trainloader = self.trainloader,
            verbose = True
        )
    
    # provide this abstract method to calculate loss 
    def evaluate(self, batch):
        x, y = batch
        logits = self.model(x)
        loss_val = self.criterion(logits, y)
        return loss_val

    def test(self):
        accuracy_count = 0
        for x, y in self.testloader:
            logits = self.model(x)
            pred = torch.argmax(F.softmax(logits, dim=1)).item()
            print(f"Prediction: {pred}, True: {y.item()}")
            if pred == y:
                accuracy_count += 1
        print("Accuracy: ", accuracy_count/len(self.testloader))

    def on_epoch_end(self):
        # to illustrate the concept of callbacks 
        print("------------------ (Epoch end) --------------------")



exp = CifarExp()
exp.train(num_epochs=100)
exp.test()
```

### More examples

See `examples/cifar` for another minimal example. See `examples/starter` for a full program example. To get started running your own experiments, you can use `examples/starter` as a base (or use cookiecutter as shown below). 

### Starter project 

The `starter` branch holds the source for a [cookiecutter](https://github.com/cookiecutter/cookiecutter) project. This allows users to easily create projects from the starter code example by running a simple command. To get started, [install cookiecutter](https://cookiecutter.readthedocs.io/en/stable/installation.html#install-cookiecutter) and then type
```
$ cookiecutter https://github.com/rosikand/torchplate.git --checkout starter
```

which will generate the following structure for you to use as a base for your projects: 

```
torchplate_starter
├── datasets.py
├── experiments.py
├── models.py
└── runner.py
```




            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "torchplate",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "Rohan Sikand <rsikand29@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/70/1d/d1250cd23f10bd42ad41f00035df721e6ff9c67d322c5aee717a50bab633/torchplate-0.0.10.tar.gz",
    "platform": null,
    "description": "# `torchplate`: Minimal Experiment Workflows in PyTorch \n\n([Github](https://github.com/rosikand/torchplate) | [PyPI](https://pypi.org/project/torchplate) | [Documentation](https://rosikand.github.io/torchplate/))\n\n[Installation](#installation) | [Example](#example) | [More examples](#more-examples) | [Starter project](#starter-project) | [Changelog](https://rosikand.github.io/torchplate/changelog/) \n\nAn extremely minimal and simple experiment module for machine learning in PyTorch (PyTorch + boilerplate = `torchplate`).\n\nIn addition to abstracting away the training loop, we provide several abstractions to improve the efficiency of machine learning workflows with PyTorch. \n\n## Installation \n\n```\n$ pip install torchplate\n```\n\n## Example \n\nTo get started, create a child class of `torchplate.experiment.Experiment` and provide several key, experiment-unique items: model, optimizer, and a training set dataloader. Then, provide an implementation of the abstract method `evaluate`. This function takes in a batch from the `trainloader` and should return the loss (i.e., implement the forward pass + loss calculation). Add whatever custom methods you may want to this class. Then starting training! That's it! \n\n```python\nimport torchplate\nfrom torchplate import experiment\nfrom torchplate import utils\nimport torch\nimport torch.optim as optim\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport requests\nimport cloudpickle as cp\nfrom urllib.request import urlopen\n\n\nclass Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.fc1 = nn.Linear(3*32*32, 120)\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 3)\n\n    def forward(self, x):\n        x = torch.flatten(x, 1)\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return x\n\n\nclass CifarExp(torchplate.experiment.Experiment):\n    def __init__(self): \n        self.model = Net()\n        self.optimizer = optim.Adam(self.model.parameters(), lr=0.001)\n        self.criterion = nn.CrossEntropyLoss()\n        dataset = cp.load(urlopen(\"https://stanford.edu/~rsikand/assets/datasets/mini_cifar.pkl\")) \n        # use various torchplate.utils to improve efficiency of common workflows \n        self.trainloader, self.testloader = torchplate.utils.get_xy_loaders(dataset)\n\n        # inherit from torchplate.experiment.Experiment and pass in\n        # model, optimizer, and dataloader \n        super().__init__(\n            model = self.model,\n            optimizer = self.optimizer,\n            trainloader = self.trainloader,\n            verbose = True\n        )\n    \n    # provide this abstract method to calculate loss \n    def evaluate(self, batch):\n        x, y = batch\n        logits = self.model(x)\n        loss_val = self.criterion(logits, y)\n        return loss_val\n\n    def test(self):\n        accuracy_count = 0\n        for x, y in self.testloader:\n            logits = self.model(x)\n            pred = torch.argmax(F.softmax(logits, dim=1)).item()\n            print(f\"Prediction: {pred}, True: {y.item()}\")\n            if pred == y:\n                accuracy_count += 1\n        print(\"Accuracy: \", accuracy_count/len(self.testloader))\n\n    def on_epoch_end(self):\n        # to illustrate the concept of callbacks \n        print(\"------------------ (Epoch end) --------------------\")\n\n\n\nexp = CifarExp()\nexp.train(num_epochs=100)\nexp.test()\n```\n\n### More examples\n\nSee `examples/cifar` for another minimal example. See `examples/starter` for a full program example. To get started running your own experiments, you can use `examples/starter` as a base (or use cookiecutter as shown below). \n\n### Starter project \n\nThe `starter` branch holds the source for a [cookiecutter](https://github.com/cookiecutter/cookiecutter) project. This allows users to easily create projects from the starter code example by running a simple command. To get started, [install cookiecutter](https://cookiecutter.readthedocs.io/en/stable/installation.html#install-cookiecutter) and then type\n```\n$ cookiecutter https://github.com/rosikand/torchplate.git --checkout starter\n```\n\nwhich will generate the following structure for you to use as a base for your projects: \n\n```\ntorchplate_starter\n\u251c\u2500\u2500 datasets.py\n\u251c\u2500\u2500 experiments.py\n\u251c\u2500\u2500 models.py\n\u2514\u2500\u2500 runner.py\n```\n\n\n\n",
    "bugtrack_url": null,
    "license": "Copyright (c) 2018 The Python Packaging Authority  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": "A minimal and simple machine learning experiment module for PyTorch.",
    "version": "0.0.10",
    "project_urls": {
        "Homepage": "https://github.com/rosikand/torchplate"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "449d53bb62cb9f53de610ef8f10a0d77a2af378b03e1f4235943ac208e6d16d1",
                "md5": "9fb5702c8a4eec96204d756c07e79b98",
                "sha256": "4f81911ec506fd9b52e3a7cafca559ee864f2eb7ed672e065c65a30ea821a3ce"
            },
            "downloads": -1,
            "filename": "torchplate-0.0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9fb5702c8a4eec96204d756c07e79b98",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 9299,
            "upload_time": "2023-06-04T06:04:19",
            "upload_time_iso_8601": "2023-06-04T06:04:19.666952Z",
            "url": "https://files.pythonhosted.org/packages/44/9d/53bb62cb9f53de610ef8f10a0d77a2af378b03e1f4235943ac208e6d16d1/torchplate-0.0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "701dd1250cd23f10bd42ad41f00035df721e6ff9c67d322c5aee717a50bab633",
                "md5": "d9d2089e4ede97c0803471dae5246c90",
                "sha256": "f6afa15b65a464a378906d64cb4ca94526bb2c5d7c522968dd1258bb7711917a"
            },
            "downloads": -1,
            "filename": "torchplate-0.0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "d9d2089e4ede97c0803471dae5246c90",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 519369,
            "upload_time": "2023-06-04T06:04:22",
            "upload_time_iso_8601": "2023-06-04T06:04:22.075706Z",
            "url": "https://files.pythonhosted.org/packages/70/1d/d1250cd23f10bd42ad41f00035df721e6ff9c67d322c5aee717a50bab633/torchplate-0.0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-04 06:04:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "rosikand",
    "github_project": "torchplate",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "torchplate"
}
        
Elapsed time: 0.07904s