
    2Vh8                         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e             Zy)    N)backend)tree)keras_export)global_state)standardize_shape)
InputLayer)Layer)saving_utils)serialization)
Functional)Model)serialization_libzkeras.Sequentialzkeras.models.Sequentialc                       e Zd ZdZ fdZd fd	ZddZddZd Zd Z	d Z
dd	Zdd
Zed        Zej                  d        ZddZd Zed        Zed        Zed        Zed        Ze fd       Zd Z fdZedd       Z xZS )
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                 J    t        j                  | t        |   |             S N)typingcastsuper__new__)clsargskwargs	__class__s      K/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/models/sequential.pyr   zSequential.__new__B   s    {{3 455    c                     t         |   ||       d | _        g | _        |r+|D ]  }| j	                  |d        | j                          y y )N)	trainablenameF)rebuild)r   __init___functional_layersadd_maybe_rebuild)selflayersr   r   layerr   s        r   r!   zSequential.__init__E   sW    948 /./! r   c                    | j                   s2t        |dd      %| j                  t        |j                               t        |d      r!|j                  d   }t        |t              r|}t        |t              st        d| dt        |       d      | j                  |      st        d	|j                   d
      t        |t              r\| j                   rPt        | j                   d   t              r3t        d| j                   d| j                   d   j                   d      | j                   j                  |       |r| j                          yd| _        d| _        y)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_layers       r   r$   zSequential.addN   s]    ||u0$7C%*@*@AB 5*+ //2L,
3$%'::? A K=+ 
 ))%0338::, ?))  uj)4<<?J7$TYYK 0&&*ll1o&A&A%B C56  	E"!DJ#Dr   c                 z    | j                   j                         }d| _        d| _        |r| j	                          |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#   popr8   r"   r%   )r&   r    r(   s      r   r;   zSequential.pop   s9       "
!r   c                    d| _         d | _        t        | j                  d   t              rCt        | j                        dkD  r+| j                  d   j                  }| j                  |       y t        | j                  d   d      rDt        | j                        dkD  r+| j                  d   j                  }| j                  |       y y y )NFr      input_shape)
r8   r"   r2   r#   r   lenr6   buildr1   r>   )r&   r>   s     r   r%   zSequential._maybe_rebuild   s    
dll1oz2s4<<7H17L,,q/55KJJ{#T\\!_m4T\\9JQ9N ,,q/55KJJ{# :O4r   c                      y r    r&   s    r   _lock_statezSequential._lock_state   s    r   c                      y)Nr   rB   rC   s    r   	_obj_typezSequential._obj_type   s    r   c                    	 t        |      }| j                  st        d| j                   d      t	        | j                  d   t
              rQ| j                  d   j                  |k7  rnt        d| j                   d| j                  d   j                   d|       | j                  d   j                  }t        ||      g| j                  z   | _        | j                  d   j                  }|}| j                  dd  D ]  }	  ||      } |}
t-        ||
      | _        y #  Y y xY w# t        $ r Y  y t        $ r}t        j                  |j                        }|j                  j                         D cg c]+  }|j                   t        j"                  j$                  k(  r|- nc c}w }	}t'        |	      dk7  r%t        d	|j(                  j*                   d
|	       |d }~ww xY w)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 )r6   dtyper=   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#   r3   r   r2   r   r6   compute_dtypeoutputNotImplementedError	TypeErrorinspect	signaturecall
parametersvaluesdefault	Parameteremptyr?   r   __name__r   r"   )r&   r>   rH   rI   xr(   erP   parampositional_argsrJ   s              r   r@   zSequential.build   s   	+K8K
 ||#DII; /6 6  dll1oz2||A**k9 ( 45||A223 4((3}6  LLO11E{%@DL
 a''\\!"% 	E!H	, %VWEc	 4 '   #--ejj9	 "+!5!5!<!<!>#}}(9(9(?(?? # # #
 '1,$338??3K3K2L M>>M=NP  s5   D& D-&D*-	G.9G.;G)<0F-,=G))G.c                 t   | j                   r  | j                   j                  |f||d|S | j                  D ]w  }t        |di       D ci c]  }||v r|||    }}|j                  r||d<   |j
                  r|||d<    ||fi |}|}t        j                  t        j                  |      }y S c c}w )Ntrainingmask_call_has_context_argr_   r^   )
