B
    )`B                 @   s   d 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G dd dejZdS )zBuilt-in linear model classes.    )absolute_import)division)print_function)activations)initializers)regularizers)
base_layer)training)core)nn)keras_exportzkeras.experimental.LinearModelc                   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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superr   __init__r   Z_keras_model_gaugeZget_cellset)	selfr   r   r   r   r   r   r   kwargs)	__class__ U/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow/python/keras/premade/linear.pyr   ?   s    zLinearModel.__init__c             C   s   g | _ t|ttfrJxZ|D ]*}tj| jd| j| j|d}| j 	| qW n&tj| jd| j| j|d}| j 	| | j
r| jd| j| j| j| jdd| _nd | _d S )NF)r   r   r   r   input_shapebiasT)shapeZinitializerZregularizerdtypeZ	trainable)dense_layers
isinstancetuplelistr
   ZDenser   r   r   appendr   Z
add_weightr   r   r#   r!   )r   r    r"   layerr   r   r   buildb   s4    
zLinearModel.buildc             C   s   t |ttfs|g}t|t| jkr@tdt| jt|d }x6t|| jD ]&\}}||}|d krp|}qR||7 }qRW | jrt	
|| j}| jd k	r| |S |S )Nz%Expected {} inputs, but got {} inputs)r%   r&   r'   lenr$   
ValueErrorformatzipr   r   Zbias_addr!   r   )r   inputsresultinpr)   outputr   r   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   ZLayer
get_configdictr'   items)r   configZbase_configr   r   r   r5      s    



zLinearModel.get_configc             C   s   ~| f |S )Nr   )clsr8   Zcustom_objectsr   r   r   from_config   s    zLinearModel.from_config)r   NTr   r   NN)N)__name__
__module____qualname____doc__r   r*   r3   r5   classmethodr:   __classcell__r   r   )r   r   r      s         r   N)r>   
__future__r   r   r   Ztensorflow.python.kerasr   r   r   Ztensorflow.python.keras.enginer   r	   Ztensorflow.python.keras.layersr
   Ztensorflow.python.opsr   Z tensorflow.python.util.tf_exportr   ZModelr   r   r   r   r   <module>   s   