Tensor的代码表达及方向

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]]])>
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容