Implementation:LaurentMazare Tch rs ModuleT Batch Accuracy
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Model_Evaluation |
| Last Updated | 2026-02-08 14:00 GMT |
Overview
Concrete tool for computing batched classification accuracy over a test set provided by the tch nn module.
Description
ModuleT::batch_accuracy_for_logits is a default method on the ModuleT trait that evaluates a model over a full dataset in mini-batches. It creates a no_grad_guard, iterates over the dataset using Iter2 with return_smaller_last_batch(), computes accuracy_for_logits for each batch, and returns the weighted average accuracy as an f64 in [0.0, 1.0].
Usage
Call on any model implementing ModuleT to evaluate test accuracy. Pass the full test images, labels, device, and evaluation batch size.
Code Reference
Source Location
- Repository: tch-rs
- File: src/nn/module.rs
- Lines: 16-33
Signature
fn batch_accuracy_for_logits(
&self,
xs: &Tensor,
ys: &Tensor,
d: Device,
batch_size: i64,
) -> f64
Import
use tch::nn::ModuleT;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| xs | &Tensor | Yes | Full test set images tensor |
| ys | &Tensor | Yes | Full test set labels tensor |
| d | Device | Yes | Device for computation (should match model device) |
| batch_size | i64 | Yes | Evaluation mini-batch size |
Outputs
| Name | Type | Description |
|---|---|---|
| f64 | f64 | Classification accuracy in [0.0, 1.0] |
Usage Examples
use tch::nn::ModuleT;
// After training
let test_accuracy = model.batch_accuracy_for_logits(
&dataset.test_images,
&dataset.test_labels,
vs.device(),
256, // eval batch size
);
println!("Test accuracy: {:.2}%", 100.0 * test_accuracy);
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment