
    6bi                       S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJ	s  J
r  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr   SSK!J"r"  SSK!J#r#  SSK!J$r$  SSK!J%r%  SSK!J&r'  SSK!J(r(  SSK)J*r*  SSK+J,r-  SSK.J/r/  SSK.J0r0  SSK1J2r2  SSK1J3r3  SSK1J4r4  SSK1J5r5  SSK6J7r7  SSK8J9r9  SSK8J:r:  SSK;J<r<  SS K;J=r=  SS!K;J>r>  SS"K;J?r?  SS#K;J@r@  SS$K;JArA  SS%K;JBrB  SS&K;JCrC  SS'KDJErE   SSKFrF\" S(S)5       " S* S+\"R                  \CR                  5      5       rJ " S, S-5      rK " S. S/\K5      rLS0 rMSBS1 jrNS2 rOS3 rPS4 rQS5 rRS6 rSS7 rTS8 rUS9 rVS: rWS; rXS< rYS= rZS> r[S? r\S@ r]SA r^g! \G a    SrF Nf = f)Cz-Training-related part of the TF-Keras engine.    N)distribute_utils)	input_ops)context)
tf_logging)keras_export)doc_controls)backend)	callbacks)
optimizers)dtensor_api)
layout_map)
base_layer)base_layer_utils)compile_utils)data_adapter)input_layer)training_utils)base_metric)loss_scale_optimizer)	optimizer)optimizer_v1)pickle_utils)
saving_api)
saving_lib)serialization_lib)serialization)
json_utils)model_serialization)generic_utils)io_utils)layer_utils)steps_per_execution_tuning)
tf_inspect)tf_utils)traceback_utils)version_utils)ModeKeyszkeras.Modelzkeras.models.Modelc                     ^  \ rS rSrSr\" \R                  " S\R                  R                  5      5      r
SrU 4S jr\R                  R                  R                   \R$                  U 4S j5       5       rS r\R                  R                  R                   S 5       rU 4S	 jrU 4S
 jrU 4S jrS r\R6                  U 4S j5       r\R$                  U 4S j5       r\R>                  SdS j5       r \R$                           SeS j5       r!S r"\R                  R                  R                   S 5       r#\R                  R                  R                   S 5       r$\%S 5       r&\%S 5       r'\%S 5       r(\%S 5       r)\%S 5       r*\*RV                  S 5       r*\%S 5       r,\,RV                  S 5       r,\%S 5       r-\-RV                  S 5       r-\%S 5       r.\.RV                  S 5       r.\%S  5       r/\/RV                  S! 5       r/S" r0S# r1SfS$ jr2S% r3S& r4S' r5S( r6SgS) jr7\R$                                     ShS* j5       r8S+ r9S, r:SgS- jr;\R$                             SiS. j5       r<S/ r=S0 r>S1 r?S2 r@SgS3 jrA\R$                         SjS4 j5       rBS5 rC     SkS6 jrD    SlS7 jrES8 rF\R                               SmS9 j5       rH\R                        SnS: j5       rI\R                        SnS; j5       rJ\%S< 5       rK\%S= 5       rLU 4S> jrM\R$                  SoS? j5       rN\R$                   SpS@ j5       rO\R$                   SqSA j5       rPSB rQ\R6                  SC 5       rR\SSrSD j5       rTSE rUSF rVSG rW\%\R                  SH 5       5       rX\%SI 5       rY\%SJ 5       rZ      SsSK jr[\%SL 5       r\\\RV                  SM 5       r\SdSN jr]SO r^SP r_SQ r`SR ra\R                  R                  R                   SdU 4SS jj5       rbStST jrcSU rdSV reSW rfSX rgSY rhSZ riSdS[ jrj\%S\ 5       rkSuU 4S] jjrlS^ rmStS_ jrnS` roSa rp\%Sb 5       rqScrrU =rs$ )vModelF   a  A model grouping layers into an object with training/inference features.

Args:
    inputs: The input(s) of the model: a `keras.Input` object or a
        combination of `keras.Input` objects in a dict, list or tuple.
    outputs: The output(s) of the model: a tensor that originated from
        `keras.Input` objects or a combination of such tensors in a dict,
        list or tuple. See Functional API example below.
    name: String, the name of the model.

There are two ways to instantiate a `Model`:

1 - With the "Functional API", where you start from `Input`,
you chain layer calls to specify the model's forward pass,
and finally you create your model from inputs and outputs:

```python
import tensorflow as tf

inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
```

Note: Only dicts, lists, and tuples of input tensors are supported. Nested
inputs are not supported (e.g. lists of list or dicts of dict).

A new Functional API model can also be created by using the
intermediate tensors. This enables you to quickly extract sub-components
of the model.

Example:

```python
inputs = keras.Input(shape=(None, None, 3))
processed = keras.layers.RandomCrop(width=32, height=32)(inputs)
conv = keras.layers.Conv2D(filters=2, kernel_size=3)(processed)
pooling = keras.layers.GlobalAveragePooling2D()(conv)
feature = keras.layers.Dense(10)(pooling)

full_model = keras.Model(inputs, feature)
backbone = keras.Model(processed, conv)
activations = keras.Model(conv, feature)
```

Note that the `backbone` and `activations` models are not
created with `keras.Input` objects, but with the tensors that are originated
from `keras.Input` objects. Under the hood, the layers and weights will
be shared across these models, so that user can train the `full_model`, and
use `backbone` or `activations` to do feature extraction.
The inputs and outputs of the model can be nested structures of tensors as
well, and the created models are standard Functional API models that support
all the existing APIs.

2 - By subclassing the `Model` class: in that case, you should define your
layers in `__init__()` and you should implement the model's forward pass
in `call()`.

```python
import tensorflow as tf

class MyModel(tf.keras.Model):

  def __init__(self):
    super().__init__()
    self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
    self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)

  def call(self, inputs):
    x = self.dense1(inputs)
    return self.dense2(x)

model = MyModel()
```

If you subclass `Model`, you can optionally have
a `training` argument (boolean) in `call()`, which you can use to specify
a different behavior in training and inference:

```python
import tensorflow as tf

class MyModel(tf.keras.Model):

  def __init__(self):
    super().__init__()
    self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
    self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
    self.dropout = tf.keras.layers.Dropout(0.5)

  def call(self, inputs, training=False):
    x = self.dense1(inputs)
    if training:
      x = self.dropout(x, training=training)
    return self.dense2(x)

model = MyModel()
```

