
    BVh(                     V   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ	 ddlm
Z
 ddlmZ dd	lmZ d
 Z ed       G d de
j                               Z ed      dddde
j"                  ej$                  dfd       Z ed      dddde
j"                  ej$                  dfd       Zy)zLookup operations.    )assert_cardinality)dtypes)ops)tensor)gen_experimental_dataset_ops)
lookup_ops)math_ops)	tf_exportc                 `   d}d}t        |       dk7  rt        |dt        |        d|  dz         t        | d   t        j                        st        |dt        | d          dz         t        | d	   t        j                        st        |d
t        | d	          dz         | d   j                  j                  dvr)t        |d| d   j                  j                   dz         | d	   j                  j                  dvr)t        |d| d	   j                  j                   dz         y)zGRaises an error if the given table initializer element spec is invalid.zDatasets used to initialize lookup tables must produce elements in the form (key, value), where the keys and values are scalar tensors. N   z$However, the given dataset produces zO components instead of two (key, value) components. Full dataset element spec: .r   z<However, the given dataset produces non-Tensor keys of type    z>However, the given dataset produces non-Tensor values of type )Nr   zCHowever, the given dataset produces non-scalar key Tensors of rank zEHowever, the given dataset produces non-scalar value Tensors of rank )len
ValueError
isinstancer   
TensorSpectypeshaperank)element_spec
base_errorspecific_errors      b/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/data/experimental/ops/lookup_ops.py%_check_table_initializer_element_specr      s   ;* .!
Z"HL)* +L$~Q#( ( ) ) 
LOV%6%6	7
Z #004\!_0E/Fa#I I J J	LOV%6%6	7
Z #226|A2G1H#K K L L!_y0
 **6q/*?*?*D*D)EQH 	HI I !_y0
 ,,8O,A,A,F,F+GqJ 	JK K 1    z$data.experimental.DatasetInitializerc                   (     e Zd ZdZ fdZd Z xZS )DatasetInitializera  Creates a table initializer from a `tf.data.Dataset`.

  Sample usage:

  >>> keys = tf.data.Dataset.range(100)
  >>> values = tf.data.Dataset.range(100).map(
  ...     lambda x: tf.strings.as_string(x * 2))
  >>> ds = tf.data.Dataset.zip((keys, values))
  >>> init = tf.data.experimental.DatasetInitializer(ds)
  >>> table = tf.lookup.StaticHashTable(init, "")
  >>> table.lookup(tf.constant([0, 1, 2], dtype=tf.int64)).numpy()
  array([b'0', b'2', b'4'], dtype=object)

  Attributes:
    dataset: A `tf.data.Dataset` object that produces tuples of scalars. The
      first scalar is treated as a key and the second as value.
  Raises: ValueError if `dataset` doesn't conform to specifications.
  c                     || _         | j                   j                  }t        |       |d   j                  }|d   j                  }t        t
        |   ||       y)aH  Creates a table initializer from a `tf.data.Dataset`.

    Args:
      dataset: A `tf.data.Dataset` object that produces tuples of scalars. The
        first scalar is treated as a key and the second as value.
    Raises: ValueError if `dataset` doesn't conform to specifications.
    Returns: A `DatasetInitializer` object
    r   r   N)datasetr   r   dtypesuperr   __init__)selfr   	elem_speckey_type
value_type	__class__s        r   r"   zDatasetInitializer.__init__K   sS     DL))I))4|!!H1##J	
d,XzBr   c                     t        j                  || j                  | j                         t	        j
                  |j                  | j                  j                        }t        j                  t        j                  j                  |       |S N)r   check_table_dtypes
_key_dtype_value_dtypeged_opsinitialize_table_from_datasetresource_handler   _variant_tensorr   add_to_collection	GraphKeysTABLE_INITIALIZERS)r#   tableinit_ops      r   
initializezDatasetInitializer.initialize^   sb    !!%$:K:KL33t||;;=G#--::GDNr   )__name__
__module____qualname____doc__r"   r6   __classcell__)r'   s   @r   r   r   6   s    &C&r   r   z$data.experimental.table_from_datasetNc                    | j                   }t        |       |Pd}|d   j                  j                  s5|d   j                  j                  st        d|d   j                   d      |dk  rt        d| d      t        |t        j                        s||dk  rt        d| d      |j                  s+t        j                  |j                  k7  rt        d	|       |it        |t        j                        r$t        j                  |t        j                        }| j!                  |      } | j#                  t%        |            } t'        j(                  |d
      5  t+        |       }t'        j(                  dd      5  t-        j.                  ||      }	|rt-        j0                  |	|||      }	|	cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)a  Returns a lookup table based on the given dataset.

  This operation constructs a lookup table based on the given dataset of pairs
  of (key, value).

  Any lookup of an out-of-vocabulary token will return a bucket ID based on its
  hash if `num_oov_buckets` is greater than zero. Otherwise it is assigned the
  `default_value`.
  The bucket ID range is
  `[vocabulary size, vocabulary size + num_oov_buckets - 1]`.

  Sample Usages:

  >>> keys = tf.data.Dataset.range(100)
  >>> values = tf.data.Dataset.range(100).map(
  ...     lambda x: tf.strings.as_string(x * 2))
  >>> ds = tf.data.Dataset.zip((keys, values))
  >>> table = tf.data.experimental.table_from_dataset(
  ...                               ds, default_value='n/a', key_dtype=tf.int64)
  >>> table.lookup(tf.constant([0, 1, 2], dtype=tf.int64)).numpy()
  array([b'0', b'2', b'4'], dtype=object)

  Args:
    dataset: A dataset containing (key, value) pairs.
    num_oov_buckets: The number of out-of-vocabulary buckets.
    vocab_size: Number of the elements in the vocabulary, if known.
    default_value: The value to use for out-of-vocabulary feature values.
      Defaults to -1.
    hasher_spec: A `HasherSpec` to specify the hash function to use for
      assignation of out-of-vocabulary buckets.
    key_dtype: The `key` data type.
    name: A name for this op (optional).

  Returns:
    The lookup table based on the given dataset.

  Raises:
    ValueError: If
      * `dataset` does not contain pairs
      * The 2nd item in the `dataset` pairs has a dtype which is incompatible
        with `default_value`
      * `num_oov_buckets` is negative
      * `vocab_size` is not greater than zero
      * The `key_dtype` is not integer or string
  Nr   zd`default_value` must be specified when creating a table from a dataset that produces values of type r   r   z:`num_oov_buckets` must be greater than or equal to 0, got z)`vocab_size` must be greater than 0, got z>`key_dtype` must be either an integer or string type, but got string_to_index
hash_table)num_oov_bucketshasher_spec	key_dtype)r   r   r    
is_integeris_floatingr   r   r   Tensorr   string
base_dtype	TypeErrorr	   castint64takeapplyr   r   
name_scoper   r   StaticHashTableV1IdTableWithHashBuckets)
r   r@   
vocab_sizedefault_valuerA   rB   namer$   initializerr4   s
             r   table_from_datasetrT   f   s   j "")'	2MaL))Yq\-?-?-K-K L#A,,,-Q0 1 1 q
 +,A/ 0 0
Z
/J4J1n
@AN
OO


V]]i6J6J%J
 (k+ , ,*fmm,==V\\:jll:&Gmm.z:;G
~~d-. 
$W-K	l	+ **;Fe	11+#	!
  
 
  
 
 
s$   &"G$3G;	G$G	G$$G-z*data.experimental.index_table_from_datasetr=   c           	      `    t        | j                         j                  d       ||||||      S )a  Returns an index lookup table based on the given dataset.

  This operation constructs a lookup table based on the given dataset of keys.

  Any lookup of an out-of-vocabulary token will return a bucket ID based on its
  hash if `num_oov_buckets` is greater than zero. Otherwise it is assigned the
  `default_value`.
  The bucket ID range is
  `[vocabulary size, vocabulary size + num_oov_buckets - 1]`.

  Sample Usages:

  >>> ds = tf.data.Dataset.range(100).map(lambda x: tf.strings.as_string(x * 2))
  >>> table = tf.data.experimental.index_table_from_dataset(
  ...                                     ds, key_dtype=dtypes.int64)
  >>> table.lookup(tf.constant(['0', '2', '4'], dtype=tf.string)).numpy()
  array([0, 1, 2])

  Args:
    dataset: A dataset of keys.
    num_oov_buckets: The number of out-of-vocabulary buckets.
    vocab_size: Number of the elements in the vocabulary, if known.
    default_value: The value to use for out-of-vocabulary feature values.
      Defaults to -1.
    hasher_spec: A `HasherSpec` to specify the hash function to use for
      assignation of out-of-vocabulary buckets.
    key_dtype: The `key` data type.
    name: A name for this op (optional).

  Returns:
    The lookup table based on the given dataset.

  Raises:
    ValueError: If
      * `num_oov_buckets` is negative
      * `vocab_size` is not greater than zero
      * The `key_dtype` is not integer or string
  c                 
    || fS r)    )vks     r   <lambda>z*index_table_from_dataset.<locals>.<lambda>   s
    !Q r   )rT   	enumeratemap)r   r@   rP   rQ   rA   rB   rR   s          r   index_table_from_datasetr]      s7    \ 
G--/334GH+Z'D
: :r   )r:   3tensorflow.python.data.experimental.ops.cardinalityr   tensorflow.python.frameworkr   r   r   tensorflow.python.opsr   r-   r   r	    tensorflow.python.util.tf_exportr
   r   TableInitializerBaser   FastHashSpecrF   rT   r]   rW   r   r   <module>rd      s     R . + . I , * 6K6 12,88 , 3,^ 12#'("&%)#-#:#:!' T 3Tn 78%)-.(,+-)3)@)@'-}}"&/: 9/:r   