model-a-scratch / mla /eval.py
karthik-2905's picture
Upload folder using huggingface_hub
ae9e4fe verified
Raw
History Blame Contribute Delete
472 Bytes
import gc
import math
from .data import get_batch
from .loss import cross_entropy
def eval_loss(model, ids, block_size, batch_size, n_batches, rng, ignore_index=-1):
total = 0.0
for _ in range(n_batches):
x, y = get_batch(ids, block_size, batch_size, rng)
loss = cross_entropy(model(x), y, ignore_index)
total += float(loss.data)
del loss
gc.collect()
mean = total / max(1, n_batches)
return mean, math.exp(mean)