我们在写keras的代码的时候会遇见下面的类似场景:
atten_mul=merge([image_embedding,attention_probs],mode='dot',dot_axes=(1,1))
然后会经常困惑dot_axes那个括号里面的是啥意思,这里举个例子,如果一个张量的大小为:
(19,19,5,80)
axis = 0 - 19 elements
axis = 1 - 19 elements
axis = 2 - 5 elements
axis = 3 - 80 elements
其实就是从左到右的顺序,而python numpy里面的axis:
通过不同的axis,numpy会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0,则沿着纵轴进行操作;axis=1,则沿着横轴进行操作。但这只是简单的二位数组,如果是多维的呢?可以总结为一句话:设axis=i,则numpy沿着第i个下标变化的放下进行操作。
>>> import numpy as np
>>> data=np.array([
... [1,2,1],
... [1,4,5],
... [2,3,4],
... [5,2,4]])
>>> np.sum(data,axis=0)
array([ 9, 11, 14])
>>> np.sum(data,axis=1)
array([ 4, 10, 9, 11])
上面是二维的,我们来看三维的例子:
>>> data = np.random.randint(0, 5, [4,3,2])
>>> data.shape
(4, 3, 2)
>>> data
array([[[4, 3],
[3, 2],
[2, 3]],
[[1, 3],
[2, 2],
[1, 0]],
[[4, 4],
[4, 4],
[2, 2]],
[[4, 2],
[4, 4],
[1, 1]]])
>>> data.sum(axis=0)
array([[13, 12],
[13, 12],
[ 6, 6]])
>>> data.sum(axis=1)
array([[ 9, 8],
[ 4, 5],
[10, 10],
[ 9, 7]])
>>> data.sum(axis=2)
array([[7, 5, 5],
[4, 4, 1],
[8, 8, 4],
[6, 8, 2]])
[1].numpy模块之axis.https://blog.csdn.net/fangjian1204/article/details/53055219
[2].What is the meaning of axis=-1 in keras.argmax?.
https://stackoverflow.com/questions/47435526/what-is-the-meaning-of-axis-1-in-keras-argmax