a
    SicV                     @   sx   d Z ddlZddlZddlm  mZ ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ edgdG d	d
 d
e
jZdS )zDCallbacks: utilities called at certain points during model training.    N)backend)	callbacks)
tf_logging)keras_exportzkeras.callbacks.TensorBoard)v1c                   @   s   e Zd ZdZd,d
dZdd Zdd Zdd Zdd Zd-ddZ	d.ddZ
d/ddZd0ddZd1ddZd2ddZd3d d!Zd4d"d#Zd5d$d%Zd6d&d'Zd(d) Zd*d+ ZdS )7TensorBoarda  Enable visualizations for TensorBoard.

    TensorBoard is a visualization tool provided with TensorFlow.

    This callback logs events for TensorBoard, including:
    * Metrics summary plots
    * Training graph visualization
    * Activation histograms
    * Sampled profiling

    If you have installed TensorFlow with pip, you should be able
    to launch TensorBoard from the command line:

    ```sh
    tensorboard --logdir=path_to_your_logs
    ```

    You can find more information about TensorBoard
    [here](https://www.tensorflow.org/get_started/summaries_and_tensorboard).

    Args:
        log_dir: the path of the directory where to save the log files to be
          parsed by TensorBoard.
        histogram_freq: frequency (in epochs) at which to compute activation and
          weight histograms for the layers of the model. If set to 0, histograms
          won't be computed. Validation data (or split) must be specified for
          histogram visualizations.
        write_graph: whether to visualize the graph in TensorBoard. The log file
          can become quite large when write_graph is set to True.
        write_grads: whether to visualize gradient histograms in TensorBoard.
          `histogram_freq` must be greater than 0.
        batch_size: size of batch of inputs to feed to the network for
          histograms computation.
        write_images: whether to write model weights to visualize as image in
          TensorBoard.
        embeddings_freq: frequency (in epochs) at which selected embedding
          layers will be saved. If set to 0, embeddings won't be computed. Data
          to be visualized in TensorBoard's Embedding tab must be passed as
          `embeddings_data`.
        embeddings_layer_names: a list of names of layers to keep eye on. If
          None or empty list all the embedding layer will be watched.
        embeddings_metadata: a dictionary which maps layer name to a file name
          in which metadata for this embedding layer is saved.
            [Here are details](
              https://www.tensorflow.org/how_tos/embedding_viz/#metadata_optional)
              about metadata files format. In case if the same metadata file is
              used for all embedding layers, string can be passed.
        embeddings_data: data to be embedded at layers specified in
          `embeddings_layer_names`. Numpy array (if the model has a single
          input) or list of Numpy arrays (if the model has multiple inputs).
          Learn more about embeddings [in this guide](
          https://www.tensorflow.org/programmers_guide/embedding).
        update_freq: `'batch'` or `'epoch'` or integer. When using `'batch'`,
          writes the losses and metrics to TensorBoard after each batch. The
          same applies for `'epoch'`. If using an integer, let's say `1000`, the
          callback will write the metrics and losses to TensorBoard every 1000
          samples. Note that writing too frequently to TensorBoard can slow down
          your training.
        profile_batch: Profile the batch to sample compute characteristics. By
          default, it will profile the second batch. Set profile_batch=0 to
          disable profiling.

    Raises:
        ValueError: If histogram_freq is set and no validation data is provided.

    @compatibility(eager)
    Using the `TensorBoard` callback will work when eager execution is enabled,
    with the restriction that outputting histogram summaries of weights and
    gradients is not supported. Consequently, `histogram_freq` will be ignored.
    @end_compatibility
    ./logsr       TFNepoch   c                 C   s   t j|  || _|| _| jr:t r:tt	d d| _d | _
|| _|| _|| _|| _d| _d| _d| _|| _|| _|	| _|
| _|dkrd| _n|| _d| _d| _|| _d| _d| _d S )NzaWeight and gradient histograms not supported for eagerexecution, setting `histogram_freq` to `0`.r   batch   FT)r   Callback__init__log_dirhistogram_freqtfexecuting_eagerlyloggingwarningUserWarningmergedwrite_graphwrite_gradswrite_images
batch_sizeZ_current_batch_total_batches_seen_total_val_batches_seenembeddings_freqembeddings_layer_namesembeddings_metadataembeddings_dataupdate_freq_samples_seen_samples_seen_at_last_write_profile_batch_profiler_started_chief_worker_only)selfr   r   r   r   r   r   r   r   r    r!   r"   profile_batch r*   N/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/callbacks_v1.pyr   k   s<    zTensorBoard.__init__c                 C   s   t  r`t j| j| _|js| jr| j   t j	t
  W d   q1 sT0    Y  n6| jrt jjj| jt
 | _nt jjj| j| _dS )zSets file writer.N)r   r   summarycreate_file_writerr   writerrun_eagerlyr   
