notice

This is documentation for Rasa Open Source Documentation v2.4.x, which is no longer actively maintained.
For up-to-date documentation, see the latest version (2.6.x).

Version: 2.4.x

rasa.utils.tensorflow.models

RasaModel Objects

class RasaModel(TmpKerasModel)

Abstract custom Keras model.

This model overwrites the following methods:

  • train_step
  • test_step
  • predict_step
  • save
  • load Cannot be used as tf.keras.Model.

__init__

| __init__(random_seed: Optional[int] = None, **kwargs, ,) -> None

Initialize the RasaModel.

Arguments:

  • random_seed - set the random seed to get reproducible results

batch_loss

| batch_loss(batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]]) -> tf.Tensor

Calculates the loss for the given batch.

Arguments:

  • batch_in - The batch.

Returns:

The loss of the given batch.

prepare_for_predict

| prepare_for_predict() -> None

Prepares tf graph fpr prediction.

This method should contain necessary tf calculations and set self variables that are used in batch_predict. For example, pre calculation of self.all_labels_embed.

batch_predict

| batch_predict(batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]]) -> Dict[Text, Union[tf.Tensor, Dict[Text, tf.Tensor]]]

Predicts the output of the given batch.

Arguments:

  • batch_in - The batch.

Returns:

The output to predict.

train_step

| train_step(batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]]) -> Dict[Text, float]

Performs a train step using the given batch.

Arguments:

  • batch_in - The batch input.

Returns:

Training metrics.

test_step

| test_step(batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]]) -> Dict[Text, float]

Tests the model using the given batch.

This method is used during validation.

Arguments:

  • batch_in - The batch input.

Returns:

Testing metrics.

predict_step

| predict_step(batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]]) -> Dict[Text, tf.Tensor]

Predicts the output for the given batch.

Arguments:

  • batch_in - The batch to predict.

Returns:

Prediction output.

rasa_predict

| rasa_predict(model_data: RasaModelData) -> Dict[Text, tf.Tensor]

Custom prediction method that builds tf graph on the first call.

Arguments:

  • model_data - The model data to use for prediction.

Returns:

Prediction output.

save

| save(model_file_name: Text, overwrite: bool = True) -> None

Save the model to the given file.

Arguments:

  • model_file_name - The file name to save the model to.
  • overwrite - If 'True' an already existing model with the same file name will be overwritten.

load

| @classmethod
| load(cls, model_file_name: Text, model_data_example: RasaModelData, predict_data_example: Optional[RasaModelData] = None, finetune_mode: bool = False, *args, **kwargs, *, ,) -> "RasaModel"

Loads a model from the given weights.

Arguments:

  • model_file_name - Path to file containing model weights.
  • model_data_example - Example data point to construct the model architecture.
  • predict_data_example - Example data point to speed up prediction during inference.
  • finetune_mode - Indicates whether to load the model for further finetuning.
  • *args - Any other non key-worded arguments.
  • **kwargs - Any other key-worded arguments.

Returns:

Loaded model with weights appropriately set.

batch_to_model_data_format

| @staticmethod
| batch_to_model_data_format(batch: Union[Tuple[tf.Tensor], Tuple[np.ndarray]], data_signature: Dict[Text, Dict[Text, List[FeatureSignature]]]) -> Dict[Text, Dict[Text, List[tf.Tensor]]]

Convert input batch tensors into batch data format.

Batch contains any number of batch data. The order is equal to the key-value pairs in session data. As sparse data were converted into (indices, data, shape) before, this method converts them into sparse tensors. Dense data is kept.

call

| call(inputs: Union[tf.Tensor, List[tf.Tensor]], training: Optional[tf.Tensor] = None, mask: Optional[tf.Tensor] = None) -> Union[tf.Tensor, List[tf.Tensor]]

Calls the model on new inputs.

Arguments:

  • inputs - A tensor or list of tensors.
  • training - Boolean or boolean scalar tensor, indicating whether to run the Network in training mode or inference mode.
  • mask - A mask or list of masks. A mask can be either a tensor or None (no mask).

Returns:

A tensor if there is a single output, or a list of tensors if there are more than one outputs.

TransformerRasaModel Objects

class TransformerRasaModel(RasaModel)

batch_loss

| batch_loss(batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]]) -> tf.Tensor

Calculates the loss for the given batch.

Arguments:

  • batch_in - The batch.

Returns:

The loss of the given batch.

batch_predict

| batch_predict(batch_in: Union[Tuple[tf.Tensor], Tuple[np.ndarray]]) -> Dict[Text, Union[tf.Tensor, Dict[Text, tf.Tensor]]]

Predicts the output of the given batch.

Arguments:

  • batch_in - The batch.

Returns:

The output to predict.