Multinomial diffusion
DiffusionLoss(model)
Bases: Module
Holds logic for calculating the diffusion loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
MultinomialDiffusion
|
The multinomial diffusion class. |
required |
Source code in instanovo/diffusion/multinomial_diffusion.py
forward(x_0, **kwargs)
Calculate a single Monte Carlo estimate of the multinomial diffusion loss (-ELBO).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_0 |
LongTensor[batch_size, sequence_length]
|
A batch of padded sequences. |
required |
Returns:
Type | Description |
---|---|
FloatTensor
|
torch.FloatTensor[1]: The loss estimate. |
Source code in instanovo/diffusion/multinomial_diffusion.py
kl_divergence(log_probs_first, log_probs_second)
staticmethod
Calculate the Kullback-Liebler divergence between two multinomial distributions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_probs_first |
FloatTensor[..., num_classes]
|
The log-probabilities of the base distribution. |
required |
log_probs_second |
FloatTensor[..., num_classes]
|
The log-probabilities of the comparison distribution. |
required |
Returns:
Type | Description |
---|---|
FloatTensor
|
torch.FloatTensor[1]: The KL-divergence averaged over all but the final dimension. |
Source code in instanovo/diffusion/multinomial_diffusion.py
MultinomialDiffusion(config, transition_model, diffusion_schedule, residues)
Bases: Module
This class implements Multinomial Diffusion as described in Hoogeboom et al. 2021.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
DictConfig
|
The model configuration. This should have keys:
- 'name': the model name identifier.
- 'time_steps': the number of time steps in the diffusion process
- 'max_length': the maximum sequence for the model
- 'device': the device where the This information is necessary for saving and loading the model. |
required |
transition_model |
Module
|
The model that predictions the initial sequence given the sequence sampled the current time step and the sequence sampled the previous time step. This is just a sequence tagging model. |
required |
diffusion_schedule |
FloatTensor[time_steps]
|
The sequence of diffusion probabilities. Note
that |
required |
residues |
ResidueSet
|
The residue vocabulary. This holds a mapping between residues and indices and residue masses. |
required |
Source code in instanovo/diffusion/multinomial_diffusion.py
forward(log_x_t, log_x_0, t)
Calculate the log-posterior of the t-1
-th process values given the 0-th and t-th values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_x_t |
FloatTensor[batch_size, sequence_length, num_classes]
|
The log one-hot representation of the process values at the |
required |
log_x_0 |
FloatTensor[batch_size, sequence_length, num_classes]
|
The log one-hot representation of the process values at the |
required |
t |
int
|
The time step. |
required |
Returns:
Type | Description |
---|---|
FloatTensor
|
torch.FloatTensor[batch_size, sequence_length, num_classes]:
The log-posterior probabilities of the process values at the |
Source code in instanovo/diffusion/multinomial_diffusion.py
load(path)
classmethod
Load a saved model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path to the directory where the model is saved. |
required |
Returns:
Type | Description |
---|---|
MultinomialDiffusion
|
The loaded model. |
Source code in instanovo/diffusion/multinomial_diffusion.py
mixture_categorical(log_x, log_alpha, log_alpha_complement)
A categorical mixture between a base distribution and a uniform distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_x |
FloatTensor[..., num_classes]
|
The base distribution. |
required |
log_alpha |
float
|
The log of the mixture weight. |
required |
log_alpha_complement |
float
|
The log of 1 minus the mixture weight. |
required |
Returns:
Type | Description |
---|---|
FloatTensor
|
torch.FloatTensor[..., num_classes]: The log-probabilities of the mixture. |
Source code in instanovo/diffusion/multinomial_diffusion.py
prepare_fine_tuning(residues)
Prepare a model for fine-tuning on a dataset with a new residue vocabulary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
residues |
ResidueSet
|
The residue vocabulary for the new dataset. |
required |
Source code in instanovo/diffusion/multinomial_diffusion.py
reverse_distribution(x_t, time, **kwargs)
Calculate the reverse transition distribution of the diffusion process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_t |
FloatTensor[batch_size, sequence_length]
|
The values at the |
required |
time |
int
|
The time step. |
required |
Returns:
Type | Description |
---|---|
FloatTensor
|
torch.FloatTensor[batch_size, sequence_length, num_classes]:
The log-probabilities of values for the |
Source code in instanovo/diffusion/multinomial_diffusion.py
save(path, overwrite=False)
Save the model to a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path to the base directory where the model is saved. The model is saved in a subdirectory with the model's name identifier. |
required |
overwrite |
bool
|
Whether to overwrite the directory if one already exists for the model. Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
FileExistsError
|
If |
Source code in instanovo/diffusion/multinomial_diffusion.py
cosine_beta_schedule(timesteps, s=0.008)
Cosine schedule as proposed in https://arxiv.org/abs/2102.09672 .
Returns alpha parameters, NOT Beta