
    AVhWP              
          d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dZdZd Z ej4                  e       ed       ej6                  de      	 d#d                     Z ej4                  e       ed       ej6                  de      e	j:                  fd                     Z ej4                  e       ed       ej6                  de      ddde	j:                  fd                     Z ej4                  e       ed       ej6                  de      	 	 	 d$d                     Z  ej4                  e       ed       ej6                  de      dde	jB                  dfd                     Z"d#dZ#ejH                   G d  d!ejJ                   ejL                  d!d"                   Z'y)%zThis API defines FeatureColumn for sequential input.

NOTE: This API is a work in progress and will likely be changing frequently.
    N)feature_column_v2)serialization)utils)dtypes)ops)tensor_shape)	array_ops)	check_ops)parsing_ops)
sparse_ops)deprecation)	tf_export)doc_controlsa      Warning: tf.feature_column is not recommended for new code. Instead,
    feature preprocessing can be done directly using either [Keras preprocessing
    layers](https://www.tensorflow.org/guide/migrate/migrating_feature_columns)
    or through the one-stop utility [`tf.keras.utils.FeatureSpace`](https://www.tensorflow.org/api_docs/python/tf/keras/utils/FeatureSpace)
    built on top of them. See the [migration guide](https://tensorflow.org/guide/migrate)
    for details.
    zUse Keras preprocessing layers instead, either directly or via the `tf.keras.utils.FeatureSpace` utility. Each of `tf.feature_column.*` has a functional equivalent in `tf.keras.layers` for feature preprocessing when training a Keras model.c           	         t        j                  |ddt        j                  |      g      }t        j                  |t
        j                  dj                  |j                              }t        j                  | ddt        j                  |       g      }t        j                  | t
        j                  dj                  | j                              }t        j                  ||||g      5  t        j                  |      d	   }t        j                  t        j                  | d	      t        j                  d	g|gd	ggd
            }ddd       t        j                  |gd      S # 1 sw Y   !xY w)a  Replicates `context_input` across all timesteps of `sequence_input`.

  Expands dimension 1 of `context_input` then tiles it `sequence_length` times.
  This value is appended to `sequence_input` on dimension 2 and the result is
  returned.

  Args:
    context_input: A `Tensor` of dtype `float32` and shape `[batch_size, d1]`.
    sequence_input: A `Tensor` of dtype `float32` and shape `[batch_size,
      padded_length, d0]`.

  Returns:
    A `Tensor` of dtype `float32` and shape `[batch_size, padded_length,
    d0 + d1]`.

  Raises:
    ValueError: If `sequence_input` does not have rank 3 or `context_input` does
      not have rank 2.
     zsequence_input must have rank 3)messagedataz/sequence_input must have dtype float32; got {}.)r      zcontext_input must have rank 2z.context_input must have dtype float32; got {}.   r   N)r
   assert_rankr	   shapeassert_typer   float32formatdtyper   control_dependenciestileexpand_dimsconcat)context_inputsequence_inputseq_rank_checkseq_type_checkctx_rank_checkctx_type_checkpadded_lengthtiled_context_inputs           h/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/feature_column/sequence_feature_column.pyconcatenate_context_inputr)   5   sX   ( ((/OON+,	..
 ((nn?FF


 !.
 ((.OOM*+	-.
 ((nn>EE


 .
 ~~~FH :OON3A6M#..mQ/1#4a8:: 
		>+>?	CC: :s   2AE//E8z8feature_column.sequence_categorical_column_with_identityc                 X    t        j                  t        j                  | ||            S )a  Returns a feature column that represents sequences of integers.

  Pass this to `embedding_column` or `indicator_column` to convert sequence
  categorical data into dense representation for input to sequence NN, such as
  RNN.

  Example:

  ```python
  watches = sequence_categorical_column_with_identity(
      'watches', num_buckets=1000)
  watches_embedding = embedding_column(watches, dimension=10)
  columns = [watches_embedding]

  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  sequence_feature_layer = SequenceFeatures(columns)
  sequence_input, sequence_length = sequence_feature_layer(features)
  sequence_length_mask = tf.sequence_mask(sequence_length)

  rnn_cell = tf.keras.layers.SimpleRNNCell(hidden_size)
  rnn_layer = tf.keras.layers.RNN(rnn_cell)
  outputs, state = rnn_layer(sequence_input, mask=sequence_length_mask)
  ```

  Args:
    key: A unique string identifying the input feature.
    num_buckets: Range of inputs. Namely, inputs are expected to be in the range
      `[0, num_buckets)`.
    default_value: If `None`, this column's graph operations will fail for
      out-of-range inputs. Otherwise, this value must be in the range `[0,
      num_buckets)`, and will replace out-of-range inputs.

  Returns:
    A `SequenceCategoricalColumn`.

  Raises:
    ValueError: if `num_buckets` is less than one.
    ValueError: if `default_value` is not in range `[0, num_buckets)`.
  keynum_bucketsdefault_value)fcSequenceCategoricalColumn categorical_column_with_identityr+   s      r(   )sequence_categorical_column_with_identityr2   f   s0    Z 
	%	%)){-I
J J    z;feature_column.sequence_categorical_column_with_hash_bucketc                 X    t        j                  t        j                  | ||            S )a  A sequence of categorical terms where ids are set by hashing.

  Pass this to `embedding_column` or `indicator_column` to convert sequence
  categorical data into dense representation for input to sequence NN, such as
  RNN.

  Example:

  ```python
  tokens = sequence_categorical_column_with_hash_bucket(
      'tokens', hash_bucket_size=1000)
  tokens_embedding = embedding_column(tokens, dimension=10)
  columns = [tokens_embedding]

  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  sequence_feature_layer = SequenceFeatures(columns)
  sequence_input, sequence_length = sequence_feature_layer(features)
  sequence_length_mask = tf.sequence_mask(sequence_length)

  rnn_cell = tf.keras.layers.SimpleRNNCell(hidden_size)
  rnn_layer = tf.keras.layers.RNN(rnn_cell)
  outputs, state = rnn_layer(sequence_input, mask=sequence_length_mask)
  ```

  Args:
    key: A unique string identifying the input feature.
    hash_bucket_size: An int > 1. The number of buckets.
    dtype: The type of features. Only string and integer types are supported.

  Returns:
    A `SequenceCategoricalColumn`.

  Raises:
    ValueError: `hash_bucket_size` is not greater than 1.
    ValueError: `dtype` is neither string nor integer.
  r,   hash_bucket_sizer   )r/   r0   #categorical_column_with_hash_bucketr5   s      r(   ,sequence_categorical_column_with_hash_bucketr8      s1    T 
	%	%,,$4EC
D Dr3   z?feature_column.sequence_categorical_column_with_vocabulary_filec           
      ^    t        j                  t        j                  | |||||            S )a  A sequence of categorical terms where ids use a vocabulary file.

  Pass this to `embedding_column` or `indicator_column` to convert sequence
  categorical data into dense representation for input to sequence NN, such as
  RNN.

  Example:

  ```python
  states = sequence_categorical_column_with_vocabulary_file(
      key='states', vocabulary_file='/us/states.txt', vocabulary_size=50,
      num_oov_buckets=5)
  states_embedding = embedding_column(states, dimension=10)
  columns = [states_embedding]

  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  sequence_feature_layer = SequenceFeatures(columns)
  sequence_input, sequence_length = sequence_feature_layer(features)
  sequence_length_mask = tf.sequence_mask(sequence_length)

  rnn_cell = tf.keras.layers.SimpleRNNCell(hidden_size)
  rnn_layer = tf.keras.layers.RNN(rnn_cell)
  outputs, state = rnn_layer(sequence_input, mask=sequence_length_mask)
  ```

  Args:
    key: A unique string identifying the input feature.
    vocabulary_file: The vocabulary file name.
    vocabulary_size: Number of the elements in the vocabulary. This must be no
      greater than length of `vocabulary_file`, if less than length, later
      values are ignored. If None, it is set to the length of `vocabulary_file`.
    num_oov_buckets: Non-negative integer, the number of out-of-vocabulary
      buckets. All out-of-vocabulary inputs will be assigned IDs in the range
      `[vocabulary_size, vocabulary_size+num_oov_buckets)` based on a hash of
      the input value. A positive `num_oov_buckets` can not be specified with
      `default_value`.
    default_value: The integer ID value to return for out-of-vocabulary feature
      values, defaults to `-1`. This can not be specified with a positive
      `num_oov_buckets`.
    dtype: The type of features. Only string and integer types are supported.

  Returns:
    A `SequenceCategoricalColumn`.

  Raises:
    ValueError: `vocabulary_file` is missing or cannot be opened.
    ValueError: `vocabulary_size` is missing or < 1.
    ValueError: `num_oov_buckets` is a negative integer.
    ValueError: `num_oov_buckets` and `default_value` are both specified.
    ValueError: `dtype` is neither string nor integer.
  r,   vocabulary_filevocabulary_sizenum_oov_bucketsr.   r   )r/   r0   'categorical_column_with_vocabulary_filer:   s         r(   0sequence_categorical_column_with_vocabulary_filer?      s8    x 
	%	%00)))%
 r3   z?feature_column.sequence_categorical_column_with_vocabulary_listc           	      \    t        j                  t        j                  | ||||            S )at  A sequence of categorical terms where ids use an in-memory list.

  Pass this to `embedding_column` or `indicator_column` to convert sequence
  categorical data into dense representation for input to sequence NN, such as
  RNN.

  Example:

  ```python
  colors = sequence_categorical_column_with_vocabulary_list(
      key='colors', vocabulary_list=('R', 'G', 'B', 'Y'),
      num_oov_buckets=2)
  colors_embedding = embedding_column(colors, dimension=3)
  columns = [colors_embedding]

  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  sequence_feature_layer = SequenceFeatures(columns)
  sequence_input, sequence_length = sequence_feature_layer(features)
  sequence_length_mask = tf.sequence_mask(sequence_length)

  rnn_cell = tf.keras.layers.SimpleRNNCell(hidden_size)
  rnn_layer = tf.keras.layers.RNN(rnn_cell)
  outputs, state = rnn_layer(sequence_input, mask=sequence_length_mask)
  ```

  Args:
    key: A unique string identifying the input feature.
    vocabulary_list: An ordered iterable defining the vocabulary. Each feature
      is mapped to the index of its value (if present) in `vocabulary_list`.
      Must be castable to `dtype`.
    dtype: The type of features. Only string and integer types are supported. If
      `None`, it will be inferred from `vocabulary_list`.
    default_value: The integer ID value to return for out-of-vocabulary feature
      values, defaults to `-1`. This can not be specified with a positive
      `num_oov_buckets`.
    num_oov_buckets: Non-negative integer, the number of out-of-vocabulary
      buckets. All out-of-vocabulary inputs will be assigned IDs in the range
      `[len(vocabulary_list), len(vocabulary_list)+num_oov_buckets)` based on a
      hash of the input value. A positive `num_oov_buckets` can not be specified
      with `default_value`.

  Returns:
    A `SequenceCategoricalColumn`.

  Raises:
    ValueError: if `vocabulary_list` is empty, or contains duplicate keys.
    ValueError: `num_oov_buckets` is a negative integer.
    ValueError: `num_oov_buckets` and `default_value` are both specified.
    ValueError: if `dtype` is not integer or string.
  r,   vocabulary_listr   r.   r=   )r/   r0   'categorical_column_with_vocabulary_listrA   s        r(   0sequence_categorical_column_with_vocabulary_listrD     s5    t 
	%	%00)%)+
