# Batch

``` python
Batch(inputs, targets=None, weights=None)
```

What `prepare` produces and `update` consumes.

An example is one entry along the leading (batch) dimension of every input leaf: a sequence is one example, and per-token terms are summed within it. Inputs shared by the whole batch (a scalar timestep) are expanded to the batch dimension by `prepare`. Losses that don’t decompose per example (pairwise, listwise) are out of scope. A pair, or any group scored together, is one example when its log-likelihood depends only on its own outputs.

A mapping that torch’s pytree treats as a leaf (a `UserDict`, a tokenizer’s `BatchEncoding`) is stored as a plain `dict`, in `inputs` and `targets`, so `vmap` sees its tensors.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| inputs | Inputs | Whatever `forward` takes. Code that knows its input type narrows it with `batch.inputs_as(MyType)` or `batch.inputs_as(is_my_type)`. | *required* |
| targets | Targets \| None | Targets, or `None` when the batch has none. | `None` |
| weights | Tensor \| None | Per-example weights. An example of weight $w$ counts as $w$ examples: in the curvature, in the data fit, and in the `n_data` that `lk.laplace` reads from the data. Weight 2 on every example is the data twice, and weight 0 drops an example. `None` means all ones. | `None` |

## Attributes

| Name | Description |
|----|----|
| [size](#laplacekit.Batch.size) | Number of examples: the leading dimension of the first input leaf. |

## Methods

| Name | Description |
|----|----|
| [from_pair](#laplacekit.Batch.from_pair) | Batch from an `(inputs, targets)` pair, as most data loaders yield. |
| [inputs_as](#laplacekit.Batch.inputs_as) | `inputs`, checked at runtime. |

### from_pair

``` python
Batch.from_pair(pair)
```

Batch from an `(inputs, targets)` pair, as most data loaders yield.

#### Parameters

| Name | Type                     | Description         | Default    |
|------|--------------------------|---------------------|------------|
| pair | tuple\[Inputs, Targets\] | Inputs and targets. | *required* |

#### Returns

| Name | Type  | Description              |
|------|-------|--------------------------|
|      | Batch | A batch without weights. |

### inputs_as

``` python
Batch.inputs_as(check, /)
```

`inputs`, checked at runtime.

#### Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| check | type\[T\] \| Callable\[\[Inputs\], TypeIs\[T\]\] | A plain class, or a `TypeIs` predicate for generic types such as `dict[str, Tensor]` (`isinstance` can’t check those). | *required* |

#### Returns

| Name | Type | Description             |
|------|------|-------------------------|
|      | T    | `inputs`, typed as `T`. |

#### Raises

| Name | Type      | Description             |
|------|-----------|-------------------------|
|      | TypeError | `inputs` fails `check`. |
