tasks.classification

All Text classification models share the same API.

__init__

  1. def __init__(self,
  2. embedding: Optional[Embedding] = None,
  3. hyper_parameters: Optional[Dict[str, Dict[str, Any]]] = None)

Args:

  • embedding: model embedding
  • hyper_parameters: a dict of hyper_parameters.

You could change customize hyper_parameters like this:

  1. # get default hyper_parameters
  2. hyper_parameters = BiLSTM_Model.get_default_hyper_parameters()
  3. # change lstm hidden unit to 12
  4. hyper_parameters['layer_blstm']['units'] = 12
  5. # init new model with customized hyper_parameters
  6. labeling_model = BiLSTM_Model(hyper_parameters=hyper_parameters)
  7. labeling_model.fit(x, y)

Properties

token2idx

Returns model’s token index map, type: Dict[str, int]

label2idx

Returns model’s label index map, type: Dict[str, int]

Methods

get_default_hyper_parameters

Return the defualt hyper parameters

!!! attention “You must implement this function when customizing a model” When you are customizing your own model, you must implement this function.

  1. Customization example: [customize-your-own-mode](../tutorial/text-classification.md#customize-your-own-model)
  1. @classmethod
  2. def get_default_hyper_parameters(cls) -> Dict[str, Dict[str, Any]]:

Returns:

  • dict of the defualt hyper parameters

build_model_arc

build model architectural, define models structure in this function.

!!! attention “You must implement this function when customizing a model” When you are customizing your own model, you must implement this function.

  1. Customization example: [customize-your-own-mode](../tutorial/text-classification.md#customize-your-own-model)
  1. def build_model_arc(self):

build_model

build model with corpus

  1. def build_model(self,
  2. x_train: Union[Tuple[List[List[str]], ...], List[List[str]]],
  3. y_train: Union[List[List[str]], List[str]],
  4. x_validate: Union[Tuple[List[List[str]], ...], List[List[str]]] = None,
  5. y_validate: Union[List[List[str]], List[str]] = None)

Args:

  • x_train: Array of train feature data (if the model has a single input),
    1. or tuple of train feature data array (if the model has multiple inputs)
  • y_train: Array of train label data
  • x_validate: Array of validation feature data (if the model has a single input),
    1. or tuple of validation feature data array (if the model has multiple inputs)
  • y_validate: Array of validation label data

build_multi_gpu_model

Build multi-GPU model with corpus

  1. def build_multi_gpu_model(self,
  2. gpus: int,
  3. x_train: Union[Tuple[List[List[str]], ...], List[List[str]]],
  4. y_train: Union[List[List[str]], List[str]],
  5. cpu_merge: bool = True,
  6. cpu_relocation: bool = False,
  7. x_validate: Union[Tuple[List[List[str]], ...], List[List[str]]] = None,
  8. y_validate: Union[List[List[str]], List[str]] = None):

Args:

  • gpus: Integer >= 2, number of on GPUs on which to create model replicas.
  • cpu_merge: A boolean value to identify whether to force merging model weights under the scope of the CPU or not.
  • cpu_relocation: A boolean value to identify whether to create the model’s weights under the scope of the CPU. If the model is not defined under any preceding device scope, you can still rescue it by activating this option.
  • x_train: Array of train feature data (if the model has a single input), or tuple of train feature data array (if the model has multiple inputs)
  • y_train: Array of train label data
  • x_validate: Array of validation feature data (if the model has a single input), or tuple of validation feature data array (if the model has multiple inputs)
  • y_validate: Array of validation label data

build_tpu_model

Build TPU model with corpus

  1. def build_tpu_model(self, strategy: tf.contrib.distribute.TPUStrategy,
  2. x_train: Union[Tuple[List[List[str]], ...], List[List[str]]],
  3. y_train: Union[List[List[str]], List[str]],
  4. x_validate: Union[Tuple[List[List[str]], ...], List[List[str]]] = None,
  5. y_validate: Union[List[List[str]], List[str]] = None):

Args:

  • strategy: TPUDistributionStrategy. The strategy to use for replicating model across multiple TPU cores.
  • x_train: Array of train feature data (if the model has a single input), or tuple of train feature data array (if the model has multiple inputs)
  • y_train: Array of train label data
  • x_validate: Array of validation feature data (if the model has a single input), or tuple of validation feature data array (if the model has multiple inputs)
  • y_validate: Array of validation label data

compile_model

Configures the model for training.

Using compile() function of tf.keras.Model

  1. def compile_model(self, **kwargs):

Args:

  • **kwargs: arguments passed to compile() function of tf.keras.Model

Defaults:

  • loss: categorical_crossentropy
  • optimizer: adam
  • metrics: ['accuracy']

get_data_generator

data generator for fit_generator

  1. def get_data_generator(self,
  2. x_data,
  3. y_data,
  4. batch_size: int = 64,
  5. shuffle: bool = True)

Args:

  • x_data: Array of feature data (if the model has a single input), or tuple of feature data array (if the model has multiple inputs)
  • y_data: Array of label data
  • batch_size: Number of samples per gradient update, default to 64.
  • shuffle:

Returns:

  • data generator

fit

Trains the model for a given number of epochs with fit_generator (iterations on a dataset).

  1. def fit(self,
  2. x_train: Union[Tuple[List[List[str]], ...], List[List[str]]],
  3. y_train: Union[List[List[str]], List[str]],
  4. x_validate: Union[Tuple[List[List[str]], ...], List[List[str]]] = None,
  5. y_validate: Union[List[List[str]], List[str]] = None,
  6. batch_size: int = 64,
  7. epochs: int = 5,
  8. callbacks: List[keras.callbacks.Callback] = None,
  9. fit_kwargs: Dict = None):

Args:

  • x_train: Array of train feature data (if the model has a single input), or tuple of train feature data array (if the model has multiple inputs)
  • y_train: Array of train label data
  • x_validate: Array of validation feature data (if the model has a single input), or tuple of validation feature data array (if the model has multiple inputs)
  • y_validate: Array of validation label data
  • batch_size: Number of samples per gradient update, default to 64.
  • epochs: Integer. Number of epochs to train the model. default 5.
  • callbacks:
  • fit_kwargs: additional arguments passed to fit_generator() function from tensorflow.keras.Model

Returns:

  • A tf.keras.callbacks.History object.

fit_without_generator

Trains the model for a given number of epochs (iterations on a dataset). Large memory Cost.

  1. def fit_without_generator(self,
  2. x_train: Union[Tuple[List[List[str]], ...], List[List[str]]],
  3. y_train: Union[List[List[str]], List[str]],
  4. x_validate: Union[Tuple[List[List[str]], ...], List[List[str]]] = None,
  5. y_validate: Union[List[List[str]], List[str]] = None,
  6. batch_size: int = 64,
  7. epochs: int = 5,
  8. callbacks: List[keras.callbacks.Callback] = None,
  9. fit_kwargs: Dict = None):

Args:

  • x_train: Array of train feature data (if the model has a single input), or tuple of train feature data array (if the model has multiple inputs)
  • y_train: Array of train label data
  • x_validate: Array of validation feature data (if the model has a single input), or tuple of validation feature data array (if the model has multiple inputs)
  • y_validate: Array of validation label data
  • batch_size: Number of samples per gradient update, default to 64.
  • epochs: Integer. Number of epochs to train the model. default 5.
  • callbacks:
  • fit_kwargs: additional arguments passed to fit_generator() function from tensorflow.keras.Model

Returns:

  • A tf.keras.callbacks.History object.

predict

Generates output predictions for the input samples. Computation is done in batches.

  1. def predict(self,
  2. x_data,
  3. batch_size=None,
  4. multi_label_threshold: float = 0.5,
  5. debug_info=False,
  6. predict_kwargs: Dict = None):

Args:

  • x_data: The input data, as a Numpy array (or list of Numpy arrays if the model has multiple inputs).
  • batch_size: Integer. If unspecified, it will default to 32.
  • multi_label_threshold:
  • debug_info: Bool, Should print out the logging info.
  • predict_kwargs: Dict, arguments passed to predict() function of tensorflow.keras.Model

Returns:

  • array of predictions.

predict_top_k_class

Generates output predictions with confidence for the input samples.

Computation is done in batches.

  1. def predict_top_k_class(self,
  2. x_data,
  3. top_k=5,
  4. batch_size=32,
  5. debug_info=False,
  6. predict_kwargs: Dict = None) -> List[Dict]:

Args:

  • x_data: The input data, as a Numpy array (or list of Numpy arrays if the model has multiple inputs).
  • top_k: int
  • batch_size: Integer. If unspecified, it will default to 32.
  • debug_info: Bool, Should print out the logging info.
  • predict_kwargs: Dict, arguments passed to predict() function of tensorflow.keras.Model

Returns:

array(s) of prediction result dict.

  • sample result of single-label classification:
  1. [
  2. {
  3. "label": "chat",
  4. "confidence": 0.5801531,
  5. "candidates": [
  6. { "label": "cookbook", "confidence": 0.1886314 },
  7. { "label": "video", "confidence": 0.13805099 },
  8. { "label": "health", "confidence": 0.013852648 },
  9. { "label": "translation", "confidence": 0.012913573 }
  10. ]
  11. }
  12. ]
  • sample result of multi-label classification:
  1. [
  2. {
  3. "candidates": [
  4. { "confidence": 0.9959336, "label": "toxic" },
  5. { "confidence": 0.9358089, "label": "obscene" },
  6. { "confidence": 0.6882098, "label": "insult" },
  7. { "confidence": 0.13540423, "label": "severe_toxic" },
  8. { "confidence": 0.017219543, "label": "identity_hate" }
  9. ]
  10. }
  11. ]

evaluate

Evaluate model

  1. def evaluate(self,
  2. x_data,
  3. y_data,
  4. batch_size=None,
  5. digits=4,
  6. debug_info=False) -> Tuple[float, float, Dict]:

Args:

  • x_data:
  • y_data:
  • batch_size:
  • digits:
  • debug_info:

save

Save model info json and model weights to given folder path

  1. def save(self, model_path: str):

Args:

  • model_path: target model folder path

info

Returns a dictionary containing the configuration of the model.

  1. def info(self)