B
    ӻdM                 @   s   d 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 edgdG dd dejZdS )zDCallbacks: utilities called at certain points during model training.    N)context)dtypes)errors)backend)	callbacks)	array_ops)	state_ops)summary_ops_v2)	variables)
tf_logging)profiler_v2)summary)saver)keras_exportzkeras.callbacks.TensorBoard)Z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   ZCallback__init__log_dirhistogram_freqr   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_startedZ_chief_worker_only)selfr   r   r"   r   r    r!   r%   r&   r'   r(   r)   Zprofile_batch r/   V/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/keras/callbacks_v1.pyr   s   s6    
zTensorBoard.__init__c          	   C   sv   t  rHt| j| _|jsr| jrr| j  t	t
  W dQ R X n*| jrdt| jt
 | _nt| j| _dS )zSets file writer.N)r   r   r	   Zcreate_file_writer_v2r   writerZrun_eagerlyr   
as_defaultgraphKZ	get_graph
tf_summaryZ
FileWriter)r.   modelr/   r/   r0   _init_writer   s    zTensorBoard._init_writerc       
   	      s@  | j r<| jdkr<x$| jjD ]}xJ|jD ]>}|jdd}t|| | j	r0t
|}t|}t|dkr|d |d krt
|}t|}t
|d|d |d dg}nt|dkrt dkrt
j|dddgd	}t|}t
||d |d |d dg}n(t|dkr0t
|d|d ddg}nq0t|}t|d
kr`|d dksdtt|| q0W | jrxZ|jD ]P}|jdd}|j|j|}dd   fdd|D }td|| qW t|dr t|jtr"xFt|jD ] \}}	td|j||	 qW q td|j|j q W dS )z.Defines histogram ops when histogram_freq > 0.N:_r   r   r      Zchannels_last)Zperm   )r   r:   r;   c             S   s   t | jdkS )NZIndexedSlices)type__name__)gradr/   r/   r0   is_indexed_slices   s    z:TensorBoard._make_histogram_ops.<locals>.is_indexed_slicesc                s   g | ]} |r|j n|qS r/   )values).0r?   )r@   r/   r0   
<listcomp>   s   z3TensorBoard._make_histogram_ops.<locals>.<listcomp>z{}_gradoutputz	{}_out_{}z{}_out)r   r   r6   layersweightsnamereplacer5   Z	histogramr!   r   Zsqueezer4   Z	int_shapelenZ	transposereshapeZimage_data_formatAssertionErrorimager    Ztrainable_weightsZ	optimizerZget_gradientsZ
total_lossformathasattr
isinstancerD   list	enumerate)
r.   r6   layerweightZmapped_weight_nameZw_imgshapeZgradsirD   r/   )r@   r0   _make_histogram_ops   sL    





 

zTensorBoard._make_histogram_opsc                s.  | _  | t s, | t  _ jr* j	dk	r*ddl
m} | j	|j _	 j}|szdd  j jD }g  _i }ttj  _}ttj  _}x j jD ]}|j|k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$| qW t%&t'|(  _%t) j*t+r fd	d
|, D }n j*}yddl-m.} W n t/k
r   t/dY nX |0 }xF|1 D ]:\}}|j23 }|j|_4|dk	r||kr|| |_5qW |6 j7| 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 )Z	Embedding)r=   r>   rG   )rB   rR   r/   r/   r0   rC      s   z)TensorBoard.set_model.<locals>.<listcomp>r   Z
_embedding)rG   c                s   i | ]} j |qS r/   )r'   )rB   
layer_name)r.   r/   r0   
<dictcomp>  s   z)TensorBoard.set_model.<locals>.<dictcomp>)	projectorzYFailed to import TensorBoard. Please make sure that TensorBoard integration is complete.")8r6   r7   r   r   rV   r5   Z	merge_allr   r%   r(   Ztensorflow.python.keras.enginerW   Zstandardize_input_dataZinput_namesr&   rE   assign_embeddingsr   placeholderr   Zint32batch_idsteprG   Z	get_layerrD   npprodrT   rJ   intr
   VariableZzerosr   Zassignappendr   ZSaverrP   rA   rO   r'   strkeysZtensorboard.pluginsrZ   ImportErrorZProjectorConfigitemsZ
embeddingsaddZtensor_nameZmetadata_pathZvisualize_embeddingsr1   )r.   r6   rW   r&   Zembeddings_varsr]   r^   rR   Zembedding_inputZembedding_sizerT   Z	embeddingr   r'   rZ   configrX   Ztensorr/   )r.   r0   	set_model   s^    








zTensorBoard.set_modelc             C   s"   | j || j |  jd7  _d S )Nr   )r1   add_summaryr$   )r.   r   r/   r/   r0   _fetch_callback9  s    zTensorBoard._fetch_callbackc          
   C   s   |pi }t  rx| j V td@ x8| D ],\}}t|tj	rN|
 }tj|||d q2W W dQ R X W dQ R X nVxT| D ]H\}}t|tj	r|
 }t }|j }||_||_| j|| qW | 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)r^   N)r   r   r1   r2   r	   Z	record_ifrg   rO   r_   ZndarrayitemZscalarr5   Summaryvaluerh   Zsimple_valuetagrk   flush)r.   r^   logsrG   ro   r   Zsummary_valuer/   r/   r0   _write_custom_summaries=  s     	*
z#TensorBoard._write_custom_summariesc             C   s   | j | jd kr|   d S )Nr   )r#   r,   _start_profiler)r.   r   rr   r/   r/   r0   on_train_batch_beginZ  s    z TensorBoard.on_train_batch_beginc             C   s   |  ||S )N)on_batch_end)r.   r   rr   r/   r/   r0   on_train_batch_end^  s    zTensorBoard.on_train_batch_endc             C   s   d S )Nr/   )r.   rr   r/   r/   r0   on_test_begina  s    zTensorBoard.on_test_beginc             C   s   d S )Nr/   )r.   rr   r/   r/   r0   on_test_endd  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 kr|d| qS ))r   rz   	num_stepsZbatch_r/   )rB   kvr/   r/   r0   rY   q  s   z,TensorBoard.on_batch_end.<locals>.<dictcomp>N)r*   getr+   r)   rg   rs   r#   _stop_profiler)r.   r   rr   Zsamples_seen_sinceZ
batch_logsr/   r/   r0   rv   g  s    zTensorBoard.on_batch_endc             C   s   d S )Nr/   )r.   rr   r/   r/   r0   on_train_beginy  s    zTensorBoard.on_train_beginc             C   sV   | j rR|| j  dkrR| j  | j| jjjkrR| jjj| j | j| jjj| j< dS )zEAdd histogram op to Model eval_function callbacks, reset batch count.r   N)	r   r6   Z_make_test_functionr   test_functionfetchesrc   rl   fetch_callbacks)r.   r   rr   r/   r/   r0   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kr\| jjj	| j | j| jjj
kr~| jjj
| j | jdkr| jrtd| jr| jdk	r|| j dkr| jd jd }d}t }x||k rt| j|| }t|||  t| jjtr2 fddt| jjD }n| jjd   i}|| j|| j|i tt tsxd|t < |j| j|d	 | j !|t"j#$| j%d
| || j7 }qW dS )zCChecks if summary ops should run next epoch, logs scalar summaries.c             S   s"   i | ]\}}|d kr|d| qS ))r   rz   r{   Zepoch_r/   )rB   r|   r}   r/   r/   r0   rY     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/   )rB   idxZmodel_input)r   r(   r/   r0   rY     s   F)	feed_dictzkeras_embedding.ckpt)&rg   r)   r*   rs   r   r   r6   r   r   remover   popr(   r%   
ValueErrorrT   r4   Zget_sessionminr"   slicerO   inputrP   rQ   updater]   r^   Zlearning_phasera   runr[   r   saveospathjoinr   )r.   r   rr   r^   Z	n_samplesrU   sessr   r/   )r   r(   r0   on_epoch_end  sD    
zTensorBoard.on_epoch_endc             C   s   |    | j  d S )N)r   r1   close)r.   rr   r/   r/   r0   on_train_end  s    zTensorBoard.on_train_endc          
   C   sZ   | j r
dS ytj| jd d| _ W n2 tjk
rT } ztd|j W dd}~X Y nX dS )z*Starts the profiler if currently inactive.N)ZlogdirTzFailed to start profiler: %s)	r-   profilerstartr   r   ZAlreadyExistsErrorr   errormessage)r.   er/   r/   r0   rt     s    
zTensorBoard._start_profilerc          
   C   s\   | j s
dS zDyt  W n2 tjk
rJ } ztd|j W dd}~X Y nX W dd| _ X dS )z'Stops the profiler if currently active.NzFailed to stop profiler: %sF)r-   r   stopr   ZUnavailableErrorr   r   r   )r.   r   r/   r/   r0   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   r7   rV   rj   rl   rs   ru   rw   rx   ry   rv   r   r   r   r   rt   r   r/   r/   r/   r0   r   &   s:   I           
'7P








@
r   ) r   r   numpyr_   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   r   Ztensorflow.python.kerasr   r4   r   Ztensorflow.python.opsr   r   r	   r
   Ztensorflow.python.platformr   r   Ztensorflow.python.profilerr   r   Ztensorflow.python.summaryr   r5   Ztensorflow.python.trainingr   Z tensorflow.python.util.tf_exportr   r   r/   r/   r/   r0   <module>   s$   