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: Replaces a `DenseForSparse` layer with a new one.
  
  Replaces an existing `DenseForSparse` layer with a new one
  in order to adapt it to incremental training.
  
  Args:
      layer_to_replace: a `DenseForSparse` layer that is used to create a new one.
      new_sparse_feature_sizes: sizes of sparse features that will be
                                the input of the layer.
      old_sparse_feature_sizes: sizes of sparse features that used to be
                                the input of the layer.
      attribute: an attribute of the data fed to the layer.
      feature_type: a feature type of the data fed to the layer.
      reg_lambda: regularization constant.
  
  Returns:
      New `DenseForSparse` layer.
docstr_tok: ["Replaces","a","`","DenseForSparse","`","layer","with","a","new","one",".","Replaces","an","existing","`","DenseForSparse","`","layer","with","a","new","one","in","order","to","adapt","it","to","incremental","training",".","Args",":","layer_to_replace",":","a","`","DenseForSparse","`","layer","that","is","used","to","create","a","new","one",".","new_sparse_feature_sizes",":","sizes","of","sparse","features","that","will","be","the","input","of","the","layer",".","old_sparse_feature_sizes",":","sizes","of","sparse","features","that","used","to","be","the","input","of","the","layer",".","attribute",":","an","attribute","of","the","data","fed","to","the","layer",".","feature_type",":","a","feature","type","of","the","data","fed","to","the","layer",".","reg_lambda",":","regularization","constant",".","Returns",":","New","`","DenseForSparse","`","layer","."]
code: def _replace_dense_for_sparse_layer(
      layer_to_replace: layers.DenseForSparse,
      new_sparse_feature_sizes: List[int],
      old_sparse_feature_sizes: List[int],
      attribute: Text,
      feature_type: Text,
      reg_lambda: float,
  ) -> layers.DenseForSparse:
      """Replaces a `DenseForSparse` layer with a new one.
  
      Replaces an existing `DenseForSparse` layer with a new one
      in order to adapt it to incremental training.
  
      Args:
          layer_to_replace: a `DenseForSparse` layer that is used to create a new one.
          new_sparse_feature_sizes: sizes of sparse features that will be
                                    the input of the layer.
          old_sparse_feature_sizes: sizes of sparse features that used to be
                                    the input of the layer.
          attribute: an attribute of the data fed to the layer.
          feature_type: a feature type of the data fed to the layer.
          reg_lambda: regularization constant.
  
      Returns:
          New `DenseForSparse` layer.
      """
      kernel = layer_to_replace.get_kernel().numpy()
      bias = layer_to_replace.get_bias()
      if bias is not None:
          bias = bias.numpy()
      units = layer_to_replace.get_units()
      # split kernel by feature sizes to update the layer accordingly
      kernel_splits = []
      splitting_index = 0
      for size in old_sparse_feature_sizes:
          kernel_splits.append(
              kernel[splitting_index : splitting_index + size, :]
          )
          splitting_index += size
      additional_sizes = [
          new_size - old_size
          for new_size, old_size in zip(
              new_sparse_feature_sizes, old_sparse_feature_sizes
          )
      ]
      std, mean = np.std(kernel), np.mean(kernel)
      additional_weights = [
          np.random.normal(mean, std, size=(num_rows, units)).astype(
              np.float32
          )
          for num_rows in additional_sizes
      ]
      merged_weights = [
          np.vstack((existing, new))
          for existing, new in zip(kernel_splits, additional_weights)
      ]
      # stack each merged weight to form a new weight tensor
      new_weights = np.vstack(merged_weights)
      kernel_init = tf.constant_initializer(new_weights)
      bias_init = (
          tf.constant_initializer(bias) if bias is not None else None
      )
      new_layer = layers.DenseForSparse(
          name=f"sparse_to_dense.{attribute}_{feature_type}",
          reg_lambda=reg_lambda,
          units=units,
          use_bias=bias is not None,
          kernel_initializer=kernel_init,
          bias_initializer=bias_init,
      )
      return new_layer
  
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 2 for calling the RasaCustomLayer obj's underlying member method code with example usage: obj._replace_dense_for_sparse_layer(layer_to_replace, new_sparse_feature_sizes, old_sparse_feature_sizes, attribute, feature_type, reg_lambda) and returns: new_layer