Download src/train/utils.py from joelinator/dflow-novo-model: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/joelinator/dflow-novo-model/resolve/main/src/train/utils.py
- Command line
-
hf download hf://joelinator/dflow-novo-model/src/train/utils.py
-
curl -L -o utils.py https://huggingface.co/joelinator/dflow-novo-model/resolve/main/src/train/utils.py
1.08 kB
| import random | |
| import torch | |
| from data.lengths import MAX_PEPTIDE_LENGTH, MIN_PEPTIDE_LENGTH | |
| def length_noiser( | |
| batch_lengths: torch.Tensor, | |
| noising_prob: float = 0.1, | |
| max_: int = MAX_PEPTIDE_LENGTH, | |
| min_: int = MIN_PEPTIDE_LENGTH, | |
| ) -> tuple[torch.Tensor, bool]: | |
| """ | |
| Perturb batch peptide lengths by +/-1 with probability `noising_prob`. | |
| Args: | |
| batch_lengths: Tensor of peptide lengths (B,). | |
| noising_prob: Probability of perturbing lengths. | |
| max_: Maximum allowed peptide length. | |
| min_: Minimum allowed peptide length. | |
| Returns: | |
| (noised_lengths, is_noised) | |
| """ | |
| r = random.random() | |
| if r <= noising_prob / 2.0: | |
| noised = (batch_lengths - 1).clamp(min=min_, max=max_) | |
| is_noised = bool((noised != batch_lengths).any().item()) | |
| return noised, is_noised | |
| elif r <= noising_prob: | |
| noised = (batch_lengths + 1).clamp(min=min_, max=max_) | |
| is_noised = bool((noised != batch_lengths).any().item()) | |
| return noised, is_noised | |
| return batch_lengths, False | |