MCPcopy Create free account
hub / github.com/google-research/language / BertInputs

Class BertInputs

language/conpono/reconstruct/preprocess.py:431–474  ·  view source on GitHub ↗

A namedtuple that groups together all BERT inputs.

Source from the content-addressed store, hash-verified

429
430
431class BertInputs(collections.namedtuple("BertInputs", BERT_INPUTS_LIST)):
432 """A namedtuple that groups together all BERT inputs."""
433
434 def pretty_print(self, tokenizer, show_padding=True):
435 """Pretty-prints BERT inputs for human inspection.
436
437 NOTE: this method assumes that every attribute of BertInputs is a Numpy
438 array, not a Tensor. Also, it assumes that the attributes are NOT batched:
439 the first dimension of each array should not be a batch dimension.
440
441 Args:
442 tokenizer: an instance of language.bert.FullTokenizer.
443 show_padding: boolean indicating whether to print pad tokens.
444
445 Returns:
446 a pretty-printed unicode string.
447 """
448 # Convert IDs back to tokens.
449 tokens = tokenizer.convert_ids_to_tokens(self.input_ids)
450 target_tokens = tokenizer.convert_ids_to_tokens(self.masked_lm_ids)
451
452 # Annotate masked tokens with prediction targets.
453 for target, pos, weight in safe_zip(target_tokens, self.masked_lm_positions,
454 self.masked_lm_weights):
455 if weight != 0:
456 if weight != 1:
457 raise ValueError("Weight must be either 0 or 1.")
458 tokens[pos] += u"({})".format(target)
459
460 # Sequences to display.
461 seqs = [[" Tokens:"] + tokens,
462 ["Segment IDs:"] + list(self.segment_ids),
463 [" Input mask:"] + list(self.input_mask)]
464
465 if not show_padding:
466 max_seq_length = sum(self.input_mask)
467 for seq in seqs:
468 # +1 here, because we added an extra "Label" in front of each sequence.
469 del seq[max_seq_length + 1:]
470
471 # Format seqs into strings, so that tokens line up vertically.
472 aligned_seqs = whitespace_align_seqs(seqs)
473
474 return u"\n".join(u" ".join(seq) for seq in aligned_seqs)
475
476
477def bert_preprocess(text_segments,

Callers 3

bert_formatFunction · 0.85
bert_preprocessFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…