# `TorchStream`

### *class* capymoa.stream.TorchStream[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L19)

Bases: [`Stream`](capymoa.stream.Stream.md#capymoa.stream.Stream)

A stream adapter for PyTorch datasets.

This class converts PyTorch datasets into CapyMOA streams for both classification
and regression tasks.

Creating a classification stream from a PyTorch dataset:

```pycon
>>> from capymoa.datasets import get_download_dir
>>> from capymoa.stream import TorchStream
>>> from torchvision import datasets, transforms
>>>
>>> dataset = datasets.FashionMNIST(
...     root=get_download_dir(),
...     train=True,
...     download=True,
...     transform=transforms.ToTensor()
... )  
>>> stream = TorchStream.from_classification(
...     dataset, num_classes=10, class_names=dataset.classes
... )  
>>> stream.next_instance()  
LabeledInstance(...)
```

Creating a shuffled classification stream:

```pycon
>>> import torch
>>> from torch.utils.data import TensorDataset
>>>
>>> dataset = TensorDataset(
...     torch.tensor([[1.0], [2.0], [3.0]]),
...     torch.tensor([0, 1, 2])
... )
>>> stream = TorchStream.from_classification(
...     dataset, num_classes=3, shuffle=True, shuffle_seed=0
... )
>>> [float(inst.x[0]) for inst in stream]
[3.0, 1.0, 2.0]
```

Streams can be restarted to iterate again:

```pycon
>>> stream.restart()
>>> [float(inst.x[0]) for inst in stream]
[3.0, 1.0, 2.0]
```

Creating a regression stream:

```pycon
>>> dataset = TensorDataset(
...     torch.tensor([[1.0], [2.0], [3.0]]),
...     torch.tensor([0.5, 1.5, 2.5])
... )
>>> stream = TorchStream.from_regression(
...     dataset, shuffle=True, shuffle_seed=0
... )
>>> [(float(inst.x[0]), float(inst.y_value)) for inst in stream]
[(3.0, 2.5), (1.0, 0.5), (2.0, 1.5)]
```

#### \_\_init_\_(dataset: [Dataset](https://docs.pytorch.org/docs/stable/data.html#torch.utils.data.Dataset), schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema))[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L152)

Construct a TorchStream from a PyTorch Dataset and a Schema.

Usually you want [`from_classification()`](#capymoa.stream.TorchStream.from_classification) or [`from_regression()`](#capymoa.stream.TorchStream.from_regression).

* **Parameters:**
  * **dataset** – A PyTorch Dataset that yields tuples of (features, target).
  * **schema** – A Schema object that describes the structure of the data,
    including feature names and target information.

#### \_\_iter_\_() → [Self](https://docs.python.org/3/library/typing.html#typing.Self)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/_stream.py#L356)

Get an iterator over the stream.

This will NOT restart the stream if it has already been iterated over.
Please use the [`restart()`](#capymoa.stream.TorchStream.restart) method to restart the stream.

* **Yield:**
  An iterator over the stream.

#### \_\_next_\_() → \_AnyInstance[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/_stream.py#L366)

Get the next instance in the stream.

* **Returns:**
  The next instance in the stream.

#### cli_help() → [str](https://docs.python.org/3/builtins/stdtypes.html#str)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/_stream.py#L379)

Return a help message

#### *static* from_classification(dataset: [Dataset](https://docs.pytorch.org/docs/stable/data.html#torch.utils.data.Dataset)[[tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor), [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor) | [int](https://docs.python.org/3/builtins/functions.html#int)]], num_classes: [int](https://docs.python.org/3/builtins/functions.html#int), class_names: [Sequence](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [None](https://docs.python.org/3/builtins/constants.html#None) = None, dataset_name: [str](https://docs.python.org/3/builtins/stdtypes.html#str) = 'TorchStream', shape: [Sequence](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)[[int](https://docs.python.org/3/builtins/functions.html#int)] | [None](https://docs.python.org/3/builtins/constants.html#None) = None, shuffle: [bool](https://docs.python.org/3/builtins/functions.html#bool) = False, shuffle_seed: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None) → [TorchStream](#capymoa.stream.TorchStream)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L109)

Construct a stream for classification from a PyTorch Dataset.

* **Parameters:**
  * **dataset** – A PyTorch Dataset that yields tuples of (features, target).
  * **num_classes** – The number of classes in the classification task.
  * **class_names** – An optional sequence of class names corresponding to the class indices.
  * **dataset_name** – An optional name for the stream.
  * **shape** – An optional shape for the features. If not provided, features will
    be treated as flat vectors.
  * **shuffle** – Whether to shuffle the dataset.
  * **shuffle_seed** – An optional seed for shuffling the dataset.
* **Returns:**
  A TorchStream instance.

#### *static* from_regression(dataset: [Dataset](https://docs.pytorch.org/docs/stable/data.html#torch.utils.data.Dataset)[[tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor), [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor) | [float](https://docs.python.org/3/builtins/functions.html#float)]], dataset_name: [str](https://docs.python.org/3/builtins/stdtypes.html#str) = 'TorchStream', shuffle: [bool](https://docs.python.org/3/builtins/functions.html#bool) = False, shuffle_seed: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None) → [TorchStream](#capymoa.stream.TorchStream)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L77)

Construct a stream for regression from a PyTorch Dataset.

* **Parameters:**
  * **dataset** – A PyTorch Dataset that yields tuples of (features, target) for
    regression tasks.
  * **dataset_name** – An optional name for the stream.
  * **shape** – An optional shape for the features. If not provided, features will
    be treated as flat vectors.
  * **shuffle** – Whether to shuffle the dataset.
  * **shuffle_seed** – An optional seed for shuffling the dataset.
* **Returns:**
  A TorchStream instance for regression.

#### get_moa_stream()[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L197)

Get the MOA stream object if it exists.

#### get_schema()[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L194)

Return the schema of the stream.

#### has_more_instances()[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L169)

Return `True` if the stream have more instances to read.

#### next_instance()[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L172)

Return the next instance in the stream.

* **Raises:**
  [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – If the machine learning task is neither a regression
  nor a classification task.
* **Returns:**
  A labeled instances or a regression depending on the schema.

#### restart()[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/stream/torch.py#L200)

Restart the stream to read instances from the beginning.
