o
    ?e5                     @   s  d 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 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/Z0 ddl1m2Z2 G dd dZ3G dd  d e2j4j5Z6G d!d" d"Z7G d#d$ d$e2j4j5Z8G d%d& d&Z9d'S )(zOps for boosted_trees.    )ops)	array_ops)gen_boosted_trees_ops)	resources)boosted_trees_aggregate_stats)boosted_trees_bucketize)*boosted_trees_calculate_best_feature_split)-boosted_trees_calculate_best_feature_split_v2).boosted_trees_calculate_best_gains_per_feature)boosted_trees_center_bias)-boosted_trees_create_quantile_stream_resource)#boosted_trees_example_debug_outputs)%boosted_trees_make_quantile_summaries) boosted_trees_make_stats_summary)boosted_trees_predict)4boosted_trees_quantile_stream_resource_add_summaries)2boosted_trees_quantile_stream_resource_deserialize),boosted_trees_quantile_stream_resource_flush)<boosted_trees_quantile_stream_resource_get_bucket_boundaries)0boosted_trees_quantile_stream_resource_handle_op)$boosted_trees_sparse_aggregate_stats)1boosted_trees_sparse_calculate_best_feature_split)boosted_trees_training_predict)boosted_trees_update_ensemble) boosted_trees_update_ensemble_v2)5is_boosted_trees_quantile_stream_resource_initialized)saverc                   @   s8   e Zd ZdZedd\ZZZeeedZe	dd Z
dS )PruningModez%Class for working with Pruning modes.r      )noneprepostc                 C   s0   || j v r
| j | S tddt| j |)Nz/pruning_mode mode must be one of: {}. Found: {}z, )_map
ValueErrorformatjoinsorted)clsmode r)   h/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/ops/boosted_trees_ops.pyfrom_str8   s   

zPruningMode.from_strN)__name__
__module____qualname____doc__rangeZ
NO_PRUNINGZPRE_PRUNINGZPOST_PRUNINGr"   classmethodr+   r)   r)   r)   r*   r   2   s    r   c                       (   e Zd ZdZ fddZdd Z  ZS )QuantileAccumulatorSaveablez6SaveableObject implementation for QuantileAccumulator.c           	         s|   || _ || _|| _t| j | j}dg } fdd}t| jD ]}|||| dt| g7 }q tt| | j |  d S )N c                    s   t j|  | S N)r   BaseSaverBuilderSaveSpec)Ztensorsuffixname
slice_specr)   r*   make_save_specN   s   z<QuantileAccumulatorSaveable.__init__.<locals>.make_save_specZ_bucket_boundaries_)	resource_handle_num_streams
_create_opget_bucket_boundariesr0   strsuperr3   __init__)	selfr=   	create_opnum_streamsr:   bucket_boundariesspecsr<   i	__class__r9   r*   rC   E   s    z$QuantileAccumulatorSaveable.__init__c                 C   sD   |}t | jg t| j|dW  d    S 1 sw   Y  d S )N)rG   )r   control_dependenciesr?   quantile_resource_deserializer=   )rD   restored_tensorsZunused_tensor_shapesrG   r)   r)   r*   restoreX   s   $z#QuantileAccumulatorSaveable.restorer,   r-   r.   r/   rC   rO   __classcell__r)   r)   rJ   r*   r3   B   s    r3   c                   @   sj   e Zd ZdZ		dddZdd Zdd Zed	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )QuantileAccumulatorzSaveableObject implementation for QuantileAccumulator.

     The bucket boundaries are serialized and deserialized from checkpointing.
  Nc                 C   s   ~|| _ || _|| _t|d}|| _|  | _|  | _	| 
 }W d    n1 s,w   Y  t| j| j	| ttjjt| j| j	| j| jj d S )NrR   )_epsr>   _num_quantilesr   