as_defaultgraphr   	get_graphcompatr   
FileWriter)r(   modelr*   r*   r+   _init_writer   s    0

zTensorBoard._init_writerc           
   	      sT  | j rP| jdu rP| jjD ]0}|jD ]T}|jdd}tjj	j
|| | jr(tjj	|}t|j}t|dkr|d |d krtjj	|}t|j}t|d|d |d dg}nt|dkrt dkrtjj	j|g d	d
}t|j}t||d |d |d dg}n(t|dkr(t|d|d ddg}nq(t|j}t|dkrh|d dv slJ tjj	j
|| q(| jr|jD ]V}|jdd}|j|j|}dd   fdd|D }tjj	j
d|| qt|drt|jtr2t |jD ]&\}}	tjj	j
d|j||	 qqtjj	j
d|j|j qdS )z.Defines histogram ops when histogram_freq > 0.N:_r   r   r      channels_last)r   r   r   )perm   )r   r9   r<   c                 S   s   t | jdkS )NIndexedSlices)type__name__)gradr*   r*   r+   is_indexed_slices   s    z:TensorBoard._make_histogram_ops.<locals>.is_indexed_slicesc                    s   g | ]} |r|j n|qS r*   )values).0rA   rB   r*   r+   
<listcomp>   s   z3TensorBoard._make_histogram_ops.<locals>.<listcomp>z{}_gradoutputz	{}_out_{}z{}_out)!r   r   r5   layersweightsnamereplacer   r3   r   r,   	histogramr   squeezetupleshapelen	transposereshaper   image_data_formatimager   trainable_weights	optimizerget_gradients
total_lossformathasattr
isinstancerG   list	enumerate)
r(   r5   layerweightZmapped_weight_namew_imgrO   gradsirG   r*   rE   r+   _make_histogram_ops   sh    



 







zTensorBoard._make_histogram_opsc                    s<  | _  | t s2 | tjjj  _	 j
r8 jdur8ddlm} | j|j _ j}|sdd  j jD }g  _i }tjjtj  _}tjjtj  _} j jD ]}|j|v r j |jj}t|jdd }	t||t|	f} jd jd t|	f}
tjt |
|jd d}|||j< tjj!||||  |} j"| qtjjj#$t%|&  _'t( j)t*r fd	d
|+ D }n j)}zddl,m-} W n t.y   t.dY n0 |/ }|0 D ]:\}}|j12 }|j|_3|dur||v r|| |_4q|5 j6| dS )z)Sets Keras model and creates summary ops.Nr   )training_utils_v1c                 S   s    g | ]}t |jd kr|jqS )	Embedding)r?   r@   rJ   )rD   r^   r*   r*   r+   rF     s   z)TensorBoard.set_model.<locals>.<listcomp>r   Z
_embedding)rJ   c                    s   i | ]}| j qS r*   )r    )rD   
layer_namer(   r*   r+   
<dictcomp>5  s   z)TensorBoard.set_model.<locals>.<dictcomp>)	projectorzYFailed to import TensorBoard. Please make sure that TensorBoard integration is complete.")7r5   r6   r   r   rc   r3   r   r,   	merge_allr   r   r!   keras.enginerd   standardize_input_datainput_namesr   rH   assign_embeddingsplaceholderint32batch_idsteprJ   	get_layerrG   npprodrO   rR   intVariablezerosassignappendtrainSaverr\   rC   saverr[   r    strkeysZtensorboard.pluginsri   ImportErrorProjectorConfigitems
embeddingsaddtensor_namemetadata_pathZvisualize_embeddingsr.   )r(   r5   rd   r   Zembeddings_varsrq   rr   r^   Zembedding_inputZembedding_sizerO   	embeddingr   r    ri   configrf   tensorr*   rg   r+   	set_model   sz    







	
zTensorBoard.set_modelc                 C   s"   | j || j |  jd7  _d S Nr   )r.   add_summaryr   )r(   r,   r*   r*   r+   _fetch_callbackW  s    zTensorBoard._fetch_callbackc              	   C   s  |pi }t  r| j t t jdH | D ].\}}t|tj	rN|
 }t jj|||d q2W d   n1 sv0    Y  W d   q1 s0    Y  nV| D ]L\}}t|tj	r|
 }t jj }|j }||_||_| j|| q| j  dS )zWrites metrics out as custom scalar summaries.

        Args:
            step: the global step to use for TensorBoard.
            logs: dict. Keys are scalar summary names, values are
                NumPy scalars.

        T)rr   N)r   r   r.   r0   r,   	record_ifr   r[   rt   ndarrayitemscalarr3   r   Summaryvaluer   simple_valuetagr   flush)r(   rr   logsrJ   r   r,   Zsummary_valuer*   r*   r+   _write_custom_summaries[  s     	R
z#TensorBoard._write_custom_summariesc                 C   s   | j | jd kr|   d S r   )r   r%   _start_profilerr(   r   r   r*   r*   r+   on_train_batch_beginx  s    z TensorBoard.on_train_batch_beginc                 C   s   |  ||S N)on_batch_endr   r*   r*   r+   on_train_batch_end|  s    zTensorBoard.on_train_batch_endc                 C   s   d S r   r*   r(   r   r*   r*   r+   on_test_begin  s    zTensorBoard.on_test_beginc                 C   s   d S r   r*   r   r*   r*   r+   on_test_end  s    zTensorBoard.on_test_endc                 C   s   |pi }|  j |dd7  _ | j | j }| jdkrf|| jkrfdd | D }| | j| | j | _|  jd7  _|   dS )zWrites scalar summaries for metrics on every training batch.

        Performs profiling if current batch is in profiler_batches.
        sizer   r
   c                 S   s"   i | ]\}}|d vrd| |qS )r   r   	num_stepsbatch_r*   rD   kvr*   r*   r+   rh     s   z,TensorBoard.on_batch_end.<locals>.<dictcomp>N)r#   getr$   r"   r   r   r   _stop_profiler)r(   r   r   Zsamples_seen_since
batch_logsr*   r*   r+   r     s    
zTensorBoard.on_batch_endc                 C   s   d S r   r*   r   r*   r*   r+   on_train_begin  s    zTensorBoard.on_train_beginc                 C   sV   | j rR|| j  dkrR| j  | j| jjjvrR| jjj| j | j| jjj| j< dS )zMAdd histogram op to Model eval_function callbacks, reset batch
        count.r   N)	r   r5   _make_test_functionr   test_functionfetchesrz   r   fetch_callbacks)r(   r
   r   r*   r*   r+   on_epoch_begin  s    
zTensorBoard.on_epoch_beginc                    s  dd |  D }| jdkr"|}n| j}| || | jr~| j| jjjv r\| jjj	| j | j| jjj
v r~| jjj
| j | jdu r| jrtd| jr| jdur|| j dkr| jd jd }d}t }||k rt| j|| }t|||  t| jjtr0 fddt| jjD }n| jjd   i}|| j|| j|i tt tsvd|t < |j| j|d	 | j !|t"j#$| j%d
| || j7 }qdS )zKChecks if summary ops should run next epoch, logs scalar
        summaries.c                 S   s"   i | ]\}}|d vrd| |qS )r   epoch_r*   r   r*   r*   r+   rh     s   z,TensorBoard.on_epoch_end.<locals>.<dictcomp>r
   Nz:To visualize embeddings, embeddings_data must be provided.r   c                    s   i | ]\}}||   qS r*   r*   )rD   idxZmodel_inputr   r!   r*   r+   rh     s   F)	feed_dictzkeras_embedding.ckpt)&r   r"   r#   r   r   r   r5   r   r   remover   popr!   r   
