【问题标题】:How to omit zeros in a 4-D tensor in tensorflow?如何在张量流中省略 4-D 张量中的零点?
【发布时间】:2019-01-03 06:16:37
【问题描述】:

假设我有一个张量:

import tensorflow as tf
t = tf.Variable([[[[0., 235., 0., 0., 1006., 0., 0., 23., 42.], [77., 0., 0., 12., 0., 0., 33., 55., 0.]],
                 [[0., 132., 0., 0., 234., 0., 1., 24., 0.], [43., 0., 0., 124., 0., 0., 0., 52., 645]]]])

我想省略零并留下一个形状为 (1, 2, 2, 4) 的张量,其中 4 是我的张量中非零元素的数量,例如

t = tf.Variable([[[[235., 1006., 23., 42], [77., 12., 33., 55.]],
                 [[132., 234., 1., 24.], [43., 124., 52., 645]]]])

我使用布尔掩码在一维张量上执行此操作。如何省略 4-D 张量中的零点。可以推广到更高的等级吗?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    使用 TensorFlow 1.12:

    import tensorflow as tf
    
    def batch_of_vectors_nonzero_entries(batch_of_vectors):
      """Removes non-zero entries from batched vectors.
    
      Requires that each vector have the same number of non-zero entries.
    
      Args:
        batch_of_vectors: A Tensor with length-N vectors, having shape [..., N].
      Returns:
        A Tensor with shape [..., M] where M is the number of non-zero entries in
        each vector.
      """
      nonzero_indices = tf.where(tf.not_equal(
          batch_of_vectors, tf.zeros_like(batch_of_vectors)))
      # gather_nd gives us a vector containing the non-zero entries of the
      # original Tensor
      nonzero_values = tf.gather_nd(batch_of_vectors, nonzero_indices)
      # Next, reshape so that all but the last dimension is the same as the input
      # Tensor. Note that this will fail unless each vector has the same number of
      # non-zero values.
      reshaped_nonzero_values = tf.reshape(
          nonzero_values,
          tf.concat([tf.shape(batch_of_vectors)[:-1], [-1]], axis=0))
      return reshaped_nonzero_values
    
    t = tf.Variable(
        [[[[0., 235., 0., 0., 1006., 0., 0., 23., 42.],
           [77., 0., 0., 12., 0., 0., 33., 55., 0.]],
          [[0., 132., 0., 0., 234., 0., 1., 24., 0.],
           [43., 0., 0., 124., 0., 0., 0., 52., 645]]]])
    nonzero_t = batch_of_vectors_nonzero_entries(t)
    
    with tf.Session():
        tf.global_variables_initializer().run()
        result_evaled = nonzero_t.eval()
        print(result_evaled.shape, result_evaled)
    

    打印:

    (1, 2, 2, 4) [[[[  2.35000000e+02   1.00600000e+03   2.30000000e+01   4.20000000e+01]
       [  7.70000000e+01   1.20000000e+01   3.30000000e+01   5.50000000e+01]]
    
      [[  1.32000000e+02   2.34000000e+02   1.00000000e+00   2.40000000e+01]
       [  4.30000000e+01   1.24000000e+02   5.20000000e+01   6.45000000e+02]]]]
    

    如果结果最终不完整,查看SparseTensors 可能会很有用。

    【讨论】:

    • 在`tf.concat(0, [tf.shape(batch_of_vectors)[:-1], [-1]]))`我收到一个错误ValueError: Dimension 0 in both shapes must be equal, but are 3 and 1. Shapes are [3] and [1]. From merging shape 0 with other shapes. for 'concat_2/concat_dim' (op: 'Pack') with input shapes: [3], [1].
    • 对,对不起。我已经更新了 TF 1.x 兼容性的示例(交换了 concat 参数)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2018-10-04
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多