beam_search
Author: Heli Qi Affiliation: NAIST Date: 2022.07
Modified from https://github.com/huggingface/transformers/blob/518bd02c9b71291333ef374f055a4d1ac3042654/src/transformers/generation_beam_search.py
BeamHypotheses
Bases: object
Beam Hypothesis Container.
Source code in speechain/infer_func/beam_search.py
__init__(beam_size, max_length, length_penalty)
Initialize n-best list of hypotheses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
beam_size
|
int
|
int The number of beams used in this container |
required |
max_length
|
int
|
int The maximal length of the generated hypotheses |
required |
length_penalty
|
float
|
float The penalty you put on the hypothesis lengths. The larger length_penalty is, the longer your hypotheses will be. length_penalty=1 means no penalty on lengths. |
required |
Source code in speechain/infer_func/beam_search.py
__len__()
add(hyp, sum_logprobs)
Add a new hypothesis to the container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hyp
|
Tensor
|
(hypo_len,) The generated hypothesis transcript. |
required |
sum_logprobs
|
float
|
float The sum of log probability of each token prediction in the hypothesis. |
required |
Source code in speechain/infer_func/beam_search.py
is_done(best_sum_logprobs, curr_len=None)
Whether the beam searching of this container is done or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
best_sum_logprobs
|
float
|
float The best log-prob sum we get in the current time step |
required |
curr_len
|
int
|
int The length of the current input hypothesis |
None
|
Returns:
Type | Description |
---|---|
A flag that indicates whether the beam searching of this container is done. |
|
True means the container already has 'beam_size' hypotheses and the current hypothesis is not better than anyone of them. |
|
False means either the container has some empty beams or the current input hypothesis is better than the worst hypothesis. |
Source code in speechain/infer_func/beam_search.py
beam_searching(enc_feat, enc_feat_mask, asr_decode_fn, vocab_size, sos_eos=None, padding_idx=0, beam_size=1, min_f2t_ratio=3.0, length_penalty=1.0, temperature=1.0, eos_filtering=False, eos_threshold=1.5, ctc_weight=0.0, ctc_decode_fn=None, ctc_temperature=1.0, lm_weight=0.2, lm_temperature=1.0, lm_decode_fn=None, lm_window_size=None, ilm_sub_weight=0.0, sent_per_beam=1)
Batch version of beam searching to enable parallel computation. The basic idea is reshaping batch_size sentences into (batch_size * beam_size) sentences.
However, the hypothesis text probabilities calculated by a batch of inputs and a single input are slightly different due to the model accuracy. in rare cases, the best hypothesis with the highest probability may be different when the beam searching process is performed in the batch level.
Therefore, batch-level beam searching is mainly used to speed up the pseudo text generation. For model visualization during training or evaluation after training, we recommend you to perform the beam searching for each single input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
enc_feat
|
Tensor
|
(batch_size, feat_maxlen, enc_dim) The final hidden representations from the encoder. |
required |
enc_feat_mask
|
Tensor
|
(batch_size, 1, feat_maxlen) The masks for the encoder representations. |
required |
asr_decode_fn
|
The function that decodes the hypothesis for one time step and get the next prediction. |
required | |
vocab_size
|
int
|
int The number of tokens in the vocabulary dictionary |
required |
sos_eos
|
int
|
int
The index of the |
None
|
padding_idx
|
int
|
int The index of the padding token. |
0
|
beam_size
|
int
|
int The number of beams used for each hypothesis sentence. |
1
|
min_f2t_ratio
|
float or int
|
float The ratio of the hypothesis max length to encoder representation length (feat_maxlen). Postive values mean the relative ratio. Negative values mean the absolute max length of the hypothesis sentence. |
3.0
|
length_penalty
|
float
|
float The hypothesis score is divided by its length to prevent the short hypothesis. length_penalty is the penalty on the hypothesis length. The larger the value is, the longer hypothesis you will get. |
1.0
|
temperature
|
float
|
float The temperature coefficient used for calculating the log-softmax probability for the ASR decoder. The higher temperature is (>1), the more even token probability distribution you will get. Vice versa for the lower temperature (<1). Usually, raising the temperature up helps ASR in better decoding. The temperature value needs to be tuned on your validation sets. The common practice is to start from 1.5 and search other values in (1.0, 2.0). |
1.0
|
eos_filtering
|
bool
|
bool Controls whether the eos filtering is performed. If True, the algorithm will only emit an eos when the eos probability is larger than some times the maximum probability of the other tokens. This function is default to be off since it may reversely aggravate the n-gram phrase repeating problem. It's better to turn it on only when your model suffers from meeting eos very early on many testing cases. reference: 'Sequence-to-Sequence Speech Recognition with Time-Depth Separable Convolutions' Section 3.1.2 in https://arxiv.org/pdf/1904.02619 |
False
|
eos_threshold
|
float
|
float The eos filtering threshold used for eos filtering. This threshold will be multiplied with the maximum probability of the other tokens when deciding whether to emit an eos. The larger this threshold is (>1), the easier the hypothesis emits an eos. Vice versa for the smaller temperature (<1). The default value 1.5 comes from the reference paper above. |
1.5
|
ctc_weight
|
float
|
float = 0.0 The weight putted on the CTC scores at each decoding step. |
0.0
|
ctc_decode_fn
|
= None The CTC forward function for decoding the encoder hidden features. |
None
|
|
lm_weight
|
float
|
float = 0.0 The weight putted on the LM scores at each decoding step. |
0.2
|
lm_temperature
|
float
|
float = 1.0 The temperature coefficient used for calculating the log-softmax probability for the LM decoder. |
1.0
|
lm_decode_fn
|
LM
|
LM = None The LM forward function for decoding the current partial hypothesis. |
None
|
ilm_sub_weight
|
float
|
float = 0.0 The weight putted on the subtraction of the inner LM of the ASR model. The inner LM subtraction is used for ASR-LM decoding. ilm_sub_weight is better to be tuned in [0.5*lm_weight, lm_weight]. |
0.0
|
sent_per_beam
|
int
|
int The number of sentences in each beam that are returned in this function. sent_per_beam > 1 is mainly used for data augmentation (under development). |
1
|
Source code in speechain/infer_func/beam_search.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
|