# torch-nested
[![Python 3.7+](https://img.shields.io/badge/Python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)
[![PyTorch](https://img.shields.io/badge/PyTorch-1.4+-blue.svg)](https://pypi.org/project/torch/1.4.0/)
[![PyPI](https://img.shields.io/pypi/v/torch-nested)](https://pypi.org/project/torch-nested/)
![Wheel](https://img.shields.io/pypi/wheel/torch-nested)
[![Tests](https://github.com/snimu/torch-nested/actions/workflows/test.yml/badge.svg)](https://github.com/snimu/torch-nested/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/snimu/torch-nested/branch/main/graph/badge.svg)](https://codecov.io/gh/snimu/torch-nested)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/snimu/torch-nested/main.svg)](https://results.pre-commit.ci/latest/github/snimu/torch-nested/main)
[![License](https://img.shields.io/pypi/l/torch-nested)](https://github.com/snimu/torch-nested/blob/main/LICENSE)
Easily manipulate `torch.Tensors` inside highly nested data-structures.
You may want to consider using [torch.nested](https://pytorch.org/docs/stable/nested.html),
but if you are working with nested `dicts`, `lists`, `tuples`, etc. of `torch.Tensors`,
here is the package for you.
A proper documentation is coming. Until then, a basic example is shown below, and you can look at the docstrings
or tests of this package for more information.
## Basic usage
Given a nested structure that contains `torch.Tensor`, this package makes it easy to access these `Tensors` and
work with them:
```python
import torch
from torch_nested import NestedTensors
INPUT_DATA = [
(
torch.ones(3),
torch.zeros(2)
),
torch.ones((2, 2, 2)),
{
"foo": torch.ones(2),
"bar": [],
"har": "rar"
},
1
]
tensors = NestedTensors(INPUT_DATA)
# Original data preserved in .data-member
assert tensors.data == INPUT_DATA
# Simple accessing and setting
for i, tensor in enumerate(tensors):
tensors[i] = tensor + i
# Has basic dunders
assert len(tensors) == 4
assert torch.all(next(tensors) == torch.ones(3))
```
Calling `print(tensors.shape())` would yield:
```
torch_nested.Size(
[
(
torch.Size([3]),
torch.Size([2])
),
torch.Size([2, 2, 2]),
{
foo: torch.Size([2]),
bar: None,
har: None
},
None
]
)
```
### Supported data-structures
The following data-structures are supported so far:
- `torch.Tensor`
- `dict`
- `list`
- `tuple`
- `None`
- Any class with a `.tensors`-attribute
- Any class with a `.data`-attribute, even if it isn't a `torch.Tensor`
For example
```python
class ObjWithTensors:
tensors = [torch.ones(2), torch.zeros(2)]
class ObjWithData:
data = [torch.ones(2), torch.zeros(2)]
tensors = NestedTensors([ObjWithTensors(), ObjWithData()])
```
Running `print(tensors.size())` would result in the following output:
```
NestedSize(
[
ObjWithTensors(
tensors: [
torch.Size([2]),
torch.Size([2])
]
),
ObjWithData(
data: [
torch.Size([2]),
torch.Size([2])
]
)
]
)
```
More data-structures will be supported in the future. Any data that is of an unsupported type
will not have its `Tensors` readable or writable, and `NestedShape` will show `None` there.
Raw data
{
"_id": null,
"home_page": "https://github.com/snimu/torch-nested",
"name": "torch-nested",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "torch pytorch nested torch-nested tensor tensors nested-tensors deep-learning ml",
"author": "Sebastian Nicolas Muller @snimu",
"author_email": "sebastian.nicolas.mueller@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c3/f6/c13b8ff10003ad1d31be8df1ebb34379439c451b55e60be401d47177045c/torch_nested-0.0.5.tar.gz",
"platform": null,
"description": "# torch-nested\n\n[![Python 3.7+](https://img.shields.io/badge/Python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)\n[![PyTorch](https://img.shields.io/badge/PyTorch-1.4+-blue.svg)](https://pypi.org/project/torch/1.4.0/)\n\n[![PyPI](https://img.shields.io/pypi/v/torch-nested)](https://pypi.org/project/torch-nested/)\n![Wheel](https://img.shields.io/pypi/wheel/torch-nested)\n\n[![Tests](https://github.com/snimu/torch-nested/actions/workflows/test.yml/badge.svg)](https://github.com/snimu/torch-nested/actions/workflows/test.yml)\n[![codecov](https://codecov.io/gh/snimu/torch-nested/branch/main/graph/badge.svg)](https://codecov.io/gh/snimu/torch-nested)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/snimu/torch-nested/main.svg)](https://results.pre-commit.ci/latest/github/snimu/torch-nested/main)\n\n[![License](https://img.shields.io/pypi/l/torch-nested)](https://github.com/snimu/torch-nested/blob/main/LICENSE)\n\nEasily manipulate `torch.Tensors` inside highly nested data-structures.\n\nYou may want to consider using [torch.nested](https://pytorch.org/docs/stable/nested.html),\nbut if you are working with nested `dicts`, `lists`, `tuples`, etc. of `torch.Tensors`, \nhere is the package for you.\n\nA proper documentation is coming. Until then, a basic example is shown below, and you can look at the docstrings \nor tests of this package for more information.\n\n## Basic usage\n\nGiven a nested structure that contains `torch.Tensor`, this package makes it easy to access these `Tensors` and \nwork with them: \n\n```python\nimport torch\nfrom torch_nested import NestedTensors\n\n\nINPUT_DATA = [\n (\n torch.ones(3), \n torch.zeros(2)\n ),\n torch.ones((2, 2, 2)),\n {\n \"foo\": torch.ones(2), \n \"bar\": [], \n \"har\": \"rar\"\n },\n 1\n]\n\ntensors = NestedTensors(INPUT_DATA)\n\n# Original data preserved in .data-member\nassert tensors.data == INPUT_DATA\n\n# Simple accessing and setting\nfor i, tensor in enumerate(tensors):\n tensors[i] = tensor + i \n\n# Has basic dunders\nassert len(tensors) == 4\nassert torch.all(next(tensors) == torch.ones(3))\n```\n\nCalling `print(tensors.shape())` would yield:\n\n```\ntorch_nested.Size(\n [\n (\n torch.Size([3]),\n torch.Size([2])\n ),\n torch.Size([2, 2, 2]),\n {\n foo: torch.Size([2]),\n bar: None,\n har: None\n },\n None\n ]\n)\n\n```\n\n### Supported data-structures\n\nThe following data-structures are supported so far:\n\n- `torch.Tensor`\n- `dict`\n- `list`\n- `tuple`\n- `None`\n- Any class with a `.tensors`-attribute\n- Any class with a `.data`-attribute, even if it isn't a `torch.Tensor`\n\nFor example\n\n```python\nclass ObjWithTensors:\n tensors = [torch.ones(2), torch.zeros(2)]\n\nclass ObjWithData:\n data = [torch.ones(2), torch.zeros(2)]\n\ntensors = NestedTensors([ObjWithTensors(), ObjWithData()])\n```\n\nRunning `print(tensors.size())` would result in the following output:\n\n```\nNestedSize(\n [\n ObjWithTensors(\n tensors: [\n torch.Size([2]),\n torch.Size([2])\n ]\n ),\n ObjWithData(\n data: [\n torch.Size([2]),\n torch.Size([2])\n ]\n )\n ]\n)\n```\n\nMore data-structures will be supported in the future. Any data that is of an unsupported type \nwill not have its `Tensors` readable or writable, and `NestedShape` will show `None` there.\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Easily manipulate torch.Tensors inside highly nested data-structures.",
"version": "0.0.5",
"split_keywords": [
"torch",
"pytorch",
"nested",
"torch-nested",
"tensor",
"tensors",
"nested-tensors",
"deep-learning",
"ml"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0df42271ed66da64eb15d98f58d77dd6bf92b3615b79beedff24c9329fb17a66",
"md5": "d7bc68d2a0bee50f61c813ff37cd69ec",
"sha256": "f9bd50da3ad67d933388c33f0c6be16e6efc69458c453a5f0e48184c64f656db"
},
"downloads": -1,
"filename": "torch_nested-0.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d7bc68d2a0bee50f61c813ff37cd69ec",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8501,
"upload_time": "2023-02-03T19:02:05",
"upload_time_iso_8601": "2023-02-03T19:02:05.963962Z",
"url": "https://files.pythonhosted.org/packages/0d/f4/2271ed66da64eb15d98f58d77dd6bf92b3615b79beedff24c9329fb17a66/torch_nested-0.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c3f6c13b8ff10003ad1d31be8df1ebb34379439c451b55e60be401d47177045c",
"md5": "bf4d1d92e784f2461ef2b3f2c52b3cb9",
"sha256": "4d2c57a03029ed220401258f0c1dbce2a726fb45f6fc8630c7a45645f9bab692"
},
"downloads": -1,
"filename": "torch_nested-0.0.5.tar.gz",
"has_sig": false,
"md5_digest": "bf4d1d92e784f2461ef2b3f2c52b3cb9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 8777,
"upload_time": "2023-02-03T19:02:07",
"upload_time_iso_8601": "2023-02-03T19:02:07.702607Z",
"url": "https://files.pythonhosted.org/packages/c3/f6/c13b8ff10003ad1d31be8df1ebb34379439c451b55e60be401d47177045c/torch_nested-0.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-03 19:02:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "snimu",
"github_project": "torch-nested",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "torch-nested"
}