
    6bic                         S r SSKJs  Jr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSK	Jr  SSK	Jr  SS	KJr  SS
KJr  SSKJr  \" SSS/S9\R&                  " S5       " S S\R(                  5      5       5       rg)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                   d   ^  \ rS rSrSr       S	U 4S jjrS rS rS r\	S
S j5       r
SrU =r$ )LinearModel    a  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))
```

c                 L  > Xl         [        R                  " U5      U l        X0l        [
        R                  " U5      U l        [
        R                  " U5      U l        [        R                  " U5      U l	        [        R                  " U5      U l
        [        T	U ]0  " S0 UD6  g)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__.
N )unitsr   get
activationuse_biasr   kernel_initializerbias_initializerr   kernel_regularizerbias_regularizersuper__init__)
selfr   r   r   r   r   r   r   kwargs	__class__s
            \/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/premade_models/linear.pyr   LinearModel.__init__D   s    8 
%//*5 "."2"23E"F , 0 01A B"."2"23E"F , 0 01A B"6"    c           	      n   [        U[        5      (       a  [        [        UR	                  5       5      5      n/ U l        / U l        U H  nX   n[        R                  " U R                  SU R                  U R                  US9nUR                  U5        U R
                  R                  [        R                  " XCS95        U R                  R                  U5        M     O[        U[         [        45      (       a  [#        S U 5       5      (       ar  / U l        U Hd  n[        R                  " U R                  SU R                  U R                  S9nUR                  U5        U R                  R                  U5        Mf     ON[        R                  " U R                  SU R                  U R                  S9nUR                  U5        U/U l        U R$                  (       aB  U R'                  SU R                  U R(                  U R*                  U R,                  SS9U l        OS U l        SU l        g )	NF)r   r   r   r   name)shaper#   c              3   V   #    U  H  n[        U[        R                  5      v   M!     g 7fN)
isinstancetfTensorShape).0r$   s     r   	<genexpr>$LinearModel.build.<locals>.<genexpr>|   s!      <
;F%Jubnn--;s   '))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   r0   r-   built)r   input_shapenamesr#   r$   layers         r   r9   LinearModel.buildi   s   k4((4 0 0 234E!D "D#)

**"'+'>'>'+'>'> E"  ''((u@ !!((/  eT]33 <
;F<
 9
 9
 !#D$

**"'+'>'>'+'>'>	 E"!!((/ % JJjj#'#:#:#'#:#:	E KK$!&D==jj 11 11jj ( DI DI
r!   c           	      R   S n[        U[        5      (       a  U R                   Vs/ s H  o3R                  PM     nn[	        U5      [	        UR                  5       5      -
  nU(       a4  [        S[	        U5       S[	        UR                  5       5       SU 35      eU Vs/ s H  oaU   PM	     nn[        XR                  5       H  u  psU" U5      nUc  UnM  X(-  nM     Ob[        U[        [        45      (       a2  [        XR                  5       H  u  psU" U5      nUc  UnM  X(-  nM     OU R                  S   " U5      nU R                  (       a)  [        R                  R                  X R                  5      nU R                  b  U R                  U5      $ U$ s  snf s  snf )Nz\The `inputs` dictionary does not match the structure expected by the model.
	Expected keys: z
	Received keys: z
	Missing keys: r   )r'   r2   r7   r#   setr5   
ValueErrorzipr<   r4   r   r(   nnbias_addr-   r   )	r   inputsresultrB   rA   different_keysr#   inpoutputs	            r   callLinearModel.call   su   fd##-1->->?->EZZ->E? Z#fkkm*<<N **-e*)#fkkm*<)=((8	:  055utTluF5!&*;*;<
s>#F$F = ..!&*;*;<
s>#F$F = &&q)&1F==UU^^FII6F??&??6**? @ 6s   F!F$c           	          U R                   [        R                  " U R                  5      U R                  [
        R                  " U R                  5      [
        R                  " U R                  5      [        R                  " U R                  5      [        R                  " U R                  5      S.n[        R                  R                  U 5      n[        [        UR!                  5       5      [        UR!                  5       5      -   5      $ )N)r   r   r   r   r   r   r   )r   r   	serializer   r   r   r   r   r   r   r   r   Layer
get_configr2   r4   items)r   configbase_configs      r   rT   LinearModel.get_config   s    ZZ%//@"."8"8''# !- 6 6t7L7L M"."8"8''# !- 6 6t7L7L M
 !&&11$7D**,-V\\^0DDEEr!   c                     AU " S0 UD6$ )Nr   r   )clsrV   custom_objectss      r   from_configLinearModel.from_config   s    }V}r!   )r   r-   r   r   r?   r7   r6   r   r   r   r   )   NTzerosr_   NNr&   )__name__
__module____qualname____firstlineno____doc__r   r9   rO   rT   classmethodr\   __static_attributes____classcell__)r   s   @r   r   r       sL    @ " ##J6p"HF"  r!   r   )rd   tensorflow.compat.v2compatv2r(   tf_keras.srcr   r   r   tf_keras.src.enginer   r   r   tf_keras.src.layersr	   tensorflow.python.utilr
    tensorflow.python.util.tf_exportr   deprecated_endpointsModelr   r   r!   r   <module>rr      sw    % ! ! $ % % * * ( $ / 9 $(*DE !!"BCt(.. t D	
tr!   