Install & Compatibility
Where this runs
tested against v0.0.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.8MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.9s · import 0.000s · 20MB
18MB installed
● package 18MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Metric
✓ import torcheval
✗ from torcheval import Metric
This example demonstrates how to initialize a `BinaryAccuracy` metric, update it with predictions and targets, compute the current accuracy, and reset its internal state. Metrics accumulate data, so remember to call `reset()` for new evaluation runs (e.g., per epoch).
import torch
from torcheval.metrics import BinaryAccuracy
# Initialize the metric
metric = BinaryAccuracy()
# Simulate model predictions and ground truth labels
# Ensure inputs are tensors and on the correct device
predictions = torch.tensor([0.9, 0.1, 0.8, 0.2, 0.95])
targets = torch.tensor([1, 0, 1, 0, 1])
# Update the metric with a batch of data
metric.update(predictions, targets)
# Get the computed result
accuracy = metric.compute()
print(f"Binary Accuracy: {accuracy.item():.4f}")
# Example with another batch
predictions2 = torch.tensor([0.4, 0.6, 0.7])
targets2 = torch.tensor([0, 1, 0])
metric.update(predictions2, targets2)
# Compute cumulative accuracy
cumulative_accuracy = metric.compute()
print(f"Cumulative Binary Accuracy: {cumulative_accuracy.item():.4f}")
# Reset the metric's internal state
metric.reset()
print(f"Accuracy after reset and recompute: {metric.compute().item():.4f}")
Debug
Known issues
gotchaIn distributed training environments (e.g., using `torch.distributed`), metrics accumulate local states independently on each process. To get the correct global metric value, you must call `metric.sync_and_compute()` instead of `metric.compute()`.fixUse `metric.sync_and_compute()` when operating in a distributed setting. Ensure `torch.distributed` is initialized before calling this method.
affects: All versions
gotchaMetrics accumulate their internal state across multiple calls to `update()`. If you need to calculate metrics for distinct evaluation periods (e.g., per epoch or per validation run), you must call `metric.reset()` before processing new data, or create a new metric instance.fixCall `metric.reset()` at the beginning of each new evaluation period to clear previously accumulated data.
affects: All versions
gotchaTorchEval is currently in a pre-1.0 state (0.0.x versions). While efforts are made to maintain stability, minor API changes might occur between releases. Always refer to the latest documentation for precise API details.fixPeriodically check the official documentation and release notes when upgrading for potential API adjustments. Pin minor versions if strict API stability is required.
affects: All 0.0.x versions
gotchaInput tensors for `update()` must adhere to specific shapes and dtypes expected by each metric. For instance, binary metrics typically expect 1D tensors (N,) or (N,1) for predictions and targets.fixCarefully read the documentation for each specific metric regarding expected input shapes, dtypes, and value ranges (e.g., logits vs. probabilities for classification). Reshape or cast tensors as needed, e.g., `predictions.squeeze(-1)`.
affects: All versions
Upgrade
Version history
0.0.7latest on PyPI · released Aug 24, 2023
Audit
Dependencies
torchrequiredCore dependency for tensor operations, GPU acceleration, and distributed training.