MCPcopy Create free account
hub / github.com/modelscope/FunASR / nll

Method nll

funasr/models/ct_transformer/model.py:192–260  ·  view source on GitHub ↗

Compute negative log likelihood(nll) Normally, this function is called in batchify_nll. Args: text: (Batch, Length) punc: (Batch, Length) text_lengths: (Batch,) max_lengths: int

(
        self,
        text: torch.Tensor,
        punc: torch.Tensor,
        text_lengths: torch.Tensor,
        punc_lengths: torch.Tensor,
        max_length: Optional[int] = None,
        vad_indexes: Optional[torch.Tensor] = None,
        vad_indexes_lengths: Optional[torch.Tensor] = None,
    )

Source from the content-addressed store, hash-verified

190 return logp, state_list
191
192 def nll(
193 self,
194 text: torch.Tensor,
195 punc: torch.Tensor,
196 text_lengths: torch.Tensor,
197 punc_lengths: torch.Tensor,
198 max_length: Optional[int] = None,
199 vad_indexes: Optional[torch.Tensor] = None,
200 vad_indexes_lengths: Optional[torch.Tensor] = None,
201 ) -> Tuple[torch.Tensor, torch.Tensor]:
202 """Compute negative log likelihood(nll)
203
204 Normally, this function is called in batchify_nll.
205 Args:
206 text: (Batch, Length)
207 punc: (Batch, Length)
208 text_lengths: (Batch,)
209 max_lengths: int
210 """
211 batch_size = text.size(0)
212 # For data parallel
213 if max_length is None:
214 text = text[:, : text_lengths.max()]
215 punc = punc[:, : text_lengths.max()]
216 else:
217 text = text[:, :max_length]
218 punc = punc[:, :max_length]
219
220 if self.with_vad():
221 # Should be VadRealtimeTransformer
222 assert vad_indexes is not None
223 y, _ = self.punc_forward(text, text_lengths, vad_indexes)
224 else:
225 # Should be TargetDelayTransformer,
226 y, _ = self.punc_forward(text, text_lengths)
227
228 # Calc negative log likelihood
229 # nll: (BxL,)
230 if self.training == False:
231 _, indices = y.view(-1, y.shape[-1]).topk(1, dim=1)
232 from sklearn.metrics import f1_score
233
234 f1_score = f1_score(
235 punc.view(-1).detach().cpu().numpy(),
236 indices.squeeze(-1).detach().cpu().numpy(),
237 average="micro",
238 )
239 nll = torch.Tensor([f1_score]).repeat(text_lengths.sum())
240 return nll, text_lengths
241 else:
242 self.punc_weight = self.punc_weight.to(punc.device)
243 nll = F.cross_entropy(
244 y.view(-1, y.shape[-1]),
245 punc.view(-1),
246 self.punc_weight,
247 reduction="none",
248 ignore_index=self.ignore_id,
249 )

Callers 1

forwardMethod · 0.95

Calls 3

with_vadMethod · 0.95
punc_forwardMethod · 0.95
make_pad_maskFunction · 0.90

Tested by

no test coverage detected