
    AVho                       d Z ddlZddlZddlZddl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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dl+m,Z, ddl+m-Z- ddl.m/Z/ dd l0m1Z1 dd!l2m3Z3 d"Z4d#Z5	 	 	 	 	 	 d}d$Z6 e3jn                  e4       e1d%g&       e,jp                  de5      	 	 	 	 d~d'                     Z9 G d( d)e:      Z; e3jn                  e4       e1d*g&       e,jp                  de5      	 	 	 	 	 dd+                     Z<d, Z= G d- d.ej|                        Z? G d/ d0ej|                        Z@d1 ZAd2 ZB G d3 d4ej|                        ZCd5 ZD e3jn                  e4       e1d6g&       e,jp                  de5      d7                      ZE	 	 	 	 	 	 	 dd8ZFd9dej                  dfd:ZHd; ZIej                  fd<ZKdddej                  fd=ZL	 	 	 dd>ZMdd?ZNd@ ZOej                  fdAZPddBZQ G dC dDej|                        ZR ej                  ej                         G dE dFe:             ZU G dG dHeU      ZV	 ddIZW	 ddJZX G dK dLeU      ZY	 ddMZZ G dN dOeU      Z[ G dP dQe:      Z\dR Z]ddSZ^dT Z_ G dU dVeV ej                  dVg dW            Za G dX dYeVeY ej                  dYdZd[g            Zb G d\ d]eVe[ ej                  d]d^            Zcd_ Zd G d` daeVe[ ej                  dadb            Zedc Zf G dd deeY ej                  deg df            Zg G dg dheY ej                  dhdi            Zh G dj dkeY ej                  dkdl            Zi G dm dneY ej                  dndo            Zj G dp dqeY ej                  dqdr            Zk G ds dteY ej                  dtg du            Zldv Zm G dw dxeVe[ ej                  dxdyg            Zndz Zo G d{ d|eY ej                  d|dyg            Zpy)a  This API defines FeatureColumn abstraction.

FeatureColumns can also be transformed into a generic input layer for
custom models using `input_layer`.

NOTE: Functions prefixed with "_" indicate experimental or private parts of
the API subject to change, and should not be relied upon!

NOTE: The new feature columns are being developed in feature_column_v2.py and
are a somewhat duplicate of the code here. Please make sure to update logic
in both places.
    N)context)utils)dtypes)ops)sparse_tensor)tensor_shape)base)	array_ops)array_ops_stack)	check_ops)cond)embedding_ops)init_ops)
lookup_ops)math_ops)nn_ops)parsing_ops)resource_variable_ops)
sparse_ops)
string_ops)template)variable_scope)	variables)gfile)
tf_logging)checkpoint_utils)deprecation)nest)collections_abc)	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                 d    t              D ],  }t        |t              rt        dj	                  |             t        xs g       t        j                  j                  vr)j                  t        j                  j                         t        j                  j                  vr)j                  t        j                  j                          fd}	|r |	       S t        j                  |d j                               5   |	       cddd       S # 1 sw Y   yxY w)z<See input_layer. `scope` is a name or variable scope to use.zItems of feature_columns must be a _DenseColumn. You can wrap a categorical column with an embedding_column or indicator_column. Given: {}c                     t              } g }g }t        
d       D ]  }|j                  |       t        j                  d |j                        5  |j                  |       }|j                  j                         }t        j                  |      d   }t        j                  |||f      }|j                  |       	Nt        j                  t        j                  j                  t        j                         j                         	|<   ||<   d d d         t#        ||       t        j$                  |d      S # 1 sw Y   5xY w)	Nc                     | j                   S Nnamexs    _/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/feature_column/feature_column.py<lambda>z<_internal_input_layer.<locals>._get_logits.<locals>.<lambda>n   
         keydefault_nameweight_collections	trainabler   shapescope   )_LazyBuildersortedappendr   _var_scope_name_get_dense_tensor_variable_shapenum_elementsr
   r6   reshaper   get_collection	GraphKeysGLOBAL_VARIABLESget_variable_scoper'   "_verify_static_batch_size_equalityconcat)builderoutput_tensorsordered_columnscolumntensorr@   
batch_sizeoutput_tensorcols_to_output_tensorscols_to_varsfeature_columnsfeaturesr4   r3   s           r*   _get_logitsz*_internal_input_layer.<locals>._get_logitsj   sL   8$GNO.>? 9V$((
V335 9))1 * ! --::<__V,Q/
!)):|46m,# "%!3!3mm,,"557<<">,v
 "-+8
 
(%9 99* '~GNA..)9 9s   CE

E	input_layerr1   valuesN)_normalize_feature_columns
isinstance_DenseColumn
ValueErrorformatlistr   rC   rD   r<   MODEL_VARIABLESr   rV   )
rR   rQ   r3   r4   rP   r8   rO   from_templaterK   rS   s
   ````` `   r*   _internal_input_layerr_   S   s     /?/ Lffl+<<BF6NL LL .4"5]]##+==cmm<<=]]""*<<cmm;;</ /> =		&	&M(//2C
E ]  s   D&&D/zfeature_column.input_layer)v1c                 $    t        | |||||      S )a
  Returns a dense `Tensor` as input layer based on given `feature_columns`.

  Generally a single example in training data is described with FeatureColumns.
  At the first layer of the model, this column oriented data should be converted
  to a single `Tensor`.

  Example:

  ```python
  price = numeric_column('price')
  keywords_embedded = embedding_column(
      categorical_column_with_hash_bucket("keywords", 10K), dimensions=16)
  columns = [price, keywords_embedded, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  dense_tensor = input_layer(features, columns)
  for units in [128, 64, 32]:
    dense_tensor = tf.compat.v1.layers.dense(dense_tensor, units, tf.nn.relu)
  prediction = tf.compat.v1.layers.dense(dense_tensor, 1)
  ```

  Args:
    features: A mapping from key to tensors. `_FeatureColumn`s look up via these
      keys. For example `numeric_column('price')` will look at 'price' key in
      this dict. Values can be a `SparseTensor` or a `Tensor` depends on
      corresponding `_FeatureColumn`.
    feature_columns: An iterable containing the FeatureColumns to use as inputs
      to your model. All items should be instances of classes derived from
      `_DenseColumn` such as `numeric_column`, `embedding_column`,
      `bucketized_column`, `indicator_column`. If you have categorical features,
      you can wrap them with an `embedding_column` or `indicator_column`.
    weight_collections: A list of collection names to which the Variable will be
      added. Note that variables will also be added to collections
      `tf.GraphKeys.GLOBAL_VARIABLES` and `ops.GraphKeys.MODEL_VARIABLES`.
    trainable: If `True` also add the variable to the graph collection
      `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    cols_to_vars: If not `None`, must be a dictionary that will be filled with a
      mapping from `_FeatureColumn` to list of `Variable`s.  For example, after
      the call, we might have cols_to_vars = {_EmbeddingColumn(
      categorical_column=_HashedCategoricalColumn( key='sparse_feature',
      hash_bucket_size=5, dtype=tf.string), dimension=10): [<tf.Variable
      'some_variable:0' shape=(5, 10), <tf.Variable 'some_variable:1' shape=(5,
      10)]} If a column creates no variables, its value will be an empty list.
    cols_to_output_tensors: If not `None`, must be a dictionary that will be
      filled with a mapping from '_FeatureColumn' to the associated output
      `Tensor`s.

  Returns:
    A `Tensor` which represents input layer of a model. Its shape
    is (batch_size, first_layer_dimension) and its dtype is `float32`.
    first_layer_dimension is determined based on given `feature_columns`.

  Raises:
    ValueError: if an item in `feature_columns` is not a `_DenseColumn`.
  )r3   r4   rP   rO   )r_   )rR   rQ   r3   r4   rP   rO   s         r*   rT   rT      s$    ~ 
+3
5 5r-   c                       e Zd ZdZ	 	 	 	 	 ddZd Zed        Zed        Zed        Z	ed        Z
ed	        Zed
        Zed        Zy)
InputLayerzBAn object-oriented version of `input_layer` that reuses variables.Nc                     || _         || _        || _        || _        || _        t        j                  | j                  t        |      | _        | j                  j                  | _
        y)zSee `input_layer`.)create_scope_now_N)_feature_columns_weight_collections
_trainable_cols_to_vars_namer   make_templater_   _input_layer_templater   _scope)selfrQ   r3   r4   rP   r'   create_scope_nows          r*   __init__zInputLayer.__init__   s^     ,D1DDO%DDJ!)!7!7

)=M"OD,,;;DKr-   c                 l    | j                  || j                  | j                  | j                  d d      S )NT)rR   rQ   r3   r4   rP   r^   )rl   rf   rg   rh   rn   rR   s     r*   __call__zInputLayer.__call__   s<    %%--33// &  r-   c                     | j                   S r%   )rj   rn   s    r*   r'   zInputLayer.name   s    ::r-   c                 .    | j                   j                  S r%   )rl   non_trainable_variablesru   s    r*   rw   z"InputLayer.non_trainable_variables   s    %%===r-   c                 .    | j                   j                  S r%   )rl   non_trainable_weightsru   s    r*   ry   z InputLayer.non_trainable_weights  s    %%;;;r-   c                 .    | j                   j                  S r%   )rl   trainable_variablesru   s    r*   r{   zInputLayer.trainable_variables  s    %%999r-   c                 .    | j                   j                  S r%   )rl   trainable_weightsru   s    r*   r}   zInputLayer.trainable_weights  s    %%777r-   c                 .    | j                   j                  S r%   )rl   r   ru   s    r*   r   zInputLayer.variables  s    %%///r-   c                 .    | j                   j                  S r%   )rl   weightsru   s    r*   r   zInputLayer.weights  s    %%---r-   )NTNfeature_column_input_layerT)__name__
__module____qualname____doc__rp   rs   propertyr'   rw   ry   r{   r}   r   r    r-   r*   rc   rc      s    J #' 0 $<$   > > < < : : 8 8 0 0 . .r-   rc   zfeature_column.linear_modelc                     t        j                   dd      5 }t        |j                        }ddd       t        |||||      }	 |	|       }
||j	                  |	j                                |
S # 1 sw Y   ExY w)a  Returns a linear prediction `Tensor` based on given `feature_columns`.

  This function generates a weighted sum based on output dimension `units`.
  Weighted sum refers to logits in classification problems. It refers to the
  prediction itself for linear regression problems.

  Note on supported columns: `linear_model` treats categorical columns as
  `indicator_column`s. To be specific, assume the input as `SparseTensor` looks
  like:

  ```python
    shape = [2, 2]
    {
        [0, 0]: "a"
        [1, 0]: "b"
        [1, 1]: "c"
    }
  ```
  `linear_model` assigns weights for the presence of "a", "b", "c' implicitly,
  just like `indicator_column`, while `input_layer` explicitly requires wrapping
  each of categorical columns with an `embedding_column` or an
  `indicator_column`.

  Example of usage:

  ```python
  price = numeric_column('price')
  price_buckets = bucketized_column(price, boundaries=[0., 10., 100., 1000.])
  keywords = categorical_column_with_hash_bucket("keywords", 10K)
  keywords_price = crossed_column('keywords', price_buckets, ...)
  columns = [price_buckets, keywords, keywords_price ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  prediction = linear_model(features, columns)
  ```

  The `sparse_combiner` argument works as follows
  For example, for two features represented as the categorical columns:

  ```python
    # Feature 1

    shape = [2, 2]
    {
        [0, 0]: "a"
        [0, 1]: "b"
        [1, 0]: "c"
    }

    # Feature 2

    shape = [2, 3]
    {
        [0, 0]: "d"
        [1, 0]: "e"
        [1, 1]: "f"
        [1, 2]: "f"
    }
  ```

  with `sparse_combiner` as "mean", the linear model outputs consequently
  are:

  ```
    y_0 = 1.0 / 2.0 * ( w_a + w_b ) + w_d + b
    y_1 = w_c + 1.0 / 3.0 * ( w_e + 2.0 * w_f ) + b
  ```

  where `y_i` is the output, `b` is the bias, and `w_x` is the weight
  assigned to the presence of `x` in the input features.

  Args:
    features: A mapping from key to tensors. `_FeatureColumn`s look up via these
      keys. For example `numeric_column('price')` will look at 'price' key in
      this dict. Values are `Tensor` or `SparseTensor` depending on
      corresponding `_FeatureColumn`.
    feature_columns: An iterable containing the FeatureColumns to use as inputs
      to your model. All items should be instances of classes derived from
      `_FeatureColumn`s.
    units: An integer, dimensionality of the output space. Default value is 1.
    sparse_combiner: A string specifying how to reduce if a categorical column
      is multivalent. Except `numeric_column`, almost all columns passed to
      `linear_model` are considered as categorical columns.  It combines each
      categorical column independently. Currently "mean", "sqrtn" and "sum" are
      supported, with "sum" the default for linear model. "sqrtn" often achieves
      good accuracy, in particular with bag-of-words columns.
        * "sum": do not
        normalize features in the column
        * "mean": do l1 normalization on features
        in the column
        * "sqrtn": do l2 normalization on features in the column
    weight_collections: A list of collection names to which the Variable will be
      added. Note that, variables will also be added to collections
      `tf.GraphKeys.GLOBAL_VARIABLES` and `ops.GraphKeys.MODEL_VARIABLES`.
    trainable: If `True` also add the variable to the graph collection
      `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
    cols_to_vars: If not `None`, must be a dictionary that will be filled with a
      mapping from `_FeatureColumn` to associated list of `Variable`s.  For
      example, after the call, we might have cols_to_vars = { _NumericColumn(
      key='numeric_feature1', shape=(1,): [<tf.Variable
      'linear_model/price2/weights:0' shape=(1, 1)>], 'bias': [<tf.Variable
      'linear_model/bias_weights:0' shape=(1,)>], _NumericColumn(
      key='numeric_feature2', shape=(2,)): [<tf.Variable
      'linear_model/price1/weights:0' shape=(2, 1)>]} If a column creates no
      variables, its value will be an empty list. Note that cols_to_vars will
      also contain a string key 'bias' that maps to a list of Variables.

  Returns:
    A `Tensor` which represents predictions/logits of a linear model. Its shape
    is (batch_size, units) and its dtype is `float32`.

  Raises:
    ValueError: if an item in `feature_columns` is neither a `_DenseColumn`
      nor `_CategoricalColumn`.
  Nlinear_model)rQ   unitssparse_combinerr3   r4   r'   )r   _strip_leading_slashesr'   _LinearModelupdaterP   )rR   rQ   r   r   r3   r4   rP   vs
