Skip to content

Commit

Permalink
Add minimum merge layer (keras-team#7758)
Browse files Browse the repository at this point in the history
  • Loading branch information
HelgeS authored and fchollet committed Sep 6, 2017
1 parent 48b8676 commit 813ce43
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
28 changes: 28 additions & 0 deletions keras/layers/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,21 @@ def _merge_function(self, inputs):
return output


class Minimum(_Merge):
"""Layer that computes the minimum (element-wise) a list of inputs.
It takes as input a list of tensors,
all of the same shape, and returns
a single tensor (also of the same shape).
"""

def _merge_function(self, inputs):
output = inputs[0]
for i in range(1, len(inputs)):
output = K.minimum(output, inputs[i])
return output


class Concatenate(_Merge):
"""Layer that concatenates a list of inputs.
Expand Down Expand Up @@ -586,6 +601,19 @@ def maximum(inputs, **kwargs):
return Maximum(**kwargs)(inputs)


def minimum(inputs, **kwargs):
"""Functional interface to the `Minimum` layer.
# Arguments
inputs: A list of input tensors (at least 2).
**kwargs: Standard layer keyword arguments.
# Returns
A tensor, the element-wise minimum of the inputs.
"""
return Minimum(**kwargs)(inputs)


def concatenate(inputs, axis=-1, **kwargs):
"""Functional interface to the `Concatenate` layer.
Expand Down
19 changes: 19 additions & 0 deletions tests/keras/layers/merge_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ def test_merge_maximum():
assert_allclose(out, np.maximum(x1, x2), atol=1e-4)


@keras_test
def test_merge_minimum():
i1 = layers.Input(shape=(4, 5))
i2 = layers.Input(shape=(4, 5))
o = layers.minimum([i1, i2])
assert o._keras_shape == (None, 4, 5)
model = models.Model([i1, i2], o)

max_layer = layers.Minimum()
o2 = max_layer([i1, i2])
assert max_layer.output_shape == (None, 4, 5)

x1 = np.random.random((2, 4, 5))
x2 = np.random.random((2, 4, 5))
out = model.predict([x1, x2])
assert out.shape == (2, 4, 5)
assert_allclose(out, np.minimum(x1, x2), atol=1e-4)


@keras_test
def test_merge_concatenate():
i1 = layers.Input(shape=(None, 5))
Expand Down

0 comments on commit 813ce43

Please sign in to comment.