Once the model is created, you can config the model with losses and metrics
with `model.compile()`, train the model with `model.fit()`, or use the model
to do prediction with `model.predict()`.
)_train_counter_test_counter_predict_counter_steps_per_execution_compiled_trainable_stateFc                    > [        X5      (       a$  U [        :X  a  SSKJn  UR                  " USS0UD6$ [
        [        U ]  " U /UQ70 UD6$ )Nr   
functional	skip_initT)is_functional_model_init_paramsr)   tf_keras.src.enginer2   
Functionalsuper__new__)clsargskwargsr2   	__class__s       V/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/engine/training.pyr8   Model.__new__   sN    *488SE\6(($I4I&II,SB4B6BB    c                   > SU l         SSKJn  [        X5      (       Ga.  [	        XR
                  5      (       Gd  / SQnU Vs0 s H  oUU;   d  M
  XRU   _M     nnU Vs0 s H  oUU;  d  M
  XRU   _M     nn[        U R                  5        UR
                  R                  " U /UQ70 UD6  / nSn	U R                  R                   H;  n
[        XR
                  5      (       a  Sn	M!  U	(       d  M*  UR                  U
5        M=     U(       a   U H  n
U
R                  " U /UQ70 UD6  M     g U(       a  [        SR                  U5      5      eg [        R                  " U1 Sk5        [         TU ]   " S0 UD6  SU l        S U l        S U l        S U l        S U l        SU l        S U l        S U l        S U l        SU l        U R7                  SS5        U R7                  S	S 5        [8        R:                  R=                  5       (       a$  [8        R:                  R?                  5       U l         OS U l         S U l!        S U l"        S U l#        U RI                  5         S U l%        S U l&        S U l'        [8        RP                  RS                  [T        RV                  " U 5      S
9U l,        S U l-        S U l.        SU l/        [`        Rb                  " 5       U l2        U Rg                  5         SU l4        S U l5        g s  snf s  snf )NTr   r1   )inputsoutputsname	trainabler3   FzGThe following keyword arguments passed to `Model` aren't supported: {}.>   rC   dtyperA   dynamicrB   autocastrD   _is_compiledr   )root )6_is_model_for_instrumentationr5   r2   r4   
isinstancer6   inject_functional_model_classr<   __init__	__bases__
issubclassappend	TypeErrorformatr   validate_kwargsr7   _is_graph_networkrA   rB   input_namesoutput_namesstop_traininghistorycompiled_losscompiled_metrics _compute_output_and_mask_jointly_maybe_create_attributetf
distributehas_strategyget_strategy_distribution_strategy_distribute_reduction_method_cluster_coordinator_run_eagerly_reset_compile_cache_training_state_saved_model_inputs_spec_saved_model_arg_spectrain
Checkpointweakrefref_checkpointr.   _steps_per_execution_tuner_autotune_steps_per_executionlayout_map_libget_current_layout_map_layout_map_init_batch_counters_base_model_initialized_jit_compile)selfr:   r;   r2   supported_kwargskmodel_kwargsother_kwargsclz_to_initfound_functional_classclzr<   s              r=   rN   Model.__init__   s    .2*
 	3*488''B
 B
  '-&,5E0E!9f   '-&,9I0I!9f   *$..9!!**4G$G,G
 K%*"~~//c#8#899-1*))&&s+ 0 &CLL=== '    %%+VL%9   	%%	
 	"6"!& " " $ 16- 	$$^U;$$[$7 ==%%''*,--*D*D*FD'*.D',0)$(! !!!#  $(,%%)"88..GKK4E.F$(!*.'-2*)@@B!!#'+$
 !]s   	K-	K-	K2,	K2c                    U R                   c0  [        R                  R                  n[        R                  " USUS9$ [
        R                  R                  U R                   R                  5       SS9n[
        R                  " USUS9$ )zHelper function for counter variable creation.

For the DTensor use case with layout map, since the variable are not
tracked by model, they can't be visited by the layout map, and need to
be properly initialized as DVariable.
int64)rE   aggregationr   )meshrank)rE   layout)
rs   r^   VariableAggregationONLY_FIRST_REPLICAVariabler   Layout
replicatedget_default_mesh	DVariable)rw   
init_valueaggr   s       r=   _create_counter_variableModel._create_counter_variableN  s     #((;;C;;zcJJ ''22%%668q 3 F (('& r?   c                     [         R                  " 5       (       dC  U R                  S5      U l        U R                  S5      U l        U R                  S5      U l        g g Nr   )r^   inside_functionr   r+   r,   r-   rw   s    r=   rt   Model._init_batch_countersb  sS     !!## #'"?"?"BD!%!>!>q!AD$($A$A!$DD! $r?   c                   > [        U SS5      (       d  [        TU ]	  X5        g [        S [        R
                  R                  U5       5       5      (       a   U R                    [        TU ]	  X5        g ! [         a    [        S5      ef = f)N_self_setattr_trackingTc              3      #    U  HL  n[        U[        R                  [        R                  45      =(       d    [
        R                  " U5      v   MN     g 7fN)rL   r   Layerr^   r   r   has_weights).0vs     r=   	<genexpr>$Model.__setattr__.<locals>.<genexpr>v  sG      
 , q:++R[[9: /++A./+s   AAzsIt looks like you are subclassing `Model` and you forgot to call `super().__init__()`. Always start with this line.)
getattrr7   __setattr__allr^   nestflattenru   AttributeErrorRuntimeError)rw   rC   valuer<   s      r=   r   Model.__setattr__q  s    t5t<<G, 
 WW__U+
 
 

,, 	D( " "4 s   A5 5Bc                    > U R                   (       a'  [        R                  [        R                  " U 5      44$ [        TU ]  5       $ r   )builtr   deserialize_model_from_bytecodeserialize_model_as_bytecoder7   
__reduce__rw   r<   s    r=   r   Model.__reduce__  s>    ::<<99$?A  7%''r?   c                 @  > U R                   (       a9  [        R                  " [        R                  " U 5      5      nX![	        U 5      '   U$ [
        TU ]  5       tp4nU" U6 nX![	        U 5      '   U(       a)  [        R                  " US   US9nUR                  U5        U$ )Nr   )memo)
r   r   r   r   idr7   r   copydeepcopy__setstate__)rw   r   newdeserializer
serializedreststater<   s          r=   __deepcopy__Model.__deepcopy__  s    ::>>88>C !DN 
 /4g.@.B+Lt
+C DNd1gD9  '
r?   c                 $    U R                  0 5      $ r   )r   r   s    r=   __copy__Model.__copy__  s      $$r?   c           	        > U R                   (       a  [        TU ]	  U5        gUc  [        S5      e[        [
        [        R                  [        4n[        X5      (       d#  [        SR                  [        U5      5      5      eU(       Ga  U R                  (       Gd  [        R                  " 5       (       a   [        R                  R                  S5      nO[         R"                  " 5       nUR%                  5          [        U[
        5      (       a"  ['        S U 5       5      (       a  [	        U5      n[        U[
        5      (       a'  U Vs/ s H  n[(        R*                  " U5      PM     nnOe[        U[        5      (       a:  UR-                  5        VVs0 s H  u  pdU[(        R*                  " U5      _M     nnnO[(        R*                  " U5      n0 nU R.                  R0                  nUR2                  n	[5        U	5      S:  aP  UR6                  (       a  U	S[5        UR6                  5      *  n	OU	SS n	U	 H  n
U
S:X  a  SUS'   M  [        S	5      e   O[5        U	5      S:  a  [        S
5      e U R8                  " U40 UD6   SSS5        [        TU ]	  U5        gs  snf s  snnf ! [        R:                  R<                  [>        4 a  n[        SU S35      eSnAff = f! , (       d  f       Nd= f)a%  Builds the model based on input shapes received.

This is to be used for subclassed models, which do not know at
instantiation time what their inputs look like.

This method only exists for users who want to call `model.build()` in a
standalone way (as a substitute for calling the model on real data to
build it). It will never be called by the framework (and thus it will
never throw unexpected errors in an unrelated workflow).

Args:
 input_shape: Single tuple, `TensorShape` instance, or list/dict of
   shapes, where shapes are tuples, integers, or `TensorShape`
   instances.

Raises:
  ValueError:
    1. In case of invalid user-provided data (not of type tuple,
       list, `TensorShape`, or dict).
    2. If the model requires call arguments that are agnostic
       to the input shapes (positional or keyword arg in call
       signature).
    3. If not all layers were properly built.
    4. If float type inputs are not supported within the layers.

  In each of these cases, the user should build their model by calling
  it on real tensor data.
NzIInput shape must be defined when calling `build()` on a `Model` subclass.zSpecified input shape is not one of the valid types. Please specify a batch input shape of type tuple or list of input shapes. User provided input type: {}.build_graphc              3   V   #    U  H  oS L =(       d    [        U[        5      v   M!     g 7fr   )rL   int)r   ds     r=   r   Model.build.<locals>.<genexpr>  s$      9=HI3As!33[   ')   trainingFaq  Currently, you cannot build your model if it has positional or keyword arguments that are not inputs to the model, but are required for its `call()` method. Instead, in order to instantiate and build your model, `call()` your model on real tensor data with all expected call arguments. The argument for `call()` can be a single list/tuple that contains multiple inputs.z[You can only call `build()` on a model if its `call()` method accepts an `inputs` argument.zYou cannot build your model by calling `build` if your layers do not support float type inputs. Instead, in order to instantiate and build your model, call your model on real tensor data (of the correct dtype).

The actual error from `call` is: .) rU   r7   build
ValueErrortuplelistr^   TensorShapedictrL   rS   typerA   executing_eagerly__internal__	FuncGraphr	   	get_graph
as_defaultr   r    generate_placeholders_from_shapeitems
_call_specfull_argspecr:   lendefaultscallerrorsInvalidArgumentErrorrR   )rw   input_shapevalid_typesgraphshapexry   r;   call_signature	call_argsarger<   s               r=   r   Model.build  s   < !!GM+&&  dBNND9+33" #)&k):";	  t{{{
 ##%%11-@))+!!#k400S 9=H9 6 6 #("4Kk400 &1%0E )II%P%0  A  T22
 )4(9(9(;	 )<HA +LL!  )<	  A )II#A !%!=!=*//	 y>A%%..$-a3~7N7N3O2O$P	$-abM	(*, 27F:. #-!<
# 
  )& ^a'$H 
IIa*6*u $H 	k"}
` 		66	B $&
 '(S+ w $#sJ   =AK& J .+K&#J%<B7K&4J+ K&+$K#KK##K&&
K4c                   > U R                   Gb  U R                  (       Gd  [        R                  " U5      n[        R                  " U5      nU R                  R	                  X45      u  nnnS n[
        R                  R                  Xe5      n[
        R                  R                  Xc5      n[
        R                  R                  Xd5      n[        R                  " U R                   5         [        TU ],  " U/UQ70 UD6  S S S 5        [        R                  " X R                   5        [        TU ],  " U0 UD6$ ! , (       d  f       N>= f)Nc                     [        U [        R                  [        R                  [
        [        45      (       a6  [        R                  " U 5      n [        R                  " U R                  5      $ g r   )rL   r^   Tensornpndarrayfloatr   convert_to_tensorinput_layer_moduleInputr   r   s    r=   _convert_to_graph_inputs0Model.__call__.<locals>._convert_to_graph_inputs8  sK    a"))RZZ!DEE,,Q/A-33AGG<< Fr?   )rs   r   r   r   split_out_first_argr^   r   map_structurerq   layout_map_scoper7   __call___map_subclass_model_variable)rw   r:   r;   copied_argscopied_kwargsrA   r   r<   s          r=   r   Model.__call__(  s
   '



 ))D/K IIf-M 33KO	= WW**+CLF''//(K GG11(M  001A1AB G+GG C 77>N>NOw000 CBs   -D::
Ec                     [        S5      e)a  Calls the model on new inputs and returns the outputs as tensors.

In this case `call()` just reapplies
all ops in the graph to the new inputs
(e.g. build a new computational graph from the provided inputs).

Note: This method should not be called directly. It is only meant to be
overridden when subclassing `tf.keras.Model`.
To call a model on an input, always use the `__call__()` method,
i.e. `model(inputs)`, which relies on the underlying `call()` method.

Args:
    inputs: Input tensor, or dict/list/tuple of input tensors.
    training: Boolean or boolean scalar tensor, indicating whether to
      run the `Network` in training mode or inference mode.
    mask: A mask or list of masks. A mask can be either a boolean tensor
      or None (no mask). For more details, check the guide
      [here](https://www.tensorflow.org/guide/tf_keras/masking_and_padding).

Returns:
    A tensor if there is a single output, or
    a list of tensors if there are more than one outputs.
zUnimplemented `tf.keras.Model.call()`: if you intend to create a `Model` with the Functional API, please provide `inputs` and `outputs` arguments. Otherwise, subclass `Model` with an overridden `call()` method.)NotImplementedError)rw   rA   r   masks       r=   r   
Model.callN  s    2 "*
 	
r?   c
                    U(       a  [         R                  " SS9(       d  Sn[        R                  " UUUUUUUUS9U l        U R
                  R                  5          SU
;   a.  [        R                  " S5        U(       d  U
R                  S5      nU
R                  SS5      nU R                  " X40 U
D6  X`l        U R                  U5      U l        SnU R                  b  U R                  R                  5       n[!        U["        R$                  5      (       a  X l        O&["        R$                  " UUU R(                  US	9U l        ["        R*                  " UUU R(                  UUS
9U l        US:X  aV  U R.                  c  U R1                  S5        [2        R4                  " U R                  U R.                  5      U l        SU l        OU R1                  U=(       d    S5        U R;                  U	5      U l        U R?                  5         SU l         U=(       d    0 U l!        U R                  (       d  U RD                  (       a  U(       a  [G        S5      eXl$         SSS5        g! , (       d  f       g= f)a^  Configures the model for training.

Example:

```python
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
              loss=tf.keras.losses.BinaryCrossentropy(),
              metrics=[tf.keras.metrics.BinaryAccuracy(),
                       tf.keras.metrics.FalseNegatives()])
```

Args:
    optimizer: String (name of optimizer) or optimizer instance. See
      `tf.keras.optimizers`.
    loss: Loss function. May be a string (name of loss function), or
      a `tf.keras.losses.Loss` instance. See `tf.keras.losses`. A loss
      function is any callable with the signature `loss = fn(y_true,
      y_pred)`, where `y_true` are the ground truth values, and
      `y_pred` are the model's predictions.
      `y_true` should have shape
      `(batch_size, d0, .. dN)` (except in the case of
      sparse loss functions such as
      sparse categorical crossentropy which expects integer arrays of
      shape `(batch_size, d0, .. dN-1)`).
      `y_pred` should have shape `(batch_size, d0, .. dN)`.
      The loss function should return a float tensor.
      If a custom `Loss` instance is
      used and reduction is set to `None`, return value has shape
      `(batch_size, d0, .. dN-1)` i.e. per-sample or per-timestep loss
      values; otherwise, it is a scalar. If the model has multiple
      outputs, you can use a different loss on each output by passing a
      dictionary or a list of losses. The loss value that will be
      minimized by the model will then be the sum of all individual
      losses, unless `loss_weights` is specified.
    metrics: List of metrics to be evaluated by the model during
      training and testing. Each of this can be a string (name of a
      built-in function), function or a `tf.keras.metrics.Metric`
      instance. See `tf.keras.metrics`. Typically you will use
      `metrics=['accuracy']`.
      A function is any callable with the signature `result = fn(y_true,
      y_pred)`. To specify different metrics for different outputs of a
      multi-output model, you could also pass a dictionary, such as
      `metrics={'output_a':'accuracy', 'output_b':['accuracy', 'mse']}`.
      You can also pass a list to specify a metric or a list of metrics
      for each output, such as
      `metrics=[['accuracy'], ['accuracy', 'mse']]`
      or `metrics=['accuracy', ['accuracy', 'mse']]`. When you pass the
      strings 'accuracy' or 'acc', we convert this to one of
      `tf.keras.metrics.BinaryAccuracy`,
      `tf.keras.metrics.CategoricalAccuracy`,
      `tf.keras.metrics.SparseCategoricalAccuracy` based on the shapes
      of the targets and of the model output. We do a similar
      conversion for the strings 'crossentropy' and 'ce' as well.
      The metrics passed here are evaluated without sample weighting; if
      you would like sample weighting to apply, you can specify your
      metrics via the `weighted_metrics` argument instead.
    loss_weights: Optional list or dictionary specifying scalar
      coefficients (Python floats) to weight the loss contributions of
      different model outputs. The loss value that will be minimized by
      the model will then be the *weighted sum* of all individual
      losses, weighted by the `loss_weights` coefficients.  If a list,
      it is expected to have a 1:1 mapping to the model's outputs. If a
      dict, it is expected to map output names (strings) to scalar
      coefficients.
    weighted_metrics: List of metrics to be evaluated and weighted by
      `sample_weight` or `class_weight` during training and testing.
    run_eagerly: Bool. If `True`, this `Model`'s logic will not be
      wrapped in a `tf.function`. Recommended to leave this as `None`
      unless your `Model` cannot be run inside a `tf.function`.
      `run_eagerly=True` is not supported when using
      `tf.distribute.experimental.ParameterServerStrategy`. Defaults to
       `False`.
    steps_per_execution: Int or `'auto'`. The number of batches to
      run during each `tf.function` call. If set to "auto", keras will
      automatically tune `steps_per_execution` during runtime. Running
      multiple batches inside a single `tf.function` call can greatly
      improve performance on TPUs, when used with distributed strategies
      such as `ParameterServerStrategy`, or with small models with a
      large Python overhead. At most, one full epoch will be run each
      execution. If a number larger than the size of the epoch is
      passed, the execution will be truncated to the size of the epoch.
      Note that if `steps_per_execution` is set to `N`,
      `Callback.on_batch_begin` and `Callback.on_batch_end` methods will
      only be called every `N` batches (i.e. before/after each
      `tf.function` execution). Defaults to `1`.
    jit_compile: If `True`, compile the model training step with XLA.
      [XLA](https://www.tensorflow.org/xla) is an optimizing compiler
      for machine learning.
      `jit_compile` is not enabled for by default.
      Note that `jit_compile=True`
      may not necessarily work for all models.
      For more information on supported operations please refer to the
      [XLA documentation](https://www.tensorflow.org/xla).
      Also refer to
      [known XLA issues](https://www.tensorflow.org/xla/known_issues)
      for more details.
    pss_evaluation_shards: Integer or 'auto'. Used for
      `tf.distribute.ParameterServerStrategy` training only. This arg
      sets the number of shards to split the dataset into, to enable an
      exact visitation guarantee for evaluation, meaning the model will
      be applied to each dataset element exactly once, even if workers
      fail. The dataset must be sharded to ensure separate workers do
      not process the same data. The number of shards should be at least
      the number of workers for good performance. A value of 'auto'
      turns on exact evaluation and uses a heuristic for the number of
      shards based on the number of workers. 0, meaning no
      visitation guarantee is provided. NOTE: Custom implementations of
      `Model.test_step` will be ignored when doing exact evaluation.
      Defaults to `0`.
    **kwargs: Arguments supported for backwards compatibility only.
TwarnF)r   lossmetricsloss_weightsweighted_metricsrun_eagerlysteps_per_executionjit_compile experimental_steps_per_executionzThe argument `steps_per_execution` is no longer experimental. Pass `steps_per_execution` instead of `experimental_steps_per_execution`.from_serializedN)rW   r   )rW   r  r   auto   zCYou cannot enable `run_eagerly` and `jit_compile` at the same time.)%r$   can_jit_compiler   Config_compile_configdistribute_strategyscopeloggingwarningpop_validate_compilere   _get_optimizerr   rs   r   rL   r   LossesContainerrZ   rW   MetricsContainerr[   r.   _configure_steps_per_executionr"   StepsPerExecutionTunerro   rp   _infer_exact_eval_shards_pss_evaluation_shardsrf   rH   r  rF   r   rv   )rw   r   r  r  r  r  r  r	  r
  pss_evaluation_shardsr;   r  r   s                r=   compileModel.compileo  s*   z x77TBK077%-# 3#	 
 %%++-1V;:
 +*0**:+' %jj):EBO""9@@ +!00;DND+''88:$ = =>>%)"%2%B%B !%!2!2	&" %2$B$B !.. /%D! #f,,,477:.EE(A(A /
 6:2334G4L1M*.*G*G%+D'
 %%' $D
DI!!T\\{ ( 
 %0!M .--s   G?I&&
I4c                 N   ^  U 4S jn[         R                  R                  X!5      $ )z7Wraps `optimizer` in `LossScaleOptimizer` if necessary.c                    > [         R                  " U 5      n TR                  R                  S:X  a5  [	        U [
        R                  5      (       d  [
        R                  " U 5      n U $ )Nmixed_float16)r   getdtype_policyrC   rL   lsoBaseLossScaleOptimizer)optrw   s    r=   _get_single_optimizer3Model._get_optimizer.<locals>._get_single_optimizerC  sU    ..%C  %%8S//B B
 005Jr?   )r^   r   r   )rw   r   r*  s   `  r=   r  Model._get_optimizer@  s     	 ww$$%:FFr?   c                 f    S U l         S U l        S U l        S U l        U R	                  5       U l        g r   )train_functiontest_functionpredict_functiontrain_tf_function_get_trainable_stater/   r   s    r=   rf   Model._reset_compile_cacheO  s7    "! $ "& *.)B)B)D&r?   c                 0    U R                  U5      U l        g r   )r   r.   )rw   r	  s     r=   r  $Model._configure_steps_per_execution^  s    $($A$A%
!r?   c                     g)NFrJ   r   s    r=   _should_compute_maskModel._should_compute_maskd  s    r?   c                 $   / nU R                   (       aJ  U R                  b  XR                  R                  -  nU R                  b  XR                  R                  -  nU R	                  5        H  nUR                  UR                  5        M      U$ )aw  Return metrics added using `compile()` or `add_metric()`.

Note: Metrics passed to `compile()` are available only after a
`keras.Model` has been trained/evaluated on actual data.

Examples:

>>> inputs = tf.keras.layers.Input(shape=(3,))
>>> outputs = tf.keras.layers.Dense(2)(inputs)
>>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
>>> model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
>>> [m.name for m in model.metrics]
[]

>>> x = np.random.random((2, 3))
>>> y = np.random.randint(0, 2, (2, 2))
>>> model.fit(x, y)
>>> [m.name for m in model.metrics]
['loss', 'mae']

>>> inputs = tf.keras.layers.Input(shape=(3,))
>>> d = tf.keras.layers.Dense(2, name='out')
>>> output_1 = d(inputs)
>>> output_2 = d(inputs)
>>> model = tf.keras.models.Model(
...    inputs=inputs, outputs=[output_1, output_2])
>>> model.add_metric(
...    tf.reduce_sum(output_2), name='mean', aggregation='mean')
>>> model.compile(optimizer="Adam", loss="mse", metrics=["mae", "acc"])
>>> model.fit(x, (y, y))
>>> [m.name for m in model.metrics]
['loss', 'out_loss', 'out_1_loss', 'out_mae', 'out_acc', 'out_1_mae',
'out_1_acc', 'mean']

)rH   rZ   r  r[   _flatten_layersextend_metrics)rw   r  ls      r=   r  Model.metricsh  s{    J !!---555$$000888%%'ANN1::& (r?   c                 X    U R                    Vs/ s H  oR                  PM     sn$ s  snf )a  Returns the model's display labels for all outputs.

Note: `metrics_names` are available only after a `keras.Model` has been
trained/evaluated on actual data.

Examples:

>>> inputs = tf.keras.layers.Input(shape=(3,))
>>> outputs = tf.keras.layers.Dense(2)(inputs)
>>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
>>> model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
>>> model.metrics_names
[]

>>> x = np.random.random((2, 3))
>>> y = np.random.randint(0, 2, (2, 2))
>>> model.fit(x, y)
>>> model.metrics_names
['loss', 'mae']

>>> inputs = tf.keras.layers.Input(shape=(3,))
>>> d = tf.keras.layers.Dense(2, name='out')
>>> output_1 = d(inputs)
>>> output_2 = d(inputs)
>>> model = tf.keras.models.Model(
...    inputs=inputs, outputs=[output_1, output_2])
>>> model.compile(optimizer="Adam", loss="mse", metrics=["mae", "acc"])
>>> model.fit(x, (y, y))
>>> model.metrics_names
['loss', 'out_loss', 'out_1_loss', 'out_mae', 'out_acc', 'out_1_mae',
'out_1_acc']

)r  rC   rw   ms     r=   metrics_namesModel.metrics_names  s#    L !%-1---s   'c                 d    U R                   =(       d    [        R                  R                  5       $ )z:The `tf.distribute.Strategy` this model was created under.)rb   r^   r_   ra   r   s    r=   r  Model.distribute_strategy  s"     **Jbmm.H.H.JJr?   c                 f   U R                   (       a  U R                  S:X  a  [        S5      eU R                  (       a  U R                  (       a  [        S5      eU R                   =(       dF    U R                  =(       d3    [        R
                  R                  5       =(       a    U R                  SL $ )a  Settable attribute indicating whether the model should run eagerly.

Running eagerly means that your model will be run step by step,
like Python code. Your model might run slower, but it should become
easier for you to debug it by stepping into individual layer calls.

By default, we will attempt to compile your model to a static graph to
deliver the best execution performance.

Returns:
  Boolean, whether the model should run eagerly.
FzYour model contains layers that can only be successfully run in eager execution (layers constructed with `dynamic=True`). You cannot set `run_eagerly=False`.zRWhen using `Model` with `ParameterServerStrategy`, `run_eagerly` is not supported.N)rF   re   r   rd   r^   configfunctions_run_eagerlyr   s    r=   r  Model.run_eagerly  s     <<D--66  $$):):2  LL Q  Q		//1Od6G6G46O	
r?   c                     Xl         g r   )re   rw   r   s     r=   r  rI    s    !r?   c                     U R                   $ )z:Settable property to enable tuning for steps_per_execution)rp   r   s    r=   autotune_steps_per_execution"Model.autotune_steps_per_execution  s     111r?   c                     Xl         U(       a]  U R                  cO  U R                  c  U R                  S5        [        R
                  " U R                  U R                  5      U l        g g g Nr  )rp   ro   r.   r  r"   r  r   rK  s     r=   rM  rN    s\    -2*T44<((033A6*AANND$=$= + =5r?   c                     U R                   $ )zBSettable `steps_per_execution variable. Requires a compiled model.)r.   r   s    r=   r	  Model.steps_per_execution  s     (((r?   c                 x    U R                   c  U R                  U5        g U R                   R                  U5        g r   )r.   r  assignrK  s     r=   r	  rR    s0    $$,//6%%,,U3r?   c                     U R                   $ )a  Specify whether to compile the model with XLA.

[XLA](https://www.tensorflow.org/xla) is an optimizing compiler
for machine learning. `jit_compile` is not enabled by default.
Note that `jit_compile=True` may not necessarily work for all models.

For more information on supported operations please refer to the
[XLA documentation](https://www.tensorflow.org/xla). Also refer to
[known XLA issues](https://www.tensorflow.org/xla/known_issues)
for more details.
)rv   r   s    r=   r
  Model.jit_compile  s        r?   c                     U R                   U:X  a  g U(       a!  [        R                  " SS9(       d  SU l         g Xl         U R                  5         g )NTr  F)rv   r$   r  rf   rK  s     r=   r
  rV    sD     % 11t< %D!!!#r?   c                 ,    U R                   =(       d    S$ )a  The method employed to reduce per-replica values during training.

Unless specified, the value "auto" will be assumed, indicating that
the reduction strategy should be chosen based on the current
running environment.
See `reduce_per_replica` function for more details.

r  rc   r   s    r=   distribute_reduction_method!Model.distribute_reduction_method-  s     00:F:r?   c                     Xl         g r   rY  rK  s     r=   rZ  r[  9  s    ,1)r?   c                 z    U R                   (       a  Uc  [        SU R                    S35      eUc  [        S5      eg)a  Raises error if target or loss is not found.

This method verifies that the target and loss are properly populated
when applicable, or raises errors.

Args:
  y: the target for training.
  loss: the total loss tensor including loss added via `compile` and
    `add_loss`.
Nz:Target data is missing. Your model was compiled with loss=z>, and therefore expects target data to be provided in `fit()`.z]No loss found. You may have forgotten to provide a `loss` argument in the `compile()` method.)r  r   )rw   yr  s      r=   _validate_target_and_lossModel._validate_target_and_loss=  sT    " 99		{ #OO  \6  r?   c                 V   [         R                  " U5      u  p#n[        R                  " 5        nU " USS9nU R	                  X#Xd5      nSSS5        U R                  UW5        U R                  R                  XpR                  WS9  U R                  X#WU5      $ ! , (       d  f       NW= f)a  The logic for one training step.

This method can be overridden to support custom training logic.
For concrete examples of how to override this method see
[Customizing what happens in fit](
https://www.tensorflow.org/guide/tf_keras/customizing_what_happens_in_fit).
This method is called by `Model.make_train_function`.

This method should contain the mathematical logic for one step of
training.  This typically includes the forward pass, loss calculation,
backpropagation, and metric updates.

Configuration details for *how* this logic is run (e.g. `tf.function`
and `tf.distribute.Strategy` settings), should be left to
`Model.make_train_function`, which can also be overridden.

Args:
  data: A nested structure of `Tensor`s.

Returns:
  A `dict` containing values that will be passed to
  `tf.keras.callbacks.CallbackList.on_train_batch_end`. Typically, the
  values of the `Model`'s metrics are returned. Example:
  `{'loss': 0.2, 'accuracy': 0.7}`.
Tr   N)tape)
r   unpack_x_y_sample_weightr^   GradientTapecompute_lossr_  r   minimizetrainable_variablescompute_metrics)rw   datar   r^  sample_weightrc  y_predr  s           r=   
train_stepModel.train_step^  s    4 +CCDIm__$!d+F$$Q6AD  	&&q$/&>&>TJ##A&-@@ s   B
B(c                 8    AU R                  X#X@R                  S9$ )a  Compute the total loss, validate it, and return it.

Subclasses can optionally override this method to provide custom loss
computation logic.

Example:
```python
class MyModel(tf.keras.Model):

  def __init__(self, *args, **kwargs):
    super(MyModel, self).__init__(*args, **kwargs)
    self.loss_tracker = tf.keras.metrics.Mean(name='loss')

  def compute_loss(self, x, y, y_pred, sample_weight):
    loss = tf.reduce_mean(tf.math.squared_difference(y_pred, y))
    loss += tf.add_n(self.losses)
    self.loss_tracker.update_state(loss)
    return loss

  def reset_metrics(self):
    self.loss_tracker.reset_states()

  @property
  def metrics(self):
    return [self.loss_tracker]

tensors = tf.random.uniform((10, 10)), tf.random.uniform((10,))
dataset = tf.data.Dataset.from_tensor_slices(tensors).repeat().batch(1)

inputs = tf.keras.layers.Input(shape=(10,), name='my_input')
outputs = tf.keras.layers.Dense(10)(inputs)
model = MyModel(inputs, outputs)
model.add_loss(tf.reduce_sum(outputs))

optimizer = tf.keras.optimizers.SGD()
model.compile(optimizer, loss='mse', steps_per_execution=10)
model.fit(dataset, epochs=2, steps_per_epoch=10)
print('My custom loss: ', model.loss_tracker.result().numpy())
```

Args:
  x: Input data.
  y: Target data.
  y_pred: Predictions returned by the model (output of `model(x)`)
  sample_weight: Sample weights for weighting the loss function.

Returns:
  The total loss as a `tf.Tensor`, or `None` if no loss results (which
  is the case when called by `Model.test_step`).
)regularization_losses)rZ   lossesrw   r   r^  rl  rk  s        r=   rf  Model.compute_loss  s)    f !!}KK " 
 	
r?   c                 \    AU R                   R                  X#U5        U R                  5       $ )a  Update metric states and collect all metrics to be returned.

Subclasses can optionally override this method to provide custom metric
updating and collection logic.

Example:
```python
class MyModel(tf.keras.Sequential):

  def compute_metrics(self, x, y, y_pred, sample_weight):

    # This super call updates `self.compiled_metrics` and returns
    # results for all metrics listed in `self.metrics`.
    metric_results = super(MyModel, self).compute_metrics(
        x, y, y_pred, sample_weight)

    # Note that `self.custom_metric` is not listed in `self.metrics`.
    self.custom_metric.update_state(x, y, y_pred, sample_weight)
    metric_results['custom_metric_name'] = self.custom_metric.result()
    return metric_results
```

Args:
  x: Input data.
  y: Target data.
  y_pred: Predictions returned by the model (output of `model.call(x)`)
  sample_weight: Sample weights for weighting the loss function.

Returns:
  A `dict` containing values that will be passed to
  `tf.keras.callbacks.CallbackList.on_train_batch_end()`. Typically, the
  values of the metrics listed in `self.metrics` are returned. Example:
  `{'loss': 0.2, 'accuracy': 0.7}`.
)r[   update_stateget_metrics_resultrr  s        r=   ri  Model.compute_metrics  s-    F **1mD&&((r?   c                     0 nU R                    HI  nUR                  5       n[        U[        5      (       a  UR	                  U5        M;  X1UR
                  '   MK     U$ )a:  Returns the model's metrics values as a dict.

If any of the metric result is a dict (containing multiple metrics),
each of them gets added to the top level returned dict of this method.

Returns:
  A `dict` containing values of the metrics listed in `self.metrics`.
  Example:
  `{'loss': 0.2, 'accuracy': 0.7}`.
)r  resultrL   r   updaterC   )rw   return_metricsmetricry  s       r=   rv  Model.get_metrics_result  sQ     llF]]_F&$''%%f-.4v{{+ # r?   c                    Sn U R                  5       n[        U[        5      (       aL  [        UR	                  5       5      [        UR	                  5       5      :X  a  [
        R                  " U5      nU$ U R                  (       a  [        R                  " U5        U$ ! [         a+    U R                  (       a  [        R                  " U5         U$ f = f)a  Returns model metrics as a dict if the keys match with input logs.

When the training / evalution is performed with asynchronous steps, such
as the case with `tf.distribute.ParameterServerStrategy`, the last
scheduled `train / test_step` may not give the latest metrics because it
is not guaranteed to be executed the last. This method gets metrics from
the model directly instead of relying on the return from last step
function.

It logs a warning if the metric results could not be overridden when
used with `tf.distribute.ParameterServerStrategy`.

When the user has custom train / test step functions, the metrics
returned may be different from `Model.metrics`. In those instances,
this function will be no-op and return the logs.

Args:
  logs: A `dict` of metrics returned by train / test step function.

Returns:
  A `dict` containing values of the metrics listed in `self.metrics`
  when logs and model metrics keys match. Otherwise it returns input
  `logs`.
zCould not get Model metric results.         Using the results of last step function could lead to incorrect         results when used with ParameterServerStrategy)rv  rL   r   setkeysr$   sync_to_numpy_or_python_typerd   r  r  rR   )rw   logsPSS_WARN_MSGmetric_logss       r=    _validate_and_get_metrics_result&Model._validate_and_get_metrics_result  s    28	.113K $%%#diik*:c  "? +  <<[I  **-  	.((- 	.s   B 1CCc                 <   U GH  nU R                    H  nUR                  UR                  5       ;  a6  [        R                  " [        R
                  SUR                   S3S5        MW  X#R                     n[        U5      [        UR                  5      :w  a;  [        S[        UR                  5       SUR                   S[        U5       S35      e[        UR                  U5       H  u  pVUR                  U5        M     M     GM	     U R                  5       $ )Nz$No matching result found for metric z1. This metric's computed result may be incorrect.   z	Expected z  variables in result for metric z, but found r   )r  rC   r  r  log_first_nWARNr   weightsr   zip
assign_addrv  )rw   r  shard_resultr|  metric_resultweightvals          r=   _aggregate_exact_metricsModel._aggregate_exact_metrics"  s    !L,,;;l&7&7&99''>v{{m LJ J	  ,[[ 9}%V^^)<<$#C$7#8 9&&,kk],}-.a1 
 $'v~~}#EKF%%c* $F! ' !& &&((r?   c                   ^ ^^ T R                   b  U(       d  T R                   $ U 4S jmT R                  b=  T R                  R                  5       R                  5       S:X  a  T R                  (       dq  U U4S jmT R
                  (       d  [        R                  " TSS9mTT l        T R                  (       a  U U4S jT l         T R                   $ TT l          T R                   $ T R                  (       aL  U U4S jmT R
                  (       d  [        R                  " TSS9mTT l        U U4S jT l         T R                   $ U U4S	 jmT R
                  (       d  [        R                  " TSS9mTT l        TT l         T R                   $ )
a  Creates a function that executes one step of training.

This method can be overridden to support custom training logic.
This method is called by `Model.fit` and `Model.train_on_batch`.

Typically, this method directly controls `tf.function` and
`tf.distribute.Strategy` settings, and delegates the actual training
logic to `Model.train_step`.

This function is cached the first time `Model.fit` or
`Model.train_on_batch` is called. The cache is cleared whenever
`Model.compile` is called. You can skip the cache and generate again the
function with `force=True`.

Args:
  force: Whether to regenerate the train function and skip the cached
    function if available.

Returns:
  Function. The function created by this method should accept a
  `tf.data.Iterator`, and return a `dict` containing values that will
  be passed to `tf.keras.Callbacks.on_train_batch_end`, such as
  `{'loss': 0.2, 'accuracy': 0.7}`.
c                    >^  U 4S jnTR                   (       a  [        R                  " USSS9n[        U5      nT R                  R                  X#4S9n[        UTR                  TR                  S9nU$ )zRuns a single training step.c                    > TR                  U 5      n[        R                  " [        U5      5         TR                  R                  S5        S S S 5        U$ ! , (       d  f       U$ = frP  )rm  r^   control_dependencies_minimum_control_depsr+   r  rj  rB   models     r=   run_stepBModel.make_train_function.<locals>.step_function.<locals>.run_stepY  sU    **40,,-B7-KL((33A6 M ML   A
A'Tr
  reduce_retracingr:   	reductionr
  r^   functionnextr  runreduce_per_replicarZ  r  iteratorr  rj  rB   rw   s   `    r=   step_function0Model.make_train_function.<locals>.step_functionV  st     ;;$ >D//33H73KG(((::G
 Nr?   r  c                    > T" TU 5      $ )z-Runs a training execution with a single step.rJ   r  rw   r  s    r=   r.  1Model.make_train_function.<locals>.train_functiont      $T844r?   Tr  c                 :   > TR                   R                  TU 4S9$ Nr  rd   scheduleitrw   r.  s    r=   <lambda>+Model.make_train_function.<locals>.<lambda>  s#    t88AA&bU  B  r?   c                 T   > [         R                  " U5       H  nT" TU 5      nM     W$ z.Runs a training execution with multiple steps.r^   ranger  r	  _rB   rw   r  s       r=   r.  r    (    "56A+D(;G 7r?   c                 l   > TR                   R                  TU TR                  R                  5       4S9$ r  rd   r  r.   r   r  s    r=   r  r    s5    T-F-F-O-Ob$*C*C*I*I*K%L .P .r?   c                 h   > [         R                  " TR                  5       H  nT" TU 5      nM     W$ r  r^   r  r.   r  r  rB   rw   r  s      r=   r.  r    -    $";";<A+D(;G =r?   )
r.  r.   numpyitemrM  r  r^   r  r1  rd   )rw   forcer  r.  s   ` @@r=   make_train_functionModel.make_train_function:  sY   2 *5&&&	2 %%-((..05571<555 ##!#"T" *8&(( #V """K '5#J """A && ##!#"T" *8&#D$ """ ##!#"T" *8&"0D"""r?   c                    [         R                  " SS5        U R                  5         U R                  S5        [	        S5        [        XPR                  5      nU(       a!  Uc  [        R                  " XU4US9u  u  nnnnU(       a  [        R                  " U5      u  nnnU R                  R                  (       aB  [        R                  R                  R                  R                  U R                  5      U l        U R                  R#                  5          [$        R&                  " U 5         [        R(                  " UUUUUUUU	U
UUUU U R*                  S9n[-        U[.        R0                  5      (       d'  [.        R0                  " USUS:g  U UUUR2                  S9nS	U l        U R7                  5       U l        U R:                  R=                  S5        UR?                  5         SnU R@                  (       a  U RB                  RE                  5         U=(       d    UR2                  nU RG                  UU5      u  Ul$        Ul%        SnURM                  5        GHq  u  nnU RO                  5         URQ                  U5        URS                  5          URU                  5        H  n[        RV                  R                  RY                  S
UUUSS9   UR[                  U5        U R9                  U5      nUR\                  (       a  [^        R`                  " 5         UnUURb                  -   nURe                  UU5        U R4                  (       a   SSS5          O SSS5        M     SSS5        [f        Rh                  " U5      nUc  [k        S5      eU Rm                  U5      n[n        Rn                  " U5      n U(       a  U Rq                  UU5      (       a  U Rr                  (       a  U Ru                  5         [w        U SS5      cB  [        R(                  " WWWU=(       d    UUSSUUUU U R*                  U Rr                  S9U l<        U R{                  WWWU=(       d    UUUUUUSSS9n!U!R}                  5        V"V#s0 s H  u  n"n#SU"-   U#_M     n!n"n#U R                  U!5        UR                  UU 5        U nU R4                  (       d  GMr    O   [-        U R                  [        R                  5      (       a+  US:  a%  U R                  R                  U R                  5        [w        U SS5      b  U ?<U R@                  (       a  U RB                  R                  5         UR                  US9  U R                  sSSS5        sSSS5        $ ! , (       d  f       GM  = f! , (       d  f       GN@= fs  sn#n"f ! , (       d  f       O= fSSS5        g! , (       d  f       g= f)a(2  Trains the model for a fixed number of epochs (dataset iterations).

Args:
    x: Input data. It could be:
      - A Numpy array (or array-like), or a list of arrays
        (in case the model has multiple inputs).
      - A TensorFlow tensor, or a list of tensors
        (in case the model has multiple inputs).
      - A dict mapping input names to the corresponding array/tensors,
        if the model has named inputs.
      - A `tf.data` dataset. Should return a tuple
        of either `(inputs, targets)` or
        `(inputs, targets, sample_weights)`.
      - A generator or `keras.utils.Sequence` returning `(inputs,
        targets)` or `(inputs, targets, sample_weights)`.
      - A `tf.keras.utils.experimental.DatasetCreator`, which wraps a
        callable that takes a single argument of type
        `tf.distribute.InputContext`, and returns a `tf.data.Dataset`.
        `DatasetCreator` should be used when users prefer to specify the
        per-replica batching and sharding logic for the `Dataset`.
        See `tf.keras.utils.experimental.DatasetCreator` doc for more
        information.
      A more detailed description of unpacking behavior for iterator
      types (Dataset, generator, Sequence) is given below. If these
      include `sample_weights` as a third component, note that sample
      weighting applies to the `weighted_metrics` argument but not the
      `metrics` argument in `compile()`. If using
      `tf.distribute.experimental.ParameterServerStrategy`, only
      `DatasetCreator` type is supported for `x`.
    y: Target data. Like the input data `x`,
      it could be either Numpy array(s) or TensorFlow tensor(s).
      It should be consistent with `x` (you cannot have Numpy inputs and
      tensor targets, or inversely). If `x` is a dataset, generator,
      or `keras.utils.Sequence` instance, `y` should
      not be specified (since targets will be obtained from `x`).
    batch_size: Integer or `None`.
        Number of samples per gradient update.
        If unspecified, `batch_size` will default to 32.
        Do not specify the `batch_size` if your data is in the
        form of datasets, generators, or `keras.utils.Sequence`
        instances (since they generate batches).
    epochs: Integer. Number of epochs to train the model.
        An epoch is an iteration over the entire `x` and `y`
        data provided
        (unless the `steps_per_epoch` flag is set to
        something other than None).
        Note that in conjunction with `initial_epoch`,
        `epochs` is to be understood as "final epoch".
        The model is not trained for a number of iterations
        given by `epochs`, but merely until the epoch
        of index `epochs` is reached.
    verbose: 'auto', 0, 1, or 2. Verbosity mode.
        0 = silent, 1 = progress bar, 2 = one line per epoch.
        'auto' becomes 1 for most cases, but 2 when used with
        `ParameterServerStrategy`. Note that the progress bar is not
        particularly useful when logged to a file, so verbose=2 is
        recommended when not running interactively (eg, in a production
        environment). Defaults to 'auto'.
    callbacks: List of `keras.callbacks.Callback` instances.
        List of callbacks to apply during training.
        See `tf.keras.callbacks`. Note
        `tf.keras.callbacks.ProgbarLogger` and
        `tf.keras.callbacks.History` callbacks are created automatically
        and need not be passed into `model.fit`.
        `tf.keras.callbacks.ProgbarLogger` is created or not based on
        `verbose` argument to `model.fit`.
        Callbacks with batch-level calls are currently unsupported with
        `tf.distribute.experimental.ParameterServerStrategy`, and users
        are advised to implement epoch-level calls instead with an
        appropriate `steps_per_epoch` value.
    validation_split: Float between 0 and 1.
        Fraction of the training data to be used as validation data.
        The model will set apart this fraction of the training data,
        will not train on it, and will evaluate
        the loss and any model metrics
        on this data at the end of each epoch.
        The validation data is selected from the last samples
        in the `x` and `y` data provided, before shuffling. This
        argument is not supported when `x` is a dataset, generator or
        `keras.utils.Sequence` instance.
        If both `validation_data` and `validation_split` are provided,
        `validation_data` will override `validation_split`.
        `validation_split` is not yet supported with
        `tf.distribute.experimental.ParameterServerStrategy`.
    validation_data: Data on which to evaluate
        the loss and any model metrics at the end of each epoch.
        The model will not be trained on this data. Thus, note the fact
        that the validation loss of data provided using
        `validation_split` or `validation_data` is not affected by
        regularization layers like noise and dropout.
        `validation_data` will override `validation_split`.
        `validation_data` could be:
          - A tuple `(x_val, y_val)` of Numpy arrays or tensors.
          - A tuple `(x_val, y_val, val_sample_weights)` of NumPy
            arrays.
          - A `tf.data.Dataset`.
          - A Python generator or `keras.utils.Sequence` returning
          `(inputs, targets)` or `(inputs, targets, sample_weights)`.
        `validation_data` is not yet supported with
        `tf.distribute.experimental.ParameterServerStrategy`.
    shuffle: Boolean (whether to shuffle the training data
        before each epoch) or str (for 'batch'). This argument is
        ignored when `x` is a generator or an object of tf.data.Dataset.
        'batch' is a special option for dealing
        with the limitations of HDF5 data; it shuffles in batch-sized
        chunks. Has no effect when `steps_per_epoch` is not `None`.
    class_weight: Optional dictionary mapping class indices (integers)
        to a weight (float) value, used for weighting the loss function
        (during training only).
        This can be useful to tell the model to
        "pay more attention" to samples from
        an under-represented class. When `class_weight` is specified
        and targets have a rank of 2 or greater, either `y` must be
        one-hot encoded, or an explicit final dimension of `1` must
        be included for sparse class labels.
    sample_weight: Optional Numpy array of weights for
        the training samples, used for weighting the loss function
        (during training only). You can either pass a flat (1D)
        Numpy array with the same length as the input samples
        (1:1 mapping between weights and samples),
        or in the case of temporal data,
        you can pass a 2D array with shape
        `(samples, sequence_length)`,
        to apply a different weight to every timestep of every sample.
        This argument is not supported when `x` is a dataset, generator,
        or `keras.utils.Sequence` instance, instead provide the
        sample_weights as the third element of `x`.
        Note that sample weighting does not apply to metrics specified
        via the `metrics` argument in `compile()`. To apply sample
        weighting to your metrics, you can specify them via the
        `weighted_metrics` in `compile()` instead.
    initial_epoch: Integer.
        Epoch at which to start training
        (useful for resuming a previous training run).
    steps_per_epoch: Integer or `None`.
        Total number of steps (batches of samples)
        before declaring one epoch finished and starting the
        next epoch. When training with input tensors such as
        TensorFlow data tensors, the default `None` is equal to
        the number of samples in your dataset divided by
        the batch size, or 1 if that cannot be determined. If x is a
        `tf.data` dataset, and 'steps_per_epoch'
        is None, the epoch will run until the input dataset is
        exhausted.  When passing an infinitely repeating dataset, you
        must specify the `steps_per_epoch` argument. If
        `steps_per_epoch=-1` the training will run indefinitely with an
        infinitely repeating dataset.  This argument is not supported
        with array inputs.
        When using `tf.distribute.experimental.ParameterServerStrategy`:
          * `steps_per_epoch=None` is not supported.
    validation_steps: Only relevant if `validation_data` is provided and
        is a `tf.data` dataset. Total number of steps (batches of
        samples) to draw before stopping when performing validation
        at the end of every epoch. If 'validation_steps' is None,
        validation will run until the `validation_data` dataset is
        exhausted. In the case of an infinitely repeated dataset, it
        will run into an infinite loop. If 'validation_steps' is
        specified and only part of the dataset will be consumed, the
        evaluation will start from the beginning of the dataset at each
        epoch. This ensures that the same validation samples are used
        every time.
    validation_batch_size: Integer or `None`.
        Number of samples per validation batch.
        If unspecified, will default to `batch_size`.
        Do not specify the `validation_batch_size` if your data is in
        the form of datasets, generators, or `keras.utils.Sequence`
        instances (since they generate batches).
    validation_freq: Only relevant if validation data is provided.
      Integer or `collections.abc.Container` instance (e.g. list, tuple,
      etc.).  If an integer, specifies how many training epochs to run
      before a new validation run is performed, e.g. `validation_freq=2`
      runs validation every 2 epochs. If a Container, specifies the
      epochs on which to run validation, e.g.
      `validation_freq=[1, 2, 10]` runs validation at the end of the
      1st, 2nd, and 10th epochs.
    max_queue_size: Integer. Used for generator or
      `keras.utils.Sequence` input only. Maximum size for the generator
      queue.  If unspecified, `max_queue_size` will default to 10.
    workers: Integer. Used for generator or `keras.utils.Sequence` input
        only. Maximum number of processes to spin up
        when using process-based threading. If unspecified, `workers`
        will default to 1.
    use_multiprocessing: Boolean. Used for generator or
        `keras.utils.Sequence` input only. If `True`, use process-based
        threading. If unspecified, `use_multiprocessing` will default to
        `False`. Note that because this implementation relies on
        multiprocessing, you should not pass non-pickleable arguments to
        the generator as they can't be passed easily to children
        processes.

Unpacking behavior for iterator-like inputs:
    A common pattern is to pass a tf.data.Dataset, generator, or
  tf.keras.utils.Sequence to the `x` argument of fit, which will in fact
  yield not only features (x) but optionally targets (y) and sample
  weights.  TF-Keras requires that the output of such iterator-likes be
  unambiguous. The iterator should return a tuple of length 1, 2, or 3,
  where the optional second and third elements will be used for y and
  sample_weight respectively. Any other type provided will be wrapped in
  a length one tuple, effectively treating everything as 'x'. When
  yielding dicts, they should still adhere to the top-level tuple
  structure.
  e.g. `({"x0": x0, "x1": x1}, y)`. TF-Keras will not attempt to
  separate features, targets, and weights from the keys of a single
  dict.
    A notable unsupported data type is the namedtuple. The reason is
  that it behaves like both an ordered datatype (tuple) and a mapping
  datatype (dict). So given a namedtuple of the form:
      `namedtuple("example_tuple", ["y", "x"])`
  it is ambiguous whether to reverse the order of the elements when
  interpreting the value. Even worse is a tuple of the form:
      `namedtuple("other_tuple", ["x", "y", "z"])`
  where it is unclear if the tuple was intended to be unpacked into x,
  y, and sample_weight or passed through as a single element to `x`. As
  a result the data processing code will simply raise a ValueError if it
  encounters a namedtuple. (Along with instructions to remedy the
  issue.)

Returns:
    A `History` object. Its `History.history` attribute is
    a record of training loss values and metrics values
    at successive epochs, as well as validation loss values
    and validation metrics values (if applicable).

Raises:
    RuntimeError: 1. If the model was never compiled or,
    2. If `model.fit` is  wrapped in `tf.function`.

    ValueError: In case of mismatch between the provided input data
        and what the model expects or when the input data is empty.
r)   fitN)validation_split)r   r^  rk  
batch_sizesteps_per_epochinitial_epochepochsshuffleclass_weightmax_queue_sizeworkersuse_multiprocessingr  r	  Tr   add_historyadd_progbarr  verboser  stepsFrj   r  )	epoch_numstep_numr  _ra:  Unexpected result of `train_function` (Empty logs). This could be due to issues in input pipeline that resulted in an empty dataset. Otherwise, please use `Model.compile(..., run_eagerly=True)`, or `tf.config.run_functions_eagerly(True)` for more information of where went wrong, or file a issue/bug to `tf.keras`._eval_data_handlerr   r^  rk  r  r  r  r  r  r  r  r  r	  r  )r   r^  rk  r  r  r
   r  r  r  return_dict_use_cached_eval_datasetval_r  )Hr&   disallow_legacy_graph_assert_compile_was_called_check_call_args_disallow_inside_tf_function_get_verbosityr  r   train_validation_splitrd  _should_use_with_coordinatorr^   r_   experimentalcoordinatorClusterCoordinatorrd   r  r   RespectCompiledTrainableStateget_data_handlerr.   rL   callbacks_moduleCallbackListinferred_stepsrX   r  r.  r+   rT  on_train_beginrM  ro   start&_maybe_load_initial_counters_from_ckpt_initial_epoch_initial_stepenumerate_epochsreset_metricson_epoch_begincatch_stop_iterationr  profilerTraceon_train_batch_beginshould_syncr   
async_waitstep_incrementon_train_batch_endr$   r  r   r  r   _should_evalr  %_disallow_exact_eval_with_add_metricsr   r  evaluater   rz  on_epoch_endr   	Optimizerfinalize_variable_valuesrh  stopon_train_endrY   )$rw   r   r^  r  r  r  r
   r  validation_datar  r  rk  r  r  validation_stepsvalidation_batch_sizevalidation_freqr  r  r  val_xval_yval_sample_weightdata_handlertraining_logssteps_per_epoch_inferredr  epochr  steptmp_logsend_step
epoch_logsval_logsrC   r  s$                                       r=   r  	Model.fit  sm   | 	++GU;'')e$$U+ *B*BC 7 ".!D!D}%8H"	  
 55oF	! ##@@**66II,, % %%++-~/[/[0
 (88+% /+)-$7$($=$=L$ i)9)F)FGG,99 $ '1#!&55	 "'D"&":":"<D&&q)$$& M00//557
  ><#>#> % ;;(-+* D#/#@#@#Bx""$((/!668 , 2 2 4[[55;;#&+%)'1  <  &::4@'+':':8'DH+77 ' 2 2 4#+D'+l.I.I'IH%884H#11 %!   2  !5 9(  <<TB<$3	 	 <<TB!YYt_
 #t'8'8?( ( 22BBDt%94@H2>2O2O##*;'<'J
,<*+#$+9$+0C"&040I0I262M2M3/  $}}&7#8#FJ."+'5 ',?$(15  -  H =ENN<L <LytSs*<L    %%h/&&uj9 *%%%g $Cj $..)*=*=>>6A:77,,
 t148D+00//446"""6<<k0
 0
--t  98P A0
 0
---sp   W0E$V9?V!	A>V
V!	V!	(DV9?V37V9B-V9;	W
V	V!	!
V0+V99
W	W
W!c                     [         R                  " U5      u  p#nU " USS9nU R                  X#XT5        U R                  X#XT5      $ )a  The logic for one evaluation step.

This method can be overridden to support custom evaluation logic.
This method is called by `Model.make_test_function`.

This function should contain the mathematical logic for one step of
evaluation.
This typically includes the forward pass, loss calculation, and metrics
updates.

Configuration details for *how* this logic is run (e.g. `tf.function`
and `tf.distribute.Strategy` settings), should be left to
`Model.make_test_function`, which can also be overridden.

Args:
  data: A nested structure of `Tensor`s.

Returns:
  A `dict` containing values that will be passed to
  `tf.keras.callbacks.CallbackList.on_train_batch_end`. Typically, the
  values of the `Model`'s metrics are returned.
Frb  )r   rd  rf  ri  )rw   rj  r   r^  rk  rl  s         r=   	test_stepModel.test_stepa  sH    . +CCDIma%(!6##A&@@r?   c                    ^ ^^ [        T SS 5      (       a  T R                  $ U 4S jmU U4S jmT R                  (       d  [        R                  " TSS9mU U4S jT l        T R                  $ )N_shard_test_functionc                    > U4S jnTR                   (       a  [        R                  " USSS9nTR                  R	                  X4S9n[        UTR                  TR                  S9nU$ )Nc                 L   > [         R                  " U 5      u  pnT" USS9nXXC4$ )NFrb  r   rd  )rj  r   r^  rk  rl  rw   s        r=   r  HModel._make_test_function_exact.<locals>.step_function.<locals>.run_step  s4    &2&K&K'#m a%0V22r?   Tr  r  r  )rv   r^   r  r  r  r  rZ  )batchr  rB   rw   s      r=   r  6Model._make_test_function_exact.<locals>.step_function  sk    3   ;;$ ..228(2KG(((::G
 Nr?   c                 b  > / / pC[         R                  " 5          TR                  R                   H-  nUc  M  UR	                  [
        R                  " U5      5        M/     TR                  R                   H-  nUc  M  UR	                  [
        R                  " U5      5        M/     [        R                  R                  TR                  R                  5       5      nS S S 5        [        R                  " XU5      n [        U 5      n[         R"                  " 5          U HL  nT" U5      u  ppU H  nUR%                  XU5        M     U H  nUR%                  X5        M     W" XU5        MN     S S S 5        UU-   WR&                  -   nU Vs0 s H  oUR(                  UR*                  _M     nn[,        R.                  " [1        U5      5         TR2                  R5                  S5        S S S 5        U$ ! , (       d  f       GN= f! , (       d  f       N= fs  snf ! , (       d  f       U$ = frP  )r$   with_metric_local_vars_scoper[   unweighted_metricsrQ   r   clone_metricr  r   r  from_configrZ   
get_configr   auto_shard_datasetiterr   cache_variable_readsru  r  rC   r  r^   r  r  r,   r  )datasettotal_shards	shard_idxlocal_unweighted_metricslocal_weighted_metricsr|  
local_lossr  r&  r   r^  rl  rk  weighted_metricunweighted_metriclocal_metricsrB   rw   r  s                    r=   shard_test_function<Model._make_test_function_exact.<locals>.shard_test_function  s   
 @B2&<668 #33FFF)077'44V< G
 #33DDF).55'44V< E
 +::FF&&113
 9"  22yG G}H!668%E2?2F/A&+A'44QN ,B-E))66qA .Fq-8 & 9 )()$$% 
 BOOv{{FNN2GO(()>w)GH""--a0 INK 98* 98 PHHNs7   G7AG7 A G7AH	 HH7
H	
H
H.Tr  c                  8   > TR                   R                  TU S9$ r  r  )r:   rw   r:  s    r=   r  1Model._make_test_function_exact.<locals>.<lambda>  s!    $33<<# = r?   )r   r!  r  r^   r  )rw   r:  r  s   `@@r=   _make_test_function_exactModel._make_test_function_exact  sd    4/66,,,	,+	Z "$++#d#
 	! (((r?   c                   ^ ^^ T R                   b  U(       d  T R                   $ U 4S jmT R                  b=  T R                  R                  5       R                  5       S:X  a{  T R                  (       dj  U U4S jmT R
                  (       d  [        R                  " TSS9mT R                  (       a  U U4S jT l         T R                   $ TT l          T R                   $ T R                  (       aE  U U4S jmT R
                  (       d  [        R                  " TSS9mU U4S jT l         T R                   $ U U4S	 jmT R
                  (       d  [        R                  " TSS9mTT l         T R                   $ )
an  Creates a function that executes one step of evaluation.

This method can be overridden to support custom evaluation logic.
This method is called by `Model.evaluate` and `Model.test_on_batch`.

Typically, this method directly controls `tf.function` and
`tf.distribute.Strategy` settings, and delegates the actual evaluation
logic to `Model.test_step`.

This function is cached the first time `Model.evaluate` or
`Model.test_on_batch` is called. The cache is cleared whenever
`Model.compile` is called. You can skip the cache and generate again the
function with `force=True`.

Args:
  force: Whether to regenerate the test function and skip the cached
    function if available.

Returns:
  Function. The function created by this method should accept a
  `tf.data.Iterator`, and return a `dict` containing values that will
  be passed to `tf.keras.Callbacks.on_test_batch_end`.
c                    >^  U 4S jnTR                   (       a  [        R                  " USSS9n[        U5      nT R                  R                  X#4S9n[        UTR                  TR                  S9nU$ )Runs a single evaluation step.c                    > TR                  U 5      n[        R                  " [        U5      5         TR                  R                  S5        S S S 5        U$ ! , (       d  f       U$ = frP  )r  r^   r  r  r,   r  r  s     r=   r  AModel.make_test_function.<locals>.step_function.<locals>.run_step  sS    //$/,,-B7-KL''2215 M MLr  Tr  r  r  r  r  s   `    r=   r  /Model.make_test_function.<locals>.step_function  st     ;;$ >D//33H73KG(((::G
 Nr?   r  c                    > T" TU 5      $ )z)Runs a test execution with a single step.rJ   r  s    r=   r/  /Model.make_test_function.<locals>.test_function  r  r?   Tr  c                 :   > TR                   R                  TU 4S9$ r  r  r  rw   r/  s    r=   r  *Model.make_test_function.<locals>.<lambda>  s#    t88AA%RE  B  r?   c                 T   > [         R                  " U5       H  nT" TU 5      nM     W$ z*Runs a test execution with multiple steps.r  r  s       r=   r/  rG  $  r  r?   c                 l   > TR                   R                  TU TR                  R                  5       4S9$ r  r  rI  s    r=   r  rJ  /  s5    D,E,E,N,NR)B)B)H)H)J$K -O -r?   c                 h   > [         R                  " TR                  5       H  nT" TU 5      nM     W$ rL  r  r  s      r=   r/  rG  4  r  r?   )	r/  r.   r  r  rM  r  r^   r  rd   )rw   r  r  r/  s   ` @@r=   make_test_functionModel.make_test_function  s>   0 )%%%%	4 %%-((..05571<555 ## "!D! (( "R !!!G &3"F !!!= && ## "!D!"D" !!! ## "!D! "/D!!!r?   c                    [         R                  " SS5        U R                  5         U R                  S5        U R	                  X5        [        S5        UR                  SS5      nU(       a%  [        S[        UR                  5       5       35      eU R                  R                  (       aB  [        R                  R                  R                  R!                  U R                  5      U l        [%        X@R                  5      nU R&                  (       a  U R)                  5         U R                  R+                  5          U(       a  [-        U SS5      b  U R.                  nO4[0        R2                  " UUUUUSS	UU	U
U U R4                  U R&                  S
9n[7        U[8        R:                  5      (       d'  [8        R:                  " USUS:g  U US	UR<                  S9n0 nU R?                  U5      nU R@                  RC                  S5        URE                  5         U RF                  (       a  U RH                  RK                  5         URM                  5        H  u  nnU RO                  5         URQ                  5          URS                  5        Hd  n[        RT                  R                  RW                  SUS	S9   URY                  U5        UR[                  UUUU R&                  5      nSSS5        Mf     SSS5        M     [\        R^                  " U5      nU R&                  (       a  U Ra                  U5      nOU Rc                  U5      nU RF                  (       a  U RH                  Re                  5         URg                  US9  U(       a  UsSSS5        $ [i        XRj                  5      sSSS5        $ ! , (       d  f       GM8  = f! , (       d  f       GM  = f! , (       d  f       g= f)a  Returns the loss value & metrics values for the model in test mode.

Computation is done in batches (see the `batch_size` arg.)

Args:
    x: Input data. It could be:
      - A Numpy array (or array-like), or a list of arrays
        (in case the model has multiple inputs).
      - A TensorFlow tensor, or a list of tensors
        (in case the model has multiple inputs).
      - A dict mapping input names to the corresponding array/tensors,
        if the model has named inputs.
      - A `tf.data` dataset. Should return a tuple
        of either `(inputs, targets)` or
        `(inputs, targets, sample_weights)`.
      - A generator or `keras.utils.Sequence` returning `(inputs,
        targets)` or `(inputs, targets, sample_weights)`.
      A more detailed description of unpacking behavior for iterator
      types (Dataset, generator, Sequence) is given in the `Unpacking
      behavior for iterator-like inputs` section of `Model.fit`.
    y: Target data. Like the input data `x`, it could be either Numpy
      array(s) or TensorFlow tensor(s). It should be consistent with `x`
      (you cannot have Numpy inputs and tensor targets, or inversely).
      If `x` is a dataset, generator or `keras.utils.Sequence` instance,
      `y` should not be specified (since targets will be obtained from
      the iterator/dataset).
    batch_size: Integer or `None`. Number of samples per batch of
      computation. If unspecified, `batch_size` will default to 32. Do
      not specify the `batch_size` if your data is in the form of a
      dataset, generators, or `keras.utils.Sequence` instances (since
      they generate batches).
    verbose: `"auto"`, 0, 1, or 2. Verbosity mode.
        0 = silent, 1 = progress bar, 2 = single line.
        `"auto"` becomes 1 for most cases, and to 2 when used with
        `ParameterServerStrategy`. Note that the progress bar is not
        particularly useful when logged to a file, so `verbose=2` is
        recommended when not running interactively (e.g. in a production
        environment). Defaults to 'auto'.
    sample_weight: Optional Numpy array of weights for the test samples,
      used for weighting the loss function. You can either pass a flat
      (1D) Numpy array with the same length as the input samples
        (1:1 mapping between weights and samples), or in the case of
          temporal data, you can pass a 2D array with shape `(samples,
          sequence_length)`, to apply a different weight to every
          timestep of every sample. This argument is not supported when
          `x` is a dataset, instead pass sample weights as the third
          element of `x`.
    steps: Integer or `None`. Total number of steps (batches of samples)
      before declaring the evaluation round finished. Ignored with the
      default value of `None`. If x is a `tf.data` dataset and `steps`
      is None, 'evaluate' will run until the dataset is exhausted. This
      argument is not supported with array inputs.
    callbacks: List of `keras.callbacks.Callback` instances. List of
      callbacks to apply during evaluation. See
      [callbacks](https://www.tensorflow.org/api_docs/python/tf/tf_keras/callbacks).
    max_queue_size: Integer. Used for generator or
      `keras.utils.Sequence` input only. Maximum size for the generator
      queue. If unspecified, `max_queue_size` will default to 10.
    workers: Integer. Used for generator or `keras.utils.Sequence` input
      only. Maximum number of processes to spin up when using
      process-based threading. If unspecified, `workers` will default to
      1.
    use_multiprocessing: Boolean. Used for generator or
      `keras.utils.Sequence` input only. If `True`, use process-based
      threading. If unspecified, `use_multiprocessing` will default to
      `False`. Note that because this implementation relies on
      multiprocessing, you should not pass non-pickleable arguments to
      the generator as they can't be passed easily to children
      processes.
    return_dict: If `True`, loss and metric results are returned as a
      dict, with each key being the name of the metric. If `False`, they
      are returned as a list.
    **kwargs: Unused at this time.

See the discussion of `Unpacking behavior for iterator-like inputs` for
`Model.fit`.

Returns:
    Scalar test loss (if the model has a single output and no metrics)
    or list of scalars (if the model has multiple outputs
    and/or metrics). The attribute `model.metrics_names` will give you
    the display labels for the scalar outputs.

Raises:
    RuntimeError: If `model.evaluate` is wrapped in a `tf.function`.
r)   r  r  FzInvalid keyword arguments: r  Nr   r  r  Tr  test)r  r  r  )6r&   r  r  r  _check_sample_weight_warningr  r  rR   r   r  r  r  r^   r_   r  r  r  rd   r  r  r  r  r   r  r   r  r.   rL   r  r  r  _get_test_function_runnerr,   rT  on_test_beginrM  ro   r  r  r  r  r  r  r  on_test_batch_beginr  r$   r  r  r  r
  on_test_endflatten_metrics_in_orderrB  )rw   r   r^  r  r  rk  r  r
   r  r  r  r  r;   use_cached_eval_datasetr  r  test_function_runnerr  dataset_or_iteratorr  s                       r=   r  Model.evaluateB  sM   L 	++GZ@'')j)))!;$Z0"(**-G"O9$v{{}:M9NOPP##@@**66II,, % !*B*BC&&668%%++- (D"6=I#66  ,<<"/)$)"##1#(;(,(A(A*.*E*E " i)9)F)FGG,99 $ '1#&55	 D#'#A#A)#L %%a(##%00//557 ..0#""$!668 , 2 2 4[[55;;"Ta <  &99$?#7#@#@ 3 , $ $ ; ;	$D	  !5 98 1 88>D**44T:<<TB00//446!!t!,W .-Z 06H6HI[ .-j  98g .-sD    D<O<=O90N5)O5BOO5
O?	O
O	O
O)c                    U R                  5        VVs/ s H  nUR                    H  nUPM     M     nnnU R                  R                  n[	        U Vs/ s H  nX$;  PM	     sn5      (       a  [        S5      eg s  snnf s  snf )NzDetected that a metric was added to this model via `Model.add_metric`. This is not currently supported when using exact evaluation with `tf.distribute.ParameterServerStrategy`.)r:  r<  r[   r  anyr   )rw   layerr|  metrics_from_add_metricr[   s        r=   r  +Model._disallow_exact_eval_with_add_metrics
	  s     --/#
/.. ( / 	  #

  0088 65F .5
 
 ; 
#
s    A<Bc                 |    U R                   R                  (       d  gUS:X  a  U R                   R                  S-  $ U$ )Nr   r     )r  r  _num_workers)rw   r  s     r=   r  Model._infer_exact_eval_shards	  s9    ''DD F*++881<<$$r?   c                    U R                   (       aH  U R                  R                  (       a-  U R                  5       U l        [        U R                  U5      nU$ U R                  5       U l        [        U R                  U5      nU$ r   )r  r  r  r>  r/  _ExactTestFunctionrO  _TestFunction)rw   r
   rZ  s      r=   rT  Model._get_test_function_runner&	  su    ''((EE!%!?!?!AD#5""I$  $# "&!8!8!:D#01C1CY#O ##r?   c                 D    [         R                  " U5      u  n  nU " USS9$ )ah  The logic for one inference step.

This method can be overridden to support custom inference logic.
This method is called by `Model.make_predict_function`.

This method should contain the mathematical logic for one step of
inference.  This typically includes the forward pass.

Configuration details for *how* this logic is run (e.g. `tf.function`
and `tf.distribute.Strategy` settings), should be left to
`Model.make_predict_function`, which can also be overridden.

Args:
  data: A nested structure of `Tensor`s.

Returns:
  The result of one inference step, typically the output of calling the
  `Model` on data.
Frb  r$  )rw   rj  r   r  s       r=   predict_stepModel.predict_step4	  s'    ( 77=1aA&&r?   c                 v  ^ ^ T R                   b  U(       d  T R                   $ U 4S jmT R                  b=  T R                  R                  5       R                  5       S:X  a  T R                  (       d  U U4S jnOU U4S jnT R
                  (       d  [        R                  " USS9nUT l         T R                   $ )a6  Creates a function that executes one step of inference.

This method can be overridden to support custom inference logic.
This method is called by `Model.predict` and `Model.predict_on_batch`.

Typically, this method directly controls `tf.function` and
`tf.distribute.Strategy` settings, and delegates the actual evaluation
logic to `Model.predict_step`.

This function is cached the first time `Model.predict` or
`Model.predict_on_batch` is called. The cache is cleared whenever
`Model.compile` is called. You can skip the cache and generate again the
function with `force=True`.

Args:
  force: Whether to regenerate the predict function and skip the cached
    function if available.

Returns:
  Function. The function created by this method should accept a
  `tf.data.Iterator`, and return the outputs of the `Model`.
c                    >^  U 4S jnTR                   (       a  [        R                  " USSS9n[        U5      nT R                  R                  X#4S9n[        UTR                  SS9nU$ )rB  c                    > TR                  U 5      n[        R                  " [        U5      5         TR                  R                  S5        S S S 5        U$ ! , (       d  f       U$ = frP  )rk  r^   r  r  r-   r  r  s     r=   r  DModel.make_predict_function.<locals>.step_function.<locals>.run_steph	  sU    ,,T2,,-B7-KL**55a8 M MLr  Tr  r  concatr  )r
  r^   r  r  r  r  r  r  s   `    r=   r  2Model.make_predict_function.<locals>.step_functione	  sl     ;;$ >D//33H73KG(11XG Nr?   r  c                    > T" TU 5      $ )z0Runs an evaluation execution with a single step.rJ   r  s    r=   r0  5Model.make_predict_function.<locals>.predict_function	  r  r?   c                 T  > T" TU 5      n[         R                  " TR                  S-
  5       Hv  n[         R                  R                  R                  U[         R                  R                  S U5      4/S9  T" TU 5      n[         R                  R                  S X5      nMx     U$ )z1Runs an evaluation execution with multiple steps.r  c                 @    [         R                  " U SS9R                  $ )NT)dynamic_batch)r$   get_tensor_specr   )ts    r=   r  GModel.make_predict_function.<locals>.predict_function.<locals>.<lambda>	  s    h.F.F()/&&+e/,r?   )shape_invariantsc                     [        X/5      $ r   )rq  )t1t2s     r=   r  rz  	  s    vrh'7r?   )r^   r  r.   	autographr  set_loop_optionsr   r   )r  rB   r  step_outputsrw   r  s       r=   r0  rt  	  s    'h7$";";a"?@ALL-->> !( " 5 5%, %,	!"
* ?  $1x#@L gg337G A$ r?   Tr  )r0  r.   r  r  rM  r  r^   r  )rw   r  r0  r  s   `  @r=   make_predict_functionModel.make_predict_functionK	  s    .   ,U(((	0 %%-((..05571<555. !{{ 4  !1$$$r?   c	                    [         R                  " SS5        U R                  S5        [        S5        Sn	U R                  R
                  (       a  U R                  n	SU l        U R                  (       a  SU l        [        X0R                  5      nSn
U R                  R                  5          [        R                  R                  R                  R                  [        R                  R                  4nU R                  5       (       d  [!        U R                  5      (       a  [#        X5      (       ao   [        R                  R%                  5       n[        R                  R&                  R(                  R*                  nUUR,                  l        UR1                  U5      n[8        R:                  " UUUSSUUUU U R<                  S	9
n[#        U[>        R@                  5      (       d'  [>        R@                  " US
US:g  U USURB                  S9nU RE                  5       U l#        U RH                  RK                  S5        URM                  5         U RN                  (       a  U RP                  RS                  5         SnURU                  5        H  u  nnURW                  5          URY                  5        H  nUR[                  U5        U RG                  U5      nUR\                  (       a  [^        R`                  " 5         UnU
c"  [        Rb                  Re                  S U5      n
O-[        Rf                  Rb                  Ri                  US U
U5        UURj                  -   nURm                  USU05        M     SSS5        M     Uc  [3        S5      eU RN                  (       a  U RP                  Ro                  5         URq                  5         SSS5        [        Rf                  Rb                  Ri                  W[r        U
5      nU	b  Xl        [t        Rv                  " U5      $ ! [2         a    [4        R6                  " SSS9   GNf = f! , (       d  f       GM  = f! , (       d  f       N= f)a  Generates output predictions for the input samples.

Computation is done in batches. This method is designed for batch
processing of large numbers of inputs. It is not intended for use inside
of loops that iterate over your data and process small numbers of inputs
at a time.

For small numbers of inputs that fit in one batch,
directly use `__call__()` for faster execution, e.g.,
`model(x)`, or `model(x, training=False)` if you have layers such as
`tf.keras.layers.BatchNormalization` that behave differently during
inference. You may pair the individual model call with a `tf.function`
for additional performance inside your inner loop.
If you need access to numpy array values instead of tensors after your
model call, you can use `tensor.numpy()` to get the numpy array value of
an eager tensor.

Also, note the fact that test loss is not affected by
regularization layers like noise and dropout.

Note: See [this FAQ entry](
https://keras.io/getting_started/faq/#whats-the-difference-between-model-methods-predict-and-call)
for more details about the difference between `Model` methods
`predict()` and `__call__()`.

Args:
    x: Input samples. It could be:
      - A Numpy array (or array-like), or a list of arrays
        (in case the model has multiple inputs).
      - A TensorFlow tensor, or a list of tensors
        (in case the model has multiple inputs).
      - A `tf.data` dataset.
      - A generator or `keras.utils.Sequence` instance.
      A more detailed description of unpacking behavior for iterator
      types (Dataset, generator, Sequence) is given in the `Unpacking
      behavior for iterator-like inputs` section of `Model.fit`.
    batch_size: Integer or `None`.
        Number of samples per batch.
        If unspecified, `batch_size` will default to 32.
        Do not specify the `batch_size` if your data is in the
        form of dataset, generators, or `keras.utils.Sequence` instances
        (since they generate batches).
    verbose: `"auto"`, 0, 1, or 2. Verbosity mode.
        0 = silent, 1 = progress bar, 2 = single line.
        `"auto"` becomes 1 for most cases, and to 2 when used with
        `ParameterServerStrategy`. Note that the progress bar is not
        particularly useful when logged to a file, so `verbose=2` is
        recommended when not running interactively (e.g. in a production
        environment). Defaults to 'auto'.
    steps: Total number of steps (batches of samples)
        before declaring the prediction round finished.
        Ignored with the default value of `None`. If x is a `tf.data`
        dataset and `steps` is None, `predict()` will
        run until the input dataset is exhausted.
    callbacks: List of `keras.callbacks.Callback` instances.
        List of callbacks to apply during prediction.
        See [callbacks](
        https://www.tensorflow.org/api_docs/python/tf/tf_keras/callbacks).
    max_queue_size: Integer. Used for generator or
        `keras.utils.Sequence` input only. Maximum size for the
        generator queue. If unspecified, `max_queue_size` will default
        to 10.
    workers: Integer. Used for generator or `keras.utils.Sequence` input
        only. Maximum number of processes to spin up when using
        process-based threading. If unspecified, `workers` will default
        to 1.
    use_multiprocessing: Boolean. Used for generator or
        `keras.utils.Sequence` input only. If `True`, use process-based
        threading. If unspecified, `use_multiprocessing` will default to
        `False`. Note that because this implementation relies on
        multiprocessing, you should not pass non-pickleable arguments to
        the generator as they can't be passed easily to children
        processes.

See the discussion of `Unpacking behavior for iterator-like inputs` for
`Model.fit`. Note that Model.predict uses the same interpretation rules
as `Model.fit` and `Model.evaluate`, so inputs must be unambiguous for
all three methods.

Returns:
    Numpy array(s) of predictions.

Raises:
    RuntimeError: If `model.predict` is wrapped in a `tf.function`.
    ValueError: In case of mismatch between the provided
        input data and the model's expectations,
        or in case a stateful model receives a number of samples
        that is not a multiple of the batch size.
r)   predictNzUsing Model.predict with MultiWorkerMirroredStrategy or TPUStrategy and AutoShardPolicy.FILE might lead to out-of-order result. Consider setting it to AutoShardPolicy.DATA.r   
stacklevelr   r  )
r   r  r  r  r  r  r  r  r  r	  Tr  c                     U /$ r   rJ   )batch_outputs    r=   r  Model.predict.<locals>.<lambda>b
  s    l^r?   c                 $    U R                  U5      $ r   )rQ   )outputr  s     r=   r  r  h
  s    V]]$0>"r?   rB   zUnexpected result of `predict_function` (Empty batch_outputs). Please use `Model.compile(..., run_eagerly=True)`, or `tf.config.run_functions_eagerly(True)` for more information of where went wrong, or file a issue/bug to `tf.keras`.)<r&   r  r  r  r  r  rb   rd   r  r  r^   compatv1rj  Dataset_in_multi_worker_mode_is_tpu_multi_hostrL   Optionsr  AutoShardPolicyDATAexperimental_distributeauto_shard_policywith_optionsr   warningsr  r   r  r.   r  r  r  r  r0  r-   rT  on_predict_beginrM  ro   r  r  r  r  on_predict_batch_beginr   r   r  r   r   r   map_structure_up_tor  on_predict_batch_endr
  on_predict_endpotentially_ragged_concatr$   r  )rw   r   r  r  r  r
   r  r  r  original_pss_strategyrB   dataset_typesoptionsdata_optionr  batch_outputsr  r  r  tmp_batch_outputsr  all_outputss                         r=   r  Model.predict	  s   J 	++GY?i($Y/
 !%##@@$($<$<!*.D'
 $$(,D% *B*BC%%++-YY\\..66HM**,,%d&>&>??Q.. ggoo/G"$''"6"6"F"F"K"KK# 33E w/A (88% %-$7$($=$=L i)9)F)FGG,99 $ '1#&55	 %)$>$>$@D!!!((+&&(00//557 M+<<>8!668 , 2 2 4!88>,0,A,A(,K)'33#..0- & #?&(gg&;&; C -'G
 OO00DD -!" !( - $(,*E*E#E!66$y-&@/ !5 98  ?8 $ /  00//446$$&{ .| oo**>>4g
 !,*?'44[AAu " MM0 $%R 98o .-sM   -BQ6A.P&$D Q$CQ;AQ&Q	QQ		Q
Q	Q
Q-c                 J    U R                    H  nUR                  5         M     g)a  Resets the state of all the metrics in the model.

Examples:

>>> inputs = tf.keras.layers.Input(shape=(3,))
>>> outputs = tf.keras.layers.Dense(2)(inputs)
>>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
>>> model.compile(optimizer="Adam", loss="mse", metrics=["mae"])

>>> x = np.random.random((2, 3))
>>> y = np.random.randint(0, 2, (2, 2))
>>> _ = model.fit(x, y, verbose=0)
>>> assert all(float(m.result()) for m in model.metrics)

>>> model.reset_metrics()
>>> assert all(float(m.result()) == 0 for m in model.metrics)

N)r  reset_stater@  s     r=   r  Model.reset_metrics
  s    & AMMO r?   c           	      H   U R                  5         U R                  S5        [        S5        U(       a  U R                  5         U R                  R                  5          [        R                  " U 5         [        R                  " U R                  XX45      nU R                  5       U l        U R                  U5      nSSS5        SSS5        [        R                  " W5      nU(       a  U$ [        XR                  5      $ ! , (       d  f       NJ= f! , (       d  f       NS= f)a  Runs a single gradient update on a single batch of data.

Args:
    x: Input data. It could be:
      - A Numpy array (or array-like), or a list of arrays
          (in case the model has multiple inputs).
      - A TensorFlow tensor, or a list of tensors
          (in case the model has multiple inputs).
      - A dict mapping input names to the corresponding array/tensors,
          if the model has named inputs.
    y: Target data. Like the input data `x`, it could be either Numpy
      array(s) or TensorFlow tensor(s).
    sample_weight: Optional array of the same length as x, containing
      weights to apply to the model's loss for each sample. In the case
      of temporal data, you can pass a 2D array with shape (samples,
      sequence_length), to apply a different weight to every timestep of
      every sample.
    class_weight: Optional dictionary mapping class indices (integers)
      to a weight (float) to apply to the model's loss for the samples
      from this class during training. This can be useful to tell the
      model to "pay more attention" to samples from an under-represented
      class. When `class_weight` is specified and targets have a rank of
      2 or greater, either `y` must be one-hot encoded, or an explicit
      final dimension of `1` must be included for sparse class labels.
    reset_metrics: If `True`, the metrics returned will be only for this
      batch. If `False`, the metrics will be statefully accumulated
      across batches.
    return_dict: If `True`, loss and metric results are returned as a
      dict, with each key being the name of the metric. If `False`, they
      are returned as a list.

Returns:
    Scalar training loss
    (if the model has a single output and no metrics)
    or list of scalars (if the model has multiple outputs
    and/or metrics). The attribute `model.metrics_names` will give you
    the display labels for the scalar outputs.

Raises:
  RuntimeError: If `model.train_on_batch` is wrapped in a `tf.function`.
train_on_batchN)r  r  r  r  r  r  r   r  r   single_batch_iteratorr  r.  r$   r  rX  rB  )	rw   r   r^  rk  r  r  r  r  r  s	            r=   r  Model.train_on_batch
  s    d 	'')./$%56 %%++-~/[/[0
 $99((!H #'":":"<D&&x0D0
- 44T:K+D2D2DEE0
 0
--s%   D5A	D>D
D	D
D!c                    U R                  5         U R                  S5        [        S5        U(       a  U R                  5         U R                  R                  5          [        R                  " U R                  XU5      nU R                  5       U l	        U R                  U5      nSSS5        [        R                  " W5      nU(       a  U$ [        XpR                  5      $ ! , (       d  f       NB= f)aG  Test the model on a single batch of samples.

Args:
    x: Input data. It could be:
      - A Numpy array (or array-like), or a list of arrays (in case the
          model has multiple inputs).
      - A TensorFlow tensor, or a list of tensors (in case the model has
          multiple inputs).
      - A dict mapping input names to the corresponding array/tensors,
          if the model has named inputs.
    y: Target data. Like the input data `x`, it could be either Numpy
      array(s) or TensorFlow tensor(s). It should be consistent with `x`
      (you cannot have Numpy inputs and tensor targets, or inversely).
    sample_weight: Optional array of the same length as x, containing
      weights to apply to the model's loss for each sample. In the case
      of temporal data, you can pass a 2D array with shape (samples,
      sequence_length), to apply a different weight to every timestep of
      every sample.
    reset_metrics: If `True`, the metrics returned will be only for this
      batch. If `False`, the metrics will be statefully accumulated
      across batches.
    return_dict: If `True`, loss and metric results are returned as a
      dict, with each key being the name of the metric. If `False`, they
      are returned as a list.

Returns:
    Scalar test loss (if the model has a single output and no metrics)
    or list of scalars (if the model has multiple outputs
    and/or metrics). The attribute `model.metrics_names` will give you
    the display labels for the scalar outputs.

Raises:
    RuntimeError: If `model.test_on_batch` is wrapped in a
      `tf.function`.
test_on_batchN)r  r  r  r  r  r  r   r  rO  r/  r$   r  rX  rB  )rw   r   r^  rk  r  r  r  r  s           r=   r  Model.test_on_batch
  s    V 	'')o.$_5 %%++-#99((!H "&!8!8!:D%%h/D . 44T:K+D2D2DEE .-s   A	C##
C1c                 \   U R                  S5        [        S5        U R                  R                  5          [        R
                  " U R                  U5      nU R                  5       U l        U R                  U5      nSSS5        [        R                  " W5      $ ! , (       d  f       N$= f)a  Returns predictions for a single batch of samples.

Args:
    x: Input data. It could be:
      - A Numpy array (or array-like), or a list of arrays (in case the
          model has multiple inputs).
      - A TensorFlow tensor, or a list of tensors (in case the model has
          multiple inputs).

Returns:
    Numpy array(s) of predictions.

Raises:
    RuntimeError: If `model.predict_on_batch` is wrapped in a
      `tf.function`.
predict_on_batchN)
r  r  r  r  r   r  r  r0  r$   r  )rw   r   r  rB   s       r=   r  Model.predict_on_batch#  s    " 	01$%78%%++-#99((!H %)$>$>$@D!++H5G . 44W== .-s   AB
B+c                 d    [         R                  " SSS9  U R                  UUUUUUUUU	U
UUUUS9$ )zFits the model on data yielded batch-by-batch by a Python generator.

DEPRECATED:
  `Model.fit` now supports generators, so there is no longer any need to
  use this endpoint.
z`Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.r   r  )r  r  r  r
   r  r  r  r  r  r  r  r  r  )r  r  r  )rw   	generatorr  r  r  r
   r  r  r  r  r  r  r  r  r  s                  r=   fit_generatorModel.fit_generator>  s]    0 	A 		
 xx++-+%) 3'  
 	
r?   c           
      x    [         R                  " SSS9  U R                  S5        U R                  UUUUUUUS9$ )zEvaluates the model on a data generator.

DEPRECATED:
  `Model.evaluate` now supports generators, so there is no longer any
  need to use this endpoint.
z`Model.evaluate_generator` is deprecated and will be removed in a future version. Please use `Model.evaluate`, which supports generators.r   r  evaluate_generatorr  r  r  r  r  r
   )r  r  r  r  rw   r  r  r
   r  r  r  r  s           r=   r  Model.evaluate_generatorm  sW    " 	F 		
 	23}}) 3  
 	
r?   c           
      V    [         R                  " SSS9  U R                  UUUUUUUS9$ )zGenerates predictions for the input samples from a data generator.

DEPRECATED:
  `Model.predict` now supports generators, so there is no longer any
  need to use this endpoint.
z`Model.predict_generator` is deprecated and will be removed in a future version. Please use `Model.predict`, which supports generators.r   r  r  )r  r  r  r  s           r=   predict_generatorModel.predict_generator  sH    " 	E 		
 ||) 3  
 	
r?   c                     U R                  5         U R                  (       d  / $ / nU R                   H  nXR                  -  nM     XR                  -  nU R                  U5      $ r   )_assert_weights_created
_trainable_self_tracked_trackablesrh  _trainable_weights_dedup_weights)rw   rh  trackable_objs      r=   trainable_weightsModel.trainable_weights  s`    $$&I !::M#D#DD ;666""#677r?   c                 N   U R                  5         / nU R                   H  nXR                  -  nM     U R                  (       dC  / nU R                   H  nX2R                  -  nM     UU R
                  -   U-   U R                  -   nOXR                  -   nU R                  U5      $ r   )r  r  non_trainable_variablesr  rh  r  _non_trainable_weightsr  )rw   r  r  rh  s       r=   non_trainable_weightsModel.non_trainable_weights  s    $$&"$!::M#'L'LL# ; "$!%!>!>#'H'HH# "? $))*)* --. $ (*E*EE $ ""#:;;r?   c                    > U R                   R                  5          [        TU ]  5       sSSS5        $ ! , (       d  f       g= f)zORetrieves the weights of the model.

Returns:
    A flat list of Numpy arrays.
N)r  r  r7   get_weightsr   s    r=   r  Model.get_weights  s.     %%++-7&( .--s	   4
Ac                 :    [         R                  " U 4UUUS.UD6  g)a9  Saves a model as a TensorFlow SavedModel or HDF5 file.

See the [Serialization and Saving guide](
    https://keras.io/guides/serialization_and_saving/) for details.

Args:
    model: TF-Keras model instance to be saved.
    filepath: `str` or `pathlib.Path` object. Path where to save the
        model.
    overwrite: Whether we should overwrite any existing model at the
        target location, or instead ask the user via an interactive
        prompt.
    save_format: Either `"keras"`, `"tf"`, `"h5"`,
        indicating whether to save the model
        in the native TF-Keras format (`.keras`),
        in the TensorFlow SavedModel format
        (referred to as "SavedModel" below),
        or in the legacy HDF5 format (`.h5`).
        Defaults to `"tf"` in TF 2.X, and `"h5"` in TF 1.X.

SavedModel format arguments:
    include_optimizer: Only applied to SavedModel and legacy HDF5
        formats. If False, do not save the optimizer state.
        Defaults to `True`.
    signatures: Only applies to SavedModel format. Signatures to save
        with the SavedModel. See the `signatures` argument in
        `tf.saved_model.save` for details.
    options: Only applies to SavedModel format.
        `tf.saved_model.SaveOptions` object that specifies SavedModel
        saving options.
    save_traces: Only applies to SavedModel format. When enabled, the
        SavedModel will store the function traces for each layer. This
        can be disabled, so that only the configs of each layer are
        stored. Defaults to `True`.
        Disabling this will decrease serialization time
        and reduce file size, but it requires that all custom
        layers/models implement a `get_config()` method.

Example:

```python
model = tf.keras.Sequential([
    tf.keras.layers.Dense(5, input_shape=(3,)),
    tf.keras.layers.Softmax()])
model.save("model.keras")
loaded_model = tf.keras.models.load_model("model.keras")
x = tf.random.uniform((10, 3))
assert np.allclose(model.predict(x), loaded_model.predict(x))
```

Note that `model.save()` is an alias for `tf.keras.models.save_model()`.
)filepath	overwritesave_formatN)r   
save_model)rw   r  r  r  r;   s        r=   save
Model.save  s.    l 		
#		

 	
r?   c                 4    [         R                  " U UUUUS9  g)a  Saves all layer weights.

Either saves in HDF5 or in TensorFlow format based on the `save_format`
argument.

When saving in HDF5 format, the weight file has:
  - `layer_names` (attribute), a list of strings
      (ordered names of model layers).
  - For every layer, a `group` named `layer.name`
      - For every such layer group, a group attribute `weight_names`,
          a list of strings
          (ordered names of weights tensor of the layer).
      - For every weight in the layer, a dataset
          storing the weight value, named after the weight tensor.

When saving in TensorFlow format, all objects referenced by the network
are saved in the same format as `tf.train.Checkpoint`, including any
`Layer` instances or `Optimizer` instances assigned to object
attributes. For networks constructed from inputs and outputs using
`tf.keras.Model(inputs, outputs)`, `Layer` instances used by the network
are tracked/saved automatically. For user-defined classes which inherit
from `tf.keras.Model`, `Layer` instances must be assigned to object
attributes, typically in the constructor. See the documentation of
`tf.train.Checkpoint` and `tf.keras.Model` for details.

While the formats are the same, do not mix `save_weights` and
`tf.train.Checkpoint`. Checkpoints saved by `Model.save_weights` should
be loaded using `Model.load_weights`. Checkpoints saved using
`tf.train.Checkpoint.save` should be restored using the corresponding
`tf.train.Checkpoint.restore`. Prefer `tf.train.Checkpoint` over
`save_weights` for training checkpoints.

The TensorFlow format matches objects and variables by starting at a
root object, `self` for `save_weights`, and greedily matching attribute
names. For `Model.save` this is the `Model`, and for `Checkpoint.save`
this is the `Checkpoint` even if the `Checkpoint` has a model attached.
This means saving a `tf.keras.Model` using `save_weights` and loading
into a `tf.train.Checkpoint` with a `Model` attached (or vice versa)
will not match the `Model`'s variables. See the
[guide to training checkpoints](
https://www.tensorflow.org/guide/checkpoint) for details on
the TensorFlow format.

Args:
    filepath: String or PathLike, path to the file to save the weights
        to. When saving in TensorFlow format, this is the prefix used
        for checkpoint files (multiple files are generated). Note that
        the '.h5' suffix causes weights to be saved in HDF5 format.
    overwrite: Whether to silently overwrite any existing file at the
        target location, or provide the user with a manual prompt.
    save_format: Either 'tf' or 'h5'. A `filepath` ending in '.h5' or
        '.keras' will default to HDF5 if `save_format` is `None`.
        Otherwise, `None` becomes 'tf'. Defaults to `None`.
    options: Optional `tf.train.CheckpointOptions` object that specifies
        options for saving weights.

Raises:
    ImportError: If `h5py` is not available when attempting to save in
        HDF5 format.
)r  r  r  r  N)r   save_weights)rw   r  r  r  r  s        r=   r  Model.save_weights"  s"    @ 	#	
r?   c                 2    [         R                  " U UUUUS9$ )a  Loads all layer weights from a saved files.

The saved file could be a SavedModel file, a `.keras` file (v3 saving
format), or a file created via `model.save_weights()`.

By default, weights are loaded based on the network's
topology. This means the architecture should be the same as when the
weights were saved. Note that layers that don't have weights are not
taken into account in the topological ordering, so adding or removing
layers is fine as long as they don't have weights.

**Partial weight loading**

If you have modified your model, for instance by adding a new layer
(with weights) or by changing the shape of the weights of a layer,
you can choose to ignore errors and continue loading
by setting `skip_mismatch=True`. In this case any layer with
mismatching weights will be skipped. A warning will be displayed
for each skipped layer.

**Weight loading by name**

If your weights are saved as a `.h5` file created
via `model.save_weights()`, you can use the argument `by_name=True`.

In this case, weights are loaded into layers only if they share
the same name. This is useful for fine-tuning or transfer-learning
models where some of the layers have changed.

Note that only topological loading (`by_name=False`) is supported when
loading weights from the `.keras` v3 format or from the TensorFlow
SavedModel format.

Args:
    filepath: String, path to the weights file to load. For weight files
        in TensorFlow format, this is the file prefix (the same as was
        passed to `save_weights()`). This can also be a path to a
        SavedModel or a `.keras` file (v3 saving format) saved
        via `model.save()`.
    skip_mismatch: Boolean, whether to skip loading of layers where
        there is a mismatch in the number of weights, or a mismatch in
        the shape of the weights.
    by_name: Boolean, whether to load weights by name or by topological
        order. Only topological loading is supported for weight files in
        the `.keras` v3 format or in the TensorFlow SavedModel format.
    options: Optional `tf.train.CheckpointOptions` object that specifies
        options for loading weights (only valid for a SavedModel file).
)r  by_nameskip_mismatchr  )r   load_weights)rw   r  r  r  r  s        r=   r  Model.load_weightsj  s'    h &&'
 	
r?   c                     SSK Jn  U R                  5       nU R                  R                  UU[
        R
                  " 5       S.nU$ )zyUtil shared between different serialization methods.

Returns:
    Model config with TF-Keras version information added.
r   )__version__)
class_namerG  keras_versionr	   )tf_keras.srcr  r-  r<   __name__r	   )rw   r  rG  model_configs       r=   _updated_configModel._updated_config  s?     	>"..11*(	
 r?   c                     [         R                  " U R                  5      (       a"   [        R                  R                  U 5      nU$ 0 nU$ ! [
         a    0 n[        R                  " S5         U$ f = f)a  Returns the config of the `Model`.

Config is a Python dictionary (serializable) containing the
configuration of an object, which in this case is a `Model`. This allows
the `Model` to be be reinstantiated later (without its trained weights)
from this configuration.

Note that `get_config()` does not guarantee to return a fresh copy of
dict every time it is called. The callers should make a copy of the
returned dict if they want to modify it.

Developers of subclassed `Model` are advised to override this method,
and continue to update the dict from `super(MyModel, self).get_config()`
to provide the proper configuration of this `Model`. The default config
will return config dict for init parameters if they are basic types.
Raises `NotImplementedError` when in cases where a custom
`get_config()` implementation is required for the subclassed model.

Returns:
    Python dictionary containing the configuration of this `Model`.
zModel's `__init__()` arguments contain non-serializable objects. Please implement a `get_config()` method in the subclassed Model for proper saving and loading. Defaulting to empty config.)r   
is_defaultr-  r   r   r   r  r  )rw   rG  s     r=   r-  Model.get_config  su    2 ##DOO44	#))44T:  F ' 2 s   A "A21A2c                   ^ SSK Jn  [        R                  " 5          / SQn[	        U4S jU 5       5      n[
        R                  " U R                  5      n[
        R                  " UR                  R                  5      R                  SS  nXR                  [        1;   =(       d>    UR                  SS  U:H  =(       d%    UR                  S:H  =(       a    UR                  S:H  nU(       aD  U(       a=  UR                  TU5      u  pnU " XTR                  S5      S	9nUR                  X5        O	 U " S0 TD6nUsS S S 5        $ ! [          a&  n[!        S
U  SU R"                   ST SU 35      eS nAff = f! , (       d  f       g = f)Nr   r1   )rC   layersinput_layersoutput_layersc              3   ,   >#    U  H	  oT;   v   M     g 7fr   rJ   )r   keyrG  s     r=   r   $Model.from_config.<locals>.<genexpr>  s      ')?#v)?s   r  r:   r;   rC   )rA   rB   rC   zUnable to revive model from config. When overriding the `get_config()` method, make sure that the returned config contains all items used as arguments in the  constructor to z, which is the default behavior. You can override this default behavior by defining a `from_config(cls, config)` class method to specify how to create an instance of z# from its config.

Received config=z,

Error encountered during deserialization: rJ   )r5   r2   r   SharedObjectLoadingScoper   r#   getfullargspecrN   r6   r:   r)   varargsvarkwreconstruct_from_configr%  connect_ancillary_layersrR   r  )r9   rG  custom_objectsr2   functional_config_keysis_functional_configargspecfunctional_init_argsrevivable_as_functionalrA   rB   r  r  r   s    `            r=   r,  Model.from_config  s    	3335&" $' ')?' $  !//=G#-#<#<%%..$d12$  --u55 M<<#';;MOOv-K'--82K $
 $(? +5*L*LN+' !F9K 33EBM&ME m 65P ! #2 36 7'
 (+||n 5++1( 3EEFC
I Q 65s0   DE93E;E9
E6!E11E66E99
Fc                 p    U R                  5       n[        R                  " U4S[        R                  0UD6$ )a"  Returns a JSON string containing the network configuration.

To load a network from a JSON save file, use
`keras.models.model_from_json(json_string, custom_objects={})`.

Args:
    **kwargs: Additional keyword arguments to be passed to
        *`json.dumps()`.

Returns:
    A JSON string.
default)r  jsondumpsr   get_json_type)rw   r;   r  s      r=   to_jsonModel.to_json  s<     ++-zz
",":":
>D
 	
r?   c                     [        S5      e)aG  Returns a yaml string containing the network configuration.

Note: Since TF 2.6, this method is no longer supported and will raise a
RuntimeError.

To load a network from a yaml save file, use
`keras.models.model_from_yaml(yaml_string, custom_objects={})`.

`custom_objects` should be a dictionary mapping
the names of custom losses / layers / etc to the corresponding
functions / classes.

Args:
    **kwargs: Additional keyword arguments
        to be passed to `yaml.dump()`.

Returns:
    A YAML string.

Raises:
    RuntimeError: announces that the method poses a security risk
zMethod `model.to_yaml()` has been removed due to security risk of arbitrary code execution. Please use `model.to_json()` instead.)r   )rw   r;   s     r=   to_yamlModel.to_yaml1  s    . N
 	
r?   c                     U R                    H:  n[        US5      (       d  M  [        USS5      (       d  M*  UR                  5         M<     g )Nreset_statesstatefulF)r  hasattrr   r  )rw   r_  s     r=   r  Model.reset_statesM  s>    [[Eun--'z53 3 ""$	 !r?   c                     [         R                  " SSS9  / nU R                   H8  n[        USS5      (       d  M  [	        US5      (       d  M*  XR
                  -  nM:     U$ )a  Deprecated, do NOT use!

Returns the `updates` from all layers that are stateful.

This is useful for separating training updates and
state updates, e.g. when we need to update a layer's internal state
during prediction.

Returns:
    A list of update ops.
z`Model.state_updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.r   r  r  Fupdates)r  r  r  r   r  r  )rw   state_updatesr_  s      r=   r  Model.state_updatesT  s_     	6 		
 [[Euj%005),,!]]2M ! r?   c                 8    U R                  U R                  5      $ )zReturns the list of all layer variables/weights.

Note: This will not track the weights of nested `tf.Modules` that are
not themselves TF-Keras layers.

Returns:
  A list of variables.
)r  _undeduplicated_weightsr   s    r=   r  Model.weightso  s     ""4#?#?@@r?   c                     U R                  5         / nU R                   H  nXR                  -  nM     XR                  U R                  -   -  nU$ )z?Returns the undeduplicated list of all layer variables/weights.)r  r  	variablesr  r  )rw   r  r_  s      r=   r  Model._undeduplicated_weights{  sO     	$$&22E&G 3**T-H-HHHr?   c           
      p    U R                   (       d  [        S5      e[        R                  " U UUUUUUS9  g)ap  Prints a string summary of the network.

Args:
    line_length: Total length of printed lines
        (e.g. set this to adapt the display to different
        terminal window sizes).
    positions: Relative or absolute positions of log elements
        in each line. If not provided, becomes
        `[0.3, 0.6, 0.70, 1.]`. Defaults to `None`.
    print_fn: Print function to use. By default, prints to `stdout`.
        If `stdout` doesn't work in your environment, change to `print`.
        It will be called on each line of the summary.
        You can set it to a custom function
        in order to capture the string summary.
    expand_nested: Whether to expand the nested models.
        Defaults to `False`.
    show_trainable: Whether to show if a layer is trainable.
        Defaults to `False`.
    layer_range: a list or tuple of 2 strings,
        which is the starting layer name and ending layer name
        (both inclusive) indicating the range of layers to be printed
        in summary. It also accepts regex patterns instead of exact
        name. In such case, start predicate will be the first element
        it matches to `layer_range[0]` and the end predicate will be
        the last element it matches to `layer_range[1]`.
        By default `None` which considers all layers of model.

Raises:
    ValueError: if `summary()` is called before the model is built.
zyThis model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data.)line_length	positionsprint_fnexpand_nestedshow_trainablelayer_rangeN)r   r   r!   print_summary)rw   r  r  r  r  r  r  s          r=   summaryModel.summary  sC    N zz0 
 	!!#')#	
r?   c                 4    [        U R                  SSS95      $ )NF)include_self	recursive)r   r:  r   s    r=   r  Model.layers  s    D((eu(MNNr?   c                     [        S5      e)NzU`Model.layers` attribute is reserved and should not be used. Please use another name.)r   )rw   r  s     r=   r  r&    s    '
 	
r?   c           	         Ub  Ub  [        SU SU S35      eUbM  [        U R                  5      U::  a%  [        SU S[        U R                  5       S35      eU R                  U   $ UbS  U R                   H  nUR                  U:X  d  M  Us  $    [        SU S[	        S	 U R                   5       5       S35      e[        S
5      e)a8  Retrieves a layer based on either its name (unique) or index.

If `name` and `index` are both provided, `index` will take precedence.
Indices are based on order of horizontal graph traversal (bottom-up).

Args:
    name: String, name of layer.
    index: Integer, index of layer.

Returns:
    A layer instance.
z<Provide only a layer name or a layer index. Received: index=z, name=r   z%Was asked to retrieve layer at index z but model only has z layers.zNo such layer: z. Existing layers are: c              3   8   #    U  H  oR                   v   M     g 7fr   )rC   )r   r_  s     r=   r   "Model.get_layer.<locals>.<genexpr>  s     <u

   z:Provide either a layer name or layer index at `get_layer`.)r   r   r  rC   r   )rw   rC   indexr_  s       r=   	get_layerModel.get_layer  s     !1wtfA/ 
 4;;5( ;E7*3t{{+;*<  {{5))::% L % !$'><<<=Q@  H
 	
r?   c                 B   0 n[         R                  R                  R                  U 5      R	                  5       u  nnU HW  n[        U[         R                  5      (       d  M$  X4   nSR                  U Vs/ s H  ofR                  PM     sn5      nXAU'   MY     U$ s  snf )a
  Retrieve all the variables and their paths for the model.

The variable path (string) is a stable key to identify a `tf.Variable`
instance owned by the model. It can be used to specify variable-specific
configurations (e.g. DTensor, quantization) from a global view.

This method returns a dict with weight object paths as keys
and the corresponding `tf.Variable` instances as values.

Note that if the model is a subclassed model and the weights haven't
been initialized, an empty dict will be returned.

Returns:
    A dict where keys are variable paths and values are `tf.Variable`
     instances.

Example:

```python
class SubclassModel(tf.keras.Model):

  def __init__(self, name=None):
    super().__init__(name=name)
    self.d1 = tf.keras.layers.Dense(10)
    self.d2 = tf.keras.layers.Dense(20)

  def call(self, inputs):
    x = self.d1(inputs)
    return self.d2(x)

model = SubclassModel()
model(tf.zeros((10, 10)))
weight_paths = model.get_weight_paths()
# weight_paths:
# {
#    'd1.kernel': model.d1.kernel,
#    'd1.bias': model.d1.bias,
#    'd2.kernel': model.d2.kernel,
#    'd2.bias': model.d2.bias,
# }

# Functional model
inputs = tf.keras.Input((10,), batch_size=10)
x = tf.keras.layers.Dense(20, name='d1')(inputs)
output = tf.keras.layers.Dense(30, name='d2')(x)
model = tf.keras.Model(inputs, output)
d1 = model.layers[1]
d2 = model.layers[2]
weight_paths = model.get_weight_paths()
# weight_paths:
# {
#    'd1.kernel': d1.kernel,
#    'd1.bias': d1.bias,
#    'd2.kernel': d2.kernel,
#    'd2.bias': d2.bias,
# }
```
r   )	r^   r   trackingObjectGraphViewbreadth_first_traversalrL   r   joinrC   )rw   ry  descendantsobject_paths_dict
descendanttrackable_referencesry  object_paths           r=   get_weight_pathsModel.get_weight_paths  s    v  OO$$44

!
!
#	
 &J*bkk22'8'D$!hh8L'M8L18L'MN&0{#	 &
  (Ns   6B
c                 ~    U R                   (       a,  [        U S5      (       a  U R                  R                  5       $ gg)a  Returns a serialized config with information for compiling the model.

This method returns a config dictionary containing all the information
(optimizer, loss, metrics, etc.) with which the model was compiled.

Returns:
    A dict containing information for compiling the model.
r  N)rH   r  r  	serializer   s    r=   get_compile_configModel.get_compile_config;  s8     /@!A!A''1133 "Br?   c                    U R                   R                  [        R                  :g  nU(       a  [        R                  " S5        g[
        R                  " U5      nU R                  " S0 UD6  [        U S5      (       ab  [        U R                  [        R                  5      (       a8  U R                  (       a&  U R                  R                  U R                  5        gggg)zCompiles the model with the information given in config.

This method uses the information in the config (optimizer, loss,
metrics, etc.) to compile the model.

Args:
    config: Dict containing information for compiling the model.
a+  `compile()` was not called as part of model loading because the model's `compile()` method is custom. All subclassed Models that have `compile()` overridden should also override `get_compile_config()` and `compile_from_config(config)`. Alternatively, you can call `compile()` manually after loading.Nr   rJ   )r<   r   r)   r  r  r   deserialize_keras_objectr  rL   r   r  r   r   rh  )rw   rG  has_overridden_compiles      r=   compile_from_configModel.compile_from_configG  s     "&!7!75==!H!OO; 44V<vD+&&4>>9+>+>??

 NN  !9!9:  @ 'r?   c                 2    SSK Jn  UR                  X5        g)a  Create a SavedModel artifact for inference (e.g. via TF-Serving).

This method lets you export a model to a lightweight SavedModel artifact
that contains the model's forward pass only (its `call()` method)
and can be served via e.g. TF-Serving. The forward pass is registered
under the name `serve()` (see example below).

The original code of the model (including any custom layers you may
have used) is *no longer* necessary to reload the artifact -- it is
entirely standalone.

Args:
    filepath: `str` or `pathlib.Path` object. Path where to save
        the artifact.

Example:

```python
# Create the artifact
model.export("path/to/location")

# Later, in a different process / environment...
reloaded_artifact = tf.saved_model.load("path/to/location")
predictions = reloaded_artifact.serve(input_data)
```

If you would like to customize your serving endpoints, you can
use the lower-level `keras.export.ExportArchive` class. The `export()`
method relies on `ExportArchive` internally.
r   )
export_libN)tf_keras.src.exportrE  export_model)rw   r  rE  s      r=   exportModel.exportg  s    > 	3/r?   c           	      P  > U R                   b  gU=(       d    / nU=(       d    0 nU R                  nU(       d  [        R                  " U5      n[        R
                  R                  U5      n/ n[        XE5       H*  u  pxUR                  [        R                  " USUS95        M,     [        R
                  R                  X5      n[        T	U ]5  XbU5        U R                  R                  S:X  a5  U R                   c'  [        R
                  R#                  S U5      U l        ggg)a   Defines the save spec so that serialization can trace `call()`.

The TensorSpecs of the call function `inputs`, `args`, and `kwargs` are
saved into a tuple of `([inputs] + args, kwargs)`. The input
`TensorSpec` names are updated to match the built `input_names`.

The specs can be retrieved with the `save_spec` property.

Args:
  inputs: possibly nested inputs passed into the call function.
  args: a list of positional arguments passed into call.
  kwargs: a dictionary of keyword arguments passed into call.
NF)rw  rC   
Sequentialc                 $    U c  S $ U R                   $ r   )r   r   s    r=   r  &Model._set_save_spec.<locals>.<lambda>  s    !)$88r?   )rh   rV   r   create_pseudo_input_namesr^   r   r   r  rQ   r$   rx  pack_sequence_asr7   _set_save_specr<   r  _build_input_shaper   )
rw   rA   r:   r;   rV   flat_inputsinputs_specrC   tensorr<   s
            r=   rP  Model._set_save_spec  s     ((4zr2&&'AA&IKggoof-9LD((u4P : gg..vC{&9 NN##|3''/&(gg&;&;8+'D# 0 4r?   c                 "    U R                  USS9$ )aL  Returns the `tf.TensorSpec` of call args as a tuple `(args, kwargs)`.

This value is automatically defined after calling the model for the
first time. Afterwards, you can use it when exporting the model for
serving:

```python
model = tf.keras.Model(...)

@tf.function
def serve(*args, **kwargs):
  outputs = model(*args, **kwargs)
  # Apply postprocessing steps, or add additional outputs.
  ...
  return outputs

# arg_specs is `[tf.TensorSpec(...), ...]`. kwarg_specs, in this
# example, is an empty dict since functional models do not use keyword
# arguments.
arg_specs, kwarg_specs = model.save_spec()

model.save(path, signatures={
  'serving_default': serve.get_concrete_function(*arg_specs,
                                                 **kwarg_specs)
})
```

Args:
  dynamic_batch: Whether to set the batch sizes of all the returned
    `tf.TensorSpec` to `None`. (Note that when defining functional or
    Sequential models with `tf.keras.Input([...], batch_size=X)`, the
    batch size will always be preserved). Defaults to `True`.
Returns:
  If the model inputs are defined, returns a tuple `(args, kwargs)`. All
  elements in `args` and `kwargs` are `tf.TensorSpec`.
  If the model inputs are not defined, returns `None`.
  The model inputs are automatically set when calling the model,
  `model.fit`, `model.evaluate` or `model.predict`.
F)inputs_only)_get_save_spec)rw   rw  s     r=   	save_specModel.save_spec  s    P ""=e"DDr?   c                     U R                   (       a  gSU R                  R                  ;   a@  U R                  [        :w  a+  U R                  (       d  [        SU R                   S35      eggg)a>  Asserts that all the weights for the model have been created.

For a non-dynamic model, the weights must already be created after the
layer has been called. For a dynamic model, the exact list of weights
can never be known for certain since it may change at any time during
execution.

We run this check right before accessing weights or getting the Numpy
value for the current weights. Otherwise, if the layer has never been
called, the user would just get an empty list, which is misleading.

Raises:
  ValueError: if the weights of the network have not yet been created.
Nr   zWeights for model 'z' have not yet been created. Weights are created when the model is first called on inputs or `build()` is called with an `input_shape`.)rF   r<   __dict__r)   r   r   rC   r   s    r=   r  Model._assert_weights_created  sj     << t~~...%'JJ %dii[ 1G G   ( /r?   c                 ,   U R                   R                  nUR                  (       a$  UR                  S[	        UR                  5      *  nOUR                  nSU;   a  UR                  S5        [	        U5      S:  a  USS n[        SU SU S35      eg)z0Check that `call()` has only one positional arg.Nr   r   zModels passed to `z^` can only have `training` and the first argument in `call()` as positional arguments, found: r   )r   r   r   r:   r   remover   )rw   method_namefullargspecpositional_args
extra_argss        r=   r  Model._check_call_args   s     oo22)../K#k6J6J2K1KLO)..O("":. !#(,J$[M 2$Q(  $r?   c           	         [        S [        R                  R                  U5       5       5      (       a  [	        SU S35      eUR                  SS5        UR                  SS5        UR                  SS5      nUb  [	        SU S	35      eUR                  S
S5      nUb  [	        SU S	35      e[        U5      S1-
  nU(       a  [        SU4 S35      eU R                  (       a  [        R                  R                  5       (       ac  [        R                  R                  5       nU R                   H5  nUR                  R                  U5      (       a  M%  [	        SU SU S35      e   U R                  n[        R                  R                  U5       HI  n	[!        U	S/ 5       H5  nUR                  R                  U5      (       a  M%  [	        SU	 SU S35      e   MK     [        R                  R                  U5       HI  n
[!        U
S/ 5       H5  nUR                  R                  U5      (       a  M%  [	        SU SU S35      e   MK     g)z7Performs validation checks for the default `compile()`.c              3   V   #    U  H  n[        U[        R                  5      v   M!     g 7fr   )rL   r   r  )r   r)  s     r=   r   *Model._validate_compile.<locals>.<genexpr>  s'      
1 sL22331r   z `tf.compat.v1.keras` Optimizer (zs) is not supported when eager execution is enabled. Use a `tf.keras` Optimizer instead, or disable eager execution.cloningNexperimental_run_tf_functionr_   z}`distribute` argument in compile is not available in TF 2.0. Please create the model under the `strategy.scope()`. Received: r   target_tensorszM`target_tensors` argument is not supported when executing eagerly. Received: sample_weight_modez,Invalid keyword argument(s) in `compile()`: z. Valid keyword arguments include "cloning", "experimental_run_tf_function", "distribute", "target_tensors", or "sample_weight_mode".z
Variable (z9) was not created in the distribution strategy scope of (z). It is most likely because some layers, model, or optimizer was being created outside the distribution strategy scope. Try to make sure your code looks similar to the following.
with strategy.scope():
  model=_create_model()
  model.compile(...)r  zMetric (z) passed to `model.compile` was created inside a different distribution strategy scope than the model. All metrics must be created in the same distribution strategy scope as the model (in this case z). If you pass in a string identifier for a metric to compile, the metric will automatically be created in the correct distribution strategy scope._weightszOptimizer (z) passed to `model.compile` was created inside a different distribution strategy scope than the model. All optimizers must be created in the same distribution strategy scope as the model (in this case z). If you pass in a string identifier for an optimizer to compile, the optimizer will automatically be created in the correct distribution strategy scope.)r^  r^   r   r   r   r  r  rR   r   r_   r`   ra   r  extendedvariable_created_in_scoper  r   )rw   r   r  r;   distribute_argtarget_tensor_arginvalid_kwargsstrategyr   r|  r)  s              r=   r  Model._validate_compile  ss    
wwy1
 
 
 29+ >   	

9d#

148L$7%+,A/ 
 #JJ'7>(&&7%8;  V(<'==>"$% &>>  ::"--4466}}113H^^((BB1EE$$QC (..6Z 8//	 	 $ ++ggoog.FV["5((BB1EE$"6( +< =E: FF	F	 	 6 /  77??9-CS*b1((BB1EE$%i[ 1) *2
 37	7	 	 2 .r?   c                 v    SnU R                   b(  U R                   R                  X[        R                  S9$ X#4$ )a=  Maybe load initial epoch from ckpt, considering worker recovery.

Refer to tensorflow/python/tf_keras/distribute/worker_training_state.py
for more information.

Args:
  steps_per_epoch: The number of step per epoch.
  initial_epoch: The original initial_epoch user passes in `fit()`.
  mode: The mode for running `model.fit()`.

Returns:
  If the training is recovering from previous failure under multi-worker
  training setting, return the (epoch, step) the training is supposed to
  continue at. Otherwise, return the `initial_epoch, initial_step` the
  user passes in.
r   )mode)rg   %maybe_load_initial_counters_from_ckptr'   TRAIN)rw   r  r  initial_steps       r=   r  ,Model._maybe_load_initial_counters_from_ckptj  sH    & +''MMX^^ N   ,,r?   c                 <    U R                   (       d  [        S5      eg )NzZYou must compile your model before training/testing. Use `model.compile(optimizer, loss)`.)rH   r   r   s    r=   r   Model._assert_compile_was_called  s$    
   8  !r?   c                 N   US L=(       dd    [        U[        R                  R                  5      =(       a9    [        UR                  [
        5      =(       a    [        UR                  5      S:H  nU(       a/  U R                  R                  c  [        R                  " S5        g g g )Nr  a  `evaluate()` received a value for `sample_weight`, but `weighted_metrics` were not provided.  Did you mean to pass metrics to `weighted_metrics` in `compile()`?  If this is intentional you can pass `weighted_metrics=[]` to `compile()` in order to silence this warning.)rL   r^   rj  r  element_specr   r   r[   _user_weighted_metricsr  r  )rw   r   rk  sample_weight_presents       r=   rS  "Model._check_sample_weight_warning  s     !.T 9 !
q"''//* )1>>51)ANN#q( 	 "%%<<DOO4 E "r?   c                 &    U R                  U5        g)zDThis method is for compat with Modelv1. Only inputs are needed
here.N)rP  )rw   rA   rB   r   s       r=   _set_inputsModel._set_inputs  s     	F#r?   c                 .    [         R                  " U 5      $ r   )r   ModelSavedModelSaverr   s    r=   _trackable_saved_model_saver"Model._trackable_saved_model_saver  s    "77==r?   c                   > US:X  aL  U R                   nU R                  nU R                  nU R                  nS U l         S U l        S U l        S U l        [        TU ]  " U40 UD6nUS:X  a  WU l         WU l        WU l        WU l        U$ )N
savedmodel)r.  r/  r0  r1  r7   _trackable_children)	rw   	save_typer;   r.  r/  r0  r1  childrenr<   s	           r=   r  Model._trackable_children  s    $!00N ..M#44 $ 6 6"&D!%D$(D!%)D"7.yCFC$"0D!.D$4D!%6D"r?   c                     US-   n[        U[        5      (       a  X-  S:H  $ [        U[        5      (       a  X;   $ [        SU S[	        U5       S35      e)Nr  r   zJExpected `validation_freq` to be a list or int. Received: validation_freq=z of the type r   )rL   r   r   r   r   )rw   r  r  s      r=   r  Model._should_eval  sk    	os++*a//..++--<,= >_-.a1 r?   c                 d   U R                  5         U R                  R                  nU R                  R                  nU(       d2  Ub  U R                  R                  nUb  U R                  R
                  nU R                  U R                  R                  UUU R                  R                  S.nU$ )a  Used for saving or cloning a Model.

Args:
  user_metrics: Whether to return user-supplied metrics or `Metric`
    objects. If True, returns the user-supplied metrics.
    Defaults to `True`.

Returns:
  Dictionary of arguments that were used when compiling the model.
)r   r  r  r  r  )
r  r[   _user_metricsr~  r<  _weighted_metricsr   rZ   _user_losses_user_loss_weights)rw   user_metricssaved_metricssaved_weighted_metricscompile_argss        r=   _get_compile_argsModel._get_compile_args  s     	'')--;;!%!6!6!M!M( $ 5 5 > >%1)-)>)>)P)P& &&33$ 6 ..AA
 r?   c                     U $ r   rJ   r   s    r=   _get_callback_modelModel._get_callback_model  s    r?   c                 J    U R                   R                  R                  5       $ r   )r  rm  r  r   s    r=   r  Model._in_multi_worker_mode  s    ''00FFHHr?   c                     U R                   $ r   )rH   r   s    r=   _compile_was_calledModel._compile_was_called  s       r?   ))rp   ru   rQ  rn   rd   r  r/   r\   rc   rb   r  rH   rU   rK   rv   rs   r-   r  re   ri   rh   r!  r.   ro   r,   r+   rg   rZ   r[   rY   rV   rA   r  r   rW   rB   r0  rX   r/  r.  r1  )NN)	rmspropNNNNNNNr   )NNNN)F)NNNr  r  Ng        NTNNr   NNNr  
   r  F)NNNr  NNNr  r  FF)Nr  NNr  r  F)NNNTF)NNTF)Nr  r  NNNr  Nr  r  FTr   )NNr  r  Fr   )TN)TNN)FFNr   )NNNFFN)T)
checkpoint)tr  
__module____qualname____firstlineno____doc__	frozenset	itertoolschainr   r   _TF_MODULE_IGNORED_PROPERTIES_SCALAR_UPRANKING_ONr8   r^   r   r0   no_automatic_dependency_trackingr%   filter_tracebackrN   r   rt   r   r   r   r   r   r  r   r   r   doc_in_current_and_subclassesr   r   r  rf   r  propertyr7  r  rB  r  r  setterrM  r	  r
  rZ  r_  rm  rf  ri  rv  r  r  r  r  r  r>  rO  r  r  r  rT  rk  r  r  r  r  r  r  do_not_generate_docsr  r  r  r  r  r  r  r  r  r  r-  classmethodr,  r  r  r  r  r  r  r!  r  r-  r9  r=  rB  rH  rP  rY  r  r  r  r  r  rS  r  r  r  r  r  r  r  r  __static_attributes____classcell__r<   s   @r=   r)   r)   F   s   gR %. ::		
%! !C __>>%%A! & ?A!F( __>>E ?E)*(  % |# |#| %%#1 &#1J //
 0
@ %%  N0 &N0`G __>>E ?E __>>
 ?

   - -^ %. %.N K K %
 %
N " " 2 2 "((	 )	 ) ) 4  4 ! ! $ $ 	; 	; !''2 (2B"AH6
p%)N**X)0p#d %% 
"!)r  &r hA<R)hm"^ %% 
!EJ &EJN(%$'.Z%x %% !`B &`BD2 DFR ;Fz>6 && !,
 ',
\ && ! 
 ' 
D && !
 '
J 8 8 < <2) %%;
 &;
z %%BFE
 &E
N %%DH9
 &9
v" % %N = =~
$
8% && ' 2 	A 	A   5
n O O ]]
 
)
VGR
4;@!0F __>>' ?'R(ET D(Tl-4
*$
 > >,"<I ! !r?   r)   c                        \ rS rSrS rS rSrg)rh  i  c                     Xl         X l        g r   )	_function
_callbacks)rw   r  r
   s      r=   rN   _TestFunction.__init__  s    !#r?   c                     U R                  U5      nUR                  (       a  [        R                  " 5         UnX2R                  -   nU R
                  R                  Xv5        U$ r   )r  r   r   r  r  r  on_test_batch_end)rw   r[  r  r  unused_shardsr  r  r  s           r=   r  _TestFunction.run_step  sP    >>"56## 555))(9r?   )r  r  N)r  r  r  r  rN   r  r  rJ   r?   r=   rh  rh    s    $r?   rh  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )rg  i  c                 2   > [         TU ]  X5        / U l        g r   )r7   rN   _logs)rw   r  r
   r<   s      r=   rN   _ExactTestFunction.__init__  s    -
r?   c           	      F   U R                  U[        R                  " U[        R                  S9[        R                  " U[        R                  S95      nUR                  (       a  [
        R                  " 5         U R                  R                  U5        U R                  $ )N)rE   )	r  r^   constantr   r   r   r  r  rQ   )rw   r[  r  r  shardsr  s         r=   r  _ExactTestFunction.run_step  sj    >>KKbhh/KKBHH-

 ## 

(#zzr?   )r  )r  r  r  r  rN   r  r  r  r  s   @r=   rg  rg    s    	 	r?   rg  c                    ^^ TS:X  a  [         R                  " T5      (       a  SOSmUU4S jn[        R                  R	                  X05      $ )a	  Attempt to reduce the structure `values` to single values.

Given `values` (a `tf.Tensor` or a `PerReplica` structure),
which represents the values across all the replicas, `reduce_per_replica`
attempts to "reduce" those values and returns the corresponding structure
that represents only single values.

Currently, `reduce_per_replica` is only used for reducing the metric results
from `tf.distribute.Strategy.run()`. Depending on the underlying
`Strategy` implementation, `values` may be a `PerReplica` object,
 which can be thought of as a collection of values across the replicas,
or a `tf.Tensor`, if the strategy has already conducted the reduction
for the downstream library.

There are five possible outcomes of reduction:

1) if the `values` is a structure of simple `tf.Tensor`s, meaning that
   reduction is not actually needed, `reduce_per_replica` returns the
   structure as-is.
2) else, if `reduction="auto"`, then the best reduction strategy is
   chosen based on the current environment. This should only be used
   for training cases (`fit()`).
3) else, if `reduction="first"`, then `reduce_per_replica`
   returns the values of the first replica. This is used in the case of
   training and evaluation, where `values` is expected to hold the same
   value across the replicas as a result of `Strategy`'s synchronization
   across the replicas.
   `reduce_per_replica` does not synchronize the values.
4) else, if `reduction="sum"`, then `reduce_per_replica` returns the sum
   of values for all replicas. This may be used in the custom training loop
   case, where each replica contain different values which are not
   synchronized.
5) else, if `reduction="concat"`, then `reduce_per_replica`
   returns the concatenation of the values across the replicas, along the
   axis of dimension 0. This is used in the inference case (`predict()`).

Args:
  values: Structure of `PerReplica` objects or `tf.Tensor`s. `tf.Tensor`s
    are returned as-is.
  strategy: `tf.distribute.Strategy` object.
  reduction: One of `"auto"`, `"first"`, `"concat"`, or `"sum"`.
    `"auto"` will select `"first"` when used under a TPUStrategy, or
    `"sum"` otherwise.

Returns:
  Structure of `Tensor`s, representing the result of reduction.

Raises:
  ValueError: if the reduction method is not supported.
r  firstsumc                   > [        T5      (       a)  TS:X  a  [        U T5      $ TS:X  a  TR                  SU SS9$ [        U 5      (       a  [	        U TT5      $ [        U 5      (       d  U $ TS:X  a  TR                  U 5      S   $ TS:X  a6  [        T5      (       a  [        U T5      $ [        TR                  U 5      5      $ TS:X  a%  [        R                  " TR                  U 5      5      $ [        ST S	35      e)
z$Reduce a single `PerReplica` object.rq  r  SUMNaxisr  r   zM`reduction` must be "first", "concat", "sum", or "auto". Received: reduction=r   )#_collective_all_reduce_multi_worker_multi_worker_concatreduce _is_dtensor_per_replica_instance_reduce_dtensor_per_replica_is_per_replica_instanceexperimental_local_resultsr  _tpu_multi_host_concatrq  r^   
reduce_sumr   )r   r  rr  s    r=   _reduce#reduce_per_replica.<locals>._reduceU  s    .x88H$+Ax88e#uad;;+A...q(IFF)!,,H'!66q9!<<("!(++-a::hAA!DEE%==!D!DQ!GHH''0k4 r?   )r	   is_tpu_strategyr^   r   r   )valuesrr  r  r  s    `` r=   r  r    sA    h F&66x@@Ge	6 77  11r?   c                     [        U S   [        R                  5      (       a  [        R                  R	                  XS9$ [        U S   5      (       a  [        R                  " XS9$ [        R                  " XS9$ )zConcats `tensor`s along `axis`.r   r  	sp_inputsr  )rL   r^   SparseTensorsparserq  
_is_scalarstack)tensorsr  s     r=   rq  rq  s  s\    '!*boo..yyT==	GAJ		xx++yy,,r?   c                    [        U 5      S:X  a  U S   $ [        U S   [        R                  5      (       a  [        R                  R                  SU S9$ [        U S   [        R                  5      (       a  [        R
                  " U SS9$ [        R                  R                  R                  5       (       d  [        R
                  " U SS9$ [        R                  " U  Vs/ s H  n[        R                  " U5      SS PM     sn5      n[        R                  R                  X"SS :H  SS9n[        R                  R                  U5      R                  5       R                  5       (       a=  [!        U S   5      (       a  [        R                  " U SS9$ [        R
                  " U SS9$ UR                  5       R#                  5       SSS2   R%                  S5      nUS:X  a  SnOU S   R                  U* S n[        R&                  R)                  U  Vs/ s H  oR                  5       PM     snUS9R+                  SS5      $ s  snf s  snf )	zConcats `Tensor`s along their first dimension.

Args:
  tensors: List of `Tensor`s.

Returns:
  Concatenation of the inputs along the first dimension -- of type `Tensor`
  if all input shapes are compatible, or `RaggedTensor` if not.
r  r   r  r  NF)inner_shape)r   rL   r^   r  r  rq  RaggedTensorr   tf2enabledr  r   math
reduce_allr  r  r  tolistr,  raggedr  
merge_dims)r  rT  non_batch_shapesconstant_dimsconstant_inner_dimensionsconstant_inner_shapes         r=   r  r  }  s    7|qqz'!*boo..yyQ'::	GAJ	0	0yyq))__  ((**yyq))xxG LG&&!1!"!5G LMGG&&Ra00q ' M 
ww-(..05577gaj!!88G!,,99W1--
 	$$&tt,2259  !A%#&qz//1J0J0KL99&-.gFg.<P  jA+ !M, 	/s   "#IIc                     U R                   nUS:X  a$  UR                  n[        R                  " X4SS9S   $ US:X  a  U$ US:X  a  [        R                  " U5      $ [        SU S35      e)Nr  r   r  rq  r  zT`reduction` must be one of "first", "concat", "sum", or "auto". Received: reduction=r   )_dtensornum_replicas_in_syncr^   splitr  r   )r   rr  r  dtensornum_replicas        r=   r  r    sx     nnGG33xx15a88	h		e	}}W%%##,+Q0
 	
r?   c                     U S:X  a  UR                   (       a  [        SU  35      eU S:X  a-  UR                   (       d  [        R                  " 5       (       d  ggU $ )z*Find the right verbosity value for 'auto'.r  ze`verbose=1` is not allowed with `ParameterServerStrategy` for performance reasons. Received: verbose=r  r   )r  r   r    is_interactive_logging_enabled)r  r  s     r=   r  r    s^    !|+HH66=Y@
 	
 &<<::<< Nr?   c                 n    [         R                  " U 5      =(       a    U R                  R                  S:  $ rP  )r	   r  rm  	num_hostsrr  s    r=   r  r    s)    ""8,P1B1B1L1Lq1PPr?   c                     UR                  U 5      nUR                  R                  n/ n[        U5       H  nXBUSU2   -  nM     [	        U5      $ )z'Correctly order TPU PerReplica objects.N)r  rm  num_replicas_per_hostr  rq  )r   rr  replicasr  ordered_replicas
replica_ids         r=   r  r    s_    2215H %--CC12
Z%F1F%FGG 3"##r?   c                     [        U [        R                  R                  5      =(       a    U R                  R                  5       $ r   )rL   r^   r_   MultiWorkerMirroredStrategyrm  r  r  s    r=   r  r    s2    8R]]FFG4



1
1
34r?   c           
      f   UR                  U SS9n[        U 5      (       ak  [        R                  " U R                   Vs/ s H/  n[        R
                  " [        R                  " U5      S   SS9PM1     snSS9nUR                  USS9nO:UR                  [        R
                  " [        R                  " U 5      S   SS9SS9n[        R                  " UUUR                  S9n/ n[        UR                  R                  5      n[        U5       H  nXbUSU2   -  nM     [        U5      $ s  snf )zDOrder PerReplica objects for CollectiveAllReduceStrategy and concat.r   r  )num_or_size_splitsnumN)gatherr  r^   rq  r  expand_dimsr   r  r  r   rm  worker_devicesr  )	r   rr  r  single_valueshapes
all_shapesr  num_replicas_per_workerr  s	            r=   r  r    s'   qq)H"" %&HH$,L rxx5a8qA$, 
 __V!_4
 __NN288A;q>2 % 

 xx%))H
 !("3"3"B"BC34
Z%H1H%HII 5"##-s   6D.c                     [        U [        R                  [        R                  45      =(       a    U R                  R
                  S:H  $ r   )rL   r^   r   r   r   r   r   s    r=   r  r    s.    a"))R[[12Hqww||q7HHr?   c                     [         R                  " 5       (       a  / $ [         R                  R                  U SS9n U  H'  n[	        U[         R
                  5      (       a  M$  U/s  $    / $ )zBReturns the minimum control dependencies to ensure step succeeded.T)expand_composites)r^   r   r   r   rL   r   )rB   outs     r=   r  r    sU    		ggoogo>G#r{{++5L  Ir?   c                 l    [         R                  " 5       (       a  SR                  U S9n[        U5      eg )NaI  Detected a call to `Model.{method_name}` inside a `tf.function`. `Model.{method_name} is a high-level endpoint that manages its own `tf.function`. Please move the call to `Model.{method_name}` outside of all enclosing `tf.function`s. Note that you can call a `Model` directly on `Tensor`s inside a `tf.function` like: `model(x)`.)r`  )r^   r   rS   r   )r`  	error_msgs     r=   r  r    s;    	 &[&
) 	 9%% r?   c                     / nU H  nX0;   d  M
  UR                  X   5        M     [        U R                  5       5       H  nXA;  d  M
  UR                  X   5        M     [        U5      S:X  a  US   $ U$ )zFTurns the `logs` dict into a list as per key order of `metrics_names`.r  r   )rQ   sortedr  r   )r  rB  resultsrC   r  s        r=   rX  rX  +  sl    G<NN4:&  diik"#NN49% # 7|qqzNr?   c                     [        U [        R                  R                  5      =(       a$    [        U [        R                  R
                  5      $ r   )rL   r^   r_   DistributedValuesr   CompositeTensorobjs    r=   r  r  9  s7    c2==::; 
R__,,A r?   c                 p    [        U [        R                  R                  5      =(       a    [	        U S5      $ )Nr  )rL   r^   r_   r  r  r  s    r=   r  r  ?  s/    
 c2==::; ZA r?   c                 `   ^  U 4S jn[         R                  R                  R                  T US9$ )z6Decorator that disallows multi-worker use of `method`.c                 v   > U R                  5       (       a  [        TR                   S35      eT" U /UQ70 UD6$ )Nz is not supported in multi-worker mode. Please use a non-multi-worker `tf.distribute.Strategy` such as `tf.distribute.MirroredStrategy`.)r  r   r  )rw   r:   r;   methods      r=   _method_wrapper-disable_multi_worker.<locals>._method_wrapperL  sJ    %%''??# $4 4  d,T,V,,r?   )targetdecorator_func)r^   r   	decoratormake_decorator)r"  r#  s   ` r=   disable_multi_workerr)  I  s1    - ??$$33o 4  r?   c                     SSK Jn  SSK Jn  U [        :X  d  XR                  :X  a  UR                  $ U [
        :X  a  [
        $ [        S U R                   5       5      U l        U R                  U 5        U $ )z?Inject `Functional` into the hierarchy of this class if needed.r   r1   )training_v1c              3   8   #    U  H  n[        U5      v   M     g 7fr   )rM   )r   bases     r=   r   0inject_functional_model_class.<locals>.<genexpr>g  s      8E%d++r+  )	r5   r2   r+  r)   r6   objectr   rO   r8   )r9   r2   r+  s      r=   rM   rM   [  sd    ./
e|s///$$$ f} 8; CM
 KKJr?   c                 j    [        U 5      S:X  a  g[        U 5      S:X  a  SU;   a  gSU;   a  SU;   a  gg)Nr   Tr  rB   rA   F)r   )r:   r;   s     r=   r4   r4   q  s:    
4yA~
4yA~)v-6i61r?   )r   )_r  r   r  r  r  rl   r  r   tensorflow.compat.v2r  v2r^   tensorflow.python.distributer   r   tensorflow.python.eagerr   tensorflow.python.platformr   r   tensorflow.python.util.tf_exportr   tensorflow.tools.docsr   r  r	   r
   r  r   tf_keras.src.dtensorr   r   rq   r5   r   r   r   r   r   r   r   tf_keras.src.metricsr   tf_keras.src.mixed_precisionr   r'  tf_keras.src.optimizersr   r   tf_keras.src.savingr   r   r   r   tf_keras.src.saving.legacyr   &tf_keras.src.saving.legacy.saved_modelr   r   tf_keras.src.utilsr   r    r!   r"   r#   r$   r%   r&   tf_keras.src.utils.mode_keysr'   h5pyImportErrorr   ModelVersionSelectorr)   rh  rg  r  rq  r  r  r  r  r  r  r  r  r  r  rX  r  r  r)  rM   r4   rJ   r?   r=   <module>rD     s\   4       ! ! 9 2 + < 9 .   6 # , = * 0 - , A . , D - 0 , * * 1 4 = F , ' * 9 ) ' . , 1
 m12t>!Jm@@ t>! 3t>!n}  "R2j-*Z
,(Q$4$>I	
&$,
_I  Ds   F FF