model_namelinear_model_layerretvals              r*   r   r     s    x $$T>: 1b'0J1#%%+ h'&*779:	-1 1s   A11A:c                     |D ]w  }|t         j                  j                  k(  r!t        | t        j
                        r't        |       D ]  }t        j                  ||        bt        j                  ||        y y)zAdds a var to the list of weight_collections provided.

  Handles the case for partitioned and non-partitioned variables.

  Args:
    var: A variable or Partitioned Variable.
    weight_collections: List of collections to add variable to.
  N)r   rC   rD   rX   r   PartitionedVariabler\   add_to_collection)varr3   weight_collectionconstituent_vars       r*   _add_to_collectionsr     st     . 
4CMM::: #y445!#Y B//AB 
-s3
4r-   c                   :     e Zd ZdZ	 	 	 	 	 d fd	Zd Zd Z xZS )_FCLinearWrapperz\Wraps a _FeatureColumn in a layer for use in a linear model.

  See `linear_model` above.
  c                 j    t        t        | 
  d||d| || _        || _        || _        || _        y Nr4   r'   r   )superr   rp   _feature_column_units_sparse_combinerrg   )	rn   feature_columnr   r   r3   r4   r'   kwargs	__class__s	           r*   rp   z_FCLinearWrapper.__init__  sF     

D* 2$2*02)DDK+D1Dr-   c                    t        | j                  t              rR| j                  d| j                  j                  | j
                  ft        j                         | j                        }na| j                  j                  j                         }| j                  d|| j
                  gt        j                         | j                        }t        || j                         || _        d| _        y )Nr   )r'   r6   initializerr4   T)rX   r   _CategoricalColumnadd_variable_num_bucketsr   r   zeros_initializerr4   r?   r@   r   rg   _weight_varbuilt)rn   _weightr@   s       r*   buildz_FCLinearWrapper.build  s    $&&(:;  %%22DKK@002NN	 ! $f ))99FFHl  t{{+002NN	 ! $f
  8 89DDJr-   c           	          t        | j                  || j                  | j                  | j                  | j
                  | j                        }|S )NrK   rH   r   r   r3   r4   
weight_var)_create_weighted_sumr   r   r   rg   r4   r   )rn   rH   weighted_sums      r*   callz_FCLinearWrapper.call  sI    '##kk--33..##%L r-   r9   sumNTNr   r   r   r   rp   r   r   __classcell__r   s   @r*   r   r     s(     $"&2$	r-   r   c                   8     e Zd ZdZ	 	 	 	 d fd	Zd Zd Z xZS )
_BiasLayerzA layer for the bias term.c                 N    t        t        | 
  d||d| || _        || _        y r   )r   r   rp   r   rg   )rn   r   r4   r3   r'   r   r   s         r*   rp   z_BiasLayer.__init__  s-     
*d$NytNvNDK1Dr-   c                     | j                  d| j                  gt        j                         | j                        | _        t        | j
                  | j                         d| _        y )Nbias_weights)r6   r   r4   T)	r   r   r   r   r4   _bias_variabler   rg   r   rn   r   s     r*   r   z_BiasLayer.build  sX    ++{{m..0..	 , "D
 ++T-E-EFDJr-   c                     | j                   S r%   )r   r   s     r*   r   z_BiasLayer.call  s    r-   )r9   TNNr   r   s   @r*   r   r     s#    " "&	2r-   r   c                 |    t        | t        j                        st        j                  |       r| gS t        |       S r%   )rX   r   Variabler   is_resource_variabler\   )variables    r*   _get_expanded_variable_listr     s2    9--.00::>r-   c                 ,    | j                  dd      d   S )N/r9   )rsplitr&   s    r*   r   r     s    	S!	R	  r-   c                   @     e Zd ZdZ	 	 	 	 	 d fd	Zd Zd Zd Z xZS )r   zSCreates a linear model using feature columns.

  See `linear_model` for details.
  c           	      v   t        t        | 
  dd|i| d| _        t	        |      | _        t        |xs g       | _        t        j                  j                  | j                  vr3| j                  j                  t        j                  j                         t        j                  j                  | j                  vr3| j                  j                  t        j                  j                         i }t        | j
                  d       D ]a  }	t        j                  d |	j                        5 }
t!        |
j"                        }d d d        t%        |	||| j                  |fi |}|||<   c | j'                  |      | _        t+        d||| j                  dd|| _        i | _        y # 1 sw Y   hxY w)	Nr'   Tc                     | j                   S r%   r&   r(   s    r*   r+   z'_LinearModel.__init__.<locals>.<lambda>/  s
    aff r-   r.   r0   
bias_layer)r   r4   r3   r'   r   )r   r   rp   _keras_stylerW   rf   r\   rg   r   rC   rD   r<   r]   r;   r   r=   r   r'   r   _add_layers_column_layersr   _bias_layerri   )rn   rQ   r   r   r3   r4   r'   r   column_layersrK   r   column_namecolumn_layerr   s                r*   rp   z_LinearModel.__init__  s    
,&;D;F; D6GD#$6$<"=D
}}%%T-E-EE
%%cmm&D&DE
}}$$D,D,DD
%%cmm&C&CDM..4DE 
0((
V335 68: -RWW56 &fe_&*&>&>	&1=5;=l $0mK 
0 **=9D! 33	
 D D#6 6s   2F//F8	c                     | j                   S )zReturns a dict mapping _FeatureColumns to variables.

    See `linear_model` for more information.
    This is not populated till `call` is called i.e. layer is built.
    )ri   ru   s    r*   rP   z_LinearModel.cols_to_varsC  s     r-   c           	      |   t        j                   | j                        5  | j                  D ]2  }t        |t        t
        f      rt        dj                  |             g }g }t        |      }t        | j                  j                         d       D ]~  }|j                  }|j                  |        ||      }|j                  |       t        j                  t        j                   j"                  |j$                        | j&                  |<    t)        ||       t+        j,                  |d      }t/        j0                  || j3                  |t        j4                               d      }	| j2                  j6                  d   }
t9        |
      | j&                  d	<   d d d        |	S # 1 sw Y   	S xY w)
NzWItems of feature_columns must be either a _DenseColumn or _CategoricalColumn. Given: {}c                     | j                   S r%   r&   r(   s    r*   r+   z#_LinearModel.call.<locals>.<lambda>U  s
    aff r-   r.   r7   weighted_sum_no_biasr&   r   r   bias)r   r'   rf   rX   rY   r   rZ   r[   r:   r;   r   rV   r   r<   r   rB   rC   rD   
scope_nameri   rF   r   add_nr   bias_addr   rE   r   r   )rn   rR   rK   weighted_sumsrJ   rH   layerr   predictions_no_biaspredictionsr   s              r*   r   z_LinearModel.callK  s   		&	&tyy	1 E)) N&&<1C"DE>>DfVnN NN
 moX&g$--446<LM D%&&v&W~\*%(%7%7MM**%2B2B&D6"D )H$NN
46OO



"557  9 k ''*d#>t#Dd 7E8 9E8 s   &F1E F11F;c                 V    |j                         D ]  \  }}t        | d|z  |        |S )Nzlayer-%s)itemssetattr)rn   layersr'   r   s       r*   r   z_LinearModel._add_layersj  s3     ||~ .edJ%u-.Mr-   r   )	r   r   r   r   rp   rP   r   r   r   r   s   @r*   r   r     s.     $"&)V>r-   r   c                 j   t        |      }i }t        j                  dd| j                               5  t	        |       }t        |d       D ]@  }t        j                  d|j                        5  |j                  |      ||<   ddd       B 	 ddd       |S # 1 sw Y   WxY w# 1 sw Y   |S xY w)a  Returns transformed features based on features columns passed in.

  Please note that most probably you would not need to use this function. Please
  check `input_layer` and `linear_model` to see whether they will
  satisfy your use case or not.

  Example:

  ```python
  # Define features and transformations
  crosses_a_x_b = crossed_column(
      columns=["sparse_feature_a", "sparse_feature_b"], hash_bucket_size=10000)
  price_buckets = bucketized_column(
      source_column=numeric_column("price"), boundaries=[...])

  columns = [crosses_a_x_b, price_buckets]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  transformed = transform_features(features=features, feature_columns=columns)

  assertCountEqual(columns, transformed.keys())
  ```

  Args:
    features: A mapping from key to tensors. `_FeatureColumn`s look up via these
      keys. For example `numeric_column('price')` will look at 'price' key in
      this dict. Values can be a `SparseTensor` or a `Tensor` depends on
      corresponding `_FeatureColumn`.
    feature_columns: An iterable containing all the `_FeatureColumn`s.

  Returns:
    A `dict` mapping `_FeatureColumn` to `Tensor` and `SparseTensor` values.
  Ntransform_featuresrU   c                     | j                   S r%   r&   r(   s    r*   r+   z%_transform_features.<locals>.<lambda>  r,   r-   r.   r0   )rW   r   
