美文网首页
Tensor的代码表达及方向

Tensor的代码表达及方向

作者: 何哀何欢 | 来源:发表于2020-05-09 13:46 被阅读0次
tensor

矩阵的索引顺序:

x方向(就是最内层数组)永远是最后一个方向:d_n
y是倒数第二
z是倒数第三
依次往前推……

比如,一个3维矩阵(如图),第一个元素的坐标是:N_{zyx}或者:N_{d_3d_2d_1}
如果是一个4维矩阵,第一个元素的坐标是:N_{d_4d_3d_2d_1}
如果4个维度分别为xyzw,那么第一个元素的坐标是:N_{wzyx}


代码对一个矩阵a = [\color{red}{1}, \color{green}{2}, \cdots]的表达方式如下:

#1维,x方向
a = [1, 2, 3, 4]

二维矩阵a = [\color{red}{[1\cdots4]}, \color{green}{[5\cdots8]}, \cdots]

#2维,yx方向
a = [ [1, 2, 3, 4],     
      [5, 6, 7, 8] ]

三维矩阵a = [ \color{red}{[[1\cdots4],\cdots]}, \color{green}{[[1\cdots4],\cdots]}, \cdots ]

#3维,zyx方向
a = [ [ [1, 2, 3, 4],   
        [5, 6, 7, 8],
        [9, A, B, C] ],
      [ [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, A, B, C] ] ]

助记:

  • [ [ [ [1, 2, 3, ... ] ] ] ] 数字前,有几个括号,就是几维。
  • [ 1维内的所有矩阵:[], [], [],.. ]
  • [ [ 2维内的所有矩阵:[], [], ...]]
  • [ [ [ 3维内的所有矩阵:[], [], ... ]]]
  • [ [ [ ... [ X维内的所有数字:1, 2, 3, 4,... ] ... ] ] ]


<tf.Tensor: shape=(2, 3, 4), dtype=int32, numpy=
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]],
       [[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]]])>

tf.reduce_sum(a, axis=0, keepdims=True)

<tf.Tensor: shape=(1, 3, 4), dtype=int32, numpy=
array([[[ 2,  4,  6,  8],
        [10, 12, 14, 16],
        [18, 20, 22, 24]]])>

tf.reduce_sum(a, axis=1, keepdims=True)

<tf.Tensor: shape=(2, 1, 4), dtype=int32, numpy=
array([[[15, 18, 21, 24]],
       [[15, 18, 21, 24]]])>

tf.reduce_sum(a, axis=2, keepdims=True)

<tf.Tensor: shape=(2, 3, 1), dtype=int32, numpy=
array([[[10],
        [26],
        [42]],
       [[10],
        [26],
        [42]]])>

相关文章

网友评论

      本文标题:Tensor的代码表达及方向

      本文链接:https://www.haomeiwen.com/subject/moihnhtx.html