Maosi Chen
Maosi Chen

Reputation: 1491

How to sort a batch of 2D tensors in tensorflow?

I have a tensor A with the shape of [#batch, #MaxSequence, #Features], where the actual lengths of the 2nd dimension (may be less than #MaxSequence) are stored in a tensor L. I want to sort A by the 2nd feature in the 3rd dimension on the sequence in each batch. I saw this post using tf.gather and tf.nn.top_k to sort a 2D tensor, but I don't know to apply it on the 3D case. Do I need to use loop to do it?

Upvotes: 3

Views: 1741

Answers (1)

greeness
greeness

Reputation: 16114

I have something working but it might exist better solutions. I guess my code is probably over-complicated for this simple problem.

The idea is that, we have to convert the return indices of tf.nn.top_k(a[:,:,1].indices (order by the second feature in the third dimension) to something tf.gather_nd can use. Particularly for my example below, we need to convert a tensor of

 [[3, 2, 1, 0],
  [0, 1, 2, 3],
  [2, 0, 3, 1]] 

to

 [[[0 3], [0 2], [0 1], [0 0]]
  [[1 0], [1 1], [1 2], [1 3]]
  [[2 2], [2 0], [2 3], [2 1]]]

What I figured out:

  • flatten the target indices first thus we get [3 2 1 0 0 1 2 3 2 0 3 1].
  • construct a paired vector intentionally [0 0 0 0 1 1 1 1 2 2 2]
  • tf.stack the above two vectors and then we reshape the result into what it is desired to be.

The complete tf code is below (get_shape method is defined here):

import tensorflow as tf
a = tf.Variable([[[0.51, 0.52, 0.53, 0.94, 0.35],
                  [0.32, 0.72, 0.83, 0.74, 0.55],
                  [0.23, 0.73, 0.63, 0.64, 0.35],
                  [0.01, 0.74, 0.73, 0.04, 0.75]],
                 [[0.32, 0.76, 0.83, 0.74, 0.55],
                  [0.23, 0.72, 0.63, 0.64, 0.35],
                  [0.11, 0.02, 0.03, 0.14, 0.15],
                  [0.01, 0.00, 0.73, 0.04, 0.75]],
                 [[0.51, 0.52, 0.53, 0.94, 0.35],
                  [0.32, 0.00, 0.83, 0.74, 0.55],
                  [0.23, 0.92, 0.63, 0.64, 0.35],
                  [0.11, 0.02, 0.03, 0.14, 0.15]]], tf.float32)
batch_size, seq_length, num_features = get_shape(a)

idx = tf.reshape(range(batch_size), [-1, 1])
idx_flat = tf.reshape(tf.tile(idx, [1, seq_length]), [-1])
top_k_flat = tf.reshape(tf.nn.top_k(a[:,:,1], 
                                    k=seq_length).indices, [-1])
final_idx = tf.reshape(tf.stack([idx_flat, top_k_flat], 1),
                       [batch_size, seq_length, 2])
reordered = tf.gather_nd(a, final_idx)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
print sess.run(reordered)

                 #ORDERED
                 #by this
                 #Column (within each example)
[[[ 0.01      ,  0.74000001,  0.73000002,  0.04      ,  0.75      ],
  [ 0.23      ,  0.73000002,  0.63      ,  0.63999999,  0.34999999],
  [ 0.31999999,  0.72000003,  0.82999998,  0.74000001,  0.55000001],
  [ 0.50999999,  0.51999998,  0.52999997,  0.94      ,  0.34999999]],
 [[ 0.31999999,  0.75999999,  0.82999998,  0.74000001,  0.55000001],
  [ 0.23      ,  0.72000003,  0.63      ,  0.63999999,  0.34999999],
  [ 0.11      ,  0.02      ,  0.03      ,  0.14      ,  0.15000001],
  [ 0.01      ,  0.        ,  0.73000002,  0.04      ,  0.75      ]],
 [[ 0.23      ,  0.92000002,  0.63      ,  0.63999999,  0.34999999],
  [ 0.50999999,  0.51999998,  0.52999997,  0.94      ,  0.34999999],
  [ 0.11      ,  0.02      ,  0.03      ,  0.14      ,  0.15000001],
  [ 0.31999999,  0.        ,  0.82999998,  0.74000001,  0.55000001]]

Note in the output, we have three examples. Within each example, the sequences are ordered by the second feature descendingly.

Upvotes: 4

Related Questions