agentsclimarketplace

Contrastive loss integration with encoders

Skill HolobiomicsLab/asb-skill-collections/collections/metabolomics/v2/skills/contrastive-loss-integration-with-encoders

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.From its SKILL.md

Install
npx -y skills add HolobiomicsLab/asb-skill-collections --skill contrastive-loss-integration-with-encoders

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 15 stars15 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its file declares

Copied from the file, not written here

The file declares its own license as CC-BY-4.0. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.

SKILL.md

7.7 KB, ~1.3k tokens by cl100k_base, as published. Nobody here has run it

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"

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.