MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / get_predictions

Function get_predictions

demo/BERT/helpers/data_processing.py:334–497  ·  view source on GitHub ↗
(doc_tokens, features, results, n_best_size, max_answer_length)

Source from the content-addressed store, hash-verified

332
333
334def get_predictions(doc_tokens, features, results, n_best_size, max_answer_length):
335 _PrelimPrediction = collections.namedtuple( # pylint: disable=invalid-name
336 "PrelimPrediction",
337 ["feature_index", "start_index", "end_index", "start_logit", "end_logit"])
338
339 prediction = ""
340 scores_diff_json = 0.0
341
342 prelim_predictions = []
343 # keep track of the minimum score of null start+end of position 0
344 score_null = 1000000 # large and positive
345 min_null_feature_index = 0 # the paragraph slice with min mull score
346 null_start_logit = 0 # the start logit at the slice with min null score
347 null_end_logit = 0 # the end logit at the slice with min null score
348 version_2_with_negative = False
349
350 for result in results:
351 start_indexes = _get_best_indexes(result.start_logits, n_best_size)
352 end_indexes = _get_best_indexes(result.end_logits, n_best_size)
353 feature = features[result.feature_index]
354
355 # if we could have irrelevant answers, get the min score of irrelevant
356 if version_2_with_negative:
357 feature_null_score = result.start_logits[0] + result.end_logits[0]
358 if feature_null_score < score_null:
359 score_null = feature_null_score
360 min_null_feature_index = 0
361 null_start_logit = result.start_logits[0]
362 null_end_logit = result.end_logits[0]
363
364 for start_index in start_indexes:
365 for end_index in end_indexes:
366 # We could hypothetically create invalid predictions, e.g., predict
367 # that the start of the span is in the question. We throw out all
368 # invalid predictions.
369 if start_index >= len(feature.tokens):
370 continue
371 if end_index >= len(feature.tokens):
372 continue
373 if start_index not in feature.token_to_orig_map:
374 continue
375 if end_index not in feature.token_to_orig_map:
376 continue
377 if not feature.token_is_max_context.get(start_index, False):
378 continue
379 if end_index < start_index:
380 continue
381 length = end_index - start_index + 1
382 if length > max_answer_length:
383 continue
384 prelim_predictions.append(
385 _PrelimPrediction(
386 feature_index=result.feature_index,
387 start_index=start_index,
388 end_index=end_index,
389 start_logit=result.start_logits[start_index],
390 end_logit=result.end_logits[end_index]))
391

Callers

nothing calls this directly

Calls 5

_get_best_indexesFunction · 0.85
get_final_textFunction · 0.85
_compute_softmaxFunction · 0.85
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected