Data Databatch#
databatch
#
| CLASS | DESCRIPTION |
|---|---|
DataBatch |
Data class to hold a batch of data from |
Classes#
DataBatch(data: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None)
dataclass
#
Data class to hold a batch of data from DatasetWeighted.
| ATTRIBUTE | DESCRIPTION |
|---|---|
data |
Input data tensor for the batch.
TYPE:
|
weights |
Weights tensor for the batch, if provided.
TYPE:
|
target |
Target tensor for the batch, if provided.
TYPE:
|
Examples:
Creating a Batch instance
>>> data = torch.randn(32, 3, 224, 224)
>>> weights = torch.rand(32)
>>> target = torch.randint(0, 2, (32,))
>>> batch = DataBatch(data=data, weights=weights, target=target)
| METHOD | DESCRIPTION |
|---|---|
to |
Move all tensors in the batch to the specified device. |
device |
Return the device of the batch. |
cpu |
Move all tensors in the batch to CPU. |
cuda |
Move all tensors in the batch to CUDA (GPU). |
pin_memory |
Pin memory for all tensors in the batch for faster GPU transfers. |
has_target |
Check if the batch has target values. |
has_weights |
Check if the batch has weights. |
check_shape |
Check if data, weights, and target (if present) have compatible shapes. |
check_type |
Check if data, weights, and target (if present) are of type torch.Tensor. |
check_dim |
Check if data, weights, and target (if present) have valid dimensions. |
check |
Run all checks on the batch. |
Functions#
to(device: torch.device) -> DataBatch
#
Move all tensors in the batch to the specified device.
Source code in spectre/data/databatch.py
device() -> torch.device
#
cpu() -> DataBatch
#
cuda() -> DataBatch
#
pin_memory() -> DataBatch
#
Pin memory for all tensors in the batch for faster GPU transfers.
Note: Pinned memory only applies to CPU tensors. If tensors are already on GPU, they are returned as-is.
Source code in spectre/data/databatch.py
has_target() -> bool
#
has_weights() -> bool
#
check_shape() -> bool
#
Check if data, weights, and target (if present) have compatible shapes.
Source code in spectre/data/databatch.py
check_type() -> bool
#
Check if data, weights, and target (if present) are of type torch.Tensor.
Source code in spectre/data/databatch.py
check_dim() -> bool
#
Check if data, weights, and target (if present) have valid dimensions.
Data must be 2D, weights must be 1D, targets can be 1D or 2D.
Source code in spectre/data/databatch.py
Functions#
collate_databatch(batch: list[DataBatch]) -> DataBatch
#
Collate function for batching DataBatch objects.
| PARAMETER | DESCRIPTION |
|---|---|
batch
|
List of DataBatch objects to collate.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataBatch
|
Collated batch with stacked tensors. |