name_scoperV   r:   r;   r'   get)rR   rQ   outputsrH   rK   s        r*   _transform_featuresr   s  s    B /?/'
~~
-hoo6GI .8$G.>? .>>$V[[9 .!++f-. ... 
.. .	. 
.s#   >B(2B
B(B%!B((B2z&feature_column.make_parse_example_specc           
      8   i }| D ]  }t        |t              st        dj                  |            |j                  }t        j                  |      D ]1  \  }}||v s|||   k7  st        dj                  ||||                |j                  |        |S )aa  Creates parsing spec dictionary from input feature_columns.

  The returned dictionary can be used as arg 'features' in
  `tf.io.parse_example`.

  Typical usage example:

  ```python
  # Define features and transformations
  feature_a = categorical_column_with_vocabulary_file(...)
  feature_b = numeric_column(...)
  feature_c_bucketized = bucketized_column(numeric_column("feature_c"), ...)
  feature_a_x_feature_c = crossed_column(
      columns=["feature_a", feature_c_bucketized], ...)

  feature_columns = set(
      [feature_b, feature_c_bucketized, feature_a_x_feature_c])
  features = tf.io.parse_example(
      serialized=serialized_examples,
      features=make_parse_example_spec(feature_columns))
  ```

  For the above example, make_parse_example_spec would return the dict:

  ```python
  {
      "feature_a": parsing_ops.VarLenFeature(tf.string),
      "feature_b": parsing_ops.FixedLenFeature([1], dtype=tf.float32),
      "feature_c": parsing_ops.FixedLenFeature([1], dtype=tf.float32)
  }
  ```

  Args:
    feature_columns: An iterable containing all feature columns. All items
      should be instances of classes derived from `_FeatureColumn`.

  Returns:
    A dict mapping each feature key to a `FixedLenFeature` or `VarLenFeature`
    value.

  Raises:
    ValueError: If any of the given `feature_columns` is not a `_FeatureColumn`
      instance.
  z?All feature_columns must be _FeatureColumn instances. Given: {}zHfeature_columns contain different parse_spec for key {}. Given {} and {})rX   _FeatureColumnrZ   r[   _parse_example_specsix	iteritemsr   )rQ   resultrK   configr/   values         r*   make_parse_example_specr     s    ` & 	ffn- ##)6&>3 3''FmmF+ P
U	5F3K/ //5vc5&+/NP 	PP MM&	 
-r-   c	                   
 ||dk  rt        dj                  |            |du |du k7  rt        d      /t              s$t        dj                  | j                              -t	        j
                  ddt        j                  |      z        | j                  |f

fd}	t        | |||	||||		      S )
a>
  `_DenseColumn` that converts from sparse, categorical input.

  Use this when your inputs are sparse, but you want to convert them to a dense
  representation (e.g., to feed to a DNN).

  Inputs must be a `_CategoricalColumn` created by any of the
  `categorical_column_*` function. Here is an example of using
  `embedding_column` with `DNNClassifier`:

  Args:
    categorical_column: A `_CategoricalColumn` created by a
      `categorical_column_with_*` function. This column produces the sparse IDs
      that are inputs to the embedding lookup.
    dimension: An integer specifying dimension of the embedding, must be > 0.
    combiner: A string specifying how to reduce if there are multiple entries in
      a single row. Currently 'mean', 'sqrtn' and 'sum' are supported, with
      'mean' the default. 'sqrtn' often achieves good accuracy, in particular
      with bag-of-words columns. Each of this can be thought as example level
      normalizations on the column. For more information, see
      `tf.embedding_lookup_sparse`.
    initializer: A variable initializer function to be used in embedding
      variable initialization. If not specified, defaults to
      `tf.compat.v1.truncated_normal_initializer` with mean `0.0` and standard
      deviation `1/sqrt(dimension)`.
    ckpt_to_load_from: String representing checkpoint name/pattern from which to
      restore column weights. Required if `tensor_name_in_ckpt` is not `None`.
    tensor_name_in_ckpt: Name of the `Tensor` in `ckpt_to_load_from` from which
      to restore the column weights. Required if `ckpt_to_load_from` is not
      `None`.
    max_norm: If not `None`, embedding values are l2-normalized to this value.
    trainable: Whether or not the embedding is trainable. Default is True.
    use_safe_embedding_lookup: If true, uses safe_embedding_lookup_sparse
      instead of embedding_lookup_sparse. safe_embedding_lookup_sparse ensures
      there are no empty rows and all weights and ids are positive at the
      expense of extra compute cost. This only applies to rank 2 (NxM) shaped
      input tensors. Defaults to true, consider turning off if the above checks
      are not needed. Note that having empty rows will not trigger any error
      though the output result might be 0 or omitted.

  Returns:
    `_DenseColumn` that converts from sparse input.

  Raises:
    ValueError: if `dimension` not > 0.
    ValueError: if exactly one of `ckpt_to_load_from` and `tensor_name_in_ckpt`
      is specified.
    ValueError: if `initializer` is specified and is not callable.
    RuntimeError: If eager execution is enabled.
  Nr9   zInvalid dimension {}.zPMust specify both `ckpt_to_load_from` and `tensor_name_in_ckpt` or none of them.zGinitializer must be callable if specified. Embedding of column_name: {}        )meanstddevc                 8    t        | d      } |d |      S )Nembedding_column_layer)embedding_shaper   r3   r4   r'   r7   )_EmbeddingColumnLayer)r3   r8   r   r   r   r4   s      r*   _creatorz#_embedding_column.<locals>._creator'  s,    2'-%' "$e44r-   	categorical_column	dimensioncombinerlayer_creatorckpt_to_load_fromtensor_name_in_ckptmax_normr4   use_safe_embedding_lookup)
rZ   r[   callabler'   r   truncated_normal_initializermathsqrtr   _EmbeddingColumn)r  r  r  r   r  r  r  r4   r	  r   r   s      `   `  @r*   _embedding_columnr    s    t Y]
,33I>
??4%8D%@A
 > ? ? (=
 44:F+00523 3 77TYYy113K '33Y>/5 
+)- 9	
; 	;r-   )r9   c                 H   t        ||       }|j                  s'|j                  st        dj	                  ||             t        j                  ||||       }|%t        |      st        dj	                  |            t        j                  |        t        | ||||      S )a	  Represents real valued or numerical features.

  Example:

  ```python
  price = numeric_column('price')
  columns = [price, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  dense_tensor = input_layer(features, columns)

  # or
  bucketized_price = bucketized_column(price, boundaries=[...])
  columns = [bucketized_price, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction = linear_model(features, columns)
  ```

  Args:
    key: A unique string identifying the input feature. It is used as the column
      name and the dictionary key for feature parsing configs, feature `Tensor`
      objects, and feature columns.
    shape: An iterable of integers specifies the shape of the `Tensor`. An
      integer can be given which means a single dimension `Tensor` with given
      width. The `Tensor` representing the column will have the shape of
      [batch_size] + `shape`.
    default_value: A single value compatible with `dtype` or an iterable of
      values compatible with `dtype` which the column takes on during
      `tf.Example` parsing if data is missing. A default value of `None` will
      cause `tf.io.parse_example` to fail if an example does not contain this
      column. If a single value is provided, the same value will be applied as
      the default value for every item. If an iterable of values is provided,
      the shape of the `default_value` should be equal to the given `shape`.
    dtype: defines the type of values. Default value is `tf.float32`. Must be a
      non-quantized, real integer or floating point type.
    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 `_NumericColumn`.

  Raises:
    TypeError: if any dimension in shape is not an int
    ValueError: if any dimension in shape is not a positive integer
    TypeError: if `default_value` is an iterable but not compatible with `shape`
    TypeError: if `default_value` is not compatible with `dtype`.
    ValueError: if `dtype` is not convertible to `tf.float32`.
  z6dtype must be convertible to float. dtype: {}, key: {}z+normalizer_fn must be a callable. Given: {})r6   default_valuedtypenormalizer_fn)_check_shape
is_integeris_floatingrZ   r[   fc_utilscheck_default_valuer
  	TypeErrorassert_key_is_string_NumericColumnr/   r6   r  r  r  s        r*   _numeric_columnr  <  s    p uc
"%


e//
 **0&*<> >..umUCP-x'>
5<<]KM M $		!!
# #r-   c                    t        | t              st        dj                  |             t	        | j
                        dkD  rt        dj                  |             |r t        |t              st        |t              st        d      t        t	        |      dz
        D ]  }||   ||dz      k\  st        d       t        | t        |            S )a  Represents discretized dense input.

  Buckets include the left boundary, and exclude the right boundary. Namely,
  `boundaries=[0., 1., 2.]` generates buckets `(-inf, 0.)`, `[0., 1.)`,
  `[1., 2.)`, and `[2., +inf)`.

  For example, if the inputs are

  ```python
  boundaries = [0, 10, 100]
  input tensor = [[-5, 10000]
                  [150,   10]
                  [5,    100]]
  ```

  then the output will be

  ```python
  output = [[0, 3]
            [3, 2]
            [1, 3]]
  ```

  Example:

  ```python
  price = numeric_column('price')
  bucketized_price = bucketized_column(price, boundaries=[...])
  columns = [bucketized_price, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction = linear_model(features, columns)

  # or
  columns = [bucketized_price, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  dense_tensor = input_layer(features, columns)
  ```

  A `bucketized_column` can also be crossed with another categorical column
  using `crossed_column`:

  ```python
  price = numeric_column('price')
  # bucketized_column converts numerical feature to a categorical one.
  bucketized_price = bucketized_column(price, boundaries=[...])
  # 'keywords' is a string feature.
  price_x_keywords = crossed_column([bucketized_price, 'keywords'], 50K)
  columns = [price_x_keywords, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction = linear_model(features, columns)
  ```

  Args:
    source_column: A one-dimensional dense column which is generated with
      `numeric_column`.
    boundaries: A sorted list or tuple of floats specifying the boundaries.

  Returns:
    A `_BucketizedColumn`.

  Raises:
    ValueError: If `source_column` is not a numeric column, or if it is not
      one-dimensional.
    ValueError: If `boundaries` is not a sorted list or tuple.
  zIsource_column must be a column generated with numeric_column(). Given: {}r9   z7source_column must be one-dimensional column. Given: {}z!boundaries must be a sorted list.)
rX   r  rZ   r[   lenr6   r\   tuplerange_BucketizedColumn)source_column
boundariesis      r*   _bucketized_columnr&    s    D 
M>	2
	F=)+ + 			!
 !!'!68 8
j$':j%+H
8
99Z1$% <a!}
1q5)):;;< 
=%
*;	<<r-   c                 
   |t        dj                  |             |dk  rt        dj                  ||             t        j                  |        t        j                  |dj                  |              t        | ||      S )aa  Represents sparse feature where ids are set by hashing.

  Use this when your sparse features are in string or integer format, and you
  want to distribute your inputs into a finite number of buckets by hashing.
  output_id = Hash(input_feature_string) % bucket_size for string type input.
  For int type input, the value is converted to its string representation first
  and then hashed by the same formula.

  For input dictionary `features`, `features[key]` is either `Tensor` or
  `SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int
  and `''` for string, which will be dropped by this feature column.

  Example:

  ```python
  keywords = categorical_column_with_hash_bucket("keywords", 10K)
  columns = [keywords, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction = linear_model(features, columns)

  # or
  keywords_embedded = embedding_column(keywords, 16)
  columns = [keywords_embedded, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  dense_tensor = input_layer(features, columns)
  ```

  Args:
    key: A unique string identifying the input feature. It is used as the column
      name and the dictionary key for feature parsing configs, feature `Tensor`
      objects, and feature columns.
    hash_bucket_size: An int > 1. The number of buckets.
    dtype: The type of features. Only string and integer types are supported.

  Returns:
    A `_HashedCategoricalColumn`.

  Raises:
    ValueError: `hash_bucket_size` is not greater than 1.
    ValueError: `dtype` is neither string nor integer.
  z%hash_bucket_size must be set. key: {}r9   zBhash_bucket_size must be at least 1. hash_bucket_size: {}, key: {}column_name: {}prefix)rZ   r[   r  r  assert_string_or_int_HashedCategoricalColumnr/   hash_bucket_sizer  s      r*   $_categorical_column_with_hash_bucketr/    s    X 
?FFsK
LL
 55;V)3601 1 $
.?.F.Fs.KL	!#'7	??r-   c                    |st        dj                  |             |wt        j                  |      st        dj                  |             t        j                  |      5 }t        d |D              }ddd       t        j                  d|| |       |dk  rt        dj                  |             |r<|t        dj                  |             |d	k  rt        d
j                  ||             t        j                  |dj                  |              t        j                  |        t        | |||d	n||d|      S ||      S # 1 sw Y   xY w)a  A `_CategoricalColumn` with a vocabulary file.

  Use this when your inputs are in string or integer format, and you have a
  vocabulary file that maps each value to an integer ID. By default,
  out-of-vocabulary values are ignored. Use either (but not both) of
  `num_oov_buckets` and `default_value` to specify how to include
  out-of-vocabulary values.

  For input dictionary `features`, `features[key]` is either `Tensor` or
  `SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int
  and `''` for string, which will be dropped by this feature column.

  Example with `num_oov_buckets`:
  File '/us/states.txt' contains 50 lines, each with a 2-character U.S. state
  abbreviation. All inputs with values in that file are assigned an ID 0-49,
  corresponding to its line number. All other values are hashed and assigned an
  ID 50-54.

  ```python
  states = categorical_column_with_vocabulary_file(
      key='states', vocabulary_file='/us/states.txt', vocabulary_size=50,
      num_oov_buckets=5)
  columns = [states, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction = linear_model(features, columns)
  ```

  Example with `default_value`:
  File '/us/states.txt' contains 51 lines - the first line is 'XX', and the
  other 50 each have a 2-character U.S. state abbreviation. Both a literal 'XX'
  in input, and other values missing from the file, will be assigned ID 0. All
  others are assigned the corresponding line number 1-50.

  ```python
  states = categorical_column_with_vocabulary_file(
      key='states', vocabulary_file='/us/states.txt', vocabulary_size=51,
      default_value=0)
  columns = [states, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction, _, _ = linear_model(features, columns)
  ```

  And to make an embedding with either:

  ```python
  columns = [embedding_column(states, 3),...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  dense_tensor = input_layer(features, columns)
  ```

  Args:
    key: A unique string identifying the input feature. It is used as the column
      name and the dictionary key for feature parsing configs, feature `Tensor`
      objects, and feature columns.
    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 `_CategoricalColumn` with a vocabulary file.

  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.
  zMissing vocabulary_file in {}.Nz%vocabulary_file in {} does not exist.c              3       K   | ]  }d   yw)r9   Nr   ).0r   s     r*   	<genexpr>z;_categorical_column_with_vocabulary_file.<locals>.<genexpr>o  s     >!A>s   z]vocabulary_size = %d in %s is inferred from the number of elements in the vocabulary_file %s.r9   zInvalid vocabulary_size in {}.;Can't specify both num_oov_buckets and default_value in {}.r   !Invalid num_oov_buckets {} in {}.r(  r)  r   )r/   vocabulary_filevocabulary_sizenum_oov_bucketsr  r  )rZ   r[   r   ExistsGFiler   logginginfor  r+  r   _VocabularyFileCategoricalColumn)r/   r6  r7  r8  r  r  fs          r*   (_categorical_column_with_vocabulary_filer?    sl   h 

5<<SA
BB<<(>EEcJKK	_	% *>q>)o*LL	%&5sOM
 q
5<<SA
BB 
H
O
O  :AA
3  ! !
.?.F.Fs.KL
$	)
%%*2a'/B
 
 6C
 '* *s   #EEc                    |t        |      dk  rt        dj                  ||             t        t        |            t        |      k7  rt        dj                  ||             t	        j
                  t        j                  |      j                        }|r?|dk7  rt        dj                  |             |dk  rt        dj                  ||             t        j                  |dj                  |       	       ||}n5|j                  |j                  k7  rt        d
j                  |||             t        j                  |dj                  |       	       t        j                  |        t        | t        |      |||      S )a  A `_CategoricalColumn` with in-memory vocabulary.

  Use this when your inputs are in string or integer format, and you have an
  in-memory vocabulary mapping each value to an integer ID. By default,
  out-of-vocabulary values are ignored. Use either (but not both) of
  `num_oov_buckets` and `default_value` to specify how to include
  out-of-vocabulary values.

  For input dictionary `features`, `features[key]` is either `Tensor` or
  `SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int
  and `''` for string, which will be dropped by this feature column.

  Example with `num_oov_buckets`:
  In the following example, each input in `vocabulary_list` is assigned an ID
  0-3 corresponding to its index (e.g., input 'B' produces output 2). All other
  inputs are hashed and assigned an ID 4-5.

  ```python
  colors = categorical_column_with_vocabulary_list(
      key='colors', vocabulary_list=('R', 'G', 'B', 'Y'),
      num_oov_buckets=2)
  columns = [colors, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction, _, _ = linear_model(features, columns)
  ```

  Example with `default_value`:
  In the following example, each input in `vocabulary_list` is assigned an ID
  0-4 corresponding to its index (e.g., input 'B' produces output 3). All other
  inputs are assigned `default_value` 0.


  ```python
  colors = categorical_column_with_vocabulary_list(
      key='colors', vocabulary_list=('X', 'R', 'G', 'B', 'Y'), default_value=0)
  columns = [colors, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction, _, _ = linear_model(features, columns)
  ```

  And to make an embedding with either:

  ```python
  columns = [embedding_column(colors, 3),...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  dense_tensor = input_layer(features, columns)
  ```

  Args:
    key: A unique string identifying the input feature. It is used as the column
      name and the dictionary key for feature parsing configs, feature `Tensor`
      objects, and feature columns.
    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 `_CategoricalColumn` with in-memory vocabulary.

  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.
  r9   z5vocabulary_list {} must be non-empty, column_name: {}z5Duplicate keys in vocabulary_list {}, column_name: {}r   r4  r   r5  zcolumn_name: {} vocabularyr)  z>dtype {} and vocabulary dtype {} do not match, column_name: {}r(  r/   vocabulary_listr  r  r8  )r  rZ   r[   setr   as_dtypenparrayr  r  r+  r  r   _VocabularyListCategoricalColumnr   )r/   rB  r  r  r8  vocabulary_dtypes         r*   (_categorical_column_with_vocabulary_listrI    s   ` 3#7!#;
?FFS	"# # 	_	#o"66
?FFS	"# # __RXXo%>%D%DE
H
O
O  :AA
3  ! !
;BB3GI
]E+666
HOO#S	*+ + .?.F.Fs.KL
$	)
O,!%
' 'r-   c                     |dk  rt        dj                  ||             |&|dk  s||k\  rt        dj                  |||             t        j                  |        t	        | ||      S )a  A `_CategoricalColumn` that returns identity values.

  Use this when your inputs are integers in the range `[0, num_buckets)`, and
  you want to use the input value itself as the categorical ID. Values outside
  this range will result in `default_value` if specified, otherwise it will
  fail.

  Typically, this is used for contiguous ranges of integer indexes, but
  it doesn't have to be. This might be inefficient, however, if many of IDs
  are unused. Consider `categorical_column_with_hash_bucket` in that case.

  For input dictionary `features`, `features[key]` is either `Tensor` or
  `SparseTensor`. If `Tensor`, missing values can be represented by `-1` for int
  and `''` for string, which will be dropped by this feature column.

  In the following examples, each input in the range `[0, 1000000)` is assigned
  the same value. All other inputs are assigned `default_value` 0. Note that a
  literal 0 in inputs will result in the same default ID.

  Linear model:

  ```python
  video_id = categorical_column_with_identity(
      key='video_id', num_buckets=1000000, default_value=0)
  columns = [video_id, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction, _, _ = linear_model(features, columns)
  ```

  Embedding for a DNN model:

  ```python
  columns = [embedding_column(video_id, 9),...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  dense_tensor = input_layer(features, columns)
  ```

  Args:
    key: A unique string identifying the input feature. It is used as the column
      name and the dictionary key for feature parsing configs, feature `Tensor`
      objects, and feature columns.
    num_buckets: Range of inputs and outputs is `[0, num_buckets)`.
    default_value: If set, values outside of range `[0, num_buckets)` will be
      replaced with this value. If not set, values >= num_buckets will cause a
      failure while values < 0 will be dropped.

  Returns:
    A `_CategoricalColumn` that returns identity values.

  Raises:
    ValueError: if `num_buckets` is less than one.
    ValueError: if `default_value` is not in range `[0, num_buckets)`.
  r9   z"num_buckets {} < 1, column_name {}r   z5default_value {} not in range [0, {}), column_name {}r/   num_bucketsr  )rZ   r[   r  r  _IdentityCategoricalColumnrK  s      r*   !_categorical_column_with_identityrN    s    l 1_
9@@S  }q'8'4'C
?FF;	-. . $	#
;m
E Er-   c                     t        |       S )a  Represents multi-hot representation of given categorical column.

  - For DNN model, `indicator_column` can be used to wrap any
    `categorical_column_*` (e.g., to feed to DNN). Consider to Use
    `embedding_column` if the number of buckets/unique(values) are large.

  - For Wide (aka linear) model, `indicator_column` is the internal
    representation for categorical column when passing categorical column
    directly (as any element in feature_columns) to `linear_model`. See
    `linear_model` for details.

  ```python
  name = indicator_column(categorical_column_with_vocabulary_list(
      'name', ['bob', 'george', 'wanda'])
  columns = [name, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  dense_tensor = input_layer(features, columns)

  dense_tensor == [[1, 0, 0]]  # If "name" bytes_list is ["bob"]
  dense_tensor == [[1, 0, 1]]  # If "name" bytes_list is ["bob", "wanda"]
  dense_tensor == [[2, 0, 0]]  # If "name" bytes_list is ["bob", "bob"]
  ```

  Args:
    categorical_column: A `_CategoricalColumn` which is created by
      `categorical_column_with_*` or `crossed_column` functions.

  Returns:
    An `_IndicatorColumn`.
  )_IndicatorColumn)r  s    r*   _indicator_columnrQ  A  s    > 
,	--r-   c                     ||j                   s&|j                  st        dj                  |            t	        | ||      S )a  Applies weight values to a `_CategoricalColumn`.

  Use this when each of your sparse inputs has both an ID and a value. For
  example, if you're representing text documents as a collection of word
  frequencies, you can provide 2 parallel sparse input features ('terms' and
  'frequencies' below).

  Example:

  Input `tf.Example` objects:

  ```proto
  [
    features {
      feature {
        key: "terms"
        value {bytes_list {value: "very" value: "model"}}
      }
      feature {
        key: "frequencies"
        value {float_list {value: 0.3 value: 0.1}}
      }
    },
    features {
      feature {
        key: "terms"
        value {bytes_list {value: "when" value: "course" value: "human"}}
      }
      feature {
        key: "frequencies"
        value {float_list {value: 0.4 value: 0.1 value: 0.2}}
      }
    }
  ]
  ```

  ```python
  categorical_column = categorical_column_with_hash_bucket(
      column_name='terms', hash_bucket_size=1000)
  weighted_column = weighted_categorical_column(
      categorical_column=categorical_column, weight_feature_key='frequencies')
  columns = [weighted_column, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction, _, _ = linear_model(features, columns)
  ```

  This assumes the input dictionary contains a `SparseTensor` for key
  'terms', and a `SparseTensor` for key 'frequencies'. These 2 tensors must have
  the same indices and dense shape.

  Args:
    categorical_column: A `_CategoricalColumn` created by
      `categorical_column_with_*` functions.
    weight_feature_key: String key for weight values.
    dtype: Type of weights, such as `tf.float32`. Only float and integer weights
      are supported.

  Returns:
    A `_CategoricalColumn` composed of two sparse features: one represents id,
    the other represents weight (value) of the id feature in that example.

  Raises:
    ValueError: if `dtype` is not convertible to float.
  z%dtype {} is not convertible to float.r  weight_feature_keyr  )r  r  rZ   r[   _WeightedCategoricalColumnrS  s      r*   _weighted_categorical_columnrV  c  sG    F mU--1B1B
<CCEJ
KK	#++
 r-   c                    |r|dk  rt        dj                  |            | rt        |       dk  rt        dj                  |             | D ]p  }t        |t        j
                        s*t        |t              st        dj                  |            t        |t              sXt        dj                  |             t        t        |       ||      S )a  Returns a column for performing crosses of categorical features.

  Crossed features are hashed according to `hash_bucket_size`. Conceptually,
  the transformation can be thought of as:
    Hash(cartesian product of features) % `hash_bucket_size`

  For example, if the input features are:

  * SparseTensor referred by first key:

    ```python
    shape = [2, 2]
    {
        [0, 0]: "a"
        [1, 0]: "b"
        [1, 1]: "c"
    }
    ```

  * SparseTensor referred by second key:

    ```python
    shape = [2, 1]
    {
        [0, 0]: "d"
        [1, 0]: "e"
    }
    ```

  then crossed feature will look like:

  ```python
   shape = [2, 2]
  {
      [0, 0]: Hash64("d", Hash64("a")) % hash_bucket_size
      [1, 0]: Hash64("e", Hash64("b")) % hash_bucket_size
      [1, 1]: Hash64("e", Hash64("c")) % hash_bucket_size
  }
  ```

  Here is an example to create a linear model with crosses of string features:

  ```python
  keywords_x_doc_terms = crossed_column(['keywords', 'doc_terms'], 50K)
  columns = [keywords_x_doc_terms, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction = linear_model(features, columns)
  ```

  You could also use vocabulary lookup before crossing:

  ```python
  keywords = categorical_column_with_vocabulary_file(
      'keywords', '/path/to/vocabulary/file', vocabulary_size=1K)
  keywords_x_doc_terms = crossed_column([keywords, 'doc_terms'], 50K)
  columns = [keywords_x_doc_terms, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction = linear_model(features, columns)
  ```

  If an input feature is of numeric type, you can use
  `categorical_column_with_identity`, or `bucketized_column`, as in the example:

  ```python
  # vertical_id is an integer categorical feature.
  vertical_id = categorical_column_with_identity('vertical_id', 10K)
  price = numeric_column('price')
  # bucketized_column converts numerical feature to a categorical one.
  bucketized_price = bucketized_column(price, boundaries=[...])
  vertical_id_x_price = crossed_column([vertical_id, bucketized_price], 50K)
  columns = [vertical_id_x_price, ...]
  features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
  linear_prediction = linear_model(features, columns)
  ```

  To use crossed column in DNN model, you need to add it in an embedding column
  as in this example:

  ```python
  vertical_id_x_price = crossed_column([vertical_id, bucketized_price], 50K)
  vertical_id_x_price_embedded = embedding_column(vertical_id_x_price, 10)
  dense_tensor = input_layer(features, [vertical_id_x_price_embedded, ...])
  ```

  Args:
    keys: An iterable identifying the features to be crossed. Each element can
      be either:
      * string: Uses the corresponding feature which must be of string type.
      * `_CategoricalColumn`: Uses the transformed tensor produced by this
        column. Does not support hashed categorical column.
    hash_bucket_size: An int > 1. The number of buckets.
    hash_key: Specify the hash_key that will be used by the `FingerprintCat64`
      function to combine the crosses fingerprints on SparseCrossOp (optional).

  Returns:
    A `_CrossedColumn`.

  Raises:
    ValueError: If `len(keys) < 2`.
    ValueError: If any of the keys is neither a string nor `_CategoricalColumn`.
    ValueError: If any of the keys is `_HashedCategoricalColumn`.
    ValueError: If `hash_bucket_size < 1`.
  r9   z2hash_bucket_size must be > 1. hash_bucket_size: {}   z.keys must be a list with length > 1. Given: {}zvUnsupported key type. All keys must be either string, or categorical column except _HashedCategoricalColumn. Given: {}zcategorical_column_with_hash_bucket is not supported for crossing. Hashing before crossing will increase probability of collision. Instead, use the feature name as a string. Given: {}keysr.  hash_key)
rZ   r[   r  rX   r   string_typesr   r,  _CrossedColumnr   )rZ  r.  r[  r/   s       r*   _crossed_columnr^    s    P 
-1
 ,,2F3C,DF F	TQ
8??EG G NcsC,,-s./fSk# # #/0AAGN NN 
;)9H
N Nr-   c                   <     e Zd ZdZ	 	 	 d fd	Zd Zd Zd Z xZS )r   zBA layer that stores all the state required for a embedding column.c                 \    t        t        | 
  d||d| || _        || _        || _        y)a  Constructor.

    Args:
      embedding_shape: Shape of the embedding variable used for lookup.
      initializer: A variable initializer function to be used in embedding
        variable initialization.
      weight_collections: A list of collection names to which the Variable will
        be added. Note that, variables will also be added to collections
        `tf.GraphKeys.GLOBAL_VARIABLES` and `ops.GraphKeys.MODEL_VARIABLES`.
      trainable: If `True` also add the variable to the graph collection
        `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
      name: Name of the layer
      **kwargs: keyword named properties.
    r   Nr   )r   r   rp   _embedding_shape_initializerrg   )rn   r   r   r3   r4   r'   r   r   s          r*   rp   z_EmbeddingColumnLayer.__init__0  s?    * 

/ 2$2*02+D#D1Dr-   c                     || _         y)zSets the weight collections for the layer.

    Args:
      weight_collections: A list of collection names to which the Variable will
        be added.
    N)rg   )rn   r3   s     r*   set_weight_collectionsz,_EmbeddingColumnLayer.set_weight_collectionsK  s      2Dr-   c                     | j                  d| j                  t        j                  | j                  | j
                        | _        | j                  r4t        j                         s t        | j                  | j                         d| _        y )Nembedding_weights)r'   r6   r  r   r4   T)r   ra  r   float32rb  r4   _embedding_weight_varrg   r   executing_eagerlyr   r   r   s     r*   r   z_EmbeddingColumnLayer.buildT  sp    !%!2!2 ##nn%%.. "3 ""D (A(A(C$44d6N6NODJr-   c                     | j                   S r%   )rh  r   s     r*   r   z_EmbeddingColumnLayer.call_  s    %%%r-   )NTN)	r   r   r   r   rp   rd  r   r   r   r   s   @r*   r   r   -  s%    J
 #'262	&r-   r   c                       e Zd ZdZej
                  d        Zd Zd Ze	d        Z
ej                  d        Zej
                  d        Zd Zy	)
r   aW  Represents a feature column abstraction.

  WARNING: Do not subclass this layer unless you know what you are doing:
  the API is subject to future changes.

  To distinguish the concept of a feature family and a specific binary feature
  within a family, we refer to a feature family like "country" as a feature
  column. Following is an example feature in a `tf.Example` format:
    {key: "country",  value: [ "US" ]}
  In this example the value of feature is "US" and "country" refers to the
  column of the feature.

  This class is an abstract class. User should not create instances of this.
  c                      y)z3Returns string. Used for naming and for name_scope.Nr   ru   s    r*   r'   z_FeatureColumn.namet       	r-   c                 0    t        |       t        |      k  S )a8  Allows feature columns to be sorted in Python 3 as they are in Python 2.

    Feature columns need to occasionally be sortable, for example when used as
    keys in a features dictionary passed to a layer.

    In CPython, `__lt__` must be defined for all objects in the
    sequence being sorted. If any objects do not have an `__lt__` compatible
    with feature column objects (such as strings), then CPython will fall back
    to using the `__gt__` method below.
    https://docs.python.org/3/library/stdtypes.html#list.sort

    Args:
      other: The other object to compare to.

    Returns:
      True if the string representation of this object is lexicographically less
      than the string representation of `other`. For FeatureColumn objects,
      this looks like "<__main__.FeatureColumn object at 0xa>".
    strrn   others     r*   __lt__z_FeatureColumn.__lt__y  s    ( t9s5z!!r-   c                 0    t        |       t        |      kD  S )a  Allows feature columns to be sorted in Python 3 as they are in Python 2.

    Feature columns need to occasionally be sortable, for example when used as
    keys in a features dictionary passed to a layer.

    `__gt__` is called when the "other" object being compared during the sort
    does not have `__lt__` defined.
    Example:
    ```
    # __lt__ only class
    class A():
      def __lt__(self, other): return str(self) < str(other)

    a = A()
    a < "b" # True
    "0" < a # Error

    # __lt__ and __gt__ class
    class B():
      def __lt__(self, other): return str(self) < str(other)
      def __gt__(self, other): return str(self) > str(other)

    b = B()
    b < "c" # True
    "0" < b # True
    ```


    Args:
      other: The other object to compare to.

    Returns:
      True if the string representation of this object is lexicographically
      greater than the string representation of `other`. For FeatureColumn
      objects, this looks like "<__main__.FeatureColumn object at 0xa>".
    ro  rq  s     r*   __gt__z_FeatureColumn.__gt__  s    J t9s5z!!r-   c                     | j                   S )z?Returns string. Used for variable_scope. Defaults to self.name.r&   ru   s    r*   r=   z_FeatureColumn._var_scope_name  s     99r-   c                      y)a]  Returns intermediate representation (usually a `Tensor`).

    Uses `inputs` to create an intermediate representation (usually a `Tensor`)
    that other feature columns can use.

    Example usage of `inputs`:
    Let's say a Feature column depends on raw feature ('raw') and another
    `_FeatureColumn` (input_fc). To access corresponding `Tensor`s, inputs will
    be used as follows:

    ```python
    raw_tensor = inputs.get('raw')
    fc_tensor = inputs.get(input_fc)
    ```

    Args:
      inputs: A `_LazyBuilder` object to access inputs.

    Returns:
      Transformed feature `Tensor`.
    Nr   rn   inputss     r*   _transform_featurez!_FeatureColumn._transform_feature      . 	r-   c                      y)ax  Returns a `tf.Example` parsing spec as dict.

    It is used for get_parsing_spec for `tf.io.parse_example`. Returned spec is
    a dict from keys ('string') to `VarLenFeature`, `FixedLenFeature`, and other
    supported objects. Please check documentation of `tf.io.parse_example` for
    all supported spec objects.

    Let's say a Feature column depends on raw feature ('raw') and another
    `_FeatureColumn` (input_fc). One possible implementation of
    _parse_example_spec is as follows:

    ```python
    spec = {'raw': tf.io.FixedLenFeature(...)}
    spec.update(input_fc._parse_example_spec)
    return spec
    ```
    Nr   ru   s    r*   r   z"_FeatureColumn._parse_example_spec  s    & 	r-   c                      y)zResets the configuration in the column.

    Some feature columns e.g. embedding or shared embedding columns might
    have some state that is needed to be reset sometimes. Use this method
    in that scenario.
    Nr   ru   s    r*   _reset_configz_FeatureColumn._reset_config  s    r-   N)r   r   r   r   abcabstractpropertyr'   rs  ru  r   r=   abstractmethodrz  r   r~  r   r-   r*   r   r   c  sy     	 	",%"N   	 	0 	 	(r-   r   c                   Z    e Zd ZdZej
                  d        Zej                  dd       Zy)rY   a  Represents a column which can be represented as `Tensor`.

  WARNING: Do not subclass this layer unless you know what you are doing:
  the API is subject to future changes.

  Some examples of this type are: numeric_column, embedding_column,
  indicator_column.
  c                      y)z>`TensorShape` of `_get_dense_tensor`, without batch dimension.Nr   ru   s    r*   r?   z_DenseColumn._variable_shape  rm  r-   Nc                      y)a  Returns a `Tensor`.

    The output of this function will be used by model-builder-functions. For
    example the pseudo code of `input_layer` will be like:

    ```python
    def input_layer(features, feature_columns, ...):
      outputs = [fc._get_dense_tensor(...) for fc in feature_columns]
      return tf.concat(outputs)
    ```

    Args:
      inputs: A `_LazyBuilder` object to access inputs.
      weight_collections: List of graph collections to which Variables (if any
        will be created) are added.
      trainable: If `True` also add variables to the graph collection
        `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).

    Returns:
      `Tensor` of shape [batch_size] + `_variable_shape`.
    Nr   rn   ry  r3   r4   s       r*   r>   z_DenseColumn._get_dense_tensor  r{  r-   NN)	r   r   r   r   r  r  r?   r  r>   r   r-   r*   rY   rY     s;     	 	 	 	r-   rY   c           	      h    t        | t              rt        | ||||||      S t        | |||||      S )zGCreates a weighted sum for a dense/categorical column for linear_model.r   )rK   rH   r   r3   r4   r   )rX   r   '_create_categorical_column_weighted_sum!_create_dense_column_weighted_sumr   s          r*   r   r     sR     *+2'-  -- r-   c                 V   | j                  |||      }| j                  j                         }t        j                  |      d   }t        j
                  |||f      }||}	n.t        j                  d||gt        j                         ||      }	t        j                  ||	d      S )z9Create a weighted sum of a dense column for linear_model.r2   r   r5   r   r'   r6   r   r4   collectionsr   r&   )r>   r?   r@   r
   r6   rA   r   get_variabler   r   r   matmul)
rK   rH   r   r3   r4   r   rL   r@   rM   r   s
             r*   r  r  6  s     ##+ $ & ''446,v&q)*VJ+EF&F((U#..0&(F 
n	==r-   c                       e Zd ZdZ ej
                  dddg      Zej                  d        Z	ej                  	 	 dd       Zy)	r   zRepresents a categorical feature.

  WARNING: Do not subclass this layer unless you know what you are doing:
  the API is subject to future changes.

  A categorical feature typically handled with a `tf.sparse.SparseTensor` of
  IDs.
  IdWeightPair	id_tensorweight_tensorc                      y)1Returns number of buckets in this sparse feature.Nr   ru   s    r*   r   z_CategoricalColumn._num_buckets]  rm  r-   Nc                      y)a  Returns an IdWeightPair.

    `IdWeightPair` is a pair of `SparseTensor`s which represents ids and
    weights.

    `IdWeightPair.id_tensor` is typically a `batch_size` x `num_buckets`
    `SparseTensor` of `int64`. `IdWeightPair.weight_tensor` is either a
    `SparseTensor` of `float` or `None` to indicate all weights should be
    taken to be 1. If specified, `weight_tensor` must have exactly the same
    shape and indices as `sp_ids`. Expected `SparseTensor` is same as parsing
    output of a `VarLenFeature` which is a ragged matrix.

    Args:
      inputs: A `LazyBuilder` as a cache to get input tensors required to create
        `IdWeightPair`.
      weight_collections: List of graph collections to which variables (if any
        will be created) are added.
      trainable: If `True` also add variables to the graph collection
        `GraphKeys.TRAINABLE_VARIABLES` (see `tf.compat.v1.get_variable`).
    Nr   r  s       r*   _get_sparse_tensorsz&_CategoricalColumn._get_sparse_tensorsb  s    2 	r-   r  )r   r   r   r   r  
namedtupler  r  r  r   r  r  r   r-   r*   r   r   P  sa     (''{O46, 	 	  .2$(	 	r-   r   c                    | j                  |||      }t        j                  |j                  t	        j
                  |j                        d   dg      }|j                  }	|	.t        j                  |	t	        j
                  |	      d   dg      }	||}
n8t        j                  d| j                  |ft        j                         ||      }
t        j                  |
||	|d      S )a  Create a weighted sum of a categorical column for linear_model.

  Note to maintainer: As implementation details, the weighted sum is
  implemented via embedding_lookup_sparse toward efficiency. Mathematically,
  they are the same.

  To be specific, conceptually, categorical column can be treated as multi-hot
  vector. Say:

  ```python
    x = [0 0 1]  # categorical column input
    w = [a b c]  # weights
  ```
  The weighted sum is `c` in this case, which is same as `w[2]`.

  Another example is

  ```python
    x = [0 1 1]  # categorical column input
    w = [a b c]  # weights
  ```
  The weighted sum is `b + c` in this case, which is same as `w[2] + w[3]`.

  For both cases, we can implement weighted sum via embedding_lookup with
  sparse_combiner = "sum".
  r2   r   r   r   r  r   )sparse_weightsr  r'   )r  r   sparse_reshaper  r
   r6   r  r   r  r   r   r   r   safe_embedding_lookup_sparse)rK   rH   r   r   r3   r4   r   sparse_tensorsr  r  r   s              r*   r  r  ~  s    F --+ . . ''~//03R8:) !..---	6q92>@M F((""E*..0&(F 
	3	3"
 r-   c                   d    e Zd ZdZ ej
                  dddg      Zej                  	 	 dd       Z	y)_SequenceDenseColumnzRepresents dense sequence data.TensorSequenceLengthPairdense_tensorsequence_lengthNc                      y)z%Returns a `TensorSequenceLengthPair`.Nr   r  s       r*   _get_sequence_dense_tensorz/_SequenceDenseColumn._get_sequence_dense_tensor  s     	r-   r  )
r   r   r   r   r  r  r  r  r  r  r   r-   r*   r  r    sF    '3[33 >3D"EG  59+/	 	r-   r  c                   "    e Zd ZdZd Zd Zd Zy)r:   a  Handles caching of transformations while building the model.

  `_FeatureColumn` specifies how to digest an input column to the network. Some
  feature columns require data transformations. This class caches those
  transformations.

  Some features may be used in more than one place. For example, one can use a
  bucketized feature by itself and a cross with it. In that case we
  should create only one bucketization op instead of creating ops for each
  feature column separately. To handle re-use of transformed columns,
  `_LazyBuilder` caches all previously transformed columns.

  Example:
  We're trying to use the following `_FeatureColumn`s:

  ```python
  bucketized_age = fc.bucketized_column(fc.numeric_column("age"), ...)
  keywords = fc.categorical_column_with_hash_buckets("keywords", ...)
  age_X_keywords = fc.crossed_column([bucketized_age, "keywords"])
  ... = linear_model(features,
                          [bucketized_age, keywords, age_X_keywords]
  ```

  If we transform each column independently, then we'll get duplication of
  bucketization (one for cross, one for bucketization itself).
  The `_LazyBuilder` eliminates this duplication.
  c                 <    |j                         | _        i | _        y)a  Creates a `_LazyBuilder`.

    Args:
      features: A mapping from feature column to objects that are `Tensor` or
        `SparseTensor`, or can be converted to same via
        `sparse_tensor.convert_to_tensor_or_sparse_tensor`. A `string` key
        signifies a base feature (not-transformed). A `_FeatureColumn` key means
        that this `Tensor` is the output of an existing `_FeatureColumn` which
        can be reused.
    N)copy	_features_feature_tensorsrr   s     r*   rp   z_LazyBuilder.__init__  s     ]]_DNDr-   c                    || j                   v r| j                   |   S || j                  v r"| j                  |      }|| j                   |<   |S t        |t        j
                        rt        dj                  |            t        |t              st        dj                  |            |}t        j                  d|       |j                  |       }|$t        dj                  |j                              || j                   |<   |S )a&  Returns a `Tensor` for the given key.

    A `str` key is used to access a base feature (not-transformed). When a
    `_FeatureColumn` is passed, the transformed feature is returned if it
    already exists, otherwise the given `_FeatureColumn` is asked to provide its
    transformed output, which is then cached.

    Args:
      key: a `str` or a `_FeatureColumn`.

    Returns:
      The transformed `Tensor` corresponding to the `key`.

    Raises:
      ValueError: if key is not found or a transformed `Tensor` cannot be
        computed.
    z)Feature {} is not in features dictionary.z>"key" must be either a "str" or "_FeatureColumn". Provided: {}zTransforming feature_column %s.zColumn {} is not supported.)r  r  _get_raw_feature_as_tensorrX   r   r\  rZ   r[   r   r  r;  debugrz  r'   )rn   r/   feature_tensorrK   transformeds        r*   r   z_LazyBuilder.get  s    $ d###""3''
dnn66s;n#1dC #s''(BII#NOOc>* %%+VC[2 2 FMM3V<++D1K4;;FKKHII$/D&!r-   c           	      <   | j                   |   }t        j                  |      d j                         j                  }|/|dk(  rt        dj                  |            |dk7  rS        S t        j                  t        j                  t        j                        dj                  |            g      5  t        j                  t        j                  dt        j                              fdfd      cddd       S # 1 sw Y   yxY w)	a  Gets the raw_feature (keyed by `key`) as `tensor`.

    The raw feature is converted to (sparse) tensor and maybe expand dim.

    For both `Tensor` and `SparseTensor`, the rank will be expanded (to 2) if
    the rank is 1. This supports dynamic rank also. For rank 0 raw feature, will
    error out as it is not supported.

    Args:
      key: A `str` key to access the raw feature.

    Returns:
      A `Tensor` or `SparseTensor`.

    Raises:
      ValueError: if the raw feature has rank 0.
    c                     t        | t        j                        r.t        j                  | t        j                  |       d   dg      S t        j                  | d      S )Nr   r9   r   )rX   sparse_tensor_libSparseTensorr   r  r
   r6   expand_dims)input_tensors    r*   r  z<_LazyBuilder._get_raw_feature_as_tensor.<locals>.expand_dims8  sU    	L"3"@"@	A((*3//,*G*JA)NP 	P $$\266r-   Nr   z/Feature (key: {}) cannot have rank 0. Given: {}r9   )messagec                              S r%   r   )r  r  s   r*   r+   z9_LazyBuilder._get_raw_feature_as_tensor.<locals>.<lambda>Q  s    +n- r-   c                       S r%   r   )r  s   r*   r+   z9_LazyBuilder._get_raw_feature_as_tensor.<locals>.<lambda>Q  s    ~ r-   )r  r  "convert_to_tensor_or_sparse_tensor	get_shapendimsrZ   r[   r   control_dependenciesr   assert_positiver
   rankr   r   equal)rn   r/   raw_featurer  r  r  s       @@r*   r  z'_LazyBuilder._get_raw_feature_as_tensor"  s   $ ..%K&IIN7 ##%++D	=DD^%& 	&  $qy^Ik..II 
	!	!!!NN>*ELL^%	&# 
 G YY
..INN>:
;
-/EGG G Gs   ADDN)r   r   r   r   rp   r   r  r   r-   r*   r:   r:     s    8(T/Gr-   r:   c                     g }t        |       D ]-  }|r|j                  ||d   z         |j                  |       / |j                          |S )z5Returns moving offset for each dimension given shape.r   )reversedr<   reverse)r6   offsetsdims      r*   _shape_offsetsr  U  sO    'e_ cnnS72;&'nnS	
 
//	.r-   c           
         t        j                  |       } t        | t         j                        r| S t	        j
                  dd| |f      5  |S| j                  t        j                  k(  rd}n3| j                  j                  rd}n| j                  j                         }t        j                  || j                  d      }t        j                  t        j                  | |      d      }t        j                  |t        j                   | |d      t        j"                  | t        j$                  d	
            cddd       S # 1 sw Y   yxY w)a(  Converts a `Tensor` to a `SparseTensor`, dropping ignore_value cells.

  If `input_tensor` is already a `SparseTensor`, just return it.

  Args:
    input_tensor: A string or integer `Tensor`.
    ignore_value: Entries in `dense_tensor` equal to this value will be absent
      from the resulting `SparseTensor`. If `None`, default value of
      `dense_tensor`'s dtype will be used ('' for `str`, -1 for `int`).

  Returns:
    A `SparseTensor` with the same shape as `input_tensor`.

  Raises:
    ValueError: when `input_tensor`'s rank is `None`.
  Nto_sparse_input r   ignore_valuer&   indicesrV   dense_shape)out_typer'   r  rV   r  )r  r  rX   r  r   r   r  r   stringr  as_numpy_dtyper   castr
   where	not_equal	gather_ndr6   int64)r  r  r  s      r*   '_to_sparse_input_and_drop_ignore_valuesr  b  s$   " #EE,/<<=
~~d-0  F 			v}}	,((
 $))88:==l((~?Loo<6YHG))""<xHOO6<<mEF'F F Fs   C4E		Ec                    t        | t              r| g} t        | t        j                        rt	        |       } t        | t
              rt        d      | D ]6  }t        |t              rt        dj                  t        |      |             | st        d      i }| D ]G  }|j                  |v r(t        dj                  |||j                                 |||j                  <   I | S )a  Normalizes the `feature_columns` input.

  This method converts the `feature_columns` to list type as best as it can. In
  addition, verifies the type and other parts of feature_columns, required by
  downstream library.

  Args:
    feature_columns: The raw feature columns, usually passed by users.

  Returns:
    The normalized feature column list.

  Raises:
    ValueError: for any invalid inputs, such as empty, duplicated names, etc.
  z4Expected feature_columns to be iterable, found dict.zGItems of feature_columns must be a _FeatureColumn. Given (type {}): {}.z"feature_columns must not be empty.zDuplicate feature column name found for columns: {} and {}. This usually means that these columns refer to same base feature. Either one must be discarded or a duplicated but renamed item must be inserted in features dict.)
rX   r   r   Iteratorr\   dictrZ   r[   typer'   )rQ   rK   name_to_columns      r*   rW   rW     s      0&'O!9!9:?+O&
K
LL Lffn- ..4fT&\6.JL LL 

9
::. )f{{n$ ( )/v/=fkk/J)L	M M #)N6;;) 
r-   c                   N    e Zd ZdZed        Zed        Zd Zed        ZddZ	y)	r  zsee `numeric_column`.c                     | j                   S r%   r.   ru   s    r*   r'   z_NumericColumn.name      88Or-   c                     | j                   t        j                  | j                  | j                  | j
                        iS r%   )r/   r   FixedLenFeaturer6   r  r  ru   s    r*   r   z"_NumericColumn._parse_example_spec  s8     	''

DJJ(,(:(:< r-   c                 6   |j                  | j                        }t        |t        j                        r$t        dj                  | j                              | j                  | j                  |      }t        j                  |t        j                        S )NzeThe corresponding Tensor of numerical column must be a Tensor. SparseTensor is not supported. key: {})r   r/   rX   r  r  rZ   r[   r  r   r  r   rg  )rn   ry  r  s      r*   rz  z!_NumericColumn._transform_feature  sz    ::dhh'L, 1 > >?3396$((3CE E %''5l==v~~66r-   c                 @    t        j                  | j                        S r%   )r   TensorShaper6   ru   s    r*   r?   z_NumericColumn._variable_shape  s    ##DJJ//r-   Nc                 (    ~~|j                  |       S )a  Returns dense `Tensor` representing numeric feature.

    Args:
      inputs: A `_LazyBuilder` object to access inputs.
      weight_collections: Unused `weight_collections` since no variables are
        created in this function.
      trainable: Unused `trainable` bool since no variables are created in this
        function.

    Returns:
      Dense `Tensor` created within `_transform_feature`.
    )r   r  s       r*   r>   z _NumericColumn._get_dense_tensor  s     	 ::dr-   r  )
r   r   r   r   r   r'   r   rz  r?   r>   r   r-   r*   r  r    sK    
    7 0 0r-   r  r  c                   j    e Zd ZdZed        Zed        Zd Zed        Zd
dZ	ed        Z
	 	 d
d	Zy)r"  zSee `bucketized_column`.c                 L    dj                  | j                  j                        S )Nz{}_bucketized)r[   r#  r'   ru   s    r*   r'   z_BucketizedColumn.name  s    !!$"4"4"9"9::r-   c                 .    | j                   j                  S r%   )r#  r   ru   s    r*   r   z%_BucketizedColumn._parse_example_spec  s    111r-   c                 z    |j                  | j                        }t        j                  || j                        S )N)r$  )r   r#  r   
_bucketizer$  )rn   ry  source_tensors      r*   rz  z$_BucketizedColumn._transform_feature	  s3    JJt112M??$ $r-   c                     t        j                  t        | j                  j                        t        | j                        dz   fz         S )Nr9   )r   r  r   r#  r6   r  r$  ru   s    r*   r?   z!_BucketizedColumn._variable_shape		  sA    ##d  &&'3t+?!+C*EEG Gr-   Nc                     ~~|j                  |       }t        j                  t        j                  |t
        j                        t        | j                        dz   dd      S )Nr9         ?r   )r  depthon_value	off_value)	r   r
   one_hotr   r  r   r  r  r$  )rn   ry  r3   r4   r  s        r*   r>   z#_BucketizedColumn._get_dense_tensor	  sR    ::d#LlFLL9$//"Q&	 r-   c                 f    t        | j                        dz   | j                  j                  d   z  S )Nr9   r   )r  r$  r#  r6   ru   s    r*   r   z_BucketizedColumn._num_buckets	  s.      1$(:(:(@(@(CCCr-   c           
      N   |j                  |       }t        j                  |      d   }| j                  j                  d   }t        j                  t        j
                  t        j                  t        j                  d|      d      d|g      d      }t        j
                  t        j                  d|      |g      }t        j                  |d      t        | j                        dz   |z  z   }	t        j                  t        j                  t        j                  ||f            t        j                         }
t        j                  t        j                  ||g      t        j                         }t#        j$                  |
|	|      }t&        j)                  |d      S )zDConverts dense inputs to SparseTensor so downstream code can use it.r   r9   )r   r  N)r   r
   r6   r#  rA   tiler  r   r!  r  r$  r  	transposer   stackr   r  r  r  r   r  )rn   ry  r3   r4   r  rM   source_dimensioni1i2bucket_indicesr  r  r   s                r*   r  z%_BucketizedColumn._get_sparse_tensors	  s]   
 ::d#L.q1J))//2			!!(..J"?C !	#$)
+B 
q*:;j\	JB 	,	!$'$81$<#B	C  mmO112r(;<fllLG--z+;<=v||MK%22KIM**=$??r-   r  )r   r   r   r   r   r'   r   rz  r?   r>   r   r  r   r-   r*   r"  r"    sq     !; ; 2 2$ G G D D .2$(@r-   r"  r#  r$  c                   |     e Zd ZdZ	 d
 fd	Zed        Zed        Zd Zed        Z		 	 ddZ
ddZ	 	 dd	Z xZS )r  See `embedding_column`.c
                 >    t         t        |   | |||||||||	
      S )Nr  )r   r  __new__)clsr  r  r  r  r  r  r  r4   r	  r   s             r*   r  z_EmbeddingColumn.__new__D	  s=     !3/-#+/"; 0 
= 
=r-   c                     t        | d      s*dj                  | j                  j                        | _        | j                  S )Nrj   z{}_embeddinghasattrr[   r  r'   rj   ru   s    r*   r'   z_EmbeddingColumn.nameZ	  s4    4!!(()@)@)E)EFdj::r-   c                 .    | j                   j                  S r%   r  r   ru   s    r*   r   z$_EmbeddingColumn._parse_example_spec`	      ""666r-   c                 8    |j                  | j                        S r%   r   r  rx  s     r*   rz  z#_EmbeddingColumn._transform_featured	      ::d--..r-   c                 |    t        | d      s%t        j                  | j                  g      | _        | j                  S N_shaper   r   r  r  r	  ru   s    r*   r?   z _EmbeddingColumn._variable_shapeg	  /    4" ,,dnn-=>dk;;r-   c                    | j                   j                  |||      }|j                  }|j                  }| j	                  |t        j                               }| j                  X|}t        |t        j                        r|j                         }t        j                  | j                  | j                  |i       t        j                   |j"                  j%                         d         }	t&        j(                  }
| j*                  s|	|	dk  rt&        j,                  }
 |
