class_docstr: Parent class for all classes in `rasa_layers.py`.
  
  Allows a shared implementation for adjusting `DenseForSparse`
  layers during incremental training.
  
  During fine-tuning, sparse feature sizes might change due to addition of new data.
  If this happens, we need to adjust our `DenseForSparse` layers to a new size.
  `ConcatenateSparseDenseFeatures`, `RasaSequenceLayer` and
  `RasaFeatureCombiningLayer` all inherit from `RasaCustomLayer` and thus can
  change their own `DenseForSparse` layers if it's needed.
class_docstr_tok: ["Parent","class","for","all","classes","in","`","rasa_layers.py","`",".","Allows","a","shared","implementation","for","adjusting","`","DenseForSparse","`","layers","during","incremental","training",".","During","fine-tuning",",","sparse","feature","sizes","might","change","due","to","addition","of","new","data",".","If","this","happens",",","we","need","to","adjust","our","`","DenseForSparse","`","layers","to","a","new","size",".","`","ConcatenateSparseDenseFeatures","`",",","`","RasaSequenceLayer","`","and","`","RasaFeatureCombiningLayer","`","all","inherit","from","`","RasaCustomLayer","`","and","thus","can","change","their","own","`","DenseForSparse","`","layers","if","it","'s","needed","."]
docstr: Finds and adjusts `DenseForSparse` layers during incremental training.
  
  Recursively looks through the layers until it finds all the `DenseForSparse`
  ones and adjusts those which have their sparse feature sizes increased.
  
  This function heavily relies on the name of `DenseForSparse` layer being
  in the following format - f"sparse_to_dense.{attribute}_{feature_type}" -
  in order to correctly extract the attribute and feature type.
  
  New and old sparse feature sizes could look like this:
  {TEXT: {FEATURE_TYPE_SEQUENCE: [4, 24, 128], FEATURE_TYPE_SENTENCE: [4, 128]}}
  
  Args:
      new_sparse_feature_sizes: sizes of current sparse features.
      old_sparse_feature_sizes: sizes of sparse features the model was
                                previously trained on.
      reg_lambda: regularization constant.
docstr_tok: ["Finds","and","adjusts","`","DenseForSparse","`","layers","during","incremental","training",".","Recursively","looks","through","the","layers","until","it","finds","all","the","`","DenseForSparse","`","ones","and","adjusts","those","which","have","their","sparse","feature","sizes","increased",".","This","function","heavily","relies","on","the","name","of","`","DenseForSparse","`","layer","being","in","the","following","format","-","f","''","sparse_to_dense",".","{","attribute","}","_","{","feature_type","}","''","-","in","order","to","correctly","extract","the","attribute","and","feature","type",".","New","and","old","sparse","feature","sizes","could","look","like","this",":","{","TEXT",":","{","FEATURE_TYPE_SEQUENCE",":","[","4",",","24",",","128","]",",","FEATURE_TYPE_SENTENCE",":","[","4",",","128","]","}","}","Args",":","new_sparse_feature_sizes",":","sizes","of","current","sparse","features",".","old_sparse_feature_sizes",":","sizes","of","sparse","features","the","model","was","previously","trained","on",".","reg_lambda",":","regularization","constant","."]
code: def adjust_sparse_layers_for_incremental_training(
      self,
      new_sparse_feature_sizes: Dict[Text, Dict[Text, List[int]]],
      old_sparse_feature_sizes: Dict[Text, Dict[Text, List[int]]],
      reg_lambda: float,
  ) -> None:
      """Finds and adjusts `DenseForSparse` layers during incremental training.
  
      Recursively looks through the layers until it finds all the `DenseForSparse`
      ones and adjusts those which have their sparse feature sizes increased.
  
      This function heavily relies on the name of `DenseForSparse` layer being
      in the following format - f"sparse_to_dense.{attribute}_{feature_type}" -
      in order to correctly extract the attribute and feature type.
  
      New and old sparse feature sizes could look like this:
      {TEXT: {FEATURE_TYPE_SEQUENCE: [4, 24, 128], FEATURE_TYPE_SENTENCE: [4, 128]}}
  
      Args:
          new_sparse_feature_sizes: sizes of current sparse features.
          old_sparse_feature_sizes: sizes of sparse features the model was
                                    previously trained on.
          reg_lambda: regularization constant.
      """
      for name, layer in self._tf_layers.items():
          if isinstance(layer, RasaCustomLayer):
              layer.adjust_sparse_layers_for_incremental_training(
                  new_sparse_feature_sizes=new_sparse_feature_sizes,
                  old_sparse_feature_sizes=old_sparse_feature_sizes,
                  reg_lambda=reg_lambda,
              )
          elif isinstance(layer, layers.DenseForSparse):
              attribute = layer.get_attribute()
              feature_type = layer.get_feature_type()
              if (
                  attribute in new_sparse_feature_sizes
                  and feature_type
                  in new_sparse_feature_sizes[attribute]
              ):
                  new_feature_sizes = new_sparse_feature_sizes[
                      attribute
                  ][feature_type]
                  old_feature_sizes = old_sparse_feature_sizes[
                      attribute
                  ][feature_type]
                  if sum(new_feature_sizes) > sum(old_feature_sizes):
                      self._tf_layers[
                          name
                      ] = self._replace_dense_for_sparse_layer(
                          layer_to_replace=layer,
                          new_sparse_feature_sizes=new_feature_sizes,
                          old_sparse_feature_sizes=old_feature_sizes,
                          attribute=attribute,
                          feature_type=feature_type,
                          reg_lambda=reg_lambda,
                      )
  
imports: import tensorflow
  import numpy
  from typing import Text, List, Dict, Any, Union, Optional, Tuple, Callable
  from rasa.shared.nlu.constants import TEXT
  from rasa.utils.tensorflow.model_data import FeatureSignature
  from rasa.utils.tensorflow.constants import REGULARIZATION_CONSTANT, CONNECTION_DENSITY, NUM_TRANSFORMER_LAYERS, TRANSFORMER_SIZE, NUM_HEADS, UNIDIRECTIONAL_ENCODER, KEY_RELATIVE_ATTENTION, VALUE_RELATIVE_ATTENTION, MAX_RELATIVE_POSITION, MASKED_LM, HIDDEN_LAYERS_SIZES, DROP_RATE, SPARSE_INPUT_DROPOUT, DENSE_INPUT_DROPOUT, DENSE_DIMENSION, CONCAT_DIMENSION, DROP_RATE_ATTENTION, SEQUENCE, SENTENCE
  from rasa.utils.tensorflow import layers
  from rasa.utils.tensorflow.exceptions import TFLayerConfigException
  from rasa.utils.tensorflow.transformer import TransformerEncoder
  from rasa.nlu.constants import DEFAULT_TRANSFORMER_SIZE
label_desc: Use image node_id 1 for calling the RasaCustomLayer obj's underlying member method code with example usage: obj.adjust_sparse_layers_for_incremental_training(new_sparse_feature_sizes, old_sparse_feature_sizes, reg_lambda) without return types