o
    2h8                     @   s   d dl 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 eddgG dd deZdS )    N)backend)tree)keras_export)global_state)standardize_shape)
InputLayer)Layer)saving_utils)serialization)
Functional)Model)serialization_libzkeras.Sequentialzkeras.models.Sequentialc                       s   e Zd ZdZ fddZd- fdd	Zd.dd	Zd.d
dZdd Zdd Z	dd Z
d/ddZd0ddZedd Zejdd Zd0ddZdd Zedd Zedd  Zed!d" Zed#d$ Ze fd%d&Zd'd( Z fd)d*Zed/d+d,Z  ZS )1
SequentialaL  `Sequential` groups a linear stack of layers into a `Model`.

    Examples:

    ```python
    model = keras.Sequential()
    model.add(keras.Input(shape=(16,)))
    model.add(keras.layers.Dense(8))

    # Note that you can also omit the initial `Input`.
    # 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 = keras.Sequential()
    model.add(keras.layers.Dense(8))
    model.add(keras.layers.Dense(4))
    # model.weights not created yet

    # Whereas if you specify an `Input`, the model gets built
    # continuously as you are adding layers:
    model = keras.Sequential()
    model.add(keras.Input(shape=(16,)))
    model.add(keras.layers.Dense(8))
    len(model.weights)  # Returns "2"

    # 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 = keras.Sequential()
    model.add(keras.layers.Dense(8))
    model.add(keras.layers.Dense(4))
    model.build((None, 16))
    len(model.weights)  # Returns "4"

    # 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 = keras.Sequential()
    model.add(keras.layers.Dense(8))
    model.add(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)
    ```
    c                    s   t | t | S N)typingcastsuper__new__)clsargskwargs	__class__ V/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/models/sequential.pyr   B   s   zSequential.__new__NTc                    sH   t  j||d d | _g | _|r"|D ]	}| j|dd q|   d S d S )N)	trainablenameF)rebuild)r   __init___functional_layersadd_maybe_rebuild)selflayersr   r   layerr   r   r   r   E   s   zSequential.__init__c                 C   s   | j st|dddur| t|jd t|dr%|jd }t|tr%|}t|ts7t	d| dt
| d| |sEt	d	|j d
t|tre| j ret| j d tret	d| j d| j d j d| j | |rs|   dS d| _d| _dS )zkAdds a layer instance on top of the layer stack.

        Args:
            layer: layer instance.
        _input_shape_argN)shape_keras_historyr   zNOnly instances of `keras.Layer` can be added to a Sequential model. Received: z
 (of type )zGAll layers added to a Sequential model should have unique names. Name 'za' is already the name of a layer in this model. Update the `name` argument to pass a unique name.Sequential model '1' has already been configured to use input shape z/. You cannot add a different Input layer to it.F)r    getattrr!   r   r&   hasattrr(   
isinstancer   
ValueErrortype_is_layer_name_uniquer   batch_shapeappendr"   builtr   )r#   r%   r   origin_layerr   r   r   r!   N   sJ   







zSequential.addc                 C   s&   | j  }d| _d| _|r|   |S )zRemoves the last layer in the model.

        Args:
            rebuild: `bool`. Whether to rebuild the model after removing
            the layer. Defaults to `True`.

        Returns:
            layer: layer instance.
        FN)r    popr4   r   r"   )r#   r   r%   r   r   r   r6      s   

zSequential.popc                 C   s   d| _ d | _t| jd tr"t| jdkr"| jd j}| | d S t| jd dr>t| jdkr@| jd j	}| | d S d S d S )NFr      input_shape)
r4   r   r.   r    r   lenr2   buildr-   r8   )r#   r8   r   r   r   r"      s   zSequential._maybe_rebuildc                 C   s   d S r   r   r#   r   r   r   _lock_state   s   zSequential._lock_statec                 C   s   dS )Nr   r   r;   r   r   r   	_obj_type   s   zSequential._obj_typec           
      C   sT  zt |}W n   Y d S | jstd| j dt| jd tr<| jd j|kr;td| j d| jd j d| n| jd j}t||dg| j | _| jd j}|}| jdd  D ]B}z||}W q\ t	yo   Y  d S  t
y } z$t|j}d	d
 |j D }t|dkrtd|jj d| |d }~ww |}	t||	d| _d S )NzSequential model zC cannot be built because it has no layers. Call `model.add(layer)`.r   r*   r+   z'. You cannot build it with input_shape )r2   dtyper7   c                 S   s   g | ]}|j tjjkr|qS r   )defaultinspect	Parameterempty).0paramr   r   r   
