def test_predict(self): task_config = tagging.TaggingConfig( model=tagging.ModelConfig(encoder=self._encoder_config), train_data=self._train_data_config, class_names=["O", "B-PER", "I-PER"]) task = tagging.TaggingTask(task_config) model = task.build_model() test_data_path = os.path.join(self.get_temp_dir(), "test.tf_record") seq_length = 16 num_examples = 100 _create_fake_dataset(test_data_path, seq_length=seq_length, num_labels=len(task_config.class_names), num_examples=num_examples) test_data_config = tagging_dataloader.TaggingDataConfig( input_path=test_data_path, seq_length=seq_length, is_training=False, global_batch_size=16, drop_remainder=False, include_sentence_id=True) results = tagging.predict(task, test_data_config, model) self.assertLen(results, num_examples) self.assertLen(results[0], 3)
def bert_tagging() -> cfg.ExperimentConfig: """BERT tagging task.""" config = cfg.ExperimentConfig( task=tagging.TaggingConfig( train_data=tagging_dataloader.TaggingDataConfig(), validation_data=tagging_dataloader.TaggingDataConfig( is_training=False, drop_remainder=False)), trainer=cfg.TrainerConfig( optimizer_config=optimization.OptimizationConfig({ 'optimizer': { 'type': 'adamw', 'adamw': { 'weight_decay_rate': 0.01, 'exclude_from_weight_decay': ['LayerNorm', 'layer_norm', 'bias'], } }, 'learning_rate': { 'type': 'polynomial', 'polynomial': { 'initial_learning_rate': 8e-5, 'end_learning_rate': 0.0, } }, 'warmup': { 'type': 'polynomial' } })), restrictions=[ 'task.train_data.is_training != None', 'task.validation_data.is_training != None', ]) return config
def test_task_with_hub(self): hub_module_url = self._export_bert_tfhub() config = tagging.TaggingConfig(hub_module_url=hub_module_url, model=self._encoder_config, class_names=["O", "B-PER", "I-PER"], train_data=self._train_data_config) self._run_task(config)
def test_task_with_hub(self): hub_module_url = self._export_bert_tfhub() config = tagging.TaggingConfig(hub_module_url=hub_module_url, model=self._encoder_config, num_classes=4, train_data=self._train_data_config) self._run_task(config)
def test_tagging(self, use_v2_feature_names, output_encoder_outputs): if use_v2_feature_names: input_word_ids_field = "input_word_ids" input_type_ids_field = "input_type_ids" else: input_word_ids_field = "input_ids" input_type_ids_field = "segment_ids" hidden_size = 768 num_classes = 3 config = tagging.TaggingConfig( model=tagging.ModelConfig(encoder=encoders.EncoderConfig( bert=encoders.BertEncoderConfig(hidden_size=hidden_size, num_layers=1))), class_names=["class_0", "class_1", "class_2"]) task = tagging.TaggingTask(config) model = task.build_model() params = serving_modules.Tagging.Params( parse_sequence_length=10, use_v2_feature_names=use_v2_feature_names, output_encoder_outputs=output_encoder_outputs) export_module = serving_modules.Tagging(params=params, model=model) functions = export_module.get_inference_signatures({ "serve": "serving_default", "serve_examples": "serving_examples" }) dummy_ids = tf.ones((10, 10), dtype=tf.int32) outputs = functions["serving_default"](input_word_ids=dummy_ids, input_mask=dummy_ids, input_type_ids=dummy_ids) self.assertEqual(outputs["logits"].shape, (10, 10, num_classes)) if output_encoder_outputs: self.assertEqual(outputs["encoder_outputs"].shape, (10, 10, hidden_size)) dummy_ids = tf.ones((10, ), dtype=tf.int32) examples = _create_fake_serialized_examples({ input_word_ids_field: dummy_ids, "input_mask": dummy_ids, input_type_ids_field: dummy_ids }) outputs = functions["serving_examples"](examples) self.assertEqual(outputs["logits"].shape, (10, 10, num_classes)) if output_encoder_outputs: self.assertEqual(outputs["encoder_outputs"].shape, (10, 10, hidden_size)) with self.assertRaises(ValueError): _ = export_module.get_inference_signatures({"foo": None})
def test_task_with_fit(self): config = tagging.TaggingConfig( network=self._encoder_config, train_data=self._train_data_config, num_classes=3) task = tagging.TaggingTask(config) model = task.build_model() model = task.compile_model( model, optimizer=tf.keras.optimizers.SGD(lr=0.1), train_step=task.train_step, metrics=[tf.keras.metrics.SparseCategoricalAccuracy(name="accuracy")]) dataset = task.build_inputs(config.train_data) logs = model.fit(dataset, epochs=1, steps_per_epoch=2) self.assertIn("loss", logs.history) self.assertIn("accuracy", logs.history)
def test_tagging(self, output_encoder_outputs): hidden_size = 768 num_classes = 3 config = tagging.TaggingConfig( model=tagging.ModelConfig( encoder=encoders.EncoderConfig( bert=encoders.BertEncoderConfig( hidden_size=hidden_size, num_layers=1))), class_names=["class_0", "class_1", "class_2"]) task = tagging.TaggingTask(config) model = task.build_model() ckpt = tf.train.Checkpoint(model=model) ckpt_path = ckpt.save(self.get_temp_dir()) export_module_cls = export_savedmodel.lookup_export_module(task) serving_params = { "parse_sequence_length": 10, } params = export_module_cls.Params( **serving_params, output_encoder_outputs=output_encoder_outputs) export_module = export_module_cls(params=params, model=model) export_dir = export_savedmodel_util.export( export_module, function_keys={ "serve": "serving_default", "serve_examples": "serving_examples" }, checkpoint_path=ckpt_path, export_savedmodel_dir=self.get_temp_dir()) imported = tf.saved_model.load(export_dir) self.assertCountEqual(imported.signatures.keys(), ["serving_default", "serving_examples"]) serving_fn = imported.signatures["serving_default"] dummy_ids = tf.ones((1, 5), dtype=tf.int32) inputs = dict( input_word_ids=dummy_ids, input_mask=dummy_ids, input_type_ids=dummy_ids) outputs = serving_fn(**inputs) self.assertEqual(outputs["logits"].shape, (1, 5, num_classes)) if output_encoder_outputs: self.assertEqual(outputs["encoder_outputs"].shape, (1, 5, hidden_size))
def test_task(self): # Saves a checkpoint. encoder = encoders.instantiate_encoder_from_cfg(self._encoder_config) ckpt = tf.train.Checkpoint(encoder=encoder) saved_path = ckpt.save(self.get_temp_dir()) config = tagging.TaggingConfig(init_checkpoint=saved_path, model=self._encoder_config, train_data=self._train_data_config, class_names=["O", "B-PER", "I-PER"]) task = tagging.TaggingTask(config) model = task.build_model() metrics = task.build_metrics() dataset = task.build_inputs(config.train_data) iterator = iter(dataset) optimizer = tf.keras.optimizers.SGD(lr=0.1) task.train_step(next(iterator), model, optimizer, metrics=metrics) task.validation_step(next(iterator), model, metrics=metrics) task.initialize(model)
def test_seqeval_metrics(self): config = tagging.TaggingConfig(model=self._encoder_config, train_data=self._train_data_config, class_names=["O", "B-PER", "I-PER"]) task = tagging.TaggingTask(config) model = task.build_model() dataset = task.build_inputs(config.train_data) iterator = iter(dataset) strategy = tf.distribute.get_strategy() distributed_outputs = strategy.run(functools.partial( task.validation_step, model=model), args=(next(iterator), )) outputs = tf.nest.map_structure(strategy.experimental_local_results, distributed_outputs) aggregated = task.aggregate_logs(step_outputs=outputs) aggregated = task.aggregate_logs(state=aggregated, step_outputs=outputs) self.assertCountEqual({"f1", "precision", "recall", "accuracy"}, task.reduce_aggregated_logs(aggregated).keys())