r"   rQ   r'   r0   _call_has_mask_arg_call_has_training_argr   map_structurer   get_keras_mask)	r&   rI   r^   r_   r   r(   klayer_kwargsrJ   s	            r   rQ   zSequential.call   s    (4##((!)8>  [[ 	GE !(?D ; 6!9L  '''+V$++0D+3Z(F3l3GF%%g&<&<gFD%	G& s   B5c                 X    | j                   }|rt        |d   t              r|dd  S |d d  S )Nr   r=   )r#   r2   r   )r&   r'   s     r   r'   zSequential.layers   s3    
 jJ7!":ayr   c                     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&   _s     r   r'   zSequential.layers  s    J
 	
r   c                     | j                   r  | j                   j                  |f||d|S | j                  D ]  } |j                  |fd|i|}|} S )Nr]   r^   )r"   compute_output_specr'   )r&   rI   r^   r_   r   r(   rJ   s          r   rl   zSequential.compute_output_spec  s    74##77!)8>  [[ 	E/e//! G
 F	 r   c                     | j                   r| j                   j                  |      S | j                  D ]  }|j                  |      }|} S r   )r"   compute_output_shaper'   )r&   r>   r(   output_shapes       r   rn   zSequential.compute_output_shape  sR    ##88EE[[ 	'E 55kBL&K	' r   c                 x    | j                   r| j                   j                  S t        d| j                   d      )Nr.   z!' has no defined input shape yet.)r"   r>   ri   r   rC   s    r   r>   zSequential.input_shape   s;    ##/// +LM
 	
r   c                 x    | j                   r| j                   j                  S t        d| j                   d      )Nr.   z"' has no defined output shape yet.)r"   ro   ri   r   rC   s    r   ro   zSequential.output_shape(  s;    ##000 +MN
 	
r   c                 x    | j                   r| j                   j                  S t        d| j                   d      )Nr.   z' has no defined inputs yet.)r"   rI   ri   r   rC   s    r   rI   zSequential.inputs0  s;    ##*** +GH
 	
r   c                 x    | j                   r| j                   j                  S t        d| j                   d      )Nr.   z' has no defined outputs yet.)r"   rJ   ri   r   rC   s    r   rJ   zSequential.outputs8  s;    ##+++ +HI
 	
r   c                 x    | j                   }|r"t        |d   t              r|d   j                  S t        |   S )Nr   )r#   r2   r   rH   r   input_dtype)r&   r'   r   s     r   ru   zSequential.input_dtype@  s7     jJ7!9??"w""r   c                 f    | j                   D ]"  }|j                  |j                  k(  s||us" y y)NFT)r#   r   )r&   r(   	ref_layers      r   r5   z Sequential._is_layer_name_uniqueI  s4     	IzzY^^+	0F	 r   c                    t         j                  }t        j                  dd      rt        j                  }g }t
        |   D ]  }|j                   ||              t        j                  |       }| j                  |d<   t        j                  |      |d<   | j                  | j                  d   j                  |d<   |S )Nuse_legacy_configFr   r'   r   build_input_shape)r   serialize_keras_objectr   get_global_attributelegacy_serializationr   r'   r7   r   
get_configr   copydeepcopyr"   r#   r6   )r&   serialize_fnlayer_configsr(   configr   s        r   r~   zSequential.get_configO  s    (??,,-@%H/FFLW^ 	6E   e!45	6 !!$'v==7x'*.,,q/*E*EF&'r   c                 x   d|v r|d   }|j                  d      }|d   }nd }|} | |      }|D ]F  }d|vrt        j                  ||      }nt        j                  ||      }|j                  |       H |j                  s5dt               v r)r't        |t        t        f      r|j                  |       |S )Nr   rz   r'   )r   module)custom_objects)getr
   model_from_configr   deserialize_keras_objectr$   r"   localsr2   tuplelistr@   )	r   r   r   r   rz   r   modellayer_configr(   s	            r   from_configzSequential.from_config`  s    V&>D &

+> ?"8,MD"M) 	L|+ %66 #1
 *BB #1 IIe	 !!#vx/!,udm<KK)*r   )NTN)Tr   )NN)rW   
__module____qualname____doc__r   r!   r$   r;   r%   rD   rF   r@   rQ   propertyr'   setterrl   rn   r>   ro   rI   rJ   ru   r5   r~   classmethodr   __classcell__)r   s   @r   r   r      s    +Z6"/$b"$4Fl:   ]]
 
 
 
 
 
 
 
 
 
 # #"  r   r   )r   rO   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   r}   keras.src.models.functionalr   keras.src.models.modelr   keras.src.savingr   r   rB   r   r   <module>r      s\         - 1 6 8 ( 0 I 2 ( . !#<=>k k ?kr   