ValueErrorrO   r   get_sessionminr   slicer[   inputr\   r]   updaterq   rr   learning_phaserv   runrn   r}   saveospathjoinr   )r(   r
   r   rr   Z	n_samplesrb   sessr   r*   r   r+   on_epoch_end  sR    


zTensorBoard.on_epoch_endc                 C   s   |    | j  d S r   )r   r.   closer   r*   r*   r+   on_train_end  s    zTensorBoard.on_train_endc              
   C   sb   | j r
dS ztjjj| jd d| _ W n6 tjjy\ } zt	d|j
 W Y d}~n
d}~0 0 dS )z*Starts the profiler if currently inactive.N)logdirTzFailed to start profiler: %s)r&   r   profilerexperimentalstartr   errorsAlreadyExistsErrorr   errormessager(   er*   r*   r+   r     s    
zTensorBoard._start_profilerc              
   C   sj   | j s
dS zRztjj  W n6 tjjyR } ztd|j	 W Y d}~n
d}~0 0 W d| _ nd| _ 0 dS )z'Stops the profiler if currently active.NzFailed to stop profiler: %sF)
r&   r   r   r   stopr   UnavailableErrorr   r   r   r   r*   r*   r+   r     s    &zTensorBoard._stop_profiler)r   r   r	   TFFr   NNNr
   r   )N)N)N)N)N)N)N)N)N)N)r@   
__module____qualname____doc__r   r6   rc   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r*   r*   r*   r+   r       s<   J            
8Db








G
r   )r   r   numpyrt   Ztensorflow.compat.v2r3   v2r   kerasr   r   tensorflow.python.platformr   r    tensorflow.python.util.tf_exportr   r   r*   r*   r*   r+   <module>   s   
