o
    i 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)   V/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/ops/boosted_trees_ops.pyfrom_str8   s   

zPruningMode.from_strN)__name__
__module____qualname____doc__range
NO_PRUNINGPRE_PRUNING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)tensorsuffixname
slice_specr)   r*   make_save_specN   s   z<QuantileAccumulatorSaveable.__init__.<locals>.make_save_spec_bucket_boundaries_)	resource_handle_num_streams
_create_opget_bucket_boundariesr0   strsuperr6   __init__)	selfrB   	create_opnum_streamsr>   bucket_boundariesspecsr@   i	__class__r=   r*   rH   E   s    z$QuantileAccumulatorSaveable.__init__c                 C   sD   |}t | jg t| j|dW  d    S 1 sw   Y  d S )N)rL   )r   control_dependenciesrD   quantile_resource_deserializerB   )rI   restored_tensorsunused_tensor_shapesrL   r)   r)   r*   restoreX   s   $z#QuantileAccumulatorSaveable.restorer,   r-   r.   r/   rH   rU   __classcell__r)   r)   rO   r*   r6   B   s    r6   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 )NrX   )_epsrC   _num_quantilesr   
name_scope_name_create_resourcerB   _initialize_init_opis_initializedr   register_resourceadd_to_collection	GraphKeysSAVEABLE_OBJECTSr6   r>   )rI   epsilonrK   num_quantilesr>   max_elementsis_initialized_opr)   r)   r*   rH   e   s&   




zQuantileAccumulator.__init__c                 C   s   t d| j| jdS Nr7   )	containershared_namer>   )quantile_resource_handle_opr\   rI   r)   r)   r*   r]   }   s   
z$QuantileAccumulator._create_resourcec                 C   s   t | j| j| jS r8   )create_quantile_stream_resourcerB   rY   rC   rm   r)   r)   r*   r^      s   
zQuantileAccumulator._initializec                 C      | j d u r
|  | _ | j S r8   r_   r^   rm   r)   r)   r*   initializer      

zQuantileAccumulator.initializerc                 C   s
   t | jS r8   ) is_quantile_resource_initializedrB   rm   r)   r)   r*   r`      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.NotImplementedErrorrm   r)   r)   r*   _serialize_to_tensors      z)QuantileAccumulator._serialize_to_tensorsc                 C   rt   NzyWhen the need arises, TF2 compatibility can be added by implementing this method, along with _serialize_to_tensors above.rv   rI   rS   r)   r)   r*   _restore_from_tensors   ry   z)QuantileAccumulator._restore_from_tensorsc                 C   s   t ||| j}t| j|}|S r8   )make_quantile_summariesrY   quantile_add_summariesrB   )rI   float_columnsexample_weights	summaries
summary_opr)   r)   r*   add_summaries   s
   z!QuantileAccumulator.add_summariesc                 C      t | j| jS r8   )quantile_flushrB   rZ   rm   r)   r)   r*   flush      zQuantileAccumulator.flushc                 C   r   r8   )rE   rB   rC   rm   r)   r)   r*   rE      r   z)QuantileAccumulator.get_bucket_boundaries)NN)r,   r-   r.   r/   rH   r]   r^   propertyrq   r`   rx   r|   r   r   rE   r)   r)   r)   r*   rX   _   s    	

rX   c                       r5   )_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.
    r7   _stamp_serializedN)
r    boosted_trees_serialize_ensembler   r9   r:   rG   r   rH   rB   rD   )rI   rB   rJ   r>   stamp_token
serializedr?   rM   rO   r)   r*   rH      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   rQ   rD   r   "boosted_trees_deserialize_ensemblerB   )rI   rS   unused_restored_shapesr)   r)   r*   rU      s   $z_TreeEnsembleSavable.restorerV   r)   r)   rO   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   Fr7   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   )	is_shared)_stamp_token_serialized_proto	_is_localr   r[   r\   r]   rB   r^   r_   r`   rb   rc   rd   r   rq   r>   r   ra   )rI   r>   r   is_localserialized_protorh   r)   r)   r*   rH      s,   


"zTreeEnsemble.__init__c                 C   s   t jd| j| jdS ri   )r   )boosted_trees_ensemble_resource_handle_opr\   rm   r)   r)   r*   r]      s   
zTreeEnsemble._create_resourcec                 C   s   t j| j| j| jdS )N)r   )r   boosted_trees_create_ensemblerB   r   r   rm   r)   r)   r*   r^      s
   zTreeEnsemble._initializec                 C   ro   r8   rp   rm   r)   r)   r*   rq      rr   zTreeEnsemble.initializerc                 C      t | jS r8   )r   %is_boosted_trees_ensemble_initializedrB   rm   r)   r)   r*   r`      s   zTreeEnsemble.is_initializedc                 C   rt   ru   rv   rm   r)   r)   r*   rx      ry   z"TreeEnsemble._serialize_to_tensorsc                 C   rt   rz   rv   r{   r)   r)   r*   r|     ry   z"TreeEnsemble._restore_from_tensorsc                 C   s   t | j\}}}}}|S )z0Returns the current stamp token of the resource.)r   !boosted_trees_get_ensemble_statesrB   )rI   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_layerslast_layer_nodes_range)r   r   rB   r   identity)rI   r   r   r   r   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   rB   rm   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   rB   )rI   r   r   r)   r)   r*   deserialize,  s   
zTreeEnsemble.deserializeN)r   Fr7   )r,   r-   r.   r/   rH   r]   r^   r   rq   r`   rx   r|   r   r   r   r   r)   r)   r)   r*   r      s    


r   N):r/   tensorflow.python.frameworkr   tensorflow.python.opsr   r   r   +tensorflow.python.ops.gen_boosted_trees_opsr   r   r   calculate_best_feature_splitr	   calculate_best_feature_split_v2r
    calculate_best_gains_per_featurer   center_biasr   rn   r   example_debug_outputsr   r}   r   make_stats_summaryr   predictr   r~   r   rR   r   r   r   rE   r   rl   r   r   #sparse_calculate_best_feature_splitr   training_predictr   update_ensembler   update_ensemble_v2r   rs   tensorflow.python.trainingr   r   r9   SaveableObjectr6   rX   r   r   r)   r)   r)   r*   <module>   sB   F-