2. Move desired_precision and desired_recall from evaluate to hyperparameters so recall@precision metrics will be reported for both training and evaluation. This also fixes a bug where recompiling the model with the previously initialized metric objects would not properly reset the metric states. 3. Remove redundant label_names from create_... class methods in text_classifier. This information is already provided by the datasets. 4. Change loss function to FocalLoss. 5. Re-enable text_classifier unit tests using ExBert 6. Add input names to avoid flaky auto-assigned input names. PiperOrigin-RevId: 550992146
73 lines
2.6 KiB
Python
73 lines
2.6 KiB
Python
# Copyright 2023 The MediaPipe Authors.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
"""Hyperparameters for training object detection models."""
|
|
|
|
import dataclasses
|
|
import enum
|
|
from typing import Sequence, Union
|
|
|
|
from mediapipe.model_maker.python.core import hyperparameters as hp
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class AverageWordEmbeddingHParams(hp.BaseHParams):
|
|
"""The hyperparameters for an AverageWordEmbeddingClassifier."""
|
|
|
|
|
|
@enum.unique
|
|
class BertOptimizer(enum.Enum):
|
|
"""Supported Optimizers for Bert Text Classifier."""
|
|
|
|
ADAMW = "adamw"
|
|
LAMB = "lamb"
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class BertHParams(hp.BaseHParams):
|
|
"""The hyperparameters for a Bert Classifier.
|
|
|
|
Attributes:
|
|
learning_rate: Learning rate to use for gradient descent training.
|
|
end_learning_rate: End learning rate for linear decay. Defaults to 0.
|
|
batch_size: Batch size for training. Defaults to 48.
|
|
epochs: Number of training iterations over the dataset. Defaults to 2.
|
|
optimizer: Optimizer to use for training. Supported values are defined in
|
|
BertOptimizer enum: ADAMW and LAMB.
|
|
weight_decay: Weight decay of the optimizer. Defaults to 0.01.
|
|
desired_precisions: If specified, adds a RecallAtPrecision metric per
|
|
desired_precisions[i] entry which tracks the recall given the constraint
|
|
on precision. Only supported for binary classification.
|
|
desired_recalls: If specified, adds a PrecisionAtRecall metric per
|
|
desired_recalls[i] entry which tracks the precision given the constraint
|
|
on recall. Only supported for binary classification.
|
|
gamma: Gamma parameter for focal loss. To use cross entropy loss, set this
|
|
value to 0. Defaults to 2.0.
|
|
"""
|
|
|
|
learning_rate: float = 3e-5
|
|
end_learning_rate: float = 0.0
|
|
|
|
batch_size: int = 48
|
|
epochs: int = 2
|
|
optimizer: BertOptimizer = BertOptimizer.ADAMW
|
|
weight_decay: float = 0.01
|
|
|
|
desired_precisions: Sequence[float] = dataclasses.field(default_factory=list)
|
|
desired_recalls: Sequence[float] = dataclasses.field(default_factory=list)
|
|
|
|
gamma: float = 2.0
|
|
|
|
|
|
HParams = Union[BertHParams, AverageWordEmbeddingHParams]
|