# 如何理解Axis？

## 一、理解axis

• axis=0将最开外头的括号去除，看成一个整体，在这个整体上进行运算
• axis=1将第二个括号去除，看成一个整体，在这个整体上进行运算
• ...依次类推

### 1.1二维数组之concat


def learn_concat():

# 二维数组
t1 = tf.constant([[1, 2, 3], [4, 5, 6]])
t2 = tf.constant([[7, 8, 9], [10, 11, 12]])

with tf.Session() as sess:

# 二维数组针对 axis 为0 和 1 的情况
print(sess.run(tf.concat([t1, t2], 0)))
print(sess.run(tf.concat([t1, t2], 1)))



ok，下面以图示的方式来说明。现在我们有两个数组，分别是t1和t2：


[
[1 2 3],
[4 5 6],
[7 8 9],
[10 11 12]
]




[
[1, 2, 3, 7, 8, 9]
[4, 5, 6, 10, 11, 12]
]



### 1.2三维数组之concat


def learn_concat():

# 三维数组
t3 = tf.constant([[[1, 2], [2, 3]], [[4, 4], [5, 3]]])
t4 = tf.constant([[[7, 4], [8, 4]], [[2, 10], [15, 11]]])

with tf.Session() as sess:

# 三维数组针对 axis 为0 和 1 和 -1 的情况
print(sess.run(tf.concat([t3, t4], 0)))
print(sess.run(tf.concat([t3, t4], 1)))
print(sess.run(tf.concat([t3, t4], -1)))



ok，下面也以图示的方式来说明。现在我们有两个数组，分别是t3和t4：


[
[
[1 2]
[2 3]
]
[
[4 4]
[5 3]
]
[
[7 4]
[8 4]
]
[
[2 10]
[15 11]
]
]




[
[
[1 2]
[2 3]
[7 4]
[8 4]
]
[
[4 4]
[5 3]
[2 10]
[15 11]
]
]


As in Python, the axis could also be negative numbers. Negative axis
are interpreted as counting from the end of the rank, i.e.,
axis + rank(values)-th dimension


[
[
[1 2 7 4]
[2 3 8 4]
]
[
[4 4 2 10]
[5 3 15 11]
]
]



>>> item = np.array([[1,4,8],[2,3,5],[2,5,1],[1,10,7]])
>>> item
array([[1, 4, 8],
[2, 3, 5],
[2, 5, 1],
[1, 10, 7]])

>>> item.sum(axis = 1)
array([13, 10,  8, 18])

>>> item.sum(axis = 0)
array([ 6, 22, 21])



• 有关axis/axes的理解

## 最后

posted @ 2019-03-27 18:59  Java3y  阅读(512)  评论(0编辑  收藏