numpy_concatenate

在深度学习网络中,特征层都是一个多维的数组。有时候为了把经过不同大小、形状的卷积核而得到的特征层链接在一起,就需要用到numpy的concatenate。在numpy中,concatenate的功能解释是:Join a sequence of arrays along an existing axis。下面展示相关的用法。

# 将数组a, b沿着0轴进行合并,合并之后的shape应该是(2, 5, 5)

import numpy as np

a = np.random.random((1, 5, 5))

b = np.random.random((1, 5, 5))

c = np.concatenate((a, b), axis = 0)

print "a:"

print a

print "b:"

print b

print "np.concatenate((a, b), axis = 0):"

print c

c = np.concatenate((a, b), axis = 1)

print "np.concatenate((a, b), axis = 1):"

print c

c = np.concatenate((a, b), axis = 2)

print "np.concatenate((a, b), axis = 2):"

print c

上面的输出结果如下:

a:

[[[0.84073463 0.46274429 0.08248961 0.19070404 0.9974667 ]

  [0.61659658 0.37714231 0.73611954 0.1912308  0.6354842 ]

  [0.12116275 0.92568538 0.24624993 0.38109115 0.49512079]

  [0.3306727  0.89910473 0.13962387 0.36557941 0.5380739 ]

  [0.49190266 0.21509959 0.5138292  0.29321129 0.03401961]]]

b:

[[[0.81091071 0.52620968 0.09808308 0.86436183 0.46453824]

  [0.53201367 0.54038408 0.98995042 0.40034224 0.56401361]

  [0.75310483 0.65040443 0.4618217  0.68053822 0.24892278]

  [0.18428724 0.37016259 0.43493183 0.0686078  0.59134009]

  [0.26952899 0.49031205 0.56216806 0.26634649 0.37192443]]]

np.concatenate((a, b), axis = 0):

[[[0.84073463 0.46274429 0.08248961 0.19070404 0.9974667 ]

  [0.61659658 0.37714231 0.73611954 0.1912308  0.6354842 ]

  [0.12116275 0.92568538 0.24624993 0.38109115 0.49512079]

  [0.3306727  0.89910473 0.13962387 0.36557941 0.5380739 ]

  [0.49190266 0.21509959 0.5138292  0.29321129 0.03401961]]

[[0.81091071 0.52620968 0.09808308 0.86436183 0.46453824]

  [0.53201367 0.54038408 0.98995042 0.40034224 0.56401361]

  [0.75310483 0.65040443 0.4618217  0.68053822 0.24892278]

  [0.18428724 0.37016259 0.43493183 0.0686078  0.59134009]

  [0.26952899 0.49031205 0.56216806 0.26634649 0.37192443]]]

np.concatenate((a, b), axis = 1):

[[[0.84073463 0.46274429 0.08248961 0.19070404 0.9974667 ]

  [0.61659658 0.37714231 0.73611954 0.1912308  0.6354842 ]

  [0.12116275 0.92568538 0.24624993 0.38109115 0.49512079]

  [0.3306727  0.89910473 0.13962387 0.36557941 0.5380739 ]

  [0.49190266 0.21509959 0.5138292  0.29321129 0.03401961]

  [0.81091071 0.52620968 0.09808308 0.86436183 0.46453824]

  [0.53201367 0.54038408 0.98995042 0.40034224 0.56401361]

  [0.75310483 0.65040443 0.4618217  0.68053822 0.24892278]

  [0.18428724 0.37016259 0.43493183 0.0686078  0.59134009]

  [0.26952899 0.49031205 0.56216806 0.26634649 0.37192443]]]

np.concatenate((a, b), axis = 2):

[[[0.84073463 0.46274429 0.08248961 0.19070404 0.9974667  0.81091071

  0.52620968 0.09808308 0.86436183 0.46453824]

  [0.61659658 0.37714231 0.73611954 0.1912308  0.6354842  0.53201367

  0.54038408 0.98995042 0.40034224 0.56401361]

  [0.12116275 0.92568538 0.24624993 0.38109115 0.49512079 0.75310483

  0.65040443 0.4618217  0.68053822 0.24892278]

  [0.3306727  0.89910473 0.13962387 0.36557941 0.5380739  0.18428724

  0.37016259 0.43493183 0.0686078  0.59134009]

  [0.49190266 0.21509959 0.5138292  0.29321129 0.03401961 0.26952899

  0.49031205 0.56216806 0.26634649 0.37192443]]]

所以如果需要把不同卷积核的输出结果接在一起时,对于numpy数组可以采用concatenate来实现。

除此之外,numpy还有一个stack的功能,stack的解释为:Join a sequence of arrays along a new axis. The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension. 翻译过来就是将沿着现有数组的一个轴来进行融合,并新建一个轴。鉴于在特征层中,前后特征层的维度基本不会改变,所以stack基本也没有什么用。

对于mxnet这个框架,可以使用mxnet.ndarray.concat来实现相同的功能。

你可能感兴趣的:(numpy_concatenate)