zoukankan      html  css  js  c++  java
  • tensorflow的Axis轴向理解

    在关于numpy,tensorflow,pytorch中均会涉及到axis的问题,它是帮助我们快速取值的利器,如果axis不清楚,采集的数据并处理比自然不对,更谈不上继续其他操作了。

    下面以tf.argmax(),取最大值索引为例,进行讲解到底axis如何指向。

    二维数据比较简单,如下图:

    matrix1 = [[1., 4., 3.], # 二维,元素为列表
                     [5., 2., 6.]]

    t1 = tf.reduce_sum(matrix1) # 所有元素求和 # tf.Tensor(21.0, shape=(), dtype=float32)
    t2 = tf.reduce_sum(matrix1, axis=0) # 纵向求和 tf.Tensor([6. 6. 9.], shape=(3,), dtype=float32)
    t3 = tf.reduce_sum(matrix1, axis=1) # 横向求和 tf.Tensor([ 8. 13.], shape=(2,), dtype=float32)

    那么三维甚至更高维呢?,谁是0维,谁是1维,谁是2维……

    matrix2 = [[[5., 2.], [3., 4.]], # 三维,元素为矩阵
                      [[1., 6.], [7., 8.]]]

    t3 = tf.reduce_sum(matrix2, axis=0) # 0向求和 tf.Tensor([[ 6. 8.] [10. 12.]], shape=(2, 2), dtype=float32).详解:5+1=6,2+6=8,3+7=10,4+8=12---》[[6,8],[10,12]]

    t4 = tf.reduce_sum(matrix2, axis=1) # 1向求和 tf.Tensor([[ 8. 6.] [ 8. 14.]], shape=(2, 2), dtype=float32).详解:5+3=8,2+4=6,1+7=8,6+8=14,--》[[8,6],[8,14]]
    t5 = tf.reduce_sum(matrix2, axis=2) # 2向求和 tf.Tensor([[ 7. 7.] [ 7. 15.]], shape=(2, 2), dtype=float32) 详解: 5+2=7,3+4=7,1+6=7,7+8=15,---》[[7,7],[7,18]]

    # 比较发现,axis = 0,指的是最外层的操作,
    print(t3, t4, t5)
    '''
    [[[1,2,3],[4,5,6]],[[a,b,c],[d,e,f]]]
    1-->a, 4-->d这个方向代表第0维,axis=0
    1-->4,这个方向代表第1维,axis = 1
    1,2,3,这个方向代表第2维,axis=2
    最熟悉的最小单元是最高维。(轴)

    '''

    t7 = tf.argmax(matrix2, axis=2) #
    t8 = tf.argmax(matrix2, axis=0)
    t9 = tf.argmax(matrix2, axis=1)
    print(t7, t8, t9)
    '''
    tf.Tensor(
    [[0 1]
    [1 1]], shape=(2, 2), dtype=int64) tf.Tensor(
    [[0 1]
    [1 1]], shape=(2, 2), dtype=int64) tf.Tensor(
    [[0 1]
    [1 1]], shape=(2, 2), dtype=int64)
    '''

    总结:0维是从最外层去看,之后一层层递进,深入内层。

    参考:https://www.cnblogs.com/logo-88/p/9295858.html

            https://blog.csdn.net/chengshuhao1991/article/details/78545723?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

    如果还不理解,敲一下以下代码,不用tensorflow也可以,a是[4,35,8]的矩阵,班级,学生,课程成绩

  • 相关阅读:
    网恋现代人的童话
    男人爱女人
    在Web页面中管理服务
    wcf使用入门学习笔记
    table显示细线边框
    wcf整理资料
    启动sqlserver服务的时候报错
    命名规范
    .net中使用xsl文件作为导航菜单
    wcf如何选择绑定
  • 原文地址:https://www.cnblogs.com/kevin-red-heart/p/13225315.html
Copyright © 2011-2022 走看看