Classification Models

Bidirectional LSTM Model

class kashgari.tasks.classification.BiLSTM_Model(embedding: kashgari.embeddings.abc_embedding.ABCEmbedding = None, *, sequence_length: int = None, hyper_parameters: Dict[str, Dict[str, Any]] = None, multi_label: bool = False, text_processor: kashgari.processors.abc_processor.ABCProcessor = None, label_processor: kashgari.processors.abc_processor.ABCProcessor = None)[source]

Bases: kashgari.tasks.classification.abc_model.ABCClassificationModel

__init__(embedding: kashgari.embeddings.abc_embedding.ABCEmbedding = None, *, sequence_length: int = None, hyper_parameters: Dict[str, Dict[str, Any]] = None, multi_label: bool = False, text_processor: kashgari.processors.abc_processor.ABCProcessor = None, label_processor: kashgari.processors.abc_processor.ABCProcessor = None)
Parameters:
  • embedding – embedding object
  • sequence_length – target sequence length
  • hyper_parameters – hyper_parameters to overwrite
  • multi_label – is multi-label classification
  • text_processor – text processor
  • label_processor – label processor
build_model(x_train: List[List[str]], y_train: Union[List[str], List[List[str]], List[Tuple[str]]]) → None

Build Model with x_data and y_data

This function will setup a CorpusGenerator,
then call py:meth:ABCClassificationModel.build_model_gen for preparing processor and model
Parameters:
  • x_train
  • y_train

Returns:

build_model_arc() → None[source]
build_model_generator(generators: List[kashgari.generators.CorpusGenerator]) → None
compile_model(loss: Any = None, optimizer: Any = None, metrics: Any = None, **kwargs) → None

Configures the model for training. call tf.keras.Model.predict() to compile model with custom loss, optimizer and metrics

Examples