, ,r3   z&feature_column.sequence_numeric_column)r   g        c                    t        j                  ||       }|j                  s'|j                  st	        dj                  ||             |%t        |      st        dj                  |            t        | ||||      S )a  Returns a feature column that represents sequences of numeric data.

  Example:

  ```python
  temperature = sequence_numeric_column('temperature')
  columns = [temperature]

  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  sequence_feature_layer = SequenceFeatures(columns)
  sequence_input, sequence_length = sequence_feature_layer(features)
  sequence_length_mask = tf.sequence_mask(sequence_length)

  rnn_cell = tf.keras.layers.SimpleRNNCell(hidden_size)
  rnn_layer = tf.keras.layers.RNN(rnn_cell)
  outputs, state = rnn_layer(sequence_input, mask=sequence_length_mask)
  ```

  Args:
    key: A unique string identifying the input features.
    shape: The shape of the input data per sequence id. E.g. if `shape=(2,)`,
      each example must contain `2 * sequence_length` values.
    default_value: A single value compatible with `dtype` that is used for
      padding the sparse data into a dense `Tensor`.
    dtype: The type of values.
    normalizer_fn: If not `None`, a function that can be used to normalize the
      value of the tensor after `default_value` is applied for parsing.
      Normalizer function takes the input `Tensor` as its argument, and returns
      the output `Tensor`. (e.g. lambda x: (x - 3.0) / 4.2). Please note that
      even though the most common use case of this function is normalization, it
      can be used for any kind of Tensorflow transformations.

  Returns:
    A `SequenceNumericColumn`.

  Raises:
    TypeError: if any dimension in shape is not an int.
    ValueError: if any dimension in shape is not a positive integer.
    ValueError: if `dtype` is not convertible to `tf.float32`.
  )r   r,   z6dtype must be convertible to float. dtype: {}, key: {}z+normalizer_fn must be a callable. Given: {})r   r.   r   normalizer_fn)	r/   _check_shape
