Use when when you have pretrained encoder-produced embeddings from MS/MS
Scanned 9/12/2026
Install to Claude Code
npx -y skills add HolobiomicsLab/asb-skill-collections --skill attention-mechanism-for-spectral-decoding --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Attention Mechanism For Spectral Decoding?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/holobiomicslab-attention-mechanism-for-spectral-decoding)More formats (shields.io, HTML) on the badges page.
---
name: attention-mechanism-for-spectral-decoding
description: Use when when you have pretrained encoder-produced embeddings from MS/MS
spectra and need to decode them into canonical SMILES strings representing molecular
structures.
license: CC-BY-4.0
metadata:
edam_operation: http://edamontology.org/operation_3761
edam_topics:
- http://edamontology.org/topic_0154
- http://edamontology.org/topic_3520
tools:
- RDKit
- PyTorch
techniques:
- LC-MS
license_tier: open
provenance_tier: literature
derived_from:
- doi: 10.1038/s42004-023-00932-3
title: Spec2Mol
evidence_spans:
- Processing of the chemical data is based on the [RDKit](https://www.rdkit.org/)
software.
claims: []
provenance:
collection: https://w3id.org/holobiomicslab/asb-skill/collection/metabolomics/v2
assembled_by: scripts/collect_metabolomics_collection.py
sources:
- build: coll_spec2mol_cq
doi: 10.1038/s42004-023-00932-3
title: Spec2Mol
dedup_kept_from: coll_spec2mol_cq
schema_version: 0.2.0
attribution:
generator: AgenticScienceBuilder
original_doi: 10.1038/s42004-023-00932-3
all_source_dois:
- 10.1038/s42004-023-00932-3
zenodo_doi: 10.5281/zenodo.20794027
curators: []
promoter: Louis-Félix Nothias
sponsor: CNRS & Université Côte d'Azur
---
# attention-mechanism-for-spectral-decoding
## Summary
Implements an attention-based sequence-to-sequence decoder that converts fixed-size MS/MS spectral embeddings into variable-length SMILES token sequences, enabling molecular structure reconstruction from mass spectra. Attention mechanisms allow the decoder to focus selectively on relevant parts of the embedding when generating each token, improving reconstruction accuracy and chemical validity.
## When to use
When you have pretrained encoder-produced embeddings from MS/MS spectra and need to decode them into canonical SMILES strings representing molecular structures. This skill is appropriate when the encoder output is a fixed-size vector but the target output (SMILES) is variable-length and benefits from position-aware focus during token generation.
## When NOT to use
- Input is already a SMILES string or molecular structure (decoding is not needed).
- Encoder output is already variable-length or non-embedding (attention-based sequence-to-sequence is over-engineered).
- Target molecules lack a standard canonical representation or tokenization (SMILES tokenizer cannot be reliably defined).
## Inputs
- Pretrained encoder embeddings (fixed-size vector, typically from MS/MS spectra)
- SMILES string dataset (canonical format, RDKit-processed)
- SMILES tokenizer or vocabulary
- Held-out test set of encoder embeddings with reference SMILES
## Outputs
- Trained decoder model weights (PyTorch .pt or checkpoint file)
- Reconstructed SMILES strings (variable-length token sequences)
- Exact-match accuracy metric
- Tanimoto similarity scores between reconstructed and reference molecules
- Validation metric logs (e.g., loss, accuracy per epoch)
## How to apply
Define a sequence-to-sequence decoder architecture in PyTorch with an attention layer that maps the fixed-size spectral embedding to a variable-length SMILES token sequence. Initialize a SMILES tokenizer and prepare training data by canonicalizing all target molecules with RDKit. Train end-to-end using cross-entropy loss on SMILES token prediction, employing teacher forcing during training to stabilize learning. During inference, generate tokens autoregressively, allowing the attention mechanism to reweight the embedding context at each decoding step. Evaluate on held-out test embeddings by computing exact-match accuracy (reconstructed SMILES matches reference) and Tanimoto similarity of the decoded molecular structures, ensuring chemical validity via RDKit parsing.
## Related tools
- **PyTorch** (Defines and trains the attention-based sequence-to-sequence decoder architecture, including attention layer, SMILES token embedding, and loss computation)
- **RDKit** (Converts molecules to canonical SMILES format for training targets, tokenizes SMILES strings, and evaluates reconstructed molecules via Tanimoto similarity and chemical validity) — https://www.rdkit.org/
## Examples
```
# After loading encoder and preparing SMILES dataset:
import torch
from torch.nn import Seq2Seq; decoder = Seq2Seq(emb_dim=256, vocab_size=len(tokenizer), max_len=120, attention=True).to(device); optimizer = torch.optim.Adam(decoder.parameters(), lr=0.001); loss_fn = torch.nn.CrossEntropyLoss()
# Train with teacher forcing: for embs, smiles in train_loader: pred_tokens = decoder(embs, smiles[:-1], teacher_force=True); loss = loss_fn(pred_tokens.view(-1, vocab_size), smiles[1:].view(-1)); loss.backward(); optimizer.step()
# Evaluate: reconstructed = [decoder.decode(emb, max_len=120) for emb in test_embs]; acc = sum(recon == ref for recon, ref in zip(reconstructed, test_smiles)) / len(test_smiles)
```
## Evaluation signals
- Exact-match accuracy: percentage of reconstructed SMILES strings that match reference SMILES exactly
- Tanimoto similarity: molecular fingerprint similarity between decoded and reference structures (higher indicates better reconstruction)
- RDKit parseability: all reconstructed SMILES successfully parse into valid molecule objects
- Cross-entropy loss convergence: training loss decreases monotonically and stabilizes on validation set
- Inference stability: token generation completes without out-of-vocabulary errors or early termination
## Limitations
- Decoder relies on quality of upstream encoder embeddings; poor encoder embeddings cannot be recovered by attention alone.
- SMILES canonicalization assumes RDKit can parse the reference dataset; non-standard or erroneous SMILES in training data will degrade learning.
- Teacher forcing during training can cause exposure bias at inference time, where the decoder sees its own predictions rather than ground-truth tokens; scheduled sampling or beam search may be needed to mitigate.
- Attention mechanism adds computational overhead during both training and inference; for very long SMILES sequences, memory and latency may become prohibitive.
- Tanimoto similarity evaluation requires molecular fingerprints; molecules with very similar scaffolds but different functional groups may show high similarity despite being distinct.
- No changelog or versioning information available in the repository for reproducibility and debugging.
## Evidence
- [other] Define the decoder architecture in PyTorch as a sequence-to-sequence model with attention: "Define the decoder architecture in PyTorch as a sequence-to-sequence model with attention, mapping fixed-size embeddings to variable-length SMILES token sequences."
- [other] Train the decoder end-to-end using cross-entropy loss on SMILES token prediction, with teacher forcing during training: "Train the decoder end-to-end using cross-entropy loss on SMILES token prediction, with teacher forcing during training."
- [other] Evaluate decoder on held-out test embeddings by measuring exact-match accuracy and Tanimoto similarity: "Evaluate decoder on held-out test embeddings by measuring exact-match accuracy and Tanimoto similarity of reconstructed vs. reference molecules (decoded via RDKit)."
- [readme] The decoder reconstructs the molecular structure, in a SMILES format, given the embedding that the encoder generates: "The decoder reconstructs the molecular structure, in a SMILES format, given the embedding that the encoder generates."
- [readme] The implementation of the Spec2Mol architecture is based on the Pytorch library: "The implementation of the Spec2Mol architecture is based on the Pytorch library."
- [readme] Processing of the chemical data is based on the RDKit software: "Processing of the chemical data is based on the RDKit software."
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!