Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between concatenate and add in keras?

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?

like image 572
Farnaz Avatar asked Nov 14 '19 12:11

Farnaz


People also ask

What is concatenate in keras?

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.

Does Resnet add or concatenate?

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.

What is concatenate in CNN?

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.


1 Answers

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.

like image 145
Vivek Mehta Avatar answered Nov 10 '22 22:11

Vivek Mehta