|||| j.                  d| j0                  z  | j2                        S )?Private method that follows the signature of _get_dense_tensor.r2   )r3   r8   r   rX  
%s_weightsr  r'   r  )r  r  r  r  r  r   rE   r  rX   r   r   _get_variable_listr   init_from_checkpointr  r   dimension_valuer  r  r   r  r	  embedding_lookup_sparse_v2r  r'   r  )rn   ry  r3   r4   r  
sparse_idsr  rf  
to_restoresparse_id_rankembedding_lookup_sparses              r*   _get_dense_tensor_internalz+_EmbeddingColumn._get_dense_tensor_internalm	  sC    ,,@@- A N  ))J#11N**-//1 + 3 )$j	J	 = =	>224
++

 
 4#;#;Z"HJ "11((*1-/N+HH**~/I! - H H"DII%   r-   c                     t        | j                  t              rCt        dj	                  | j
                  t        | j                        | j                              | j                  |||      S Na6  In embedding_column: {}. categorical_column must not be of type _SequenceCategoricalColumn. Suggested fix A: If you wish to use input_layer, use a non-sequence categorical_column_with_*. Suggested fix B: If you wish to create sequence input, use sequence_input_layer instead of input_layer. Given (type {}): {}ry  r3   r4   rX   r  _SequenceCategoricalColumnrZ   r[   r'   r  r  r  s       r*   r>   z"_EmbeddingColumn._get_dense_tensor	  s    $))+EF  !'tyy$t7N7N2O'+'>'>!@A A **- +  r-   c                    t        | j                  t              sCt        dj	                  | j
                  t        | j                        | j                              | j                  |||      }| j                  j                  |      }t        j                  |j                        }t        j                  ||      S NzIn embedding_column: {}. categorical_column must be of type _SequenceCategoricalColumn to use sequence_input_layer. Suggested fix: Use one of sequence_categorical_column_with_*. Given (type {}): {}r  r  r  rX   r  r  rZ   r[   r'   r  r  r  r  "sequence_length_from_sparse_tensorr  r  r  rn   ry  r3   r4   r  r  r  s          r*   r  z+_EmbeddingColumn._get_sequence_dense_tensor	  s     d--/IJ  !'tyy$t7N7N2O'+'>'>!@A A 22- 3 L
 ,,@@HNAA  "O88!? 9 D Dr-   )Tr  )r   r   r   r   r  r   r'   r   rz  r?   r  r>   r  r   r   s   @r*   r  r  ;	  st       )-=,  
 7 7/   59+/% N$ 59+/Dr-   r  r  c                 ~    t        | t        j                        rt        |       d   j                  S | j                  S )Nr   )rX   r   r   r\   graph)r   s    r*   _get_graph_for_variabler'  	  s0    Y2239Q<99r-   c                   v    e Zd ZdZed        Zed        Zed        Zd Zed        Z		 	 ddZ
