o
    2h                     @   sJ   d Z ddlZddlmZ ddlmZ ddlmZ G dd dejjj	Z	dS )ztKeras integration for TensorBoard hparams.

Use `tensorboard.plugins.hparams.api` to access this module's contents.
    N)api_pb2)summary)
summary_v2c                   @   s6   e Zd ZdZdddZdd ZdddZdd	d
ZdS )CallbackzwCallback for logging hyperparameters to TensorBoard.

    NOTE: This callback only works in TensorFlow eager mode.
    Nc                 C   s\   t || _|| _tj| j| jd |du rtdt|tr)tj	j
j|| _dS || _dS )a'  Create a callback for logging hyperparameters to TensorBoard.

        As with the standard `tf.keras.callbacks.TensorBoard` class, each
        callback object is valid for only one call to `model.fit`.

        Args:
          writer: The `SummaryWriter` object to which hparams should be
            written, or a logdir (as a `str`) to be passed to
            `tf.summary.create_file_writer` to create such a writer.
          hparams: A `dict` mapping hyperparameters to the values used in
            this session. Keys should be the names of `HParam` objects used
            in an experiment, or the `HParam` objects themselves. Values
            should be Python `bool`, `int`, `float`, or `string` values,
            depending on the type of the hyperparameter.
          trial_id: An optional `str` ID for the set of hyperparameter
            values used in this trial. Defaults to a hash of the
            hyperparameters.

        Raises:
          ValueError: If two entries in `hparams` share the same
            hyperparameter name.
        trial_idNz3writer must be a `SummaryWriter` or `str`, not None)dict_hparams	_trial_idr   
hparams_pb	TypeError
isinstancestrtfcompatv2r   create_file_writer_writer)selfwriterhparamsr    r   ]/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorboard/plugins/hparams/_keras.py__init__"   s   


zCallback.__init__c                 C   s(   | j d u r	tdt std| j S )Nz@hparams Keras callback cannot be reused across training sessionsz>hparams Keras callback only supported in TensorFlow eager mode)r   RuntimeErrorr   executing_eagerly)r   r   r   r   _get_writerH   s   
zCallback._get_writerc                 C   sF   ~|     tj| j| jd W d    d S 1 sw   Y  d S )Nr   )r   
as_defaultr   r   r	   r
   )r   logsr   r   r   on_train_beginS   s   "zCallback.on_train_beginc                 C   sb   ~|     ttj}| }tjj	jj
j|dd W d    n1 s'w   Y  d | _d S )Nr   )step)r   r   r   session_end_pbr   STATUS_SUCCESSSerializeToStringr   r   r   experimentalwrite_raw_pbr   )r   r   pbraw_pbr   r   r   on_train_endX   s   
zCallback.on_train_end)N)__name__
__module____qualname____doc__r   r   r   r(   r   r   r   r   r      s    
&
r   )
r,   
tensorflowr   tensorboard.plugins.hparamsr   r   r   keras	callbacksr   r   r   r   r   <module>   s   