a
    Sic9!                     @   sh   d Z ddlZddlm  mZ ddlmZ dddZ	ddd	Z
G d
d dZdd Zdd Zdd ZdS )zTraining-related utilities.    N)generic_utilsTc                    s   d}t | tsd}| g} tdd | D rd|sP fdd| D }dd |D }qp fdd| D }nt|  }|r||d	 }|S )
a  Slices batches out of provided arrays (workaround for eager tensors).

    Unfortunately eager tensors don't have the same slicing behavior as
    Numpy arrays (they follow the same slicing behavior as symbolic TF tensors),
    hence we cannot use `generic_utils.slice_arrays` directly
    and we have to implement this workaround based on `concat`. This has a
    performance cost.

    Args:
      arrays: Single array or list of arrays.
      indices: List of indices in the array that should be included in the
        output batch.
      contiguous: Boolean flag indicating whether the indices are contiguous.

    Returns:
      Slice of data (either single array or list of arrays).
    FTc                 s   s   | ]}t |V  qd S N)tf	is_tensor.0x r	   W/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/engine/training_utils.py	<genexpr>-       zslice_arrays.<locals>.<genexpr>c                    s   g | ]  fd dD qS )c                    s   g | ]} ||d   qS )   r	   )r   ir   r	   r
   
<listcomp>/   r   z+slice_arrays.<locals>.<listcomp>.<listcomp>r	   )r   indicesr   r
   r   /   r   z slice_arrays.<locals>.<listcomp>c                 S   s   g | ]}t j|d dqS )r   )axis)r   concatr   r	   r	   r
   r   0   r   c                    s$   g | ]}| d   d d  qS )r   r   r	   r   r   r	   r
   r   2   r   r   )
isinstancelistanyr   slice_arrays)arraysr   
contiguousconverted_to_listentriesslicesr	   r   r
   r      s    
r   Fc                 C   sf  |duot dd |D }|o.t dd |D }|s>d||fS |sL|||fS |rtjt|ttj| tjt| ttj|  |durtj|tj| g }t|D ]\}}|du rLt| | tj	}	| | }
|	r|
j
nt
|
}|duo|| dk}|r |d |d fn|d f}||	r>t|nt| q|| qt|||fS )a  Adds 1.0 as sample weights for the outputs for which there is no weight.

    Args:
      outputs: List of model outputs.
      sample_weights: List of sample weight inputs.
      sample_weight_modes: List of sample weight modes or None.
      check_all_flat: Ensure that inputs are not nested structures. This is not
        a free check, so we may not want to run it eagerly every iteration.

    Returns:
      Tuple of sample weights, one sample weight for every output, and booleans
      describing the raw sample weights.
    Nc                 s   s   | ]}|d uV  qd S r   r	   r   wr	   r	   r
   r   K   s   z0handle_partial_sample_weights.<locals>.<genexpr>c                 s   s   | ]}|d u V  qd S r   r	   r   r	   r	   r
   r   N   s   temporalr   r   )r   r   nestassert_same_structurelist_to_tupleflatten	enumerater   npndarrayshapeappendones)outputssample_weightssample_weight_modescheck_all_flatany_sample_weightpartial_sample_weightnew_sample_weightsr   swas_numpyoutputoutput_shapeis_temporalsw_shaper	   r	   r
   handle_partial_sample_weights;   sV    




r9   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	RespectCompiledTrainableStatea  Set and restore trainable state if it has changed since compile.

    The keras API guarantees that the value of each Layer's `trainable` property
    at `Model.compile` time will be used when training that model. In order to
    respect this requirement, it may be necessary to set the trainable value of
    layers to their compile time values before beginning a training endpoint and
    restore the values before returning from said endpoint. This scope checks if
    any layer's trainable state has changed since Model compile, and performs
    this set and un-set bookkeeping.

    However, the trainable state of a layer changes quite infrequently, if ever,
    for many kinds of workflows. Moreover, updating every layer in a model is an
    expensive operation. As a result, we will only explicitly set and unset the
    trainable state of a model if a trainable value has changed since compile.
    c                 C   s   || _ d | _d | _d| _d S NF)_model_current_trainable_state_compiled_trainable_state_should_set_trainable)selfmodelr	   r	   r
   __init__   s    z&RespectCompiledTrainableState.__init__c                 C   sd   | j  | _| j j| _| j D ]*\}}|| jv r || j| kr d| _ qLq | jr`| j | j d S )NT)r<   _get_trainable_stater=   r>   itemsr?   _set_trainable_state)r@   layer	trainabler	   r	   r
   	__enter__   s    
z'RespectCompiledTrainableState.__enter__c                 C   s   | j r| j| j dS r;   )r?   r<   rE   r=   )r@   type_arg	value_argtraceback_argr	   r	   r
   __exit__   s    z&RespectCompiledTrainableState.__exit__N)__name__
__module____qualname____doc__rB   rH   rL   r	   r	   r	   r
   r:      s   r:   c                 C   sF   dd }|| r*| j std| j d } qt| ddrB| j| jfS dS )aR  Retrieves input shape and input dtype of layer if applicable.

    Args:
      layer: Layer (or model) instance.

    Returns:
      Tuple (input_shape, input_dtype). Both could be None if the layer
        does not have a defined input shape.

    Raises:
      ValueError: in case an empty Sequential or Functional model is passed.
    c                 S   s   t | dr| jp| jjdkS )N_is_graph_network
Sequential)hasattrrQ   	__class__rM   )rF   r	   r	   r
   _is_graph_model   s    
z2get_input_shape_and_dtype.<locals>._is_graph_modelz)An empty Model cannot be used as a Layer.r   _batch_input_shapeN)NN)layers
ValueErrorgetattrrV   dtype)rF   rU   r	   r	   r
   get_input_shape_and_dtype   s    r[   c                 C   s,   t | \}}|dur(tjj|d jS dS )zGets the static batch size of a Layer.

    Args:
      layer: a `Layer` instance.

    Returns:
      The static batch size of a Layer.
    Nr   )r[   r   compatv1	Dimensionvalue)rF   batch_input_shape_r	   r	   r
   get_static_batch_size   s    	rb   c                 C   s   t | trt| S | S )zADatasets will stack the list of tensor, so switch them to tuples.)r   r   tuple)
maybe_listr	   r	   r
   r$      s    
r$   )T)F)rP   numpyr'   tensorflow.compat.v2r\   v2r   keras.utilsr   r   r9   r:   r[   rb   r$   r	   r	   r	   r
   <module>   s   
% 
H4 