dd	Z	 	 dd
Zy)_SharedEmbeddingColumnr  c                     t        | d      s*dj                  | j                  j                        | _        | j                  S )Nrj   z{}_shared_embeddingr  ru   s    r*   r'   z_SharedEmbeddingColumn.name	  s4    4!(//0G0G0L0LMdj::r-   c                     | j                   S r%   ) shared_embedding_collection_nameru   s    r*   r=   z&_SharedEmbeddingColumn._var_scope_name	  s    000r-   c                 .    | j                   j                  S r%   r  ru   s    r*   r   z*_SharedEmbeddingColumn._parse_example_spec	  r  r-   c                 8    |j                  | j                        S r%   r  rx  s     r*   rz  z)_SharedEmbeddingColumn._transform_feature	  r  r-   c                 |    t        | d      s%t        j                  | j                  g      | _        | j                  S r  r
  ru   s    r*   r?   z&_SharedEmbeddingColumn._variable_shape	  r  r-   Nc           	         t        j                  d| j                        5  | j                  j	                  |||      }|j
                  }|j                  }| j                  j                  | j                  f}t        j                  | j                        }|rt        |      dkD  rt        dj                  |            |d   }	|	j                         |k7  rt        dj                  | j                  |	j                  |	j                         |            t        j                   d|t"        j$                  | j&                  | j(                  xr ||	      }	t        j*                  | j                  |	       | j,                  X|	}
