cross_entropy
Author: Heli Qi Affiliation: NAIST Date: 2022.07
CrossEntropy
Bases: Criterion
This criterion calculates the cross entropy between model predictions and target labels.
In this implementation, we realize the following functions: 1. Sentence normalization. The loss will be normalized according to the length of each sentence. 2. Label smoothing. The target label will be transformed from a sharp one-hot vector to a smooth distribution vector. 3. Token reweighting. The weight of each token in the cross entropy calculation can be customized manually. If you want to customize the weights, you need to give the token dictionary.
Source code in speechain/criterion/cross_entropy.py
17 18 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 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 |
|
__call__(logits, text, text_len)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Tensor
|
(batch, text_maxlen, vocab_size) The model predictions for the text |
required |
text
|
Tensor
|
(batch, text_maxlen) The target text labels. |
required |
text_len
|
Tensor
|
(batch,) The text lengths |
required |
Returns:
Type | Description |
---|---|
The cross entropy between logits and text |
Source code in speechain/criterion/cross_entropy.py
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 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 |
|
criterion_init(length_normalized=False, label_smoothing=0.0, temperature=1.0, confid_threshold=0.0, confid_level='sentence', token_vocab=None, new_weights=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length_normalized
|
bool
|
bool Controls whether the sentence normalization is performed. |
False
|
label_smoothing
|
float
|
float Controls the scale of label smoothing. 0 means no smoothing. |
0.0
|
temperature
|
float
|
float Controls the temperature of the Softmax operation. |
1.0
|
confid_threshold
|
float
|
float Controls whether to ignore the prediction lower than the threshold for loss calculation. |
0.0
|
confid_level
|
str
|
str The level of confidence calculation. Either 'token' (token_level confidence) or 'sent' (sentence-level confidence). Default to be 'sentence'. |
'sentence'
|
token_vocab
|
str
|
str The path of the given token vocabulary list. Necessary if new_weights is not None. |
None
|
new_weights
|
Dict
|
Dict The customized token weights to calculate the cross entropy. Must be given in the format below: 'new_weights: token1: weight1 token2: weight2 ...' |
None
|