title: Implementing an l2 loss into a tensorflow Sequential regression model
body: <p>I created a keras- tensorflow model, much influenced by 
  <a href="https://keras.io/getting-started/sequential-model-guide/" rel="nofollow noreferrer">this guide </a>
  which looks like</p>
  
  <pre><code>import tensorflow as tf
  from tensorflow import keras
  from tensorflow.keras import layers
  import time 
  import numpy as np
  import sys
  from keras import losses
  
  
  model = keras.Sequential()
  model.add(layers.Dense(nodes,activation = tf.keras.activations.relu, input_shape=[len(data_initial.keys())]))
  model.add(layers.Dense(64,activation = tf.keras.activations.relu))
  model.add(layers.Dropout(0.1, noise_shape=None))
  model.add(layers.Dense(1))
  
  model.compile(loss='mse',    # &lt;-------- Here we define the loss function 
                optimizer=tf.keras.optimizers.Adam(lr= 0.01,
                                                  beta_1 = 0.01,
                                                  beta_2 = 0.001,
                                                  epsilon= 0.03),
                                                  metrics=['mae', 'mse'])
  model.fit(train_data,train_labels,epochs = 200)
  </code></pre>
  
  <p>It is a regression model and instead of the <strong>loss = 'mse'</strong> I would like to use 
  <a href="https://www.tensorflow.org/api_docs/python/tf/keras/losses/MSE" rel="nofollow noreferrer">tf keras mse loss</a> together with an <strong>L2 regularization term</strong>. The question is</p>
  
  <ul>
  <li><p>How can I add a predefined regularizer function (I think, it is <a href="https://www.tensorflow.org/api_docs/python/tf/nn/l2_loss" rel="nofollow noreferrer">this one</a> ) into the <strong>model.compile</strong> statement. </p></li>
  <li><p>How can I write a completely custom loss function and add it to <strong>model.compile</strong>.</p></li>
  </ul>