Repository: keras-team/keras (Python)
Task: Bug Fix (2016-12-27 19:00:13+00:00)

Issue:
Layer regularizers are not shared across models in 1.2.0
If I share a layer with regularizers with another model, the regularizers are not copied correctly. Reusing keras test for regularizers:

```{python}
from keras.models import *
model = Sequential()
model.add(wrappers.TimeDistributed(core.Dense(2, W_regularizer='l1'), input_shape=(3, 4)))
model.add(core.Activation('relu'))
model.compile(optimizer='rmsprop', loss='mse')
print model.losses

x = Input(batch_shape=(None, 3, 4))
x1 = model.layers[0](x)
x2 = model.layers[1](x1)
m2 = Model(input=x, output=x2)
m2.compile(optimizer='rmsprop', loss='mse')

print m2.losses
```

prints:

```
[Elemwise{add,no_inplace}.0]
[]
```


Discussion:

Patch:
diff --git a/keras/engine/topology.py b/keras/engine/topology.py
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -927,7 +927,10 @@ def add_update(self, updates, inputs=None):
     def get_updates_for(self, inputs):
         if not hasattr(self, '_per_input_updates'):
             return []
-        inputs_hash = object_list_uid(inputs)
+        if inputs is not None:
+            inputs_hash = object_list_uid(inputs)
+        else:
+            inputs_hash = None
         if inputs_hash in self._per_input_updates:
             return self._per_input_updates[inputs_hash]
         return []
@@ -935,7 +938,10 @@ def get_updates_for(self, inputs):
     def get_losses_for(self, inputs):
         if not hasattr(self, '_per_input_losses'):
             return []
-        inputs_hash = object_list_uid(inputs)
+        if inputs is not None:
+            inputs_hash = object_list_uid(inputs)
+        else:
+            inputs_hash = None
         if inputs_hash in self._per_input_losses:
             return self._per_input_losses[inputs_hash]
         return []


Test patch:
diff --git a/tests/keras/engine/test_topology.py b/tests/keras/engine/test_topology.py
--- a/tests/keras/engine/test_topology.py
+++ b/tests/keras/engine/test_topology.py
@@ -9,6 +9,27 @@
 from keras.models import model_from_json, model_from_yaml
 from keras.utils.test_utils import keras_test
 
+@keras_test
+def test_get_updates_for():
+    a = Input(shape=(2,))
+    dense_layer = Dense(1)
+    dense_layer.add_update(0, inputs=a)
+    dense_layer.add_update(1, inputs=None)
+
+    assert dense_layer.get_updates_for(a) == [0]
+    assert dense_layer.get_updates_for(None) == [1]
+
+
+@keras_test
+def test_get_losses_for():
+    a = Input(shape=(2,))
+    dense_layer = Dense(1)
+    dense_layer.add_loss(0, inputs=a)
+    dense_layer.add_loss(1, inputs=None)
+    
+    assert dense_layer.get_losses_for(a) == [0]
+    assert dense_layer.get_losses_for(None) == [1]
+
 
 @keras_test
 def test_trainable_weights():