【问题标题】:slicing a tensor along a dimension with given index沿具有给定索引的维度切片张量
【发布时间】:2019-08-12 07:19:47
【问题描述】:

假设我有一个张量:

tensor = tf.constant(
  [[[0.05340263, 0.27248233, 0.49127685, 0.07926575, 0.96054204],
    [0.50013988, 0.05903472, 0.43025479, 0.41379231, 0.86508251],
    [0.02033722, 0.11996034, 0.57675261, 0.12049974, 0.65760677],
    [0.71859089, 0.22825203, 0.64064407, 0.47443116, 0.64108334]],

   [[0.18813498, 0.29462021, 0.09433628, 0.97393446, 0.33451445],
    [0.01657461, 0.28126666, 0.64016929, 0.48365073, 0.26672697],
    [0.9379696 , 0.44648103, 0.39463243, 0.51797975, 0.4173626 ],
    [0.89788558, 0.31063058, 0.05492096, 0.86904097, 0.21696292]],

   [[0.07279436, 0.94773635, 0.34173115, 0.7228713 , 0.46553334],
    [0.61199848, 0.88508141, 0.97019517, 0.61465985, 0.48971128],
    [0.53037002, 0.70782324, 0.32158754, 0.2793538 , 0.62661128],
    [0.52787814, 0.17085317, 0.83711126, 0.40567032, 0.71386498]]])

形状为 (3, 4, 5)

我想对它进行切片以返回一个形状为 (3,5) 的新张量,其中给定的一维张量的值指示要检索的位置,例如:

index_tensor = tf.constant([2,1,3])

这会产生一个新的张量,如下所示:

[[0.02033722, 0.11996034, 0.57675261, 0.12049974, 0.65760677],        
 [0.01657461, 0.28126666, 0.64016929, 0.48365073, 0.26672697],     
 [0.52787814, 0.17085317, 0.83711126, 0.40567032, 0.71386498]]

也就是说,沿着第二个维度,从索引 2、1 和 3 中获取项目。 类似这样:

tensor[:,x,:]

除了这只会给我沿着维度索引'x'的项目,我希望它是灵活的。

这个可以吗?

【问题讨论】:

标签: python tensorflow


【解决方案1】:

您可以使用tf.one_hot() 屏蔽index_tensor

index = tf.one_hot(index_tensor,tensor.shape[1])

[[0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [0. 0. 0. 1.]]

然后通过tf.boolean_mask()获取结果。

result = tf.boolean_mask(tensor,index)

[[0.02033722 0.11996034 0.57675261 0.12049974 0.65760677]
 [0.01657461 0.28126666 0.64016929 0.48365073 0.26672697]
 [0.52787814 0.17085317 0.83711126 0.40567032 0.71386498]]

【讨论】:

    【解决方案2】:
    tensor = tf.constant(
      [[[0.05340263, 0.27248233, 0.49127685, 0.07926575, 0.96054204],
        [0.50013988, 0.05903472, 0.43025479, 0.41379231, 0.86508251],
        [0.02033722, 0.11996034, 0.57675261, 0.12049974, 0.65760677],
        [0.71859089, 0.22825203, 0.64064407, 0.47443116, 0.64108334]],
    
       [[0.18813498, 0.29462021, 0.09433628, 0.97393446, 0.33451445],
        [0.01657461, 0.28126666, 0.64016929, 0.48365073, 0.26672697],
        [0.9379696 , 0.44648103, 0.39463243, 0.51797975, 0.4173626 ],
        [0.89788558, 0.31063058, 0.05492096, 0.86904097, 0.21696292]],
    
       [[0.07279436, 0.94773635, 0.34173115, 0.7228713 , 0.46553334],
        [0.61199848, 0.88508141, 0.97019517, 0.61465985, 0.48971128],
        [0.53037002, 0.70782324, 0.32158754, 0.2793538 , 0.62661128],
        [0.52787814, 0.17085317, 0.83711126, 0.40567032, 0.71386498]]])
    
    
    with tf.Session() as sess :
      sess.run( tf.global_variables_initializer() )
      print(sess.run( tf.concat( [ tensor[0:1,2:3], tensor[1:2,1:2], tensor[2:3,3:4] ] , 1 ) ))
    

    这将打印这样的值。

    [[[0.02033722 0.11996034 0.5767526  0.12049974 0.6576068 ]
      [0.01657461 0.28126666 0.64016926 0.48365074 0.26672697]
      [0.52787817 0.17085317 0.83711123 0.40567032 0.713865  ]]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 2021-06-10
      • 2021-07-17
      • 2019-05-31
      • 2022-10-30
      • 2017-10-12
      • 2019-04-13
      相关资源
      最近更新 更多