name_scope_name_create_resourcer=   _initialize_init_opis_initializedr   register_resourceadd_to_collection	GraphKeysSAVEABLE_OBJECTSr3   r:   )rD   epsilonrF   Znum_quantilesr:   Zmax_elementsis_initialized_opr)   r)   r*   rC   e   s&   




zQuantileAccumulator.__init__c                 C   s   t d| j| jdS Nr4   )	containerZshared_namer:   )quantile_resource_handle_oprV   rD   r)   r)   r*   rW   }   s   
z$QuantileAccumulator._create_resourcec                 C   s   t | j| j| jS r5   )create_quantile_stream_resourcer=   rS   r>   rd   r)   r)   r*   rX      s   
zQuantileAccumulator._initializec                 C      | j d u r
|  | _ | j S r5   rY   rX   rd   r)   r)   r*   initializer      

zQuantileAccumulator.initializerc                 C   s
   t | jS r5   ) is_quantile_resource_initializedr=   rd   r)   r)   r*   rZ      s   
z"QuantileAccumulator.is_initializedc                 C      t dNzyWhen the need arises, TF2 compatibility can be added by implementing this method, along with _restore_from_tensors below.NotImplementedErrorrd   r)   r)   r*   _serialize_to_tensors      z)QuantileAccumulator._serialize_to_tensorsc                 C   rk   NzyWhen the need arises, TF2 compatibility can be added by implementing this method, along with _serialize_to_tensors above.rm   rD   rN   r)   r)   r*   _restore_from_tensors   rp   z)QuantileAccumulator._restore_from_tensorsc                 C   s   t ||| j}t| j|}|S r5   )make_quantile_summariesrS   quantile_add_summariesr=   )rD   Zfloat_columnsZexample_weightsZ	summariesZ
summary_opr)   r)   r*   add_summaries   s
   z!QuantileAccumulator.add_summariesc                 C      t | j| jS r5   )quantile_flushr=   rT   rd   r)   r)   r*   flush      zQuantileAccumulator.flushc                 C   rw   r5   )r@   r=   r>   rd   r)   r)   r*   r@      rz   z)QuantileAccumulator.get_bucket_boundaries)NN)r,   r-   r.   r/   rC   rW   rX   propertyrh   rZ   ro   rs   rv   ry   r@   r)   r)   r)   r*   rR   _   s    	

rR   c                       r2   )_TreeEnsembleSavablez/SaveableObject implementation for TreeEnsemble.c                    s^   t |\}}d}tj|||d tj|||d g}tt| ||| || _|| _	dS )zCreates a _TreeEnsembleSavable object.

    Args:
      resource_handle: handle to the decision tree ensemble variable.
      create_op: the op to initialize the variable.
      name: the name to save the tree ensemble variable under.
    r4   Z_stampZ_serializedN)
r    boosted_trees_serialize_ensembler   r6   r7   rB   r|   rC   r=   r?   )rD   r=   rE   r:   stamp_tokenZ
serializedr;   rH   rJ   r)   r*   rC      s   	