<listcomp>   s
    z$Sequential.build.<locals>.<listcomp>zgLayers added to a Sequential model can only have a single positional argument, the input tensor. Layer z$ has multiple positional arguments: )inputsoutputs)r   r    r/   r   r.   r   r2   compute_dtypeoutputNotImplementedError	TypeErrorr@   	signaturecall
parametersvaluesr9   r   __name__r   r   )
r#   r8   r>   rF   xr%   erL   positional_argsrG   r   r   r   r:      s`   


zSequential.buildc                    s   | j r| j j|f||d S | jD ]2} fddt|di D }|jr)||d< |jr4|d ur4||d< ||fi |}|}ttj	|}q|S )Ntrainingmaskc                    s   i | ]}| v r| | qS r   r   )rC   kr   r   r   
<dictcomp>   s
    z#Sequential.call.<locals>.<dictcomp>_call_has_context_argrV   rU   )
r   rM   r$   r,   _call_has_mask_arg_call_has_training_argr   map_structurer   get_keras_mask)r#   rF   rU   rV   r   r%   layer_kwargsrG   r   rX   r   rM      s(   


zSequential.callc                 C   s0   | j }|rt|d tr|dd  S |d d  S )Nr   r7   )r    r.   r   r#   r$   r   r   r   r$      s   zSequential.layersc                 C   s   t d)Nz}`Sequential.layers` attribute is reserved and should not be used. Use `add()` and `pop()` to change the layers in this model.)AttributeError)r#   _r   r   r   r$     s   c                 K   sJ   | j r| j j|f||d|S | jD ]}|j|fd|i|}|}q|S )NrT   rU   )r   compute_output_specr$   )r#   rF   rU   rV   r   r%   rG   r   r   r   rc     s$   
zSequential.compute_output_specc                 C   s0   | j r	| j |S | jD ]	}||}|}q|S r   )r   compute_output_shaper$   )r#   r8   r%   output_shaper   r   r   rd     s   

zSequential.compute_output_shapec                 C       | j r| j jS td| j d)Nr*   z!' has no defined input shape yet.)r   r8   ra   r   r;   r   r   r   r8      
   zSequential.input_shapec                 C   rf   )Nr*   z"' has no defined output shape yet.)r   re   ra   r   r;   r   r   r   re   (  rg   zSequential.output_shapec                 C   rf   )Nr*   z' has no defined inputs yet.)r   rF   ra   r   r;   r   r   r   rF   0  rg   zSequential.inputsc                 C   rf   )Nr*   z' has no defined outputs yet.)r   rG   ra   r   r;   r   r   r   rG   8  rg   zSequential.outputsc                    s*   | j }|rt|d tr|d jS t jS )Nr   )r    r.   r   r>   r   input_dtyper`   r   r   r   rh   @  s   
zSequential.input_dtypec                 C   s*   | j D ]}|j|jkr||ur dS qdS )NFT)r    r   )r#   r%   	ref_layerr   r   r   r1   I  s
   
z Sequential._is_layer_name_uniquec                    sx   t j}tddrtj}g }t jD ]	}||| qt	| }| j
|d< t||d< | jd ur:| jd j|d< |S )Nuse_legacy_configFr   r$   r   build_input_shape)r   serialize_keras_objectr   get_global_attributelegacy_serializationr   r$   r3   r   
get_configr   copydeepcopyr   r    r2   )r#   serialize_fnlayer_configsr%   configr   r   r   ro   O  s   


zSequential.get_configc           	      C   s   d|v r|d }| d}|d }nd }|}| |d}|D ]}d|vr+tj||d}ntj||d}|| q|jsNdt v rN|rNt|t	t
frN|| |S )Nr   rk   r$   )r   module)custom_objects)getr	   model_from_configr   deserialize_keras_objectr!   r   localsr.   tuplelistr:   )	r   rt   rv   r   rk   rs   modellayer_configr%   r   r   r   from_config`  s6   




zSequential.from_config)NTN)Tr   )NN)rP   
__module____qualname____doc__r   r   r!   r6   r"   r<   r=   r:   rM   propertyr$   setterrc   rd   r8   re   rF   rG   rh   r1   ro   classmethodr   __classcell__r   r   r   r   r      s>    -
	
1

6
	

	



r   )rp   r@   r   	keras.srcr   r   keras.src.api_exportr   keras.src.backend.commonr   r   !keras.src.layers.core.input_layerr   keras.src.layers.layerr   keras.src.legacy.savingr	   r
   rn   keras.src.models.functionalr   keras.src.models.modelr   keras.src.savingr   r   r   r   r   r   <module>   s"    
