예제 #1
0
    def test_bert_trainer(self):
        """Validate that the Keras object can be created."""
        # Build a transformer network to use within the BERT trainer.
        vocab_size = 100
        sequence_length = 512
        test_network = networks.TransformerEncoder(
            vocab_size=vocab_size,
            num_layers=2,
            sequence_length=sequence_length)

        # Create a BERT trainer with the created network.
        bert_trainer_model = bert_span_labeler.BertSpanLabeler(test_network)

        # Create a set of 2-dimensional inputs (the first dimension is implicit).
        word_ids = tf.keras.Input(shape=(sequence_length, ), dtype=tf.int32)
        mask = tf.keras.Input(shape=(sequence_length, ), dtype=tf.int32)
        type_ids = tf.keras.Input(shape=(sequence_length, ), dtype=tf.int32)

        # Invoke the trainer model on the inputs. This causes the layer to be built.
        cls_outs = bert_trainer_model([word_ids, mask, type_ids])

        # Validate that there are 2 outputs are of the expected shape.
        self.assertEqual(2, len(cls_outs))
        expected_shape = [None, sequence_length]
        for out in cls_outs:
            self.assertAllEqual(expected_shape, out.shape.as_list())
예제 #2
0
  def test_bert_trainer(self):
    """Validate that the Keras object can be created."""
    # Build a transformer network to use within the BERT trainer.
    vocab_size = 100
    sequence_length = 512
    test_network = networks.TransformerEncoder(
        vocab_size=vocab_size, num_layers=2, sequence_length=sequence_length)

    # Create a BERT trainer with the created network.
    num_classes = 3
    num_token_predictions = 2
    bert_trainer_model = bert_pretrainer.BertPretrainer(
        test_network,
        num_classes=num_classes,
        num_token_predictions=num_token_predictions)

    # Create a set of 2-dimensional inputs (the first dimension is implicit).
    word_ids = tf.keras.Input(shape=(sequence_length,), dtype=tf.int32)
    mask = tf.keras.Input(shape=(sequence_length,), dtype=tf.int32)
    type_ids = tf.keras.Input(shape=(sequence_length,), dtype=tf.int32)
    lm_mask = tf.keras.Input(shape=(sequence_length,), dtype=tf.int32)

    # Invoke the trainer model on the inputs. This causes the layer to be built.
    lm_outs, cls_outs = bert_trainer_model([word_ids, mask, type_ids, lm_mask])

    # Validate that the outputs are of the expected shape.
    expected_lm_shape = [None, num_token_predictions, vocab_size]
    expected_classification_shape = [None, num_classes]
    self.assertAllEqual(expected_lm_shape, lm_outs.shape.as_list())
    self.assertAllEqual(expected_classification_shape, cls_outs.shape.as_list())
예제 #3
0
  def create_lm_model(self,
                      vocab_size,
                      sequence_length,
                      hidden_size,
                      num_predictions,
                      output="predictions"):
    # First, create a transformer stack that we can use to get the LM's
    # vocabulary weight.
    xformer_stack = networks.TransformerEncoder(
        vocab_size=vocab_size,
        num_layers=1,
        sequence_length=sequence_length,
        hidden_size=hidden_size,
        num_attention_heads=4,
    )
    word_ids = tf.keras.Input(shape=(sequence_length,), dtype=tf.int32)
    mask = tf.keras.Input(shape=(sequence_length,), dtype=tf.int32)
    type_ids = tf.keras.Input(shape=(sequence_length,), dtype=tf.int32)
    lm_outputs, _ = xformer_stack([word_ids, mask, type_ids])

    # Create a maskedLM from the transformer stack.
    test_network = networks.MaskedLM(
        num_predictions=num_predictions,
        input_width=lm_outputs.shape[-1],
        source_network=xformer_stack,
        output=output)

    # Create a model from the masked LM layer.
    lm_input_tensor = tf.keras.Input(shape=(sequence_length, hidden_size))
    masked_lm_positions = tf.keras.Input(
        shape=(num_predictions,), dtype=tf.int32)
    output = test_network([lm_input_tensor, masked_lm_positions])
    return tf.keras.Model([lm_input_tensor, masked_lm_positions], output)
