forked from keras-team/keras-cv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more merging layers (keras-team#71)
* add base merge layer * format docstrings * add layer * add test cases for layer * Add import for layer * fix build function * add dynamic and static tests * fix pytest import * fix pytest decorator * remove batch size from dynamic shape test * fix keras reference * refactor test class * fix tf tests, and linting issues * add subtract layer * add tests for subtract layer * fix linting issues * add average layer * add maximum layer * dd minimum layer * add multiply layer * add tests for average, minimum, maximum, and multiply layers
- Loading branch information
1 parent
5362229
commit e8fdf09
Showing
6 changed files
with
650 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from keras_core.api_export import keras_core_export | ||
from keras_core.layers.merging.base_merge import Merge | ||
|
||
|
||
@keras_core_export("keras_core.layers.Average") | ||
class Average(Merge): | ||
"""Averages a list of inputs element-wise.. | ||
It takes as input a list of tensors, all of the same shape, | ||
and returns a single tensor (also of the same shape). | ||
Examples: | ||
>>> input_shape = (2, 3, 4) | ||
>>> x1 = np.random.rand(*input_shape) | ||
>>> x2 = np.random.rand(*input_shape) | ||
>>> y = keras_core.layers.Average()([x1, x2]) | ||
Usage in a Keras model: | ||
>>> input1 = keras_core.layers.Input(shape=(16,)) | ||
>>> x1 = keras_core.layers.Dense(8, activation='relu')(input1) | ||
>>> input2 = keras_core.layers.Input(shape=(32,)) | ||
>>> x2 = keras_core.layers.Dense(8, activation='relu')(input2) | ||
>>> # equivalent to `y = keras_core.layers.average([x1, x2])` | ||
>>> y = keras_core.layers.Average()([x1, x2]) | ||
>>> out = keras_core.layers.Dense(4)(y) | ||
>>> model = keras_core.models.Model(inputs=[input1, input2], outputs=out) | ||
""" | ||
|
||
def _merge_function(self, inputs): | ||
output = inputs[0] | ||
for i in range(1, len(inputs)): | ||
output += inputs[i] | ||
return output / len(inputs) | ||
|
||
|
||
@keras_core_export("keras_core.layers.average") | ||
def average(inputs, **kwargs): | ||
"""Functional interface to the `keras_core.layers.Average` layer. | ||
Args: | ||
inputs: A list of input tensors , all of the same shape. | ||
**kwargs: Standard layer keyword arguments. | ||
Returns: | ||
A tensor as the element-wise product of the inputs with the same | ||
shape as the inputs. | ||
Examples: | ||
>>> input_shape = (2, 3, 4) | ||
>>> x1 = np.random.rand(*input_shape) | ||
>>> x2 = np.random.rand(*input_shape) | ||
>>> y = keras_core.layers.average([x1, x2]) | ||
Usage in a Keras model: | ||
>>> input1 = keras_core.layers.Input(shape=(16,)) | ||
>>> x1 = keras_core.layers.Dense(8, activation='relu')(input1) | ||
>>> input2 = keras_core.layers.Input(shape=(32,)) | ||
>>> x2 = keras_core.layers.Dense(8, activation='relu')(input2) | ||
>>> y = keras_core.layers.average([x1, x2]) | ||
>>> out = keras_core.layers.Dense(4)(y) | ||
>>> model = keras_core.models.Model(inputs=[input1, input2], outputs=out) | ||
""" | ||
return Average(**kwargs)(inputs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from keras_core import operations as ops | ||
from keras_core.api_export import keras_core_export | ||
from keras_core.layers.merging.base_merge import Merge | ||
|
||
|
||
@keras_core_export("keras_core.layers.Maximum") | ||
class Maximum(Merge): | ||
"""Computes element-wise maximum on 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). | ||
Examples: | ||
>>> input_shape = (2, 3, 4) | ||
>>> x1 = np.random.rand(*input_shape) | ||
>>> x2 = np.random.rand(*input_shape) | ||
>>> y = keras_core.layers.Maximum()([x1, x2]) | ||
Usage in a Keras model: | ||
>>> input1 = keras_core.layers.Input(shape=(16,)) | ||
>>> x1 = keras_core.layers.Dense(8, activation='relu')(input1) | ||
>>> input2 = keras_core.layers.Input(shape=(32,)) | ||
>>> x2 = keras_core.layers.Dense(8, activation='relu')(input2) | ||
>>> # equivalent to `y = keras_core.layers.maximum([x1, x2])` | ||
>>> y = keras_core.layers.Maximum()([x1, x2]) | ||
>>> out = keras_core.layers.Dense(4)(y) | ||
>>> model = keras_core.models.Model(inputs=[input1, input2], outputs=out) | ||
""" | ||
|
||
def _merge_function(self, inputs): | ||
output = inputs[0] | ||
for i in range(1, len(inputs)): | ||
output = ops.maximum(output, inputs[i]) | ||
return output | ||
|
||
|
||
@keras_core_export("keras_core.layers.maximum") | ||
def maximum(inputs, **kwargs): | ||
"""Functional interface to the `keras_core.layers.Maximum` layer. | ||
Args: | ||
inputs: A list of input tensors , all of the same shape. | ||
**kwargs: Standard layer keyword arguments. | ||
Returns: | ||
A tensor as the element-wise product of the inputs with the same | ||
shape as the inputs. | ||
Examples: | ||
>>> input_shape = (2, 3, 4) | ||
>>> x1 = np.random.rand(*input_shape) | ||
>>> x2 = np.random.rand(*input_shape) | ||
>>> y = keras_core.layers.maximum([x1, x2]) | ||
Usage in a Keras model: | ||
>>> input1 = keras_core.layers.Input(shape=(16,)) | ||
>>> x1 = keras_core.layers.Dense(8, activation='relu')(input1) | ||
>>> input2 = keras_core.layers.Input(shape=(32,)) | ||
>>> x2 = keras_core.layers.Dense(8, activation='relu')(input2) | ||
>>> y = keras_core.layers.maximum([x1, x2]) | ||
>>> out = keras_core.layers.Dense(4)(y) | ||
>>> model = keras_core.models.Model(inputs=[input1, input2], outputs=out) | ||
""" | ||
return Maximum(**kwargs)(inputs) |
Oops, something went wrong.