B
    «»ˆd{  ã               @   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)Z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       	         sl   || _ t |¡| _|| _t |¡| _t |¡| _t |¡| _	t |¡| _
tƒ jf |Ž 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__© úM/var/www/html/venv/lib/python3.7/site-packages/keras/premade_models/linear.pyr   D   s    zLinearModel.__init__c             C   sL  t |tƒr„tt| ¡ ƒƒ}g | _g | _xê|D ]R}|| }tj| j	d| j
| j|d}| |¡ | j tj||d¡ | j |¡ q,W nŽt |ttfƒrètdd„ |D ƒƒrèg | _xf|D ]2}tj| j	d| j
| jd}| |¡ | j |¡ q°W n*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   sX    







zLinearModel.buildc                s0  d }t ˆ tƒr¨dd„ | jD ƒ}t|ƒtˆ  ¡ ƒ }|r\tdt|ƒ› dtˆ  ¡ ƒ› d|› ƒ‚‡ fdd„|D ƒ‰ xŽtˆ | jƒD ]&\}}||ƒ}|d krš|}q|||7 }q|W nVt ˆ ttfƒrðxFtˆ | jƒD ]&\}}||ƒ}|d krâ|}qÄ||7 }qÄW n| jd ˆ ƒ}| j	rt
j || j¡}| jd k	r,|  |¡S |S )Nc             S   s   g | ]
}|j ‘qS r   )r   )r"   r0   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   r1   ¯   s    r   )r    r&   r*   r   r)   Ú
ValueErrorÚzipr-   r(   r   r!   ÚnnZbias_addr$   r   )r   r2   Úresultr/   Zdifferent_keysZinpr0   Úoutputr   )r2   r   Úcall¢   s2    
$
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   ~| f |ŽS )Nr   )Úclsr<   Zcustom_objectsr   r   r   Úfrom_config×   s    zLinearModel.from_config)r   NTr   r   NN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r+   r8   r:   Úclassmethodr>   Ú__classcell__r   r   )r   r   r       s   "      8$r   )rB   Ztensorflow.compat.v2ÚcompatZ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   
