Beam search
BeamSearchDecoder(model, mass_scale=MASS_SCALE)
Bases: Decoder
A class for decoding from de novo sequence models using beam search.
This class conforms to the Decoder
interface and decodes from
models that conform to the Decodable
interface.
Source code in instanovo/inference/beam_search.py
decode(spectra, precursors, beam_size, max_length, mass_tolerance=5e-05, max_isotope=1, return_all_beams=False)
Decode predicted residue sequence for a batch of spectra using beam search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spectra |
FloatTensor
|
The spectra to be sequenced. |
required |
precursors |
torch.FloatTensor[batch size, 3]
|
The precursor mass, charge and mass-to-charge ratio. |
required |
beam_size |
int
|
The maximum size of the beam. |
required |
max_length |
int
|
The maximum length of a residue sequence. |
required |
mass_tolerance |
float
|
The maximum relative error for which a predicted sequence is still considered to have matched the precursor mass. |
5e-05
|
max_isotope |
int
|
The maximum number of additional neutrons for isotopes whose mass a predicted sequence's mass is considered when comparing to the precursor mass. All additional nucleon numbers from 1 to |
1
|
return_all_beams |
bool
|
Optionally return all beam-search results, not only the best beam. |
False
|
Returns:
Type | Description |
---|---|
list[Any]
|
list[list[str]]: The predicted sequence as a list of residue tokens. This method will return an empty list for each spectrum in the batch where decoding fails i.e. no sequence that fits the precursor mass to within a tolerance is found. |
Source code in instanovo/inference/beam_search.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
expand_candidates(beam_state, residue_masses)
Calculate log probabilities for all candidate next tokens for all sequences in the current beam.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
beam_state |
BeamState
|
The current beam state |
required |
Returns:
Name | Type | Description |
---|---|---|
Result |
torch.FloatTensor [beam size, vocabulary size]
|
|
FloatTensor
|
The tensor of log probabilities on the candidate next tokens for |
|
FloatTensor
|
each sequence in the beam for each spectrum in the batch. |
|
FloatTensor
|
Result[i, j, k] is the log probability of token |
|
FloatTensor
|
being the next token given sequence |
|
FloatTensor
|
|
|
FloatTensor
|
|
Source code in instanovo/inference/beam_search.py
filter_items(beam_state, log_probabilities, beam_size, remaining_masses, mass_buffer, max_isotope)
Separate and prune incomplete and complete sequences.
Separate candidate residues into those that lead to incomplete sequences and those that lead
to complete sequences. Prune the ones leading to incomplete sequences down to the top beam_size
and simply return the ones leading to complete sequences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
beam_state |
BeamState
|
The current beam state. |
required |
log_probabilities |
torch.FloatTensor[batch size, beam size, number of residues]
|
The candidate log probabilities for each item on the beam and each potential next residue for batch spectrum in the batch. |
required |
beam_size |
int
|
The maximum size of the beam. |
required |
remaining_masses |
torch.FloatTensor[number of residues]
|
The masses of the residues in the vocabulary as integers in units of the mass scale. |
required |
mass_buffer |
torch.FloatTensor[batch size]
|
The maximum absolute difference between the batch precursor masses and the theoretical masses of their predicted sequences. |
required |
Returns:
Type | Description |
---|---|
tuple[list[list[ScoredSequence]], BeamState]
|
tuple[list[ScoredSequence], BeamState]: A (potentially empty) list of completed sequences and the next beam state resulting from pruning incomplete sequences. |
Source code in instanovo/inference/beam_search.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
init_beam(spectra, precursor_mass_charge, residue_masses, mass_buffers, beam_size)
Construct the initial beam state.
This means precomputing the spectrum embeddings and adding the first set of candidate tokens to the beam.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spectra |
torch.FloatTensor[]
|
The spectra to be sequenced. |
required |
precursor_mass_charge |
torch.FloatTensor[batch size, 3]
|
The precursor mass, charge and mass-to-charge ratio. |
required |
residue_masses |
torch.LongTensor[]
|
The masses of the residues in the vocabulary as integers in units of the mass scale. |
required |
beam_size |
int
|
The maximum size of the beam. |
required |
Returns:
Name | Type | Description |
---|---|---|
BeamState |
BeamState
|
The initial beam state. |
Source code in instanovo/inference/beam_search.py
prefilter_items(log_probabilities, remaining_masses, beam_masses, mass_buffer, max_isotope)
Filter illegal next token by setting the corresponding log probabilities to -inf
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_probabilities |
torch.FloatTensor[batch size, beam size, number of residues]
|
The candidate log probabilities for each item on the beam and each potential next residue for batch spectrum in the batch. |
required |
remaining_masses |
torch.LongTensor[batch size, beam size]
|
|
required |
mass_buffer |
torch.LongTensor[batch size, 1, 1]
|
description |
required |
Returns:
Type | Description |
---|---|
FloatTensor
|
torch.FloatTensor: description |
Source code in instanovo/inference/beam_search.py
unravel_index(indices, outer_dim)
staticmethod
Get row and column coordinates for indices on a pair of dimensions that have been flattened.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indices |
LongTensor
|
The flattened indices to unravel |
required |
outer_dim |
int
|
The outermost dimension of the pair that has been flattened |
required |
Returns:
Type | Description |
---|---|
tuple[LongTensor, LongTensor]
|
tuple[torch.LongTensor, torch.LongTensor]: The rows and columns of the indices respectively |
Source code in instanovo/inference/beam_search.py
BeamState(sequences, log_probabilities, remaining_masses, precursor_mass_charge, spectrum_encoding, spectrum_mask)
dataclass
This class holds a specification of the beam state during beam search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequences |
torch.LongTensor[beam size, num. steps]
|
|
required |
log_probabilities |
torch.FloatTensor[beam size]
|
|
required |
remaining_masses |
torch.LongTensor[beam size]
|
|
required |
precursor_mass_charge |
torch.FloatTensor[beam size, 3]
|
|
required |
spectrum_encoding |
torch.FloatTensor[batch size, sequence length, hidden dim]
|
|
required |
spectrum_mask |
torch.BoolTensor[batch size, sequence length]
|
|
required |
is_empty()
Check whether the beam is empty.
Source code in instanovo/inference/beam_search.py
ScoredSequence(sequence, mass_error, log_probability)
dataclass
This class holds a residue sequence and its log probability.