Decoding utils
beam_decoding(model, tgt_tokenizer, src_tokens, src_mask, max_target_tokens=128, output_mode='str')
¶
TBA
Source code in src/tfs_mt/decoding_utils.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
greedy_decoding(model, tgt_tokenizer, src_tokens, src_mask, max_target_tokens=128, output_mode='str')
¶
Supports batch (decode multiple source sentences) greedy decoding.
Example
We input <s> and do a forward pass. We get intermediate activations for <s> and at the output at position
0, after the doing linear layer we get e.g. token <I>. Now we input <s>,<I> but <s>'s activations will remain
the same. Similarly say we now got <am> at output position 1, in the next step we input <s>,<I>,<am> and so <I>'s
activations will remain the same as it only looks at/attends to itself and to <s> and so forth.
Note
Decoding could be further optimized to cache old token activations because they can't look ahead and so adding a newly predicted token won't change old token's activations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
|
Encoder-decoder translation model. |
required |
tgt_tokenizer
|
|
Target text tokenizer. |
required |
src_tokens
|
|
Source tokens. |
required |
src_mask
|
|
Source tokens mask. |
required |
max_target_tokens
|
|
Max target tokens to output. Defaults to 128. |
128
|
output_mode
|
|
Output mode, if |
'str'
|
Returns:
| Type | Description |
|---|---|
|
list[str] | list[list[str]]: Decoded sequences as strings or as list of tokens. |
Source code in src/tfs_mt/decoding_utils.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 | |