t/        |
t0        j2                        r|
j5                         }
t7        j8                  | j,                  | j:                  |
i       t=        j>                  |j@                  j                         d         }tB        jD                  }| jF                  s||d
k  rtB        jH                  } ||	||| jJ                  d| j                  z  | jL                        cddd       S # 1 sw Y   yxY w)r  Nr0   r2   r9   zCollection {} can only contain one variable. Suggested fix A: Choose a unique name for this collection. Suggested fix B: Do not add any variables to this collection. The feature_column library already adds a variable under the hood.r   a  Shared embedding collection {} contains variable {} of unexpected shape {}. Expected shape is {}. Suggested fix A: Choose a unique name for this collection. Suggested fix B: Do not add any variables to this collection. The feature_column library already adds a variable under the hood.rf  )r'   r6   r  r   r4   r  rX  r  r  )'r   r   r'   r  r  r  r  r   r  rB   r,  r  rZ   r[   r  r   r  r   rg  r   r4   r   r  rX   r   r   r  r   r  r  r   r  r  r   r  r	  r  r  r  )rn   ry  r3   r4   r  r  r  r   shared_embedding_collectionrf  r  r  r  s                r*   r  z1_SharedEmbeddingColumn._get_dense_tensor_internal	  sT    
499	5 ="..BB
/ C n "++j%33n00==t~~No$'$6$6

