A layer that combines multiple :class:`Layer` that have the same output shapes according to an element-wise operation. If the element-wise operation is complicated, please consider to use :class:`ElementwiseLambda`. Parameters ---------- combine_fn : a TensorFlow element-wise co
| 73 | |
| 74 | |
| 75 | class Elementwise(Layer): |
| 76 | """A layer that combines multiple :class:`Layer` that have the same output shapes |
| 77 | according to an element-wise operation. |
| 78 | If the element-wise operation is complicated, please consider to use :class:`ElementwiseLambda`. |
| 79 | |
| 80 | Parameters |
| 81 | ---------- |
| 82 | combine_fn : a TensorFlow element-wise combine function |
| 83 | e.g. AND is ``tf.minimum`` ; OR is ``tf.maximum`` ; ADD is ``tf.add`` ; MUL is ``tf.multiply`` and so on. |
| 84 | See `TensorFlow Math API <https://www.tensorflow.org/versions/master/api_docs/python/math_ops.html#math>`__ . |
| 85 | If the combine function is more complicated, please consider to use :class:`ElementwiseLambda`. |
| 86 | act : activation function |
| 87 | The activation function of this layer. |
| 88 | name : None or str |
| 89 | A unique layer name. |
| 90 | |
| 91 | Examples |
| 92 | -------- |
| 93 | >>> class CustomModel(tl.models.Model): |
| 94 | >>> def __init__(self): |
| 95 | >>> super(CustomModel, self).__init__(name="custom") |
| 96 | >>> self.dense1 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu1_1') |
| 97 | >>> self.dense2 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu2_1') |
| 98 | >>> self.element = tl.layers.Elementwise(combine_fn=tf.minimum, name='minimum', act=tf.identity) |
| 99 | |
| 100 | >>> def forward(self, inputs): |
| 101 | >>> d1 = self.dense1(inputs) |
| 102 | >>> d2 = self.dense2(inputs) |
| 103 | >>> outputs = self.element([d1, d2]) |
| 104 | >>> return outputs |
| 105 | """ |
| 106 | |
| 107 | def __init__( |
| 108 | self, |
| 109 | combine_fn=tf.minimum, |
| 110 | act=None, |
| 111 | name=None, #'elementwise', |
| 112 | ): |
| 113 | |
| 114 | super(Elementwise, self).__init__(name, act=act) |
| 115 | self.combine_fn = combine_fn |
| 116 | |
| 117 | self.build(None) |
| 118 | self._built = True |
| 119 | |
| 120 | logging.info( |
| 121 | "Elementwise %s: fn: %s act: %s" % |
| 122 | (self.name, combine_fn.__name__, ('No Activation' if self.act is None else self.act.__name__)) |
| 123 | ) |
| 124 | |
| 125 | def __repr__(self): |
| 126 | actstr = self.act.__name__ if self.act is not None else 'No Activation' |
| 127 | s = ('{classname}(combine_fn={combine_fn}, ' + actstr) |
| 128 | if self.name is not None: |
| 129 | s += ', name=\'{name}\'' |
| 130 | s += ')' |
| 131 | return s.format(classname=self.__class__.__name__, **self.__dict__) |
| 132 |
no outgoing calls
no test coverage detected
searching dependent graphs…