Generative AI
tensoris.backend.losses.genai ¶
Generative AI & Multimodal Loss Functions.
Classes¶
ContrastiveInfoNCELoss ¶
Bases: Module
InfoNCE Loss for self-supervised contrastive learning and multimodal alignment (CLIP).
Formula
where \(\text{sim}(u, v) = \frac{u^T v}{\|u\|_2 \|v\|_2}\) and \(\tau\) is the temperature hyperparameter.
References
Oord, A. v. d., Li, Y., & Vinyals, O. (2018). Representation Learning with Contrastive Predictive Coding. arXiv preprint arXiv:1807.03748. https://arxiv.org/abs/1807.03748
Radford, A. et al. (2021). Learning Transferable Visual Models From Natural Language Supervision. Proceedings of the International Conference on Machine Learning (ICML 2021).
Source code in src/tensoris/backend/losses/genai.py
Methods:¶
__init__ ¶
Initialize InfoNCE Loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
float
|
Scaling temperature parameter for logit normalization. |
0.07
|
forward ¶
Compute InfoNCE contrastive alignment loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_features
|
Tensor
|
Normalized image feature representations of shape (N, D). |
required |
text_features
|
Tensor
|
Normalized text feature representations of shape (N, D). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar contrastive loss value. |
Source code in src/tensoris/backend/losses/genai.py
PerceptualLoss ¶
Bases: Module
Perceptual Reconstruction Loss for image synthesis and latent diffusion.
Formula
where \(\phi^l\) denotes feature maps extracted from layer \(l\) of a pre-trained feature extractor network.
References
Johnson, J., Alahi, A., & Fei-Fei, L. (2016). Perceptual Losses for Real-Time Style Transfer and Super-Resolution. European Conference on Computer Vision (ECCV 2016), pp. 694-711. DOI: https://doi.org/10.1007/978-3-319-46475-6_43 | arXiv: https://arxiv.org/abs/1603.08155
Source code in src/tensoris/backend/losses/genai.py
Methods:¶
__init__ ¶
forward ¶
Compute feature representation L1 difference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generated
|
Tensor
|
Generated sample tensor of shape (N, C, H, W). |
required |
target
|
Tensor
|
Ground truth target sample tensor of shape (N, C, H, W). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar perceptual loss value. |