/
/%1!	$*+a/ f89; ; 8:&&(O;
 fTBB.33.88:OMN N +77$!..((nn2*, 	dCC/	1				+&
j)"?"?@!446*--""T%=%=z$J	L $33

 
 
*
*
,Q
/1n - J J,,1K
A
"/"J"J$


==dii'=="o=" =" ="s   II;;Jc                     t        | j                  t              rCt        dj	                  | j
                  t        | j                        | j                              | j                  |||      S r  r  r  s       r*   r>   z(_SharedEmbeddingColumn._get_dense_tensor+
  r  r-   c                    t        | j                  t              sCt        dj	                  | j
                  t        | j                        | j                              | j                  |||      }| j                  j                  |      }t        j                  |j                        }t        j                  ||      S r   r"  r$  s          r*   r  z1_SharedEmbeddingColumn._get_sequence_dense_tensor;
  s     d--/IJ  !'tyy$t7N7N2O'+'>'>!@A A 22- 3 L ,,@@HNAA  "O88!? 9 D Dr-   r  )r   r   r   r   r   r'   r=   r   rz  r?   r  r>   r  r   r-   r*   r)  r)  	  s|       
 1 1 7 7/   59+/E"N$ 59+/Dr-   r)  )
r  r  r  r   r,  r  r  r  r4   r	  c                    | J t        j                  |       s| g} t        |       } | D ]W  }t        |t        j
                        st        dj                  | |            |dk  s>t        dj                  | |             | S )z4Returns shape if it's valid, raises error otherwise.z4shape dimensions must be integer. shape: {}, key: {}r9   z;shape dimensions must be greater than 0. shape: {}, key: {})	r   	is_nestedr   rX   r   integer_typesr  r[   rZ   )r6   r/   r  s      r*   r  r  R
  s    					GE
,% @ii!2!23 ++16%+=? ?1} ,,2F5#,>@ @@ 
,r-   c                   R    e Zd ZdZed        Zed        Zd Zed        Z	 	 ddZ	y)	r,  z*see `categorical_column_with_hash_bucket`.c                     | j                   S r%   r.   ru   s    r*   r'   z_HashedCategoricalColumn.nameh
  r  r-   c                 X    | j                   t        j                  | j                        iS r%   r/   r   VarLenFeaturer  ru   s    r*   r   z,_HashedCategoricalColumn._parse_example_specl
       HHk//

;<<r-   c                    t        |j                  | j                              }t        |t        j
                        st        d      t        j                  |j                  dj                  | j                               | j                  j                  |j                  j                  k7  r:t        dj                  | j                  | j                  |j                              | j                  t        j                  k(  r|j                  }nt        j                   |j                        }t        j"                  || j$                  d      }t	        j
                  |j&                  ||j(                        S )Nz*SparseColumn input must be a SparseTensor.column_name: {} input_tensorr)  dColumn dtype and SparseTensors dtype must be compatible. key: {}, column dtype: {}, tensor dtype: {}lookupr&   )r  r   r/   rX   r  r  rZ   r  r+  r  r[   r  r   r  rV   r   	as_stringstring_to_hash_bucket_fastr.  r  r  )rn   ry  r  sparse_valuessparse_id_valuess        r*   rz  z+_HashedCategoricalColumn._transform_featurep
  s+   :6::dhh;OPLl$5$B$BCCDD!!-44TXX>@ zz 2 2 = ==88>hh

L$6$6989 9
 zzV]]""))m **<+>+>?m!<<t,,8=)),*>*>*:*6*B*BD Dr-   c                     | j                   S r  r.  ru   s    r*   r   z%_HashedCategoricalColumn._num_buckets
          r-   Nc                 L    t         j                  |j                  |       d       S r%   r   r  r   r  s       r*   r  z,_HashedCategoricalColumn._get_sparse_tensors
        **6::d+;TBBr-   r  
r   r   r   r   r   r'   r   rz  r   r  r   r-   r*   r,  r,  b
  sU     3  = =D4 ! ! .2$(Cr-   r,  r-  c                   R    e Zd ZdZed        Zed        Zd Zed        Z	 	 ddZ	y)	r=  z.See `categorical_column_with_vocabulary_file`.c                     | j                   S r%   r.   ru   s    r*   r'   z%_VocabularyFileCategoricalColumn.name
  r  r-   c                 X    | j                   t        j                  | j                        iS r%   r:  ru   s    r*   r   z4_VocabularyFileCategoricalColumn._parse_example_spec
  r<  r-   c           
         t        |j                  | j                              }| j                  j                  |j                  j                  k7  r:t        dj                  | j                  | j                  |j                              t        j                  |j                  dj                  | j                               | j                  }|j                  j                  r4t        j                  }t        j                  |t        j                        }t        j                  | j                  | j                   | j"                  | j$                  |dj                  | j                              j'                  |      S )Nr?  r>  r)  	{}_lookup)r6  r8  
vocab_sizer  	key_dtyper'   )r  r   r/   r  r  rZ   r[   r  r+  r   r  r   r  r   index_table_from_filer6  r8  r7  r  r@  rn   ry  r  rS  s       r*   rz  z3_VocabularyFileCategoricalColumn._transform_feature
  s$   :6::dhh;OPLzz 2 2 = ==88>hh

L$6$6989 9
 !!-44TXX>@ 

I$$,,i]]<>l++,,,,''(()+ ,26,+?@r-   c                 4    | j                   | j                  z   S rF  )r7  r8  ru   s    r*   r   z-_VocabularyFileCategoricalColumn._num_buckets
  s     $"6"666r-   Nc                 L    t         j                  |j                  |       d       S r%   rJ  r  s       r*   r  z4_VocabularyFileCategoricalColumn._get_sparse_tensors
  rK  r-   r  rL  r   r-   r*   r=  r=  
  sU    
 7  = =@6 7 7 .2$(Cr-   r=  )r/   r6  r7  r8  r  r  c                   R    e Zd ZdZed        Zed        Zd Zed        Z	 	 ddZ	y)	rG  z.See `categorical_column_with_vocabulary_list`.c                     | j                   S r%   r.   ru   s    r*   r'   z%_VocabularyListCategoricalColumn.name
  r  r-   c                 X    | j                   t        j                  | j                        iS r%   r:  ru   s    r*   r   z4_VocabularyListCategoricalColumn._parse_example_spec
  r<  r-   c           	      
   t        |j                  | j                              }| j                  j                  |j                  j                  k7  r:t        dj                  | j                  | j                  |j                              t        j                  |j                  dj                  | j                               | j                  }|j                  j                  r4t        j                  }t        j                  |t        j                        }t        j                  t        | j                         | j"                  | j$                  |dj                  | j                              j'                  |      S )Nr?  r>  r)  rQ  )rB  r  r8  r  r'   )r  r   r/   r  r  rZ   r[   r  r+  r   r  r   r  r   index_table_from_tensorr   rB  r  r8  r@  rU  s       r*   rz  z3_VocabularyListCategoricalColumn._transform_feature
  s    :6::dhh;OPLzz 2 2 = ==88>hh

L$6$6989 9
 !!-44TXX>@ 

