> ## Documentation Index
> Fetch the complete documentation index at: https://proto.evodesign.org/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Gradient-Guided Mutation Generator

> Substitutions sampled by first-order gradient estimate of the energy change

![Gradient-Guided Mutation Generator](https://proto-bio.github.io/proto-assets/images/generator/gradient-guided-mutation/hero.png)

This generator is open source. Any third-party models, product names, or trademarks referenced are the property of their respective owners, and Proto is not affiliated with them.

***

&#x20;Source

![Proto](https://mintcdn.com/bio-pro/cMLt8hAqaP9M3LKy/assets/logo/logo-light.svg?fit=max\&auto=format\&n=cMLt8hAqaP9M3LKy\&q=85\&s=f5e5dfd1b8f9b9db3a0be0e2eb01f678)

evo-design/proto-language

&#x20;View source

[Open](https://github.com/evo-design/proto-language/blob/1a97e675c7e5e4ae0e92be91b1bb7b49f1331104/proto_language/generator/gradient_guided_mutation_generator.py#L70)

Generator contributors[![](https://avatars.githubusercontent.com/u/61667248?v=4\&s=64)adititm](https://github.com/adititm)[![](https://avatars.githubusercontent.com/u/21143637?v=4\&s=64)bviggiano](https://github.com/bviggiano)
Sample substitutions ranked by a first-order estimate of their energy change.

For each proposal the current sequence is written as one-hot logits `(L, V)`
and the stage's differentiable constraints (compiled with
`compile_gradient_providers`) return the weighted gradient
`d(energy)/d(logits)`. Substituting `new` at position `i` is estimated
to change the energy by `grad[i, new] - grad[i, current]`. `masking_strategy`
sets the substitution count and fixed positions; that many substitutions at
distinct free positions are drawn from a softmax over
`-estimate / (relative_temperature * std)`, where `std` is the spread of the estimates
over the allowed substitutions, so the draw does not depend on the energy's scale. The draw
is uniform when the spread is at float32 rounding level relative to the largest gradient
entry, which includes every estimate being equal. The owning optimizer supplies its
constraints via `_bind_constraints`; non-differentiable ones are ignored.

Unlike the `temperature` of the other generators, which divides a model's log-probabilities,
and of `MCMCOptimizer`, which divides raw energy differences, `relative_temperature` is in
units of the estimates' spread. Constraint energies are weighted `[0, 1]` scores, not
log-probabilities, and their per-substitution estimates span orders of magnitude across
constraints, so no single raw-unit default suits them. Gibbs-with-gradients samplers divide
raw estimates instead (Grathwohl et al., 2021).

## API Reference

ConfigGradientGuidedMutationGeneratorConfig[ Source](https://github.com/evo-design/proto-language/blob/1a97e675c7e5e4ae0e92be91b1bb7b49f1331104/proto_language/generator/gradient_guided_mutation_generator.py#L36)

Configuration for gradient-guided substitution sampling.

**masking\_strategy** `MaskingStrategy`

Mutation count and fixed positions; the gradient picks which free positions change.

**relative\_temperature** `number` (default: 1.0)

Softmax temperature over -estimated energy change, in units of its std; lower is greedier.

## Usage

```python python icon="python" theme={null}
>>> from proto_language.core import Segment
>>> from proto_tools.transforms.masking import MaskingStrategy
>>> gen = GradientGuidedMutationGenerator(
...     GradientGuidedMutationGeneratorConfig(masking_strategy=MaskingStrategy(num_mutations=2))
... )
>>> gen.assign(Segment(sequence="ACDEF", sequence_type="protein"))
>>> # An optimizer binds its constraints, then calls gen.sample()
```

## Metadata

| Property | Value |
| - | - |
| Key | `gradient-guided-mutation` |
| Class | `GradientGuidedMutationGenerator` |
| Category | `mutation` |
| Input Type | `starting_sequence` |
| Uses GPU | `False` |
| Supported Sequence Types | `dna`, `rna`, `protein` |
| Allows Empty Start | `False` |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.