o
    2h1[                     @   s&  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  ddl!m"Z" dZ#G dd dej$Z%dd Z&dd Z'dd Z(dd Z)dS ) zHome of the `Sequential` model.    N)tf2)ops)tensor_util)layers)
base_layer)
functional)input_layer)training_utils)model_serialization)generic_utils)layer_utils)
tf_inspect)tf_utils)module)	np_arrays)
tf_logging)base)nestzuAll layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API.c                       s   e Zd ZdZejd( fdd	Ze fddZejdd Z	ejd	d
 Z
ej	d)ddZejd) fdd	Zd( fdd	Zdd Zdd Zd*ddZd*ddZ fddZed)ddZedd  Zejd!d  Zed"d# Zd$d% Z fd&d'Z  ZS )+
Sequentiala  `Sequential` groups a linear stack of layers into a `tf.keras.Model`.

  `Sequential` provides training and inference features on this model.

  Examples:

  >>> # Optionally, the first layer can receive an `input_shape` argument:
  >>> model = tf.keras.Sequential()
  >>> model.add(tf.keras.layers.Dense(8, input_shape=(16,)))
  >>> # Afterwards, we do automatic shape inference:
  >>> model.add(tf.keras.layers.Dense(4))

  >>> # This is identical to the following:
  >>> model = tf.keras.Sequential()
  >>> model.add(tf.keras.Input(shape=(16,)))
  >>> model.add(tf.keras.layers.Dense(8))

  >>> # Note that you can also omit the `input_shape` argument.
  >>> # In that case the model doesn't have any weights until the first call
  >>> # to a training/evaluation method (since it isn't yet built):
  >>> model = tf.keras.Sequential()
  >>> model.add(tf.keras.layers.Dense(8))
  >>> model.add(tf.keras.layers.Dense(4))
  >>> # model.weights not created yet

  >>> # Whereas if you specify the input shape, the model gets built
  >>> # continuously as you are adding layers:
  >>> model = tf.keras.Sequential()
  >>> model.add(tf.keras.layers.Dense(8, input_shape=(16,)))
  >>> model.add(tf.keras.layers.Dense(4))
  >>> len(model.weights)
  4

  >>> # When using the delayed-build pattern (no input shape specified), you can
  >>> # choose to manually build your model by calling
  >>> # `build(batch_input_shape)`:
  >>> model = tf.keras.Sequential()
  >>> model.add(tf.keras.layers.Dense(8))
  >>> model.add(tf.keras.layers.Dense(4))
  >>> model.build((None, 16))
  >>> len(model.weights)
  4

  ```python
  # Note that when using the delayed-build pattern (no input shape specified),
  # the model gets built the first time you call `fit`, `eval`, or `predict`,
  # or the first time you call the model on some input data.
  model = tf.keras.Sequential()
  model.add(tf.keras.layers.Dense(8))
  model.add(tf.keras.layers.Dense(1))
  model.compile(optimizer='sgd', loss='mse')
  # This builds the model for the first time:
  model.fit(x, y, batch_size=32, epochs=10)
  ```
  Nc                    s   t tj| j|dd d| _d| _d| _d| _d| _d| _	i | _
t | _d| _d| _|r@t|ttfs6|g}|D ]	}| | q8dS dS )zCreates a `Sequential` model instance.

    Args:
      layers: Optional list of layers to add to the model.
      name: Optional name for the model.
    F)nameautocastTN)superr   
Functional__init__supports_masking _compute_output_and_mask_jointly_auto_track_sub_layers_inferred_input_shape_has_explicit_input_shape_input_dtype_layer_call_argspecsset_created_nodes_graph_initialized_use_legacy_deferred_behavior
isinstancelisttupleadd)selfr   r   layer	__class__ d/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/keras/engine/sequential.pyr   g   s(   	zSequential.__init__c                    s8   t t| j}|rt|d tjr|dd  S |d d  S )Nr      )r   r   r   r%   r   
InputLayer)r)   r   r+   r-   r.   r      s   zSequential.layersc           	      C   s  t |dr|jd }t|tjr|}td t|tjr)t|t	j
s(t|}ntdt| t|g | |sDtd|jf d| _d}| dg  | jst|tjr[d}nt|\}}|rutj|||jd	 d
}|| d}|rt|jd j}t|dkrtt|| _t !| jd | _"d| _d| _#n| jr|| jd }tt|dkrtt|g| _d| _|s| j$r| %| j"| j d| _$n| j&| | '|g t()|j*| j+|< dS )a  Adds a layer instance on top of the layer stack.

    Args:
        layer: layer instance.

    Raises:
        TypeError: If `layer` is not a layer instance.
        ValueError: In case the `layer` argument does not
            know its input shape.
        ValueError: In case the `layer` argument has
            multiple output tensors, or is already connected
            somewhere else (forbidden in `Sequential` models).
    _keras_historyr   zPlease add `keras.layers.InputLayer` instead of `keras.Input` to Sequential model. `keras.Input` is intended to be used by Functional model.z;The added layer must be an instance of class Layer. Found: zAll layers added to a Sequential model should have unique names. Name "%s" is already the name of a layer in this model. Update the `name` argument to pass a unique name.F_self_tracked_trackablesT_inputbatch_shapedtyper   r/   N),hasattrr1   r%   r   r0   loggingwarningr   Moduler   Layerr   ModuleWrapper	TypeErrorstrr   assert_no_legacy_layers_is_layer_name_unique
