o
    Ne{                     @   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                       sV   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d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__.
        ZLinearTN )unitsr   get
activationuse_biasr   kernel_initializerbias_initializerr   kernel_regularizerbias_regularizersuper__init__r   Zkeras_premade_model_gaugeZget_cellset)	selfr   r   r   r   r   r   r   kwargs	__class__r   KD:\Projects\ConvertPro\env\Lib\site-packages\keras/premade_models/linear.pyr   D   s   zLinearModel.__init__c                 C   sB  t |tr@tt| }g | _g | _|D ])}|| }tj| j	d| j
| j|d}|| | jtj||d | j| qnEt |ttfrptdd |D rpg | _|D ]}tj| j	d| j
| jd}|| | j| qUntj| j	d| j
| jd}|| |g| _| jr| 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tfZTensorShape).0r!   r   r   r   	<genexpr>}   s    
z$LinearModel.build.<locals>.<genexpr>)r   r   r   r   biasT)r!   ZinitializerZregularizerdtypeZ	trainable)r#   dictsortedlistkeysZinput_specsdense_layersr   ZDenser   r   r   buildappendr   Z	InputSpectupleallr   Z
add_weightr   r   r(   r'   Zbuilt)r   Zinput_shapenamesr    r!   layerr   r   r   r.   j   sh   




	
zLinearModel.buildc                    s$  d }t  trRdd | jD }t|t   }|r.tdt| dt   d|  fdd|D  t | jD ]\}}||}|d u rL|}q=||7 }q=n)t  ttfrtt | jD ]\}}||}|d u rn|}q_||7 }q_n| jd  }| j	rt
j|| j}| jd ur| |S |S )Nc                 S   s   g | ]}|j qS r   )r    )r%   r3   r   r   r   
<listcomp>   s    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   r4      s    r   )r#   r)   r-   r   r,   
ValueErrorzipr0   r+   r   r$   nnZbias_addr'   r   )r   r6   resultr2   Zdifferent_keysinpr3   outputr   r5   r   call   sD   





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   ZLayer
get_configr)   r+   items)r   configZbase_configr   r   r   r?      s   


zLinearModel.get_configc                 C   s   ~| di |S )Nr   r   )clsrA   Zcustom_objectsr   r   r   from_config   s   zLinearModel.from_config)r   NTr   r   NNr"   )__name__
__module____qualname____doc__r   r.   r=   r?   classmethodrC   __classcell__r   r   r   r   r       s     &8$r   )rG   Ztensorflow.compat.v2compatv2r$   Zkerasr   r   r   Zkeras.enginer   r   r   Zkeras.layersr   Ztensorflow.python.utilr	   Z tensorflow.python.util.tf_exportr
   Zdeprecated_endpointsZModelr   r   r   r   r   <module>   s"   