{"id":15066207,"url":"https://github.com/havakv/torchtuples","last_synced_at":"2025-04-10T13:42:42.929Z","repository":{"id":47442723,"uuid":"147364837","full_name":"havakv/torchtuples","owner":"havakv","description":"Training neural networks in PyTorch","archived":false,"fork":false,"pushed_at":"2024-03-07T06:07:28.000Z","size":378,"stargazers_count":27,"open_issues_count":6,"forks_count":12,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T12:21:43.819Z","etag":null,"topics":["deep-learning","machine-learning","neural-network","python","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/havakv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-04T15:07:34.000Z","updated_at":"2024-09-05T12:04:30.000Z","dependencies_parsed_at":"2024-06-19T09:37:45.188Z","dependency_job_id":null,"html_url":"https://github.com/havakv/torchtuples","commit_stats":{"total_commits":148,"total_committers":11,"mean_commits":"13.454545454545455","dds":"0.29054054054054057","last_synced_commit":"a87a5aa1d7535fa2a5f86860e585ca183ef0584d"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havakv%2Ftorchtuples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havakv%2Ftorchtuples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havakv%2Ftorchtuples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/havakv%2Ftorchtuples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/havakv","download_url":"https://codeload.github.com/havakv/torchtuples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248226244,"owners_count":21068168,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","machine-learning","neural-network","python","pytorch"],"created_at":"2024-09-25T01:03:43.447Z","updated_at":"2025-04-10T13:42:42.910Z","avatar_url":"https://github.com/havakv.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# torchtuples \n\n[![Python package](https://github.com/havakv/torchtuples/workflows/Python%20package/badge.svg)](https://github.com/havakv/torchtuples/actions)\n[![PyPI](https://img.shields.io/pypi/v/torchtuples.svg)](https://pypi.org/project/torchtuples/)\n[![PyPI](https://anaconda.org/conda-forge/torchtuples/badges/version.svg)](https://anaconda.org/conda-forge/torchtuples)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/torchtuples.svg)\n[![License](https://img.shields.io/badge/License-BSD%202--Clause-orange.svg)](https://github.com/havakv/torchtuples/blob/master/LICENSE)\n\n**torchtuples** is a small python package for training PyTorch models.\nIt works equally well for `numpy arrays` and `torch tensors`.\nOne of the main benefits of **torchtuples** is that it handles data in the form of nested tuples (see [example below](#example)).\n\n\n## Installation\n\n**torchtuples** depends on [PyTorch](https://pytorch.org/get-started/locally/) which should be installed from [HERE](https://pytorch.org/get-started/locally/).\n\nNext, **torchtuples** can be installed with pip:\n```bash\npip install torchtuples\n```\nOr, via conda:\n```bash\nconda install -c conda-forge torchtuples\n```\nFor the bleeding edge version, install directly from github (consider adding `--force-reinstall`):\n```bash\npip install git+git://github.com/havakv/torchtuples.git\n```\nor by cloning the repo:\n```bash\ngit clone https://github.com/havakv/torchtuples.git\ncd torchtuples\npython setup.py install\n```\n\n## Example\n\n```python\nimport torch\nfrom torch import nn\nfrom torchtuples import Model, optim\n```\nMake a data set with three sets of covariates `x0`, `x1` and `x2`, and a target `y`.\nThe covariates are structured in a nested tuple `x`.\n```python\nn = 500\nx0, x1, x2 = [torch.randn(n, 3) for _ in range(3)]\ny = torch.randn(n, 1)\nx = (x0, (x0, x1, x2))\n```\nCreate a simple ReLU net that takes as input the tensor `x_tensor` and the tuple `x_tuple`. Note that `x_tuple` can be of arbitrary length. The tensors in `x_tuple` are passed through a layer `lin_tuple`, averaged, and concatenated with `x_tensor`.\nWe then pass our new tensor through the layer `lin_cat`.\n```python\nclass Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.lin_tuple = nn.Linear(3, 2)\n        self.lin_cat = nn.Linear(5, 1)\n        self.relu = nn.ReLU()\n\n    def forward(self, x_tensor, x_tuple):\n        x = [self.relu(self.lin_tuple(xi)) for xi in x_tuple]\n        x = torch.stack(x).mean(0)\n        x = torch.cat([x, x_tensor], dim=1)\n        return self.lin_cat(x)\n\n    def predict(self, x_tensor, x_tuple):\n        x = self.forward(x_tensor, x_tuple)\n        return torch.sigmoid(x)\n```\n\nWe can now fit the model with\n```python\nmodel = Model(Net(), nn.MSELoss(), optim.SGD(0.01))\nlog = model.fit(x, y, batch_size=64, epochs=5)\n```\nand make predictions with either the `Net.predict` method\n```python\npreds = model.predict(x)\n```\nor with the `Net.forward` method\n```python\npreds = model.predict_net(x)\n```\n\nFor more examples, see the [examples folder](https://github.com/havakv/torchtuples/tree/master/examples).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhavakv%2Ftorchtuples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhavakv%2Ftorchtuples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhavakv%2Ftorchtuples/lists"}