ValueErrorr   built_maybe_create_attributer2   r	   get_input_shape_and_dtypeInputr   flatten_inbound_nodesoutputslenSINGLE_LAYER_OUTPUT_ERROR_MSGr   get_source_inputsinputsr   r#   _init_graph_networkappend#_handle_deferred_layer_dependenciesr   getfullargspeccallr    )	r)   r*   origin_layer
set_inputsr5   r6   xrI   output_tensorr-   r-   r.   r(      sj   




zSequential.addc                 C   s   | j std| j }| j| | j s)d| _d| _d| _d| _d| _	d| _
dS | j
rGg | j d _| j d jg| _| | j| j d| _dS dS )znRemoves the last layer in the model.

    Raises:
        TypeError: if there are no layers in the model.
    z!There are no layers in the model.NFr7   T)r   r>   r2   popr    rI   rM   rC   r   r   r#   _outbound_nodesoutputrN   )r)   r*   r-   r-   r.   rW      s"   


zSequential.popc           
      C   sn  |d u s| j s	d S t rt sd S | js| jst|}| jd u r%|}nt	| j|}|d ur|| jkrt
 j tj||| j d jd d}|}t }| j D ]5}t|| j z||}W n   d| _Y  W d    d S tt|dkr{ttt|| |}|}	qO|| _z| ||	 d| _W n   d| _Y W d    n1 sw   Y  || _d S d S d S d S d S )Nr   r3   r4   Tr/   )r   r   enabledr   #executing_eagerly_outside_functionsr   r$   r'   r   relax_input_shape
init_scoper   rF   r   r!   clear_previously_created_nodesr"   rJ   r   rG   rB   rK    track_nodes_created_by_last_callrN   r#   )
r)   input_shapeinput_dtype	new_shaperM   layer_inputcreated_nodesr*   layer_outputrI   r-   r-   r.   '_build_graph_network_for_inferred_shape	  sX   


	#


6	z2Sequential._build_graph_network_for_inferred_shapec                    s`   | j r| | j| j n|d u rtd| | | js+t|}|| _t	t
| | d| _d S )Nz+You must provide an `input_shape` argument.T)r#   rN   rM   rI   rB   rf   rC   r'   _build_input_shaper   r   build)r)   r`   r+   r-   r.   rh   V  s   

zSequential.buildc                    s  | j s0t|s(t|tjs(d| _tt	|| _
t r'tdt||f  n| |j|j | jrI| js>| | j| j tt| j|||dS |}| jD ]7}i }| j| j}d|v r`||d< d|v rh||d< ||fi |}tt|dkr}t t!|}t"|dd }qN|S )NTzLayers in a Sequential model should only have a single input tensor, but we receive a %s input: %s
Consider rewriting this model with the Functional API.)trainingmaskrj   ri   r/   _keras_mask)#r   r   
is_tf_typer%   r   ndarrayr$   r   map_structure_get_shape_tuplerg   r   rZ   r9   r:   typerf   shaper6   r#   rC   rN   rM   rI   r   r   rR   r   r    argsrJ   rG   rB   rK   getattr)r)   rM   ri   rj   rI   r*   kwargsargspecr+   r-   r.   rR   d  s<   

