o
    i 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__.
        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__r   N/var/www/myenv/lib/python3.10/site-packages/keras/src/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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   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)   r@   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"   rA      s    r   )r&   r0   r5   r   r3   
ValueErrorzipr:   r2   r   r'   nnbias_addr+   r   )r   rC   resultr?   different_keysinpr@   outputr   rB   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   Layer
get_configr0   r2   items)r   configbase_configr   r   r"   rO      s   


zLinearModel.get_configc                 C   s   ~| di |S )Nr   r   )clsrQ   custom_objectsr   r   r"   from_config   s   zLinearModel.from_config)r   NTr   r   NNr%   )__name__
__module____qualname____doc__r   r7   rL   rO   classmethodrU   __classcell__r   r   r    r"   r       s     &8$r   )rY   tensorflow.compat.v2compatv2r'   	keras.srcr   r   r   keras.src.enginer   r   r   keras.src.layersr   tensorflow.python.utilr	    tensorflow.python.util.tf_exportr
   deprecated_endpointsModelr   r   r   r   r"   <module>   s"   