AmazonScience/SWE-PolyBench huggingface dataset:
Repository: keras-team/keras (Python)
Date: 2016-12-27 19:00:13+00:00
Task type: Bug Fix

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]
[]
```


Issue discussion:

Solution 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():
Repository: keras-team/keras (Python)
Date: 2024-03-07 00:39:01+00:00
Task type: Testing

Issue:
Enable `integration_test/numerical_test.py` for CI
May need to add more layers to the model.


Issue discussion:

Solution patch:
diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml
--- a/.github/workflows/actions.yml
+++ b/.github/workflows/actions.yml
@@ -49,6 +49,7 @@ jobs:
         run: |
           pip install -r requirements.txt --progress-bar off --upgrade
           pip uninstall -y keras keras-nightly
+          pip install tf_keras==2.16.0rc0 --progress-bar off --upgrade
           pip install -e "." --progress-bar off --upgrade
       - name: Test applications with pytest
         if: ${{ steps.filter.outputs.applications == 'true' }}
@@ -67,6 +68,7 @@ jobs:
         if: ${{ matrix.backend != 'numpy'}}
         run: |
           python integration_tests/import_test.py
+          python integration_tests/numerical_test.py
       - name: Test TF-specific integrations
         if: ${{ matrix.backend == 'tensorflow'}}
         run: |


Test patch:
diff --git a/integration_tests/numerical_test.py b/integration_tests/numerical_test.py
--- a/integration_tests/numerical_test.py
+++ b/integration_tests/numerical_test.py
@@ -3,6 +3,9 @@
 import numpy as np
 import tf_keras
 
+keras.backend.set_image_data_format("channels_last")
+tf_keras.backend.set_image_data_format("channels_last")
+
 NUM_CLASSES = 10
 
 
@@ -41,7 +44,8 @@ def build_keras_model(keras_module, num_classes):
             keras_module.layers.Conv2D(
                 64, kernel_size=(3, 3), activation="relu"
             ),
-            keras_module.layers.BatchNormalization(scale=False, center=True),
+            # TODO: Renable the following line.
+            # keras_module.layers.BatchNormalization(scale=False, center=True),
             keras_module.layers.MaxPooling2D(pool_size=(2, 2)),
             keras_module.layers.Flatten(),
             keras_module.layers.Dense(num_classes, activation="softmax"),
Repository: keras-team/keras (Python)
Date: 2024-03-13 07:57:31+00:00
Task type: Bug Fix

Issue:
`keras.ops.softmax` errors out when used in a TensorFlow compiled function
## MRE

```python
import keras
from keras import ops

class SoftmaxLayer(keras.Layer):
  def call(self, x):
    return ops.softmax(x, axis=-1)

class Model(keras.Model):
  def __init__(self):
    x = keras.Input(shape=(None,))
    y = SoftmaxLayer()(x)
    super().__init__(inputs=x, outputs=y)

