Skip to content

Commit

Permalink
chore: adding a wrapper to numpy sum, started fixing layer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ariG23498 committed Jul 8, 2023
1 parent 9d639cd commit 6c8293b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion keras_core/backend/numpy/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,12 @@ def var(x, axis=None, keepdims=False):

def sum(x, axis=None, keepdims=False):
axis = tuple(axis) if isinstance(axis, list) else axis
return np.sum(x, axis=axis, keepdims=keepdims)
# `np.sum()` function, when applied to an array, returns a scalar
# value that represents the sum of all the elements in the array.
# This operation reduces the dimensionality of the input and hence
# return objects of type `np.<DTYPE>`. To fix this behaviour, we
# wrap the result in a list and explicitly reutrns an `np.ndarray`.
return np.array([np.sum(x, axis=axis, keepdims=keepdims)])


def eye(N, M=None, k=0, dtype="float32"):
Expand Down
8 changes: 8 additions & 0 deletions keras_core/layers/layer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ def call(self, x):
layer(layers.Input(batch_shape=(2, 2)))
self.assertLen(layer.losses, 0)

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Trainer not implemented for NumPy backend.",
)
def test_add_loss(self):
class LossLayer(layers.Layer):
def call(self, x):
Expand Down Expand Up @@ -378,6 +382,10 @@ def test_mixed_precision(self):
self.assertEqual(backend.standardize_dtype(y.dtype), "float16")
self.assertEqual(layer.kernel.dtype, "float32")

@pytest.mark.skipif(
backend.backend() == "numpy",
reason="Numpy backend does not support masking.",
)
def test_masking(self):
class BasicMaskedLayer(layers.Layer):
def __init__(self):
Expand Down

0 comments on commit 6c8293b

Please sign in to comment.