is_integeris_floating
ValueErrorr   callable	TypeErrorSequenceNumericColumnr,   r   r.   r   rF   s        r(   sequence_numeric_columnrO   P  s    ` //3
/%


e//
 **0&*<> >x'>
5<<]KM M 
	!!
# #r3   c                    t        j                  |d|       5  t        |       dk(  r| d   cddd       S g }| dd D ]*  }|j                  t	        j
                  | d   |             , t        j                  |      5  t        j                  | d         cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)z=Asserts that all tensors are equal and returns the first one.assert_all_equal)valuesr   r   N)	r   
name_scopelenappendr
   assert_equalr   r	   identity)tensorsnameassert_equal_opsts       r(   _assert_all_equal_and_returnr\     s    
~~d.w? ,
7|qQZ, , QR[ Ei44WQZCDE		!	!"2	3 ,
+, ,, ,, , ,, , ,s)   C A	C ?B*	C *B3	/C  C	c                       e Zd ZdZed        Zed        Zed        Zd Zed        Z	d Z
ed        Zd	 Zedd       Zy
)rM   z%Represents sequences of numeric data.c                      y)NT selfs    r(   _is_v2_columnz#SequenceNumericColumn._is_v2_column  s    r3   c                     | j                   S zSee `FeatureColumn` base class.r,   r`   s    r(   rY   zSequenceNumericColumn.name  s     88Or3   c                 X    | j                   t        j                  | j                        iS rd   )r,   r   VarLenFeaturer   r`   s    r(   parse_example_specz(SequenceNumericColumn.parse_example_spec  s"     HHk//

;<<r3   c                 x    |j                  | j                  |      }| j                  | j                  |      }|S )aa  See `FeatureColumn` base class.

    In this case, we apply the `normalizer_fn` to the input tensor.

    Args:
      transformation_cache: A `FeatureTransformationCache` object to access
        features.
      state_manager: A `StateManager` to create / access resources such as
        lookup tables.

    Returns:
      Normalized input tensor.
    )getr,   rF   )ra   transformation_cachestate_managerinput_tensors       r(   transform_featurez'SequenceNumericColumn.transform_feature  s<     (++DHHmDL%''5lr3   c                 @    t        j                  | j                        S )zAReturns a `TensorShape` representing the shape of sequence input.)r   TensorShaper   r`   s    r(   variable_shapez$SequenceNumericColumn.variable_shape  s     ##DJJ//r3   c                    |j                  | |      }t        j                  || j                        }t	        j
                  t	        j                  |      dd dg| j                  gd      }t	        j                  ||      }|j                  j                  dk(  r| j                  j                         }nd}t        j                  ||	      }t        j                  j                  ||
      S )zReturns a `TensorSequenceLengthPair`.

    Args:
      transformation_cache: A `FeatureTransformationCache` object to access
        features.
      state_manager: A `StateManager` to create / access resources such as
        lookup tables.
    )r.   Nr   r   )axis)r   r   )num_elements)dense_tensorsequence_length)rj   r   sparse_tensor_to_denser.   r	   r   r   rq   reshapendimsru   fc_utils"sequence_length_from_sparse_tensorr/   SequenceDenseColumnTensorSequenceLengthPair)ra   rk   rl   	sp_tensorrv   dense_shaperu   
