o
    ?e                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dd	d
Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )z:Defines functions common to multiple feature column files.    N)dtypes)ops)	array_ops)math_ops)nest   c                 C   s   t ddX}| jdddf }| jdddf }|t|7 }tj||d}tt|| t	j
}t| dd t|dd  }tj||jd}tj||gd|dW  d   S 1 saw   Y  dS )z?Returns a [batch_size] Tensor with per-example sequence length.NZsequence_lengthr   r   )Zsegment_ids)dtype)Zaxisname)r   
name_scopeindicesr   Z	ones_liker   Zsegment_maxcastceilr   Zint64shapeZzerosr   concat)Z	sp_tensorZnum_elementsr
   Zrow_idsZ
column_idsZ
seq_lengthZn_padpadding r   g/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/feature_column/utils.py"sequence_length_from_sparse_tensor   s   $$r   c                 C   s(   | t jkr| jstd|| d S d S )Nz.{} dtype must be string or integer. dtype: {}.)r   string
is_integer
ValueErrorformat)r   prefixr   r   r   assert_string_or_int3   s
   
r   c                 C   s$   t | tjstdt| | d S )Nz2key must be a string. Got: type {}. Given key: {}.)
isinstancesixstring_typesr   r   type)keyr   r   r   assert_key_is_string9   s   r   c                 C   s   |du rdS t |trt| |S t |tr|jrt| |S tt|ddr)| }t	|rct
|| s<td|| |tdd t|D }tdd t|D }|rZt|S |rc|jrct|S td|||)ac  Returns default value as tuple if it's valid, otherwise raises errors.

  This function verifies that `default_value` is compatible with both `shape`
  and `dtype`. If it is not compatible, it raises an error. If it is compatible,
  it casts default_value to a tuple and returns it. `key` is used only
  for error message.

  Args:
    shape: An iterable of integers specifies the shape of the `Tensor`.
    default_value: 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.
    key: Column name, used only for error messages.

  Returns:
    A tuple which will be used as default value.

  Raises:
    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`.
  Ntolistz^The shape of default_value must be equal to given shape. default_value: {}, shape: {}, key: {}c                 s       | ]}t |tV  qd S N)r   int.0vr   r   r   	<genexpr>n       

z&check_default_value.<locals>.<genexpr>c                 s   r!   r"   )r   floatr$   r   r   r   r'   p   r(   zRdefault_value must be compatible with dtype. default_value: {}, dtype: {}, key: {})r   r#   _create_tupler)   Zis_floatingcallablegetattrr    r   	is_nested&_is_shape_and_default_value_compatibler   r   allflattenany	_as_tuple	TypeError)r   default_valuer   r   Zis_list_all_intZis_list_has_floatr   r   r   check_default_value@   s>   





r5   c                    s(    rt  fddt d D S S )z7Returns a tuple with given shape and filled with value.c                    s   g | ]}t  d d qS )r   N)r*   )r%   _r   valuer   r   
<listcomp>~   s    z!_create_tuple.<locals>.<listcomp>r   )tupleranger7   r   r7   r   r*   {   s    r*   c                 C   s    t | s| S tdd | D S )Nc                 S   s   g | ]}t |qS r   )r2   r$   r   r   r   r9      s    z_as_tuple.<locals>.<listcomp>)r   r-   r:   )r8   r   r   r   r2      s   
r2   c                 C   sd   t | t|krdS |sdS t| |d krdS t|d D ]}t| | |dd s/ dS qdS )z2Verifies compatibility of shape and default_value.FTr   r   N)r   r-   boollenr;   r.   )r4   r   ir   r   r   r.      s   r.   )r   )__doc__r   Ztensorflow.python.frameworkr   r   Ztensorflow.python.opsr   r   Ztensorflow.python.utilr   r   r   r   r5   r*   r2   r.   r   r   r   r   <module>   s   
;