>>> model = BiLSTM_Model()
# Build model with corpus
>>> model.build_model(train_x, train_y)
# Compile model with custom loss, optimizer and metrics
>>> model.compile(loss='categorical_crossentropy', optimizer='rsm', metrics = ['accuracy'])
Parameters:
  • loss – name of objective function, objective function or tf.keras.losses.Loss instance.
  • optimizer – name of optimizer or optimizer instance.
  • metrics (object) – List of metrics to be evaluated by the model during training and testing.
  • **kwargs – additional params passed to tf.keras.Model.predict`().
classmethod default_hyper_parameters() → Dict[str, Dict[str, Any]][source]

The default hyper parameters of the model dict, all models must implement this function.

You could easily change model’s hyper-parameters.

For example, change the LSTM unit in BiLSTM_Model from 128 to 32.

>>> from kashgari.tasks.classification import BiLSTM_Model
>>> hyper = BiLSTM_Model.default_hyper_parameters()
>>> print(hyper)
{'layer_bi_lstm': {'units': 128, 'return_sequences': False}, 'layer_output': {}}
>>> hyper['layer_bi_lstm']['units'] = 32
>>> model = BiLSTM_Model(hyper_parameters=hyper)
Returns:hyper params dict
evaluate(x_data: List[List[str]], y_data: Union[List[str], List[List[str]], List[Tuple[str]]], *, batch_size: int = 32, digits: int = 4, multi_label_threshold: float = 0.5, truncating: bool = False) → Dict[KT, VT]
fit(x_train: List[List[str]], y_train: Union[List[str], List[List[str]], List[Tuple[str]]], x_validate: List[List[str]] = None, y_validate: Union[List[str], List[List[str]], List[Tuple[str]]] = None, *, batch_size: int = 64, epochs: int = 5, callbacks: List[keras.callbacks.Callback] = None, fit_kwargs: Dict[KT, VT] = None) → tensorflow.python.keras.callbacks.History

Trains the model for a given number of epochs with given data set list.

Parameters:
  • 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 – Number of epochs to train the model. An epoch is an iteration over the entire x and y data provided.
  • callbacks – List of tf.keras.callbacks.Callback instances. List of callbacks to apply during training. See tf.keras.callbacks.
  • fit_kwargs – fit_kwargs: additional arguments passed to tf.keras.Model.fit()
Returns:

A tf.keras.callback.History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).

fit_generator(train_sample_gen: kashgari.generators.CorpusGenerator, valid_sample_gen: kashgari.generators.CorpusGenerator = None, *, batch_size: int = 64, epochs: int = 5, callbacks: List[keras.callbacks.Callback] = None, fit_kwargs: Dict[KT, VT] = None) → tensorflow.python.keras.callbacks.History

Trains the model for a given number of epochs with given data generator.

Data generator must be the subclass of CorpusGenerator

Parameters:
  • train_sample_gen – train data generator.
  • valid_sample_gen – valid data generator.
  • batch_size – Number of samples per gradient update, default to 64.
  • epochs – Number of epochs to train the model. An epoch is an iteration over the entire x and y data provided.
  • callbacks – List of tf.keras.callbacks.Callback instances. List of callbacks to apply during training. See tf.keras.callbacks.
  • fit_kwargs – fit_kwargs: additional arguments passed to tf.keras.Model.fit()
Returns:

A tf.keras.callback.History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).

classmethod load_model(model_path: str) → Union[ABCLabelingModel, ABCClassificationModel]
predict(x_data: List[List[str]], *, batch_size: int = 32, truncating: bool = False, multi_label_threshold: float = 0.5, predict_kwargs: Dict[KT, VT] = None) → Union[List[str], List[List[str]], List[Tuple[str]]]

Generates output predictions for the input samples.

Computation is done in batches.

Parameters:
  • 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.
  • truncating – remove values from sequences larger than model.embedding.sequence_length
  • multi_label_threshold
  • predict_kwargs – arguments passed to predict() function of tf.keras.Model
Returns:

array(s) of predictions.

save(model_path: str) → str

Save model :param model_path:

to_dict() → Dict[KT, VT]

Bidirectional GRU Model

class kashgari.tasks.classification.BiGRU_Model(embedding: kashgari.embeddings.abc_embedding.ABCEmbedding = None, *, sequence_length: int = None, hyper_parameters: Dict[str, Dict[str, Any]] = None, multi_label: bool = False, text_processor: kashgari.processors.abc_processor.ABCProcessor = None, label_processor: kashgari.processors.abc_processor.ABCProcessor = None)[source]

Bases: kashgari.tasks.classification.abc_model.ABCClassificationModel

__init__(embedding: kashgari.embeddings.abc_embedding.ABCEmbedding = None, *, sequence_length: int = None, hyper_parameters: Dict[str, Dict[str, Any]] = None, multi_label: bool = False, text_processor: kashgari.processors.abc_processor.ABCProcessor = None, label_processor: kashgari.processors.abc_processor.ABCProcessor = None)
Parameters:
  • embedding – embedding object
  • sequence_length – target sequence length
  • hyper_parameters – hyper_parameters to overwrite
  • multi_label – is multi-label classification
  • text_processor – text processor
  • label_processor – label processor
build_model(x_train: List[List[str]], y_train: Union[List[str], List[List[str]], List[Tuple[str]]]) → None

Build Model with x_data and y_data

This function will setup a CorpusGenerator,
then call py:meth:ABCClassificationModel.build_model_gen for preparing processor and model
Parameters:
  • x_train
  • y_train

Returns:

build_model_arc() → None[source]
build_model_generator(generators: List[kashgari.generators.CorpusGenerator]) → None
compile_model(loss: Any = None, optimizer: Any = None, metrics: Any = None, **kwargs) → None

Configures the model for training. call tf.keras.Model.predict() to compile model with custom loss, optimizer and metrics

Examples

>>> model = BiLSTM_Model()
# Build model with corpus
>>> model.build_model(train_x, train_y)
# Compile model with custom loss, optimizer and metrics
>>> model.compile(loss='categorical_crossentropy', optimizer='rsm', metrics = ['accuracy'])
Parameters:
  • loss – name of objective function, objective function or tf.keras.losses.Loss instance.
  • optimizer – name of optimizer or optimizer instance.
  • metrics (object) – List of metrics to be evaluated by the model during training and testing.
  • **kwargs – additional params passed to tf.keras.Model.predict`().
classmethod default_hyper_parameters() → Dict[str, Dict[str, Any]][source]

The default hyper parameters of the model dict, all models must implement this function.

You could easily change model’s hyper-parameters.

For example, change the LSTM unit in BiLSTM_Model from 128 to 32.

