Use when you have a transformer encoder producing representations of
Scanned 9/12/2026
Install to Claude Code
npx -y skills add HolobiomicsLab/asb-skill-collections --skill contrastive-loss-integration-with-encoders --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Contrastive Loss Integration With Encoders?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/holobiomicslab-contrastive-loss-integration-with-encoders)More formats (shields.io, HTML) on the badges page.
---
name: contrastive-loss-integration-with-encoders
description: Use when you have a transformer encoder producing representations of
tandem mass spectra and need to train it using contrastive learning with pairs of
original and randomly masked spectra.
license: CC-BY-4.0
metadata:
edam_operation: http://edamontology.org/operation_3432
edam_topics:
- http://edamontology.org/topic_3520
- http://edamontology.org/topic_3172
tools:
- Python
- Anaconda
- Git
- PyTorch
- MSBERT
techniques:
- LC-MS
license_tier: open
provenance_tier: literature
derived_from:
- doi: 10.1021/acs.analchem.4c02426
title: MSBERT
evidence_spans:
- '[Anaconda](https://www.anaconda.com) for Python 3.12'
- Install [Git](https://git-scm.com/downloads)
claims: []
provenance:
collection: https://w3id.org/holobiomicslab/asb-skill/collection/metabolomics/v2
assembled_by: scripts/collect_metabolomics_collection.py
sources:
- build: coll_msbert_cq
doi: 10.1021/acs.analchem.4c02426
title: MSBERT
dedup_kept_from: coll_msbert_cq
schema_version: 0.2.0
attribution:
generator: AgenticScienceBuilder
original_doi: 10.1021/acs.analchem.4c02426
all_source_dois:
- 10.1021/acs.analchem.4c02426
zenodo_doi: 10.5281/zenodo.20794027
curators: []
promoter: Louis-Félix Nothias
sponsor: CNRS & Université Côte d'Azur
---
# contrastive-loss-integration-with-encoders
## Summary
Integrate a contrastive loss function with a transformer encoder backbone to learn embeddings from paired positive samples (original and masked tandem mass spectra). This skill ensures correct tensor shape flow and loss computation when training self-supervised representation models on mass spectrometry data.
## When to use
You have a transformer encoder producing representations of tandem mass spectra and need to train it using contrastive learning with pairs of original and randomly masked spectra. Apply this skill when you must verify that augmented (masked) and original spectrum representations are wired correctly into a contrastive loss that pulls positive pairs close in embedding space.
## When NOT to use
- Input spectra are already pre-computed embeddings (not raw features)—contrastive learning requires access to raw spectra and their augmentations
- You lack a masking mechanism or cannot generate positive pairs; contrastive loss requires at least two different augmented views of the same input
- Encoder backbone is not a learnable differentiable model; contrastive loss requires end-to-end gradient flow through the encoder
## Inputs
- Original tandem mass spectrum feature tensors (batch of ion intensities/m-z pairs)
- Randomly masked variant of the same spectra (stochastically masked feature tensors)
- Transformer encoder model with configurable attention heads and hidden dimensions
- Contrastive loss function implementation (e.g. NT-Xent)
## Outputs
- Paired embedding vectors (original and masked, same batch, embedding dimensionality e.g. 512-dim)
- Contrastive loss scalar value for backpropagation
- Updated encoder weights after gradient step
## How to apply
After implementing the transformer encoder backbone and random masking mechanism, integrate the masked encoder into the training pipeline by (1) passing both original and masked spectrum features through the encoder to produce paired representation tensors; (2) computing the contrastive loss (e.g. NT-Xent or similar) on these paired embeddings, treating the masked variant as a positive sample; (3) validating tensor shape transformations through the forward pass—confirm that input shapes (e.g. batch size, feature dimensions) map correctly through encoder attention layers and contrastive loss computation; (4) verify alignment of embedding dimensionality (e.g. 512-dim hidden state) with contrastive loss input expectations. The rationale is that correct wiring ensures the encoder learns invariant representations robust to the stochasticity of masking, as exploited in MSBERT's training on the GNPS dataset.
## Related tools
- **PyTorch** (Deep learning framework for implementing transformer encoder, masking mechanism, and contrastive loss computation with automatic differentiation) — https://pytorch.org/
- **MSBERT** (Reference implementation of transformer-encoder-based contrastive learning on tandem mass spectra; provides example of correct encoder–masking–loss integration) — https://github.com/zhanghailiangcsu/MSBERT
## Examples
```
model = MSBERT(100002, 512, 6, 16, 0, 100, 3); loss = contrastive_loss(model(original_spectra), model(masked_spectra)); loss.backward()
```
## Evaluation signals
- Tensor shape validation: confirm that encoder output (batch_size, embedding_dim) matches contrastive loss input expectations and that paired original/masked tensors have identical shapes
- Loss computation stability: verify that contrastive loss decreases over training epochs and does not produce NaN/Inf values, indicating correct gradient flow
- Forward pass trace: print intermediate tensor shapes at each layer (encoder input → attention heads → embedding output → contrastive loss input) to detect shape mismatches
- Embedding space visualization: after training, reduce embedding dimensionality (e.g. UMAP/t-SNE) and confirm that spectra with similar chemical structures cluster together, as demonstrated by MSBERT on Orbitrap test data (top-1 accuracy 0.7871)
- Contrastive loss sanity check: verify that loss between identical (or very similar) spectra is lower than between random pairs, confirming the encoder learned the intended invariance to masking
## Limitations
- Requires careful initialization of encoder attention heads and hidden dimensions; suboptimal choices may slow convergence or degrade embedding quality
- Masking randomness must be sufficient to generate diverse positive pairs; deterministic or under-sampled masking may lead to trivial solutions or poor generalization
- Contrastive loss integration assumes paired data availability (original + masked); cannot be applied to unpaired or single-instance spectra
- Performance depends on training dataset size and diversity (MSBERT was trained on filtered GNPS dataset); results on smaller or instrument-specific datasets may not match reported benchmarks (e.g. Orbitrap top-1 0.7871)
## Evidence
- [intro] MSBERT employs a transformer encoder backbone and leverages the randomness of masking to construct positive samples for contrastive learning during training on the GNPS dataset.: "MSBERT employs a transformer encoder backbone and leverages the randomness of masking to construct positive samples for contrastive learning"
- [intro] Integrate the masked encoder into the MSBERT training pipeline to produce paired representations from original and masked spectra.: "Integrate the masked encoder into the MSBERT training pipeline to produce paired representations from original and masked spectra"
- [intro] Verify the encoder and masking module are correctly wired to the contrastive loss computation by validating tensor shape transformations through the forward pass.: "Verify the encoder and masking module are correctly wired to the contrastive loss computation by validating tensor shape transformations through the forward pass"
- [readme] MSBERT used the transformer encoder as the backbone and take advantage of the randomness of the mask to construct positive samples for contrastive learning.: "MSBERT used the transformer encoder as the backbone and take advantage of the randomness of the mask to construct positive samples for contrastive learning"
- [readme] MSBERT had a stronger ability in library matching, with top 1, top5, and top 10 were 0.7871, 0.8950, and 0.9080 on Orbitrap test dataset.: "MSBERT had a stronger ability in library matching, with top 1, top5, and top 10 were 0.7871, 0.8950, and 0.9080 on Orbitrap test dataset"
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!