I$$,,i]]<>l--d223((,,)+
 ,26,+?@r-   c                 F    t        | j                        | j                  z   S rF  )r  rB  r8  ru   s    r*   r   z-_VocabularyListCategoricalColumn._num_buckets
  s      t##$t';';;;r-   Nc                 L    t         j                  |j                  |       d       S r%   rJ  r  s       r*   r  z4_VocabularyListCategoricalColumn._get_sparse_tensors
  rK  r-   r  rL  r   r-   r*   rG  rG  
  sU     7  = =@4 < < .2$(Cr-   rG  rA  c                   R    e Zd ZdZed        Zed        Zd Zed        Z	 	 ddZ	y)	rM  z'See `categorical_column_with_identity`.c                     | j                   S r%   r.   ru   s    r*   r'   z_IdentityCategoricalColumn.name  r  r-   c                 `    | j                   t        j                  t        j                        iS r%   )r/   r   r;  r   r  ru   s    r*   r   z._IdentityCategoricalColumn._parse_example_spec  s     HHk//=>>r-   c           
         t        |j                  | j                              }|j                  j                  s/t        dj                  | j                  |j                              |j                  }|j                  j                  t        j                  k7  r&t        j                  |t        j                  d      }| j                  t        j                  | j                  t        j                  d      }t        j                  dt        j                  d      }t        j                  t        j                   ||k  ||k\  d      t        j"                  t        j$                  |      t        j                  | j                  t        j                        d	      |      }t'        j(                  |j*                  ||j,                  
      S )Nz-Invalid input, not integer. key: {} dtype: {}rV   r&   rL  r   zeroout_of_rangedefault_values)dimsr   r'   r  )r  r   r/   r  r  rZ   r[   rV   r   r  r   r  r  rL  r
   r  
logical_orfillr6   r  r  r  r  )rn   ry  r  rV   rL  rc  s         r*   rz  z-_IdentityCategoricalColumn._transform_feature  s[   :6::dhh;OPL((FMM
((L&&( ) )  F  FLL0}}VV\\Af%MM


FLL}>k]]1fll8d


tmV{2I
..??6*MM$"4"4fllC#% '-.f ))$$ ,,. .r-   c                     | j                   S rF  )rL  ru   s    r*   r   z'_IdentityCategoricalColumn._num_buckets*  s     r-   Nc                 L    t         j                  |j                  |       d       S r%   rJ  r  s       r*   r  z._IdentityCategoricalColumn._get_sparse_tensors/  rK  r-   r  rL  r   r-   r*   rM  rM    sT     0  ? ?.4   .2$(Cr-   rM  rK  c                   R    e Zd ZdZed        Zed        Zed        Zd Z	 	 ddZ	y)	rU  z"See `weighted_categorical_column`.c                 b    dj                  | j                  j                  | j                        S )Nz{}_weighted_by_{})r[   r  r'   rT  ru   s    r*   r'   z_WeightedCategoricalColumn.name=  s,    %%d&=&=&B&B&*&=&=? ?r-   c                 
   | j                   j                  }| j                  |v r2t        dj	                  || j                     | j                              t        j                  | j                        || j                  <   |S )Nz&Parse config {} already exists for {}.)r  r   rT  rZ   r[   r   r;  r  )rn   r   s     r*   r   z._WeightedCategoricalColumn._parse_example_specB  sy    $$88F&(?FF
((
)4+B+BD E E&1&?&?

&KF4""#Mr-   c                 .    | j                   j                  S r%   r  r   ru   s    r*   r   z'_WeightedCategoricalColumn._num_bucketsK      ""///r-   c                 N   |j                  | j                        }|$t        dj                  | j                              t	        j
                  |      }| j                  |j                  j                  k7  r/t        dj                  | j                  |j                              t        |t        j                        st        |d      }|j                  j                  s$t        j                  |t        j                        }|j                  | j                         |fS )NzMissing weights {}.z#Bad dtype, expected {}, but got {}.r   )r  )r   rT  rZ   r[   r  r  r  
base_dtyperX   r  r  r  r   r  r   rg  r  )rn   ry  r  s      r*   rz  z-_WeightedCategoricalColumn._transform_featureO  s    JJt667M,33D4K4KLMM%HHMzz]((333<CC
**m))+ , ,m%6%C%CD=
c+m**mmM6>>BmJJt../??r-   Nc                 `    ~~|j                  |       }t        j                  |d   |d         S )Nr   r9   )r   r   r  )rn   ry  r3   r4   tensorss        r*   r  z._WeightedCategoricalColumn._get_sparse_tensors`  s3     	jjG**71:wqzBBr-   r  )
r   r   r   r   r   r'   r   r   rz  r  r   r-   r*   rU  rU  6  sU    
 +? ?   0 0@& .2$(Cr-   rU  rS  c                   R    e Zd ZdZed        Zed        Zd Zed        Z	 	 ddZ	y)	r]  zSee `crossed_column`.c                     g }t        |       D ]?  }t        |t              r|j                  |j                         /|j                  |       A dj                  t        |            S )N_X_)_collect_leaf_level_keysrX   r   r<   r'   joinr;   )rn   feature_namesr/   s      r*   r'   z_CrossedColumn.namep  s[    M'- "	C	(SXX&S!	"
 ::f]+,,r-   c                     i }| j                   D ]b  }t        |t              r|j                  |j                         /|j                  |t        j                  t        j                        i       d |S r%   )	rZ  rX   r   r   r   r   r;  r   r  )rn   r   r/   s      r*   r   z"_CrossedColumn._parse_example_specz  s_    Fyy G	C	(c--.sK55fmmDEF	G
 Mr-   c                     g }t        |       D ]  }t        |t        j                        r!|j	                  |j                  |             >t        |t              r]|j                  |      }|j                  $t        dj                  |j                              |j	                  |j                         t        dj                  |             t        j                  || j                  | j                         S )Nzmcrossed_column does not support weight_tensor, but the given column populates weight_tensor. Given column: {}z"Unsupported column type. Given: {})ry  rL  r[  )rx  rX   r   r\  r<   r   r   r  r  rZ   r[   r'   r  r   sparse_cross_hashedr.  r[  )rn   ry  feature_tensorsr/   ids_and_weightss        r*   rz  z!_CrossedColumn._transform_feature  s    O'- K	C))	*vzz#/c-.11&9((4!!'!13 3 	889=DDSIJJK ))))   r-   c                     | j                   S rF  rG  ru   s    r*   r   z_CrossedColumn._num_buckets  rH  r-   Nc                 L    t         j                  |j                  |       d       S r%   rJ  r  s       r*   r  z"_CrossedColumn._get_sparse_tensors  rK  r-   r  rL  r   r-   r*   r]  r]  j  sT     - -   ( ! ! .2$(Cr-   r]  rY  c                     g }| j                   D ]>  }t        |t              r|j                  t	        |             .|j                  |       @ |S )zCollects base keys by expanding all nested crosses.

  Args:
    cross: A `_CrossedColumn`.

  Returns:
    A list of strings or `_CategoricalColumn` instances.
  )rZ  rX   r]  extendrx  r<   )crossleaf_level_keysks      r*   rx  rx    sP     /::  a!^$5a89Q	 
 
r-   c                   Z    e Zd ZdZed        Zd Zed        Zed        Zd	dZ		 	 d	dZ
y)
rP  zRepresents a one-hot column for use in deep networks.

  Args:
    categorical_column: A `_CategoricalColumn` which is created by
      `categorical_column_with_*` function.
  c                 L    dj                  | j                  j                        S )Nz{}_indicator)r[   r  r'   ru   s    r*   r'   z_IndicatorColumn.name  s      !8!8!=!=>>r-   c                 "   | j                   j                  |      }|j                  }|j                  }|t	        j
                  ||t        | j                  d               }t	        j                  |ddg|j                        }t        j                  |j                  |j                  |j                        S t	        j                  |d      }t        j                  || j                  d   dd      }t!        j"                  |dg	      S )
zReturns dense `Tensor` representing feature.

    Args:
      inputs: A `_LazyBuilder` object to access inputs.

    Returns:
      Transformed feature `Tensor`.

    Raises:
      ValueError: if input rank is not known at graph building time.
    r   )sp_ids	sp_valuesrR  r   )r  r  r   )r  r  r  )axis)r  r  r  r  r   sparse_mergeintr?   sparse_slicer  r
   
scatter_ndr  rV   sparse_tensor_to_denser  r   
reduce_sum)rn   ry  id_weight_pairr  r  weighted_columndense_id_tensorone_hot_id_tensors           r*   rz  z#_IndicatorColumn._transform_feature  s    ,,@@HN((I"00M  "//!--b124o
 #//!Q0?0K0KMo !!/"9"9"1"8"8"1"="=? ? !77%O
 "))""2&	 0t<<r-   c                 .    | j                   j                  S r%   r  ru   s    r*   r   z$_IndicatorColumn._parse_example_spec  r  r-   c                 X    t        j                  d| j                  j                  g      S )zEReturns a `TensorShape` representing the shape of the dense `Tensor`.r9   )r   r  r  r   ru   s    r*   r?   z _IndicatorColumn._variable_shape  s&     ##Q(?(?(L(L$MNNr-   Nc                     ~~t        | j                  t              rCt        dj	                  | j
                  t        | j                        | j                              |j                  |       S )a  Returns dense `Tensor` representing feature.

    Args:
      inputs: A `_LazyBuilder` object to access inputs.
      weight_collections: Unused `weight_collections` since no variables are
        created in this function.
      trainable: Unused `trainable` bool since no variables are created in this
        function.

    Returns:
      Dense `Tensor` created within `_transform_feature`.

    Raises:
      ValueError: If `categorical_column` is a `_SequenceCategoricalColumn`.
    a6  In indicator_column: {}. categorical_column must not be of type _SequenceCategoricalColumn. Suggested fix A: If you wish to use input_layer, use a non-sequence categorical_column_with_*. Suggested fix B: If you wish to create sequence input, use sequence_input_layer instead of input_layer. Given (type {}): {})rX   r  r  rZ   r[   r'   r  r   r  s       r*   r>   z"_IndicatorColumn._get_dense_tensor  si    $ 	$))+EF  !'tyy$t7N7N2O'+'>'>!@A A ::dr-   c                    ~~t        | j                  t              sCt        dj	                  | j
                  t        | j                        | j                              |j                  |       }| j                  j                  |      }t        j                  |j                        }t        j                  ||      S )NzIn indicator_column: {}. categorical_column must be of type _SequenceCategoricalColumn to use sequence_input_layer. Suggested fix: Use one of sequence_categorical_column_with_*. Given (type {}): {}r!  )rX   r  r  rZ   r[   r'   r  r   r  r  r#  r  r  r  r$  s          r*   r  z+_IndicatorColumn._get_sequence_dense_tensor  s     	d--/IJ  !'tyy$t7N7N2O'+'>'>!@A A ::d#L,,@@HNAA  "O88!? 9 D Dr-   r  )r   r   r   r   r   r'   rz  r   r?   r>   r  r   r-   r*   rP  rP    s_     ? ?+=Z 7 7 O O H 59+/Dr-   rP  r  c           
         d}t        dt        |             D ]  }| |   j                  j                  d   j                  *||}| |   j                  j                  d   }K|j                  | |   j                  j                  d         rwt        dj                  |   j                  ||   j                  || |   j                  j                  d                y)a'  Validates that the first dim (batch size) of all tensors are equal or None.

  Args:
    tensors: list of tensors to check.
    columns: list of feature columns matching tensors. Will be used for error
      messaging.

  Raises:
    ValueError: if one of the tensors has a variant batch size
  Nr   zcBatch size (first dimension) of each feature must be same. Batch size of columns ({}, {}): ({}, {}))	r!  r  r6   rf  r   is_compatible_withrZ   r[   r'   )rt  columnsexpected_batch_sizer%  bath_size_column_indexs        r*   rF   rF   6  s     CL! 
@aqzQ%%1		$!"%aj..33A6"55gaj6F6F6K6KA6NO77=v./44gajoo#WQZ%5%5%:%:1%=8?@ 	@
@r-   c                   R    e Zd ZdZed        Zed        Zd Zed        Z	 	 ddZ	y)	r  z)Represents sequences of categorical data.c                 .    | j                   j                  S r%   )r  r'   ru   s    r*   r'   z_SequenceCategoricalColumn.nameV  s    ""'''r-   c                 .    | j                   j                  S r%   r  ru   s    r*   r   z._SequenceCategoricalColumn._parse_example_specZ  r  r-   c                 8    | j                   j                  |      S r%   )r  rz  rx  s     r*   rz  z-_SequenceCategoricalColumn._transform_feature^  s    ""55f==r-   c                 .    | j                   j                  S r%   ro  ru   s    r*   r   z'_SequenceCategoricalColumn._num_bucketsa  rp  r-   Nc                 \   | j                   j                  |      }|j                  }|j                  }t	        j
                  |      }|d   |d   t        j                  |dd        g}t        j                  ||      }|t        j                  ||      }t        j                  ||      S )Nr   r9   rX  )r  r  r  r  r
   r6   r   reduce_prodr   r  r   r  )	rn   ry  r3   r4   r  r  r  r6   target_shapes	            r*   r  z._SequenceCategoricalColumn._get_sparse_tensorse  s     ,,@@HN((I"00M
 OOI&E !HeAh(<(<U12Y(GHL)))\BI  //|Lm**9mDDr-   r  rL  r   r-   r*   r  r  P  sT     2( ( 7 7> 0 0
 .2$(Er-   r  )NTNNNF)NTNNr   )r   NNNNTT)Nr   r   r%   )qr   r  r  r  numpyrE  r   tensorflow.python.eagerr    tensorflow.python.feature_columnr   r  tensorflow.python.frameworkr   r   r   r  r   tensorflow.python.layersr	   tensorflow.python.opsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   tensorflow.python.platformr   r   r;  tensorflow.python.trainingr   tensorflow.python.utilr   r   tensorflow.python.util.compatr    tensorflow.python.util.tf_exportr    tensorflow.tools.docsr!   #_FEATURE_COLUMN_DEPRECATION_WARNING+_FEATURE_COLUMN_DEPRECATION_RUNTIME_WARNINGr_   header
deprecatedrT   objectrc   r   r   Layerr   r   r   r   r   r   r   r  rg  r  r&  r  r/  r?  rI  rN  rQ  rV  r^  r   add_metaclassABCMetar   rY   r   r  r   r  r  r:   r  r  rW   r  r  r"  r  r'  r)  r  r,  r=  rG  rM  rU  r]  rx  rP  rF   r  r   r-   r*   <module>r     sT       
 + > . + J 4 ) + 1 + & / * , * ( - 7 , , * 0 + , < 7 . ' 9 6 .' ## , .2$('+ $15(-;| 89
+,-IJ $(!'+B5 K . :B5R8. 8.v 89
,-.IJ !&$("E K / :EP4,0tzz 0f 4!^4:: ^B)X 89
789IJ8 K : :8z  &"&(,*.# $04\;@ "& .."&	H#VO=h 06}}7@x >B=>;?39==tr 48;==>	q'h@EF.H (.~~HV{N~3&DJJ 3&l 3;;KV K  K\&	> &	^ %)@ 26>4+	 +	h 8<=@	> 	DG6 DGP	,F^*Z4KCE4nC@&8.../B0?/NPC@L~D&K	&'~DBLD&K 	&'LD^ 1C15{55#=#G I1Ch3CK=IJ3Cl3CK*OQ3Cl1C!3!7!7!7%A%L"N1Ch1CK$=?1Ch7CK+CE7Ct$}D|%9-{--.@/C.DF}D@@4)E!3!7!7!7%A&:%;"=)Er-   