Masked Language Model Loss Code
def lm_loss(network_output, target, mask): """ batch_size, seq_len, num_classes = network_output.shape flat_output = network_output.view(-1, num_classes) flat_target = target.view(batch_size * seq_len) flat_mask = mask.view(batch_size * seq_len) per_token_loss = F.cross_entropy(flat_output, flat_target, reduce=False) return th.sum(per_token_loss * flat_mask) / th.sum(flat_mask)
A screenshot of Python code defining a masked language-model loss function. The function flattens network outputs, targets, and a mask, calculates per-token cross-entropy, and averages the loss over unmasked tokens. It is shared as a programming meme about machine-learning code and tensor-shape manipulation.
code editor screenshot / скриншот редактора кодаPython code / код на Pythonfunction definition / определение функцииtext cursor area / область текстового курсораASCII diagram / ASCII-диаграммаdashed lines / пунктирные линииloss calculation / вычисление потерьneural network output / выход нейросетиtarget tensor / целевой тензорmask tensor / тензор маскиcross-entropy function / функция кросс-энтропииbatch size / размер пакетаsequence length / длина последовательностиclass count / количество классов
- @profunctor_io / 1115 2018-07-26
- Type
- image
- Size
- 604×326 · 36.1 KiB
- Mood
- technical, humorous
- Language
- en
- Added