예제 #4
0
    def test_bert_trainer_tensor_call(self):
        """Validate that the Keras object can be invoked."""
        # Build a transformer network to use within the BERT trainer. (Here, we use
        # a short sequence_length for convenience.)
        test_network = networks.TransformerEncoder(vocab_size=100,
                                                   num_layers=2,
                                                   sequence_length=2)

        # Create a BERT trainer with the created network.
        bert_trainer_model = bert_span_labeler.BertSpanLabeler(test_network)

        # Create a set of 2-dimensional data tensors to feed into the model.
        word_ids = tf.constant([[1, 1], [2, 2]], dtype=tf.int32)
        mask = tf.constant([[1, 1], [1, 0]], dtype=tf.int32)
        type_ids = tf.constant([[1, 1], [2, 2]], dtype=tf.int32)

        # Invoke the trainer model on the tensors. In Eager mode, this does the
        # actual calculation. (We can't validate the outputs, since the network is
        # too complex: this simply ensures we're not hitting runtime errors.)
        _ = bert_trainer_model([word_ids, mask, type_ids])
예제 #5
0
    def test_bert_trainer_named_compilation(self):
        """Validate compilation using explicit output names."""
        # Build a transformer network to use within the BERT trainer.
        vocab_size = 100
        sequence_length = 512
        test_network = networks.TransformerEncoder(
            vocab_size=vocab_size,
            num_layers=2,
            sequence_length=sequence_length)

        # Create a BERT trainer with the created network.
        bert_trainer_model = bert_span_labeler.BertSpanLabeler(test_network)

        # Attempt to compile the model using a string-keyed dict of output names to
        # loss functions. This will validate that the outputs are named as we
        # expect.
        bert_trainer_model.compile(optimizer='sgd',
                                   loss={
                                       'start_positions': 'mse',
                                       'end_positions': 'mse'
                                   })
예제 #6
0
  def test_serialize_deserialize(self):
    """Validate that the BERT trainer can be serialized and deserialized."""
    # Build a transformer network to use within the BERT trainer. (Here, we use
    # a short sequence_length for convenience.)
    test_network = networks.TransformerEncoder(
        vocab_size=100, num_layers=2, sequence_length=5)

    # Create a BERT trainer with the created network. (Note that all the args
    # are different, so we can catch any serialization mismatches.)
    bert_trainer_model = bert_pretrainer.BertPretrainer(
        test_network, num_classes=4, num_token_predictions=3)

    # Create another BERT trainer via serialization and deserialization.
    config = bert_trainer_model.get_config()
    new_bert_trainer_model = bert_pretrainer.BertPretrainer.from_config(config)

    # Validate that the config can be forced to JSON.
    _ = new_bert_trainer_model.to_json()

    # If the serialization was successful, the new config should match the old.
    self.assertAllEqual(bert_trainer_model.get_config(),
                        new_bert_trainer_model.get_config())
예제 #7
0
def _create_bert_model(cfg):
    """Creates a BERT keras core model from BERT configuration.

  Args:
    cfg: A `BertConfig` to create the core model.
  Returns:
    A keras model.
  """
    bert_encoder = networks.TransformerEncoder(
        vocab_size=cfg.vocab_size,
        hidden_size=cfg.hidden_size,
        num_layers=cfg.num_hidden_layers,
        num_attention_heads=cfg.num_attention_heads,
        intermediate_size=cfg.intermediate_size,
        activation=activations.gelu,
        dropout_rate=cfg.hidden_dropout_prob,
        attention_dropout_rate=cfg.attention_probs_dropout_prob,
        sequence_length=cfg.max_position_embeddings,
        type_vocab_size=cfg.type_vocab_size,
        initializer=tf.keras.initializers.TruncatedNormal(
            stddev=cfg.initializer_range))

    return bert_encoder