I would like to add skip connections for my inner layers of a fully convolutional network in keras, there is a keras.layers.Add option and there is a keras.layers.concatenate option.
What is the difference? and which one I should use?
Concatenate class Layer that concatenates a list of inputs. It takes as input a list of tensors, all of the same shape except for the concatenation axis, and returns a single tensor that is the concatenation of all inputs.
The primary difference between ResNets and DenseNets is that DenseNets concatenates the output feature maps of the layer with the next layer rather than a summation. The idea behind the concatenation is to use features that are learned from earlier layers in deeper layers as well.
A concatenation operation is just a stacking operation. For example, the x1 layer has 256 channels, and the x2 layer has 256 channels. If we are concatenating these two layers channel-wise then the output of concatenation will have 512 channels.
What is the difference?
Add layer adds two input tensor while concatenate appends two tensors. You can refer this documentation for more info.
Example:
import keras
import tensorflow as tf
import keras.backend as K
a = tf.constant([1,2,3])
b = tf.constant([4,5,6])
add = keras.layers.Add()
print(K.eval(add([a,b])))
#output: [5 7 9]
concat = keras.layers.Concatenate()
print(K.eval(concat([a,b])))
#output: array([1, 2, 3, 4, 5, 6], dtype=int32)
which one I should use?
You can use Add for skip connections.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With