Detailed explanation of concatenate() in keras

I recently looked at modal fusion and used the concatenate() function in keras. I didn’t understand what the axis parameter means before. After some research, I finally figured it out.

Look at the code first

import numpy as np
import keras.backend as K
import tensorflow as tf

a = K.variable(np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]))
b = K.variable(np.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]]))

c1 = K.concatenate([a, b], axis=0)
c2 = K.concatenate([a, b], axis=1)
c3 = K.concatenate([a, b], axis=2)
#试试默认的参数,其实就是从倒数第一个维度进行融合的。
c4 = K.concatenate([a, b])
c5 = K.concatenate([a, b],axis=-1)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print('***************')
    print(a.shape,b.shape)
    print('***************')
    print('*****C1******',c1.shape)
    print(sess.run(c1))
    print()
    print('*****C2******',c2.shape)
    print(sess.run(c2))
    print()
    print('*****C3******',c3.shape)
    print(sess.run(c3))
    print()
    print('*****C4******',c4.shape)
    print(sess.run(c4))
    print('*****C5******',c5.shape)
    print(sess.run(c5))
    

Look at the output effect:

Insert picture description here



axis=n means splicing from the nth dimension. For a three-dimensional matrix, the value of axis can be [-3, -2, -1, 0, 1, 2].
axis=-2, which means splicing from the second-to-last dimension. For a three-dimensional matrix, this is equivalent to axis=1.
axis=-1, which means splicing from the penultimate dimension. For a three-dimensional matrix, this is equivalent to axis=2.

Simply understand:

It may be more complicated to understand from the image, but if it is very simple from a mathematical point of view, like the example above

Two (2,2,2)(2,2,2) arrays are merged,

  • The first dimension fusion is (4,2,2), that is, axis=0
  • The second dimension fusion is (2,4,2), that is, axis=1
  • The third dimension fusion is (2,2,4), that is, axis=2

references

[1]https://blog.csdn.net/leviopku/article/details/82380710
[2]https://zhuanlan.zhihu.com/p/58672698

Guess you like

Origin blog.csdn.net/zhaozhao236/article/details/109434254