a
    Sic{                     @   s   d Z ddlm  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dgdedG dd dejZdS )zBuilt-in linear model classes.    N)activations)initializers)regularizers)
base_layer)
input_spec)training)core)deprecation)keras_exportzkeras.experimental.LinearModelzkeras.models.LinearModel)v1c                       sH   e Zd ZdZd fdd	Zdd	 Zd
d Zdd ZedddZ	  Z
S )LinearModela*  Linear Model for regression and classification problems.

    This model approximates the following function:
    $$y = \beta + \sum_{i=1}^{N} w_{i} * x_{i}$$
    where $$\beta$$ is the bias and $$w_{i}$$ is the weight for each feature.

    Example:

    ```python
    model = LinearModel()
    model.compile(optimizer='sgd', loss='mse')
    model.fit(x, y, epochs=epochs)
    ```

    This model accepts sparse float inputs as well:

    Example:
    ```python
    model = LinearModel()
    opt = tf.keras.optimizers.Adam()
    loss_fn = tf.keras.losses.MeanSquaredError()
    with tf.GradientTape() as tape:
      output = model(sparse_input)
      loss = tf.reduce_mean(loss_fn(target, output))
    grads = tape.gradient(loss, model.weights)
    opt.apply_gradients(zip(grads, model.weights))
    ```

       NTzerosc           	         sp   || _ t|| _|| _t|| _t|| _t|| _	t|| _
t jf i | tjdd dS )a  Create a Linear Model.

        Args:
          units: Positive integer, output dimension without the batch size.
          activation: Activation function to use.
            If you don't specify anything, no activation is applied.
          use_bias: whether to calculate the bias/intercept for this model. If
            set to False, no bias/intercept will be used in calculations, e.g.,
            the data is already centered.
          kernel_initializer: Initializer for the `kernel` weights matrices.
          bias_initializer: Initializer for the bias vector.
          kernel_regularizer: regularizer for kernel vectors.
          bias_regularizer: regularizer for bias vector.
          **kwargs: The keyword arguments that are passed on to
            BaseLayer.__init__.
        LinearTN)unitsr   get
activationuse_biasr   kernel_initializerbias_initializerr   kernel_regularizerbias_regularizersuper__init__r   keras_premade_model_gaugeget_cellset)	selfr   r   r   r   r   r   r   kwargs	__class__ W/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/premade_models/linear.pyr   D   s    zLinearModel.__init__c                 C   sD  t |trtt| }g | _g | _|D ]R}|| }tj| j	d| j
| j|d}|| | jtj||d | j| q*nt |ttfrtdd |D rg | _|D ]2}tj| j	d| j
| jd}|| | j| qn*tj| j	d| j
| jd}|| |g| _| jr4| jd| j	| j| j| jdd	| _nd | _d| _d S )
NF)r   r   r   r   name)shaper#   c                 s   s   | ]}t |tjV  qd S N)
isinstancetfTensorShape).0r$   r!   r!   r"   	<genexpr>}   s   z$LinearModel.build.<locals>.<genexpr>)r   r   r   r   biasT)r$   initializerregularizerdtype	trainable)r&   dictsortedlistkeysinput_specsdense_layersr   Denser   r   r   buildappendr   	InputSpectupleallr   
add_weightr   r   r.   r+   built)r   input_shapenamesr#   r$   layerr!   r!   r"   r7   j   sd    




	zLinearModel.buildc                    s(  d }t  trdd | jD }t|t   }|r\tdt| dt   d|  fdd|D  t | jD ]&\}}||}|d u r|}qz||7 }qznRt  ttfrt | jD ]&\}}||}|d u r|}q||7 }qn| jd  }| j	rt
j|| j}| jd ur$| |S |S )Nc                 S   s   g | ]
}|j qS r!   )r#   )r)   r@   r!   r!   r"   
<listcomp>       z$LinearModel.call.<locals>.<listcomp>z\The `inputs` dictionary does not match the structure expected by the model.
	Expected keys: z
	Received keys: z
	Missing keys: c                    s   g | ]} | qS r!   r!   )r)   r#   inputsr!   r"   rA      rB   r   )r&   r0   r5   r   r3   
ValueErrorzipr:   r2   r   r'   nnbias_addr+   r   )r   rD   resultr?   Zdifferent_keysinpr@   outputr!   rC   r"   call   s@    


zLinearModel.callc              	   C   sh   | j t| j| jt| jt| jt| j	t| j
d}tj| }tt| t|  S )N)r   r   r   r   r   r   r   )r   r   	serializer   r   r   r   r   r   r   r   r   Layer
get_configr0   r2   items)r   configbase_configr!   r!   r"   rO      s    


zLinearModel.get_configc                 C   s   ~| f i |S r%   r!   )clsrQ   custom_objectsr!   r!   r"   from_config   s    zLinearModel.from_config)r   NTr   r   NN)N)__name__
__module____qualname____doc__r   r7   rL   rO   classmethodrU   __classcell__r!   r!   r   r"   r       s           &8$r   )rY   Ztensorflow.compat.v2compatv2r'   kerasr   r   r   keras.enginer   r   r   keras.layersr   tensorflow.python.utilr	    tensorflow.python.util.tf_exportr
   deprecated_endpointsModelr   r!   r!   r!   r"   <module>   s    