zoukankan      html  css  js  c++  java
  • tensor 维度 问题。

    tf.argmax takes two arguments: input and dimension. example: tf.argmx(arr, dimension = 1). or tf.argmax(arr, 1). let arr is ndarray

    wrong: Since the indices of array arr are arr[rows, columns], I would expect tf.argmax(arr, 0) to return the index of the maximum element per row, while I would have expected tf.argmax(arr, 1) to return the maximum element per column. Likewise for tf.argmin.

    right understand:

    Think of the dimension argument of tf.argmax as the axis across which you reduce. 

    tf.argmax(arr, 0) reduces across dimension 0, i.e. the rows. Reducing across rows means that you will get the argmax of each individual column.

    把tf.argmax变量中的dimension理解为你需要通过轴x来降解维度。 tf.argmax(arr, 0)意味着: 穿过axis=0 (即横轴row), 穿过row(行)来降解。 而穿过行来讲解,也就是你要对每一列来进行操作。 

    This might be counterintuitive, but it falls in line with the conventions used in tf.reduce_max and so on.

    Additionally: how does this behave for n-dimensional Tensors? I'm a bit lost at figuring out which dimension relates to reducing i, j, k, l or m in a 5D-Tensor. – daniel451 Jun 29 '16 at 9:12

    By definition, if you search for the maximum across rows, you are searching within columns.

    For any d-dimensional array, taking the argmax across the ith axis means that, for any possible combination of the d-1 remaining indices, you are searching for the maximum amongst arr[ind1, ind2, ..., ind_i_minus_1, : , ind_i_plus_1, ..., ind_d]  ==》还是对列进行操作, zhi

     下例中 X 定义了有784维的tf 占位符。行数没有定义 。

               Y 定义了有10维的tf 占位符。行数也没有定义 。

    X = tf.placeholder("float", [None, 784]) # create symbolic variables
    Y = tf.placeholder("float", [None, 10])
    
    w = init_weights([784, 10]) # like in linear regression, we need a shared variable weight matrix for logistic regression
    
    py_x = model(X, w)。# 可以看出py_x 同Y一样是一个有10维的tf占位符。 也就是说有10个标签,10列构成的一个行向量。  行数根据可feed情况推断。
    
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y)) # compute mean cross entropy (softmax is applied internally)
    train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct optimizer
    predict_op = tf.argmax(py_x, 1) # at predict time, evaluate the argmax of the logistic regression # dimension =1,就是穿过列,对整行进行取最大值对应的行好。 

    如果不理解,往下看:

     

    import tensorflow as tf
    mygraph = tf.Graph()
    with tf.Session() as sess:
    x = tf.constant([1,2,3])
    y = tf.constant([3,4,5])

    op = tf.add(x,y)
    result = sess.run(fetches = op)
    print result

    x = tf.constant([[1,220,55],[4,3,-1]])
    with tf.Session() as sess:
    result = tf.argmax(x,1) # 输出变量的索引
    print sess.run(result) # output: [1 0]
    x = tf.constant([[1, 220, 55], [4, 3, -1]])
    x_max = tf.reduce_max(x, reduction_indices=[1])
    print sess.run(x_max) # ==> "array([220, 4], dtype=int32)"

  • 相关阅读:
    BZOJ1001 BJOI2006狼抓兔子(最小割+最短路)
    BZOJ4569 SCOI2016萌萌哒(倍增+并查集)
    Luogu4782 【模板】2-SAT 问题(2-SAT)
    BZOJ3626 LNOI2014LCA(树链剖分+主席树)
    BZOJ4012 HNOI2015开店(树链剖分+主席树)
    Luogu2264 树上游戏(点分治)
    BZOJ3998 TJOI2015弦论(后缀数组+二分答案)
    BZOJ1045 HAOI2008糖果传递(贪心)
    BZOJ1124 POI2008枪战Maf(环套树+贪心)
    洛谷 P4568 [JLOI2011]飞行路线 解题报告
  • 原文地址:https://www.cnblogs.com/xinping-study/p/7160833.html
Copyright © 2011-2022 走看看