Skip to content

Commit 725b4e6

Browse files
committed
weight metrics by batch size
1 parent 8d49716 commit 725b4e6

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

‎model2vec/train/trainer.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ def _run_validation(
7474
device: torch.device,
7575
) -> dict[str, float]:
7676
model.eval()
77-
accumulated: dict[str, list[float]] = defaultdict(list)
77+
weighted_sums: dict[str, float] = defaultdict(float)
78+
total_samples = 0
7879
for x, y in val_loader:
7980
x, y = x.to(device), y.to(device)
81+
batch_size = x.shape[0]
8082
head_out = model(x)
8183
loss = loss_function(head_out, y)
8284
for key, value in compute_metrics(head_out, y, loss).items():
83-
accumulated[key].append(value)
85+
weighted_sums[key] += value * batch_size
86+
total_samples += batch_size
8487
model.train()
85-
return {key: sum(values) / len(values) for key, values in accumulated.items()}
88+
return {key: total / total_samples for key, total in weighted_sums.items()}
8689

8790

8891
def run_training_loop(

0 commit comments

Comments
 (0)