seq_lengths           r(   get_sequence_dense_tensorz/SequenceNumericColumn.get_sequence_dense_tensor  s     %((}=I44!3!35L ""		&r	*RD$2E2EFQPK$$\EL !((557ll<<.J !!::!: ; ? ?r3   c                     | j                   gS )See 'FeatureColumn` base class.re   r`   s    r(   parentszSequenceNumericColumn.parents  s     HH:r3   c                 v    t        t        | j                  |             }| j                  j                  |d<   |S )r   r   )dictzip_fieldsr   rY   )ra   configs     r(   
get_configz SequenceNumericColumn.get_config  s-    #dllD)*FjjooF7OMr3   Nc                     t        j                  || j                         t        j                  |      }t	        j
                  |d         |d<    | di |S )r   r   r_   )r/   _check_config_keysr   _standardize_and_copy_configr   as_dtype)clsr   custom_objectscolumns_by_namekwargss        r(   from_configz!SequenceNumericColumn.from_config  sJ     &#++.,,V4FoofWo6F7O==r3   )NN)__name__
__module____qualname____doc__propertyrb   rY   rh   rn   rq   r   r   r   classmethodr   r_   r3   r(   rM   rM     s     .    = =& 0 0?>    r3   rM   rN   )N)Nrs   r   )(r   collections tensorflow.python.feature_columnr   r/   r   r   r{   tensorflow.python.frameworkr   r   r   tensorflow.python.opsr	   r
   r   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   tensorflow.tools.docsr   #_FEATURE_COLUMN_DEPRECATION_WARNING+_FEATURE_COLUMN_DEPRECATION_RUNTIME_WARNINGr)   header
deprecatedr2   stringr8   r?   rD   r   rO   r\   register_feature_columnr}   
namedtuplerM   r_   r3   r(   <module>r      sQ  
  D : > . + 4 + + - , . 6 .' ## ,.Db 89
EFIJ =A,J K G :,J^ 89
HIIJ 8>}})D K J :)DX 89
LMIJ FJEFCG;A==@ K N :@F 89
LMIJ <@CEEF	=, K N :=,@ 89
34IJ"&*,"(..*.	:# K 5 ::#z	, &&]KCE] ']r3   