model = Model()  # Error
```

## Additional Details

The regression was introduced in [d5a4521](https://github.com/keras-team/keras/commit/d5a452155a415d5fcbe568eb2a8441f64e57aa90)

Discovered by KerasCV test run with `keras-nightly`: https://github.com/keras-team/keras-cv/actions/runs/8259807616/job/22594330565


Issue discussion:

Solution patch:
diff --git a/keras/ops/nn.py b/keras/ops/nn.py
--- a/keras/ops/nn.py
+++ b/keras/ops/nn.py
@@ -538,10 +538,13 @@ def softmax(x, axis=-1):
     array([0.09003057, 0.24472847, 0.66524096], shape=(3,), dtype=float64)
 
     """
-    if isinstance(axis, int) and backend.shape(x)[axis] == 1:
+    # Don't use `backend.shape` since TensorFlow returns
+    # symbolic tensors for unknown shape which can trigger
+    # an error in TensorFlow graph execution.
+    if isinstance(axis, int) and x.shape[axis] == 1:
         warnings.warn(
             f"You are using a softmax over axis {axis} "
-            f"of a tensor of shape {backend.shape(x)}. This axis "
+            f"of a tensor of shape {x.shape}. This axis "
             "has size 1. The softmax operation will always return "
             "the value 1, which is likely not what you intended. "
             "Did you mean to use a sigmoid instead?"


Test patch:
diff --git a/keras/ops/nn_test.py b/keras/ops/nn_test.py
--- a/keras/ops/nn_test.py
+++ b/keras/ops/nn_test.py
@@ -2,10 +2,12 @@
 import pytest
 from absl.testing import parameterized
 
+import keras
 from keras import backend
 from keras import layers
 from keras import losses
 from keras import models
+from keras import ops
 from keras import testing
 from keras.backend.common import standardize_dtype
 from keras.backend.common.keras_tensor import KerasTensor
@@ -84,6 +86,22 @@ def test_softmax(self):
         self.assertEqual(knn.softmax(x, axis=1).shape, (None, 2, 3))
         self.assertEqual(knn.softmax(x, axis=-1).shape, (None, 2, 3))
 
+    def test_softmax_in_graph(self):
+        class SoftmaxLayer(keras.Layer):
+            def call(self, x):
+                return ops.softmax(x, axis=-1)
+
+        class Model(keras.Model):
+            def __init__(self):
+                x = keras.Input(shape=(None,))
+                y = SoftmaxLayer()(x)
+                super().__init__(inputs=x, outputs=y)
+
+        # Make sure Keras is able to compile the model graph
+        model = Model()
+        x = ops.array([[1.0, 2.0, 3.0, 4.0]])
+        model.predict(x)
+
     def test_log_softmax(self):
         x = KerasTensor([None, 2, 3])
         self.assertEqual(knn.log_softmax(x).shape, (None, 2, 3))
Repository: keras-team/keras (Python)
Date: 2024-04-09 17:23:58+00:00
Task type: Bug Fix

Issue:
Numpy Ops function nonzero(x) appers to be missing check for symbolic tensors
In updating  code from Keras 2 to 3, we noticed that nonzero function continues to throw errors for use of KerasTensor in TF functions, even when run though tf.keras.ops

Digging into the source, it appears that this function does not receive the check for any_symbolic_tensors(), and thus no instantiation of the NonZero() class. In turn failing when used with a KerasTensor

https://github.com/keras-team/keras/blob/42a1535ed7d3d75711a11d295f58a2dc9a59fdae/keras/ops/numpy.py#L3976


Issue discussion:

Solution patch:
diff --git a/keras/ops/numpy.py b/keras/ops/numpy.py
--- a/keras/ops/numpy.py
+++ b/keras/ops/numpy.py
@@ -3992,6 +3992,9 @@ class Nonzero(Operation):
     def call(self, x):
         return backend.numpy.nonzero(x)
 
+    def compute_output_spec(self, x):
+        return KerasTensor([None] * len(x.shape))
+
 
 @keras_export(["keras.ops.nonzero", "keras.ops.numpy.nonzero"])
 def nonzero(x):
@@ -4003,6 +4006,8 @@ def nonzero(x):
     Returns:
         Indices of elements that are non-zero.
     """
+    if any_symbolic_tensors((x,)):
+        return Nonzero().symbolic_call(x)
     return backend.numpy.nonzero(x)
 
 


Test patch:
diff --git a/keras/ops/numpy_test.py b/keras/ops/numpy_test.py
--- a/keras/ops/numpy_test.py
+++ b/keras/ops/numpy_test.py
@@ -1311,6 +1311,10 @@ def test_ndim(self):
         x = KerasTensor((None, 3))
         self.assertEqual(knp.ndim(x).shape, (2,))
 
+    def test_nonzero(self):
+        x = KerasTensor((None, 5, 6))
+        self.assertEqual(knp.nonzero(x).shape, (None, None, None))
+
     def test_ones_like(self):
         x = KerasTensor((None, 3))
         self.assertEqual(knp.ones_like(x).shape, (None, 3))
Repository: keras-team/keras (Python)
Date: 2024-06-25 14:03:04+00:00
Task type: Bug Fix

Issue:
Unable to export reloaded model
Saving and reloading model makes it impossible to export it as a SavedModel artifact. 
Reloaded model has shapes defined as lists while export function expect tuples. 
Casting the shape to tuple in this particular place resolves the issue, but there may be other errors related to this in other places, though.

Steps to reproduce:

1) Make a subclassed model (maybe reproducible with Functional too?)
2) Save the model as `.keras`
3) Reload `.keras`
4) Try to `model.export()` on your reloaded model

Here's the notebook with the same steps for your convenience:
https://colab.research.google.com/drive/1oO4JxoYK4I4UO0VdyYPAlCQY9fT1pYlw


Issue discussion:

Solution patch:
diff --git a/keras/src/export/export_lib.py b/keras/src/export/export_lib.py
--- a/keras/src/export/export_lib.py
+++ b/keras/src/export/export_lib.py
@@ -654,13 +654,18 @@ def make_tensor_spec(structure):
         # into plain Python structures because they don't work with jax2tf/JAX.
         if isinstance(structure, dict):
             return {k: make_tensor_spec(v) for k, v in structure.items()}
-        if isinstance(structure, (list, tuple)):
+        elif isinstance(structure, tuple):
             if all(isinstance(d, (int, type(None))) for d in structure):
                 return tf.TensorSpec(
                     shape=(None,) + structure[1:], dtype=model.input_dtype
                 )
-            result = [make_tensor_spec(v) for v in structure]
-            return tuple(result) if isinstance(structure, tuple) else result
+            return tuple(make_tensor_spec(v) for v in structure)
+        elif isinstance(structure, list):
+            if all(isinstance(d, (int, type(None))) for d in structure):
+                return tf.TensorSpec(
+                    shape=[None] + structure[1:], dtype=model.input_dtype
+                )
+            return [make_tensor_spec(v) for v in structure]
         else:
             raise ValueError(
                 f"Unsupported type {type(structure)} for {structure}"


Test patch:
diff --git a/keras/src/export/export_lib_test.py b/keras/src/export/export_lib_test.py
--- a/keras/src/export/export_lib_test.py
+++ b/keras/src/export/export_lib_test.py
@@ -196,6 +196,22 @@ def call(self, inputs):
         )
         revived_model.serve(bigger_input)
 
+        # Test with keras.saving_lib
+        temp_filepath = os.path.join(
+            self.get_temp_dir(), "exported_model.keras"
+        )
+        saving_lib.save_model(model, temp_filepath)
+        revived_model = saving_lib.load_model(
+            temp_filepath,
+            {
+                "TupleModel": TupleModel,
+                "ArrayModel": ArrayModel,
+                "DictModel": DictModel,
+            },
+        )
+        self.assertAllClose(ref_output, revived_model(ref_input))
+        export_lib.export_model(revived_model, self.get_temp_dir())
+
     def test_model_with_multiple_inputs(self):
 
         class TwoInputsModel(models.Model):