Skip to content

Commit e448b6a

Browse files
committed
[LAYER] add elementwise layer for logic operation
1 parent c55e667 commit e448b6a

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

docs/modules/layers.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ Layer list
279279
FlattenLayer
280280
ConcatLayer
281281
ReshapeLayer
282+
ElementwiseLayer
282283
SlimNetsLayer
283284
PReluLayer
284285
MultiplexerLayer
@@ -432,6 +433,10 @@ Reshape layer
432433

433434
.. autoclass:: ReshapeLayer
434435

436+
Logic layer
437+
^^^^^^^^^^^^^
438+
.. autoclass:: ElementwiseLayer
439+
435440
Merge TF-Slim
436441
---------------
437442

tensorlayer/layers.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,6 +2542,57 @@ def __init__(
25422542
self.all_drop = dict(layer.all_drop)
25432543
self.all_layers.extend( [self.outputs] )
25442544

2545+
## Logic layer
2546+
class ElementwiseLayer(Layer):
2547+
"""
2548+
The :class:`ElementwiseLayer` class combines multiple :class:`Layer` which have the same output shapes by a given elemwise-wise operation.
2549+
2550+
Parameters
2551+
----------
2552+
layer : a list of :class:`Layer` instances
2553+
The `Layer` class feeding into this layer.
2554+
combine_fn : a TensorFlow elemwise-merge function
2555+
e.g. AND is ``tf.minimum`` ; OR is ``tf.maximum`` ; ADD is ``tf.add`` ; MUL is ``tf.mul`` and so on.
2556+
See `TensorFlow Math API <https://www.tensorflow.org/versions/master/api_docs/python/math_ops.html#math>`_ .
2557+
name : a string or None
2558+
An optional name to attach to this layer.
2559+
2560+
Examples
2561+
--------
2562+
- AND Logic
2563+
>>> net_0 = tl.layers.DenseLayer(net_0, n_units=500,
2564+
... act = tf.nn.relu, name='net_0')
2565+
>>> net_1 = tl.layers.DenseLayer(net_1, n_units=500,
2566+
... act = tf.nn.relu, name='net_1')
2567+
>>> net_com = tl.layers.ElementwiseLayer(layer = [net_0, net_1],
2568+
... combine_fn = tf.minimum,
2569+
... name = 'combine_layer')
2570+
"""
2571+
def __init__(
2572+
self,
2573+
layer = [],
2574+
combine_fn = tf.minimum,
2575+
name ='elementwise_layer',
2576+
):
2577+
Layer.__init__(self, name=name)
2578+
2579+
print(" tensorlayer:Instantiate ElementwiseLayer %s: %s, %s" % (self.name, layer[0].outputs._shape, combine_fn.__name__))
2580+
2581+
self.outputs = layer[0].outputs
2582+
# print(self.outputs._shape, type(self.outputs._shape))
2583+
for l in layer[1:]:
2584+
assert str(self.outputs._shape) == str(l.outputs._shape), "Hint: the input shapes should be the same. %s != %s" % (self.outputs._shape , str(l.outputs._shape))
2585+
self.outputs = combine_fn(self.outputs, l.outputs, name=name)
2586+
2587+
self.all_layers = list(layer[0].all_layers)
2588+
self.all_params = list(layer[0].all_params)
2589+
self.all_drop = dict(layer[0].all_drop)
2590+
2591+
for i in range(1, len(layer)):
2592+
self.all_layers.extend(list(layer[i].all_layers))
2593+
self.all_params.extend(list(layer[i].all_params))
2594+
self.all_drop.update(dict(layer[i].all_drop))
2595+
25452596
## TF-Slim layer
25462597
class SlimNetsLayer(Layer):
25472598
"""

0 commit comments

Comments
 (0)