zSequential.callc                 C   s   |}| j D ]}||}q|S N)r   compute_output_shape)r)   r`   rq   r*   r-   r-   r.   rw     s   
zSequential.compute_output_shapec                 C   s   | j ||d}t|dd S )N)rj   rk   )rR   rs   )r)   rM   rj   rI   r-   r-   r.   compute_mask  s   zSequential.compute_mask    r   c                 C   s>   t d | |||}| dk s| dkrtd |S )ay  Generates class probability predictions for the input samples.

    The input samples are processed batch by batch.

    Args:
        x: input data, as a Numpy array or list of Numpy arrays
            (if the model has multiple inputs).
        batch_size: integer.
        verbose: verbosity mode, 0 or 1.

    Returns:
        A Numpy array of probability predictions.
    zq`model.predict_proba()` is deprecated and will be removed after 2021-01-01. Please use `model.predict()` instead.g        g      ?zNetwork returning invalid probability values. The last layer might not normalize predictions into probabilities (like softmax or sigmoid would).)warningswarnpredictminmaxr9   r:   )r)   rU   
batch_sizeverbosepredsr-   r-   r.   predict_proba  s
   

zSequential.predict_probac                 C   sB   t d | j|||d}|jd dkr|jddS |dkdS )af  Generate class predictions for the input samples.

    The input samples are processed batch by batch.

    Args:
        x: input data, as a Numpy array or list of Numpy arrays
            (if the model has multiple inputs).
        batch_size: integer.
        verbose: verbosity mode, 0 or 1.

    Returns:
        A numpy array of class predictions.
    a  `model.predict_classes()` is deprecated and will be removed after 2021-01-01. Please use instead:* `np.argmax(model.predict(x), axis=-1)`,   if your model does multi-class classification   (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,   if your model does binary classification   (e.g. if it uses a `sigmoid` last-layer activation).)r   r   r7   r/   )axisg      ?int32)rz   r{   r|   rq   argmaxastype)r)   rU   r   r   probar-   r-   r.   predict_classes  s
   
	zSequential.predict_classesc                    sV   g }t t| jD ]
}|t| q| jt|d}| j	s)| j
d ur)| j
|d< |S )N)r   r   build_input_shape)r   r   r   rO   r   serialize_keras_objectr   copydeepcopy_is_graph_networkrg   )r)   layer_configsr*   configr+   r-   r.   
get_config  s   
zSequential.get_configc           	      C   s   d|v r|d }| d}|d }nd }d }|}| |d}|D ]}tj||d}|| q|js?|r?t|ttfr?|| |S )Nr   r   r   )r   )custom_objects)	getlayer_moduledeserializer(   rM   r%   r'   r&   rh   )	clsr   r   r   r   r   modellayer_configr*   r-   r-   r.   from_config  s$   




zSequential.from_configc                 C   s6   t | dr| jS | jrt | jd dr| jd jS d S )N_manual_input_specr   
input_spec)r8   r   r   r   r)   r-   r-   r.   r     s
   
zSequential.input_specc                 C   s
   || _ d S rv   )r   )r)   valuer-   r-   r.   r        
c                 C   s
   t | S rv   )r
   SequentialSavedModelSaverr   r-   r-   r.   _trackable_saved_model_saver  r   z'Sequential._trackable_saved_model_saverc                 C   s*   | j D ]}|j|jkr||ur dS qdS )NFT)r   r   )r)   r*   	ref_layerr-   r-   r.   rA     s
   
z Sequential._is_layer_name_uniquec                    s   | j rd S ttj|   d S rv   )r#   r   r   r   _assert_weights_createdr   r+   r-   r.   r     s   z"Sequential._assert_weights_created)NNrv   )ry   r   )__name__
__module____qualname____doc__	trackable no_automatic_dependency_trackingr   propertyr   r(   rW   rf   r   defaultrh   rR   rw   rx   r   r   r   classmethodr   r   setterr   rA   r   __classcell__r-   r-   r+   r.   r   .   s>    8$
W
L,




r   c                 C   s<   t | dr| j}t|tr|S |jd urt| S d S d S )Nrq   )r8   rq   r%   r'   rankas_list)trq   r-   r-   r.   ro     s   


ro   c                 C   s@   | d u s|d u r
d S t | t |krd S tdd t| |D S )Nc                 s   s$    | ]\}}||krd n|V  qd S rv   r-   ).0d1d2r-   r-   r.   	<genexpr>$  s   " z$relax_input_shape.<locals>.<genexpr>)rJ   r'   zip)shape_1shape_2r-   r-   r.   r\     s
   r\   c                    sR   | j D ]}|j}t|D ]} fdd|jD |_qq fdd| j D | _ dS )zARemove nodes from `created_nodes` from the layer's inbound_nodes.c                       g | ]}| vr|qS r-   r-   r   nrd   r-   r.   
<listcomp>,  s
    z2clear_previously_created_nodes.<locals>.<listcomp>c                    r   r-   r-   r   r   r-   r.   r   /  s    N)rH   inbound_layersr   rG   rX   )r*   rd   nodeprev_layers
prev_layerr-   r   r.   r^   '  s   



r^   c                 C   sP   | j sdS || j d  | j d j}t|D ]}|jr%||jd  qdS )zFAdds to `created_nodes` the nodes created by the last call to `layer`.Nr7   )rH   r(   r   r   rG   rX   )r*   rd   r   r   r-   r-   r.   r_   3  s   r_   )*r   r   rz   tensorflow.pythonr   tensorflow.python.frameworkr   r   tensorflow.python.kerasr   r   tensorflow.python.keras.enginer   r   r   r	   *tensorflow.python.keras.saving.saved_modelr
   tensorflow.python.keras.utilsr   r   r   r   tensorflow.python.moduler   tensorflow.python.ops.numpy_opsr   tensorflow.python.platformr   r9   tensorflow.python.trackabler   r   tensorflow.python.utilr   rK   r   r   ro   r\   r^   r_   r-   r-   r-   r.   <module>   s<      i