
    AVh5                        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      Z3 G d d e2jh                  jj                        Z6 G d! d"      Z7 G d# d$e2jh                  jj                        Z8 G d% d&      Z9y')(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                   F    e Zd ZdZ edd      \  ZZZeeedZe	d        Z
y)PruningModez%Class for working with Pruning modes.r      )noneprepostc           	          || j                   v r| j                   |   S t        dj                  dj                  t	        | j                               |            )Nz/pruning_mode mode must be one of: {}. Found: {}z, )_map
ValueErrorformatjoinsorted)clsmodes     W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/boosted_trees_ops.pyfrom_strzPruningMode.from_str8   sR    sxxXXd^
;
B
B499SXXD !%'( (    N)__name__
__module____qualname____doc__range
NO_PRUNINGPRE_PRUNINGPOST_PRUNINGr%   classmethodr-    r.   r,   r   r   2   s5    -*/1+'*k<[,	G$( (r.   r   c                   (     e Zd ZdZ fdZd Z xZS )QuantileAccumulatorSaveablez6SaveableObject implementation for QuantileAccumulator.c           	      2  	 || _         || _        || _        t        | j                   | j                        }d	g }	fd}t	        | j                        D ]  }| |||   dt        |      z         gz  }  t        t        | #  | j                   |       y )N c                 L    t         j                  j                  | |z         S N)r   BaseSaverBuilderSaveSpec)tensorsuffixname
slice_specs     r,   make_save_specz<QuantileAccumulatorSaveable.__init__.<locals>.make_save_specN   s"    ##,,VZOOr.   _bucket_boundaries_)	resource_handle_num_streams
_create_opget_bucket_boundariesr3   strsuperr:   __init__)selfrG   	create_opnum_streamsrC   bucket_boundariesspecsrE   irD   	__class__s       `    @r,   rM   z$QuantileAccumulatorSaveable.__init__E   s    *D#DDO-d.B.B.2.?.?AJEP 4$$% 
*1-/Ds1v/M
N e 

%t5d6J6J6;TCr.   c                     |}t        j                  | j                  g      5  t        | j                  |      cd d d        S # 1 sw Y   y xY w)N)rQ   )r   control_dependenciesrI   quantile_resource_deserializerG   )rN   restored_tensorsunused_tensor_shapesrQ   s       r,   restorez#QuantileAccumulatorSaveable.restoreX   sK    (		!	!4??"3	4 E*


2CEE E Es   AAr/   r0   r1   r2   rM   rZ   __classcell__rT   s   @r,   r:   r:   B   s    >C&Er.   r:   c                   \    e Zd ZdZ	 	 ddZd Zd Zed        Zd Z	d Z
d	 Zd
 Zd Zd Zy)QuantileAccumulatorzSaveableObject implementation for QuantileAccumulator.

     The bucket boundaries are serialized and deserialized from checkpointing.
  Nc           	      6   ~|| _         || _        || _        t        j                  |d      5 }|| _        | j                         | _        | j                         | _	        | j                         }d d d        t        j                  | j                  | j                         t        j                  t        j                  j                  t!        | j                  | j                  | j                  | j                  j"                               y # 1 sw Y   xY w)Nr_   )_epsrH   _num_quantilesr   
name_scope_name_create_resourcerG   _initialize_init_opis_initializedr   register_resourceadd_to_collection	GraphKeysSAVEABLE_OBJECTSr:   rC   )rN   epsilonrP   num_quantilesrC   max_elementsis_initialized_ops          r,   rM   zQuantileAccumulator.__init__e   s     	DI#D'D	3	4 0dj!224d&&(dm--/	0
  4 4dmm 13#--885"22DMM"//1E1E1J1JLM0 0s   ADDc                 F    t        d| j                  | j                        S Nr<   )	containershared_namerC   )quantile_resource_handle_oprd   rN   s    r,   re   z$QuantileAccumulator._create_resource}   s    &$**4::? ?r.   c                 X    t        | j                  | j                  | j                        S r>   )create_quantile_stream_resourcerG   ra   rH   rv   s    r,   rf   zQuantileAccumulator._initialize   s&    *4+?+?+/+<+<> >r.   c                 \    | j                   | j                         | _         | j                   S r>   rg   rf   rv   s    r,   initializerzQuantileAccumulator.initializer   &    }}&&(dm==r.   c                 ,    t        | j                        S r>   ) is_quantile_resource_initializedrG   rv   s    r,   rh   z"QuantileAccumulator.is_initialized   s    +D,@,@AAr.   c                     t        d      NzyWhen the need arises, TF2 compatibility can be added by implementing this method, along with _restore_from_tensors below.NotImplementedErrorrv   s    r,   _serialize_to_tensorsz)QuantileAccumulator._serialize_to_tensors       
 = > >r.   c                     t        d      NzyWhen the need arises, TF2 compatibility can be added by implementing this method, along with _serialize_to_tensors above.r   rN   rX   s     r,   _restore_from_tensorsz)QuantileAccumulator._restore_from_tensors   r   r.   c                 `    t        ||| j                        }t        | j                  |      }|S r>   )make_quantile_summariesra   quantile_add_summariesrG   )rN   float_columnsexample_weights	summaries
