Classify and Ensemble

Trainer

class caver.Trainer(model_name, data_path, **kwargs)[source]
Parameters
  • model_name (str) – name of model, case sensitive.

  • data_path (str) – file path of data

You can pass your own config as parameters to replace default value in caver.config.Config.

GPU will be used if available.

init_model_embedding()[source]

Init embedding layer use pre-trained model.

This will be used if caver.config.Config.embedding_file is not None.

Caver

class caver.Caver(model_name, model_path, data_path='', **kwargs)[source]
Parameters
  • model_name (str) – name of model, case sensitive

  • model_path (str) – file path of model

  • data_path (str) – file path of data, local JSON file will be used first if available

You can pass your own config as parameters to replace default value in caver.config.Config and model config.

get_top_label(text, top=5)[source]
Parameters
  • text (str) – text

  • top (int) – top-n most possible labels

load_data(path)[source]

Load word index and label index from file.

If there is no JSON file saved in caver.config.Config.index_path, this will generate new index file.

predict(text)[source]

This text will be transformed to lower-case and segmented by caver.config.Config.cut_model.

Ensemble

class caver.EnsembleModel(models, model_ratio)[source]
Parameters
  • models (list) – list of models, each model should have the same label number

  • model_ratio (list) – list of model`s ratio in weighted voting and empty list means no weighted

For now, this only support soft voting methods.

gmean(models_preds)[source]

geometric mean

when class parameter model_ratio is not empty list, calculate weighted harmonic mean

Parameters

models_preds (list) – list of model predict probability

Returns

ensemble predict probability for sentences

hmean(models_preds)[source]

harmonic mean

when class parameter model_ratio is not empty list, calculate weighted harmonic mean

Parameters

models_preds (list) – list of model predict probability

Returns

ensemble predict probability for sentences

mean(models_preds)[source]

arithmetic mean

when class parameter model_ratio is not empty list, calculate weighted arithmetic mean

Parameters

models_preds (list) – list of model predict probability

Returns

ensemble predict probability for sentences

model_consistance_checker(models)[source]

check all models have same labels and vocab dict

Parameters

models (list) – list of models

predict(batch_sequence_text, top_k, method)[source]
Parameters
  • batch_sequence_text (list) – list of sentences

  • top_k (int) – top_k labels

  • method (str) – voting method

Returns

top_k prediction labels