@@ -450,6 +450,7 @@ def load_from_disk(cls, dirpath: str) -> CharacterLevelCnnModel:
450450 loaded_model ._model_default_ind = loaded_model .label_mapping [
451451 loaded_model ._parameters ["default_label" ]
452452 ]
453+ loaded_model ._compile_loss (loaded_model ._model , loaded_model .num_labels )
453454 return loaded_model
454455
455456 @staticmethod
@@ -475,6 +476,28 @@ def _argmax_threshold_layer(
475476 # matrix.
476477 return ThreshArgMaxLayer (threshold , num_labels , default_ind )
477478
479+ @staticmethod
480+ def _compile_loss (model : tf .keras .Model , num_labels : int ) -> None :
481+ """Compiles the loss for the given model and number of labels."""
482+ # Compile the model
483+ softmax_output_layer_name = model .output_names [0 ]
484+ # losses = {softmax_output_layer_name: "categorical_crossentropy"}
485+ losses = ["categorical_crossentropy" , None , None ]
486+
487+ # use f1 score metric
488+ f1_score_training = labeler_utils .F1Score (
489+ num_classes = num_labels , average = "micro"
490+ )
491+ metrics = {
492+ softmax_output_layer_name : [
493+ "categorical_crossentropy" ,
494+ "acc" ,
495+ f1_score_training ,
496+ ]
497+ }
498+
499+ model .compile (loss = losses , optimizer = "adam" , metrics = metrics )
500+
478501 def _construct_model (self ) -> None :
479502 """
480503 Construct model for the data labeler.
@@ -570,24 +593,7 @@ def _construct_model(self) -> None:
570593 final_predicted_layer (argmax_layer , self ._model .outputs [0 ]),
571594 ]
572595 self ._model = tf .keras .Model (self ._model .inputs , argmax_outputs )
573-
574- # Compile the model
575- softmax_output_layer_name = self ._model .output_names [0 ]
576- losses = {softmax_output_layer_name : "categorical_crossentropy" }
577-
578- # use f1 score metric
579- f1_score_training = labeler_utils .F1Score (
580- num_classes = num_labels , average = "micro"
581- )
582- metrics = {
583- softmax_output_layer_name : [
584- "categorical_crossentropy" ,
585- "acc" ,
586- f1_score_training ,
587- ]
588- }
589-
590- self ._model .compile (loss = losses , optimizer = "adam" , metrics = metrics )
596+ self ._compile_loss (self ._model , num_labels )
591597
592598 self ._epoch_id = 0
593599 self ._model_num_labels = num_labels
@@ -632,24 +638,7 @@ def _reconstruct_model(self) -> None:
632638 final_predicted_layer (argmax_layer , final_softmax_layer ),
633639 ]
634640 self ._model = tf .keras .Model (self ._model .inputs , argmax_outputs )
635-
636- # Compile the model
637- softmax_output_layer_name = self ._model .output_names [0 ]
638- losses = {softmax_output_layer_name : "categorical_crossentropy" }
639-
640- # use f1 score metric
641- f1_score_training = labeler_utils .F1Score (
642- num_classes = num_labels , average = "micro"
643- )
644- metrics = {
645- softmax_output_layer_name : [
646- "categorical_crossentropy" ,
647- "acc" ,
648- f1_score_training ,
649- ]
650- }
651-
652- self ._model .compile (loss = losses , optimizer = "adam" , metrics = metrics )
641+ self ._compile_loss (self ._model , num_labels )
653642 self ._epoch_id = 0
654643 self ._model_num_labels = num_labels
655644 self ._model_default_ind = default_ind
@@ -699,14 +688,11 @@ def fit(
699688 f1_report : dict = {}
700689
701690 self ._model .reset_metrics ()
702- softmax_output_layer_name = self ._model .output_names [0 ]
703691
704692 start_time = time .time ()
705693 batch_id = 0
706694 for x_train , y_train in train_data :
707- model_results = self ._model .train_on_batch (
708- x_train , {softmax_output_layer_name : y_train }
709- )
695+ model_results = self ._model .train_on_batch (x_train , y_train )
710696 sys .stdout .flush ()
711697 if verbose :
712698 sys .stdout .write (
0 commit comments