【问题标题】:tf.gather_nd without "flattening" shape?tf.gather_nd 没有“扁平化”形状?
【发布时间】:2017-10-18 06:53:25
【问题描述】:

我仍在玩 tensorflow 并尝试使用gather_nd 操作,但返回值不是我想要的形状/格式...

Input Tensor: - shape: (2, 7, 4) 
array([[[ 0., 0., 1., 2.],
        [ 0., 0., 2., 2.],
        [ 0., 0., 3., 3.],
        [ 0., 0., 4., 3.],
        [ 0., 0., 5., 4.],
        [ 0., 0., 6., 4.],
        [ 0., 0., 7., 5.]],
       [[ 1., 1., 0., 2.],
        [ 1., 2., 0., 2.],
        [ 1., 3., 0., 3.],
        [ 1., 4., 0., 3.],
        [ 1., 5., 0., 4.],
        [ 1., 6., 0., 5.],
        [ 1., 7., 0., 5.]]], dtype=float32) 

Indices returned by tf.where op: - shape: (3, 2) 
array([[0, 0], 
       [0, 1],
       [1, 0]]) 

tf.gather results: (shape = [3, 4])
array([[ 0., 0., 1., 2.],
       [ 0., 0., 2., 2.],
       [ 1., 1., 0., 2.]], dtype=float32)

desired results: = (2, sparse, 4)
array([[[ 0., 0., 1., 2.],
        [ 0., 0., 2., 2.]],
       [[ 1., 1., 0., 2.]]], dtype=float32) 

记住 tf.where = 动态形状并且不能保证整个二维(轴 = 1)的形状一致性,实现这一目标的最佳方法是什么?

注意:忽略这个问题 - 查看我的回答

【问题讨论】:

    标签: python tensorflow tensor


    【解决方案1】:

    我认为这是一个 Tensorflow 版本问题。在我的版本(1.2.1)中,我从您的输入中获得了所需的确切输出。不过,我也根据旧版本尝试了以下代码。

    import tensorflow as tf
    
    indices = [[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3]],
               [[0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3]],
               [[1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 0, 3]]]
    
    params =  [[[ 0., 0., 1., 2.],
                [ 0., 0., 2., 2.],
                [ 0., 0., 3., 3.],
                [ 0., 0., 4., 3.],
                [ 0., 0., 5., 4.],
                [ 0., 0., 6., 4.],
                [ 0., 0., 7., 5.]],
               [[ 1., 1., 0., 2.],
                [ 1., 2., 0., 2.],
                [ 1., 3., 0., 3.],
                [ 1., 4., 0., 3.],
                [ 1., 5., 0., 4.],
                [ 1., 6., 0., 5.],
                [ 1., 7., 0., 5.]]]
    
    output = tf.gather_nd(params, indices)
    
    with tf.Session()as sess:
        print (sess.run(output))
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我意识到我的问题很愚蠢。

      # of tuples when 1st dim is 0    !=   # of tuples when 1st dim is 1 
      

      我不确定我的要求是否可行......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 2014-10-30
        • 2020-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-09
        相关资源
        最近更新 更多