z_TreeEnsembleSavable.__init__c                 C   sL   t | jg tj| j|d |d dW  d   S 1 sw   Y  dS )ag  Restores the associated tree ensemble from 'restored_tensors'.

    Args:
      restored_tensors: the tensors that were loaded from a checkpoint.
      unused_restored_shapes: the shapes this object should conform to after
        restore. Not meaningful for trees.

    Returns:
      The operation that restores the state of the tree ensemble variable.
    r      )r~   tree_ensemble_serializedN)r   rL   r?   r   "boosted_trees_deserialize_ensembler=   )rD   rN   Zunused_restored_shapesr)   r)   r*   rO      s   $z_TreeEnsembleSavable.restorerP   r)   r)   rJ   r*   r|      s    r|   c                   @   sn   e Zd ZdZdddZdd Zd	d
 Zedd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )TreeEnsemblezCreates TreeEnsemble resource.r   Fr4   c                 C   s   || _ || _|| _t|d8}|| _|  | _|  | _	| 
 }|s3ttjjt| j| j| jj tj| j| j|| d W d    d S 1 sJw   Y  d S )Nr   )Z	is_shared)_stamp_token_serialized_protoZ	_is_localr   rU   rV   rW   r=   rX   rY   rZ   r\   r]   r^   r|   rh   r:   r   r[   )rD   r:   r~   is_localserialized_protor`   r)   r)   r*   rC      s,   


"zTreeEnsemble.__init__c                 C   s   t jd| j| jdS ra   )r   Z)boosted_trees_ensemble_resource_handle_oprV   rd   r)   r)   r*   rW      s   
zTreeEnsemble._create_resourcec                 C   s   t j| j| j| jdS )N)r   )r   Zboosted_trees_create_ensembler=   r   r   rd   r)   r)   r*   rX      s
   zTreeEnsemble._initializec                 C   rf   r5   rg   rd   r)   r)   r*   rh      ri   zTreeEnsemble.initializerc                 C      t | jS r5   )r   Z%is_boosted_trees_ensemble_initializedr=   rd   r)   r)   r*   rZ      s   zTreeEnsemble.is_initializedc                 C   rk   rl   rm   rd   r)   r)   r*   ro      rp   z"TreeEnsemble._serialize_to_tensorsc                 C   rk   rq   rm   rr   r)   r)   r*   rs     rp   z"TreeEnsemble._restore_from_tensorsc                 C   s   t | j\}}}}}|S )z0Returns the current stamp token of the resource.)r   !boosted_trees_get_ensemble_statesr=   )rD   r~   _r)   r)   r*   get_stamp_token  s
   zTreeEnsemble.get_stamp_tokenc                 C   sV   t | j\}}}}}tj|ddtj|ddtj|ddtj|ddtj|ddfS )zReturns states of the tree ensemble.

    Returns:
      stamp_token, num_trees, num_finalized_trees, num_attempted_layers and
      range of the nodes in the latest layer.
    r~   )r:   	num_treesnum_finalized_treesnum_attempted_layersZlast_layer_nodes_range)r   r   r=   r   identity)rD   r~   r   r   r   Znodes_ranger)   r)   r*   
get_states  s   	
zTreeEnsemble.get_statesc                 C   r   )zSerializes the ensemble into proto and returns the serialized proto.

    Returns:
      stamp_token: int64 scalar Tensor to denote the stamp of the resource.
      serialized_proto: string scalar Tensor of the serialized proto.
    )r   r}   r=   rd   r)   r)   r*   	serialize"  s   zTreeEnsemble.serializec                 C   s   t | j||S )a  Deserialize the input proto and resets the ensemble from it.

    Args:
      stamp_token: int64 scalar Tensor to denote the stamp of the resource.
      serialized_proto: string scalar Tensor of the serialized proto.

    Returns:
      Operation (for dependencies).
    )r   r   r=   )rD   r~   r   r)   r)   r*   deserialize,  s   
zTreeEnsemble.deserializeN)r   Fr4   )r,   r-   r.   r/   rC   rW   rX   r{   rh   rZ   ro   rs   r   r   r   r   r)   r)   r)   r*   r      s    


r   N):r/   Ztensorflow.python.frameworkr   Ztensorflow.python.opsr   r   r   Z+tensorflow.python.ops.gen_boosted_trees_opsr   r   r   Zcalculate_best_feature_splitr	   Zcalculate_best_feature_split_v2r
   Z calculate_best_gains_per_featurer   Zcenter_biasr   re   r   Zexample_debug_outputsr   rt   r   Zmake_stats_summaryr   Zpredictr   ru   r   rM   r   rx   r   r@   r   rc   r   r   Z#sparse_calculate_best_feature_splitr   Ztraining_predictr   Zupdate_ensembler   Zupdate_ensemble_v2r   rj   Ztensorflow.python.trainingr   r   r6   ZSaveableObjectr3   rR   r|   r   r)   r)   r)   r*   <module>   sB   F-