LogitEntropyLogger#
- class LogitEntropyLogger(logits_key='teacher_logits', log_frequency=1, prefix='entropy', reduction='mean', log_per_class=False, default_temperature=0.07)[source]#
Bases:
HookBaseLog the entropy of teacher logits using Sonata’s temperature schedule.
Monitors prediction confidence by computing the Shannon entropy of the temperature-scaled softmax over
logits_keyin the model output dict. Inbefore_trainit locates the Sonata submodule (unwrapping DDP) to read its currentteacher_temp; if none is found it falls back todefault_temperature. Runs inafter_stepeverylog_frequencysteps: logs the entropy (and the temperature) to the writer undertrain_batch/{prefix}and totrainer.storage/iter_info, and stashes the temperature back into the model output dict for other consumers.after_epochlogs the epoch-average entropy undertrain/{prefix}. Registered asLogitEntropyLogger.- Parameters:
logits_key (str) – Key in the model output dict holding the logits. Defaults to
"teacher_logits".log_frequency (int) – Compute and log every this many steps. Defaults to
1.prefix (str) – Namespace prefix for the logged keys. Defaults to
"entropy".reduction (str) – How to reduce the per-token entropy:
"mean","sum", or"none". Defaults to"mean".log_per_class (bool) – If
True(andreduction="none"), also log per-class mean entropy. Defaults toFalse.default_temperature (float) – Temperature used when no Sonata model is found. Defaults to
0.07.
Note
Higher entropy indicates more uncertain predictions; lower entropy more confident ones. No-ops on a given step if the logits key is absent from the model output dict.
Example
Add to
cfg.hooksfor Sonata-style SSL; everylog_frequencysteps it logs the entropy of the teacher logits:hooks = [dict(type="LogitEntropyLogger", log_frequency=50)] # → writes "train_batch/entropy" and "entropy/temperature" to the writer # (and the epoch average "train/entropy"), puts "entropy" in # trainer.storage, and appends "entropy: <val>" to the iter log line