Natural Language Processing
tensoris.backend.losses.nlp ¶
Natural Language Processing Loss Functions.
Classes¶
LabelSmoothingCrossEntropy ¶
Bases: Module
Cross Entropy loss with label smoothing for sequence modeling and classification.
Formula
where \(\epsilon\) is the smoothing factor, \(K\) is the total number of classes, and \(p_k = \text{softmax}(z)_k\).
References
Szegedy, C., Vanhoucke, V., Ioffe, S., Shlens, J., & Wojna, Z. (2016). Rethinking the Inception Architecture for Computer Vision. Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR 2016), pp. 2818-2826. DOI: https://doi.org/10.1109/CVPR.2016.308 | arXiv: https://arxiv.org/abs/1512.00567
Vaswani, A. et al. (2017). Attention Is All You Need. NIPS 2017.
Source code in src/tensoris/backend/losses/nlp.py
Methods:¶
__init__ ¶
Initialize Label Smoothing Cross Entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smoothing
|
float
|
Label smoothing factor epsilon between 0.0 and 1.0. |
0.1
|
ignore_index
|
int
|
Target index ignored during loss computation (padding token). |
-100
|
Source code in src/tensoris/backend/losses/nlp.py
forward ¶
Compute label smoothed cross-entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
Tensor
|
Predicted vocabulary logits tensor of shape (N, C) or (N, T, C). |
required |
targets
|
Tensor
|
Ground truth token indices tensor of shape (N) or (N, T). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar smoothed cross entropy loss tensor. |