title: How to include kernel constraints in Tensorflow eager conv2D?
body: <p>I'm having trouble using <code>kernel_constraint=maxnorm(3)</code> within keras when using Tensorflow eager execution. This works fine when not using the standard <code>Sequential</code> method outside of eager execution, but seems to fail with an error here (it seems to be because of a multiplication step <code>*=</code> which I don't know if there is a substitute for in this context). </p>
  
  <p><strong>Question:</strong> Is there a way to incorporate the maximum $L^2$ norm functionality within the Eager Tensorflow execution framework? Below are more details.</p>
  
  <p>Here is how I activate <code>tensorflow</code> eager. </p>
  
  
  
  <pre class="lang-python prettyprint-override"><code>from __future__ import absolute_import, division, print_function
  import tensorflow as tf
  import tensorflow.contrib.eager as tfe
  from keras.datasets import cifar10
  tf.enable_eager_execution()
  </code></pre>
  
  <p>The following code works fine</p>
  
  <p><strong>Works:</strong></p>
  
  <pre class="lang-python prettyprint-override"><code>class ObjectDet(tf.keras.Model):
      def __init__(self):
          super(ObjectDet,self).__init__()
          self.layer1= tf.keras.layers.Conv2D(32, (3, 3), input_shape=(32,32,3), padding='same', activation='relu')
          self.layer2=tf.keras.layers.Dropout(0.2)
          self.layer3=tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')
          self.layer4=tf.keras.layers.MaxPooling2D(pool_size=(2,2))
          self.layer5=tf.keras.layers.Flatten()
          self.layer6=tf.keras.layers.Dense(512, activation='relu')
          self.layer7=tf.keras.layers.Dropout(0.1)
          self.layer8=tf.keras.layers.Dense(10, activation='softmax')
  
      def call(self, input):
          """Run the model."""
          result = self.layer1(input)
          result = self.layer2(result)
          result = self.layer3(result)
          result = self.layer4(result)
          result = self.layer5(result)
          result = self.layer6(result)
          result = self.layer7(result)
          result = self.layer8(result)
  
         return result
  
  
  
  def loss(model, x, y):
    prediction = model(x)
    return cross_entropy(prediction,y)
  
  def grad(model, inputs, targets):
    with tf.GradientTape() as tape:
      loss_value = loss(model, inputs, targets)
    return tape.gradient(loss_value, model.variables)
  
  
  x, y = iter(train_ds).next()
  print("Initial loss: {:.3f}".format(loss(model, x, y)))
  
  # Training loop
  for (i, (x, y)) in enumerate(train_ds):
    # Calculate derivatives of the input function with respect to its parameters.
    grads = grad(model, x, y)
    # Apply the gradient to the model
  
    optimizer.apply_gradients(zip(grads, model.variables),
                              global_step=tf.train.get_or_create_global_step())
    if i % 200 == 0:
      pass
      print("Loss at step {:04d}: {:.3f}".format(i, loss(model, x, y)))
  </code></pre>
  
  <p><strong>Does not work:</strong></p>
  
  <p>If I replace </p>
  
  <pre class="lang-python prettyprint-override"><code>self.layer1= tf.keras.layers.Conv2D(32, (3, 3), input_shape=(32,32,3), padding='same', activation='relu')
  </code></pre>
  
  <p>with</p>
  
  <pre class="lang-python prettyprint-override"><code>self.layer1= tf.keras.layers.Conv2D(32, (3, 3), input_shape=(32,32,3), padding='same', activation='relu',kernel_constraint=maxnorm(3))
  </code></pre>
  
  <p>I obtain the error:</p>
  
  <pre class="lang-python prettyprint-override"><code>RuntimeErrorTraceback (most recent call last)
  &lt;ipython-input-74-629273c4a534&gt; in &lt;module&gt;()
       19 
       20   optimizer.apply_gradients(zip(grads, model.variables),
  ---&gt; 21                             global_step=tf.train.get_or_create_global_step())
       22   if i % 200 == 0:
       23     pass
  
  /home/dgoldma1/.local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.pyc in apply_gradients(self, grads_and_vars, global_step, name)
      615           scope_name = var.op.name
      616         with ops.name_scope("update_" + scope_name), ops.colocate_with(var):
  --&gt; 617           update_ops.append(processor.update_op(self, grad))
      618       if global_step is None:
      619         apply_updates = self._finish(update_ops, name)
  
  /home/dgoldma1/.local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.pyc in update_op(self, optimizer, g)
      166     if self._v.constraint is not None:
      167       with ops.control_dependencies([update_op]):
  --&gt; 168         return self._v.assign(self._v.constraint(self._v))
      169     else:
      170       return update_op
  
  /home/dgoldma1/.local/lib/python2.7/site-packages/keras/constraints.pyc in __call__(self, w)
       51         norms = K.sqrt(K.sum(K.square(w), axis=self.axis, keepdims=True))
       52         desired = K.clip(norms, 0, self.max_value)
  ---&gt; 53         w *= (desired / (K.epsilon() + norms))
       54         return w
       55 
  
  /home/dgoldma1/.local/lib/python2.7/site-packages/tensorflow/python/ops/resource_variable_ops.pyc in __imul__(self, unused_other)
      931 
      932   def __imul__(self, unused_other):
  --&gt; 933     raise RuntimeError("Variable *= value not supported. Use "
      934                        "variable.assign_mul(value) to modify the variable "
      935                        "value and variable = variable * value to get a new "
  
  RuntimeError: Variable *= value not supported. Use variable.assign_mul(value) to modify the variable value and variable = variable * value to get a new Tensor object.
  </code></pre>
  
  <p>Thanks!</p>