>>> from kashgari.tasks.classification import BiLSTM_Model
>>> hyper = BiLSTM_Model.default_hyper_parameters()
>>> print(hyper)
{'layer_bi_lstm': {'units': 128, 'return_sequences': False}, 'layer_output': {}}
>>> hyper['layer_bi_lstm']['units'] = 32
>>> model = BiLSTM_Model(hyper_parameters=hyper)
Returns:hyper params dict
evaluate(x_data: List[List[str]], y_data: Union[List[str], List[List[str]], List[Tuple[str]]], *, batch_size: int = 32, digits: int = 4, multi_label_threshold: float = 0.5, truncating: bool = False) → Dict[KT, VT]
fit(x_train: List[List[str]], y_train: Union[List[str], List[List[str]], List[Tuple[str]]], x_validate: List[List[str]] = None, y_validate: Union[List[str], List[List[str]], List[Tuple[str]]] = None, *, batch_size: int = 64, epochs: int = 5, callbacks: List[keras.callbacks.Callback] = None, fit_kwargs: Dict[KT, VT] = None) → tensorflow.python.keras.callbacks.History

Trains the model for a given number of epochs with given data set list.

Parameters:
  • 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 – Number of epochs to train the model. An epoch is an iteration over the entire x and y data provided.
  • callbacks – List of tf.keras.callbacks.Callback instances. List of callbacks to apply during training. See tf.keras.callbacks.
  • fit_kwargs – fit_kwargs: additional arguments passed to tf.keras.Model.fit()
Returns:

A tf.keras.callback.History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).

fit_generator(train_sample_gen: kashgari.generators.CorpusGenerator, valid_sample_gen: kashgari.generators.CorpusGenerator = None, *, batch_size: int = 64, epochs: int = 5, callbacks: List[keras.callbacks.Callback] = None, fit_kwargs: Dict[KT, VT] = None) → tensorflow.python.keras.callbacks.History

Trains the model for a given number of epochs with given data generator.

Data generator must be the subclass of CorpusGenerator

Parameters:
  • train_sample_gen – train data generator.
  • valid_sample_gen – valid data generator.
  • batch_size – Number of samples per gradient update, default to 64.
  • epochs – Number of epochs to train the model. An epoch is an iteration over the entire x and y data provided.
  • callbacks – List of tf.keras.callbacks.Callback instances. List of callbacks to apply during training. See tf.keras.callbacks.
  • fit_kwargs – fit_kwargs: additional arguments passed to tf.keras.Model.fit()
Returns:

A tf.keras.callback.History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).

classmethod load_model(model_path: str) → Union[ABCLabelingModel, ABCClassificationModel]
predict(x_data: List[List[str]], *, batch_size: int = 32, truncating: bool = False, multi_label_threshold: float = 0.5, predict_kwargs: Dict[KT, VT] = None) → Union[List[str], List[List[str]], List[Tuple[str]]]

Generates output predictions for the input samples.

Computation is done in batches.

Parameters:
  • 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.
  • truncating – remove values from sequences larger than model.embedding.sequence_length
  • multi_label_threshold
  • predict_kwargs – arguments passed to predict() function of tf.keras.Model
Returns:

array(s) of predictions.

save(model_path: str) → str

Save model :param model_path:

to_dict() → Dict[KT, VT]

CNN Model

class kashgari.tasks.classification.CNN_Model(embedding: kashgari.embeddings.abc_embedding.ABCEmbedding = None, *, sequence_length: int = None, hyper_parameters: Dict[str, Dict[str, Any]] = None, multi_label: bool = False, text_processor: kashgari.processors.abc_processor.ABCProcessor = None, label_processor: kashgari.processors.abc_processor.ABCProcessor = None)[source]

Bases: kashgari.tasks.classification.abc_model.ABCClassificationModel

__init__(embedding: kashgari.embeddings.abc_embedding.ABCEmbedding = None, *, sequence_length: int = None, hyper_parameters: Dict[str, Dict[str, Any]] = None, multi_label: bool = False, text_processor: kashgari.processors.abc_processor.ABCProcessor = None, label_processor: kashgari.processors.abc_processor.ABCProcessor = None)
Parameters:
  • embedding – embedding object
  • sequence_length – target sequence length
  • hyper_parameters – hyper_parameters to overwrite
  • multi_label – is multi-label classification
  • text_processor – text processor
  • label_processor – label processor
build_model(x_train: List[List[str]], y_train: Union[List[str], List[List[str]], List[Tuple[str]]]) → None

Build Model with x_data and y_data

This function will setup a CorpusGenerator,
then call py:meth:ABCClassificationModel.build_model_gen for preparing processor and model
Parameters:
  • x_train
  • y_train

Returns:

build_model_arc() → None[source]
build_model_generator(generators: List[kashgari.generators.CorpusGenerator]) → None
compile_model(loss: Any = None, optimizer: Any = None, metrics: Any = None, **kwargs) → None

Configures the model for training. call tf.keras.Model.predict() to compile model with custom loss, optimizer and metrics

Examples

>>> model = BiLSTM_Model()
# Build model with corpus
>>> model.build_model(train_x, train_y)
# Compile model with custom loss, optimizer and metrics
>>> model.compile(loss='categorical_crossentropy', optimizer='rsm', metrics = ['accuracy'])
Parameters:
  • loss – name of objective function, objective function or tf.keras.losses.Loss instance.
  • optimizer – name of optimizer or optimizer instance.
  • metrics (object) – List of metrics to be evaluated by the model during training and testing.
  • **kwargs – additional params passed to tf.keras.Model.predict`().
classmethod default_hyper_parameters() → Dict[str, Dict[str, Any]][source]

The default hyper parameters of the model dict, all models must implement this function.

You could easily change model’s hyper-parameters.

For example, change the LSTM unit in BiLSTM_Model from 128 to 32.

>>> from kashgari.tasks.classification import BiLSTM_Model
>>> hyper = BiLSTM_Model.default_hyper_parameters()
>>> print(hyper)
{'layer_bi_lstm': {'units': 128, 'return_sequences': False}, 'layer_output': {}}
>>> hyper['layer_bi_lstm']['units'] = 32
>>> model = BiLSTM_Model(hyper_parameters=hyper)
Returns:hyper params dict
evaluate(x_data: List[List[str]], y_data: Union[List[str], List[List[str]], List[Tuple[str]]], *, batch_size: int = 32, digits: int = 4, multi_label_threshold: float = 0.5, truncating: bool = False) → Dict[KT, VT]
fit(x_train: List[List[str]], y_train: Union[List[str], List[List[str]], List[Tuple[str]]], x_validate: List[List[str]] = None, y_validate: Union[List[str], List[List[str]], List[Tuple[str]]] = None, *, batch_size: int = 64, epochs: int = 5, callbacks: List[keras.callbacks.Callback] = None, fit_kwargs: Dict[KT, VT] = None) → tensorflow.python.keras.callbacks.History

Trains the model for a given number of epochs with given data set list.

Parameters:
  • 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 – Number of epochs to train the model. An epoch is an iteration over the entire x and y data provided.
  • callbacks – List of tf.keras.callbacks.Callback instances. List of callbacks to apply during training. See tf.keras.callbacks.
  • fit_kwargs – fit_kwargs: additional arguments passed to tf.keras.Model.fit()
Returns:

A tf.keras.callback.History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).

fit_generator(train_sample_gen: kashgari.generators.CorpusGenerator, valid_sample_gen: kashgari.generators.CorpusGenerator = None, *, batch_size: int = 64, epochs: int = 5, callbacks: List[keras.callbacks.Callback] = None, fit_kwargs: Dict[KT, VT] = None) → tensorflow.python.keras.callbacks.History

Trains the model for a given number of epochs with given data generator.

Data generator must be the subclass of CorpusGenerator

Parameters:
  • train_sample_gen – train data generator.
  • valid_sample_gen – valid data generator.
  • batch_size – Number of samples per gradient update, default to 64.
  • epochs – Number of epochs to train the model. An epoch is an iteration over the entire x and y data provided.
  • callbacks – List of tf.keras.callbacks.Callback instances. List of callbacks to apply during training. See tf.keras.callbacks.
  • fit_kwargs – fit_kwargs: additional arguments passed to tf.keras.Model.fit()
Returns:

A tf.keras.callback.History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable).

classmethod load_model(model_path: str) → Union[ABCLabelingModel, ABCClassificationModel]
predict(x_data: List[List[str]], *, batch_size: int = 32, truncating: bool = False, multi_label_threshold: float = 0.5, predict_kwargs: Dict[KT, VT] = None) → Union[List[str], List[List[str]], List[Tuple[str]]]

Generates output predictions for the input samples.

Computation is done in batches.

Parameters:
  • 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.
  • truncating – remove values from sequences larger than model.embedding.sequence_length
  • multi_label_threshold
  • predict_kwargs – arguments passed to predict() function of tf.keras.Model
Returns:

array(s) of predictions.

save(model_path: str) → str

Save model :param model_path:

to_dict() → Dict[KT, VT]