summary_ops        r,   add_summariesz!QuantileAccumulator.add_summaries   s/    '(,		3I'(<(<iHJr.   c                 B    t        | j                  | j                        S r>   )quantile_flushrG   rb   rv   s    r,   flushzQuantileAccumulator.flush   s    $..0C0CDDr.   c                 B    t        | j                  | j                        S r>   )rJ   rG   rH   rv   s    r,   rJ   z)QuantileAccumulator.get_bucket_boundaries   s     !5!5t7H7HIIr.   )NN)r/   r0   r1   r2   rM   re   rf   propertyr{   rh   r   r   r   r   rJ   r8   r.   r,   r_   r_   _   sT      M0?>  
B>
>
EJr.   r_   c                   (     e Zd ZdZ fdZd Z xZS )_TreeEnsembleSavablez/SaveableObject implementation for TreeEnsemble.c                    t        j                  |      \  }}d}t        j                  j	                  |||dz         t        j                  j	                  |||dz         g}t
        t        |   |||       || _        || _	        y)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.
    r<   _stamp_serializedN)
r    boosted_trees_serialize_ensembler   r?   r@   rL   r   rM   rG   rI   )	rN   rG   rO   rC   stamp_token
serializedrD   rR   rT   s	           r,   rM   z_TreeEnsembleSavable.__init__   s     	>>O K
 J''Z(,x	9''
J(,}(<	>E 

.tL*DDOr.   c                     t        j                  | j                  g      5  t        j                  | j
                  |d   |d         cddd       S # 1 sw Y   yxY w)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   rV   rI   r   "boosted_trees_deserialize_ensemblerG   )rN   rX   unused_restored_shapess      r,   rZ   z_TreeEnsembleSavable.restore   sU     
	!	!4??"3	4 8"EE


&q)#3A#688 8 8s   (AAr[   r]   s   @r,   r   r      s    7 08r.   r   c                   ^    e Zd ZdZddZd Zd Zed        Zd Z	d Z
d Zd	 Zd
 Zd Zd Zy)TreeEnsemblezCreates TreeEnsemble resource.c           	      (   || _         || _        || _        t        j                  |d      5 }|| _        | j                         | _        | j                         | _	        | j                         }|sat        j                  t        j                  j                  t        | j                  | j                  | j                  j                                t#        j$                  | j                  | j                  ||        d d d        y # 1 sw Y   y xY w)Nr   )	is_shared)_stamp_token_serialized_proto	_is_localr   rc   rd   re   rG   rf   rg   rh   rj   rk   rl   r   r{   rC   r   ri   )rN   rC   r   is_localserialized_protorp   s         r,   rM   zTreeEnsemble.__init__   s    #D-DDN	n	- "dj!224d&&(dm--/cmm<<2"&"6"68H8H"&"6"6";";=	> !!






 L	"" " "s   CDDc                 Z    t        j                  d| j                  | j                        S rr   )r   )boosted_trees_ensemble_resource_handle_oprd   rv   s    r,   re   zTreeEnsemble._create_resource   s$     JJ$**4::? ?r.   c                 n    t        j                  | j                  | j                  | j                        S )N)r   )r   boosted_trees_create_ensemblerG   r   r   rv   s    r,   rf   zTreeEnsemble._initialize   s0     >>!%!7!79 9r.   c                 \    | j                   | j                         | _         | j                   S r>   rz   rv   s    r,   r{   zTreeEnsemble.initializer   r|   r.   c                 @    t        j                  | j                        S r>   )r   %is_boosted_trees_ensemble_initializedrG   rv   s    r,   rh   zTreeEnsemble.is_initialized   s     FF r.   c                     t        d      r   r   rv   s    r,   r   z"TreeEnsemble._serialize_to_tensors   r   r.   c                     t        d      r   r   r   s     r,   r   z"TreeEnsemble._restore_from_tensors  r   r.   c                 P    t        j                  | j                        \  }}}}}|S )z0Returns the current stamp token of the resource.)r   !boosted_trees_get_ensemble_statesrG   )rN   r   _s      r,   get_stamp_tokenzTreeEnsemble.get_stamp_token  s2     	??  	" KAq! r.   c                 ,   t        j                  | j                        \  }}}}}t        j                  |d      t        j                  |d      t        j                  |d      t        j                  |d      t        j                  |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   )rC   	num_treesnum_finalized_treesnum_attempted_layerslast_layer_nodes_range)r   r   rG   r   identity)rN   r   r   r   r   nodes_ranges         r,   
get_stateszTreeEnsemble.get_states  s     
	@	@!!
#[)02F {?y{;29NO$+AC{1IJL Lr.   c                 @    t        j                  | j                        S )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   rG   rv   s    r,   	serializezTreeEnsemble.serialize"  s      !AA r.   c                 D    t        j                  | 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   rG   )rN   r   r   s      r,   deserializezTreeEnsemble.deserialize,  s%     !CCk+;= =r.   N)r   Fr<   )r/   r0   r1   r2   rM   re   rf   r   r{   rh   r   r   r   r   r   r   r8   r.   r,   r   r      sL    &"*?9  
>
>
L&=r.   r   N):r2   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   rx   r   example_debug_outputsr   r   r   make_stats_summaryr   predictr   r   r   rW   r   r   r   rJ   r   ru   r   r   #sparse_calculate_best_feature_splitr   training_predictr   update_ensembler   update_ensemble_v2r   r~   tensorflow.python.trainingr   r   r?   SaveableObjectr:   r_   r   r   r8   r.   r,   <module>r      s     + + 7 + V O C I K ` I t x n X G L v N H \ Q j h n R -( ( E%"8"8"G"G E:CJ CJL*8511@@ *8Ze= e=r.   