0

Imagine I have a tensor of shape (batch_size, a, ... , c, d, e)where are a, ... ,c,d,e are defined integers. For example (batch_size, 500, 3, 2, 2, 69) or (batch_size, 2, 2).

My question is for all tensors but let's stick to the example of tensor1.get_shape() = (?, 500, 3, 2, 2, 69)

Given that I have tensor2 with tensor2.get_shape() = (?, 500, 3, 2, 2, 14) containing indices of the last axis of tensor1, I have 2 problems:

1) I want to construct a mask for tensor1 of shape (?, 500, 3, 2, 2, 69) from tensor2. For example a possible row along the last axis for tensor2 would be [1,8,3,68,2,4,58,19,20,21,26,48,56,11] but since tensor2 is constructed from tensor1 these indices vary for new input. These are the indices of the last axis that have to be kept of tensor1. Everything else has to be masked out.

2) given that I have the mask of shape (?, 500, 3, 2, 2, 69) for tensor1, how do I mask out the undesired values while maintaining the batch size dimension? The masked out tensor should have shape (?, 500, 3, 2, 2, 14).

Answers in keras or numpy would also be neat, although knowing how to do it in numpy wouldn't solve my problem, I'd still like to know.

Perm. Questiin
  • 429
  • 2
  • 9
  • 21

1 Answers1

0

answer to 1: tf.gather_nd(mask, [tf.range(tf.shape(tensor1)[0])[:,None, None, None, None, None],tf.range(tf.shape(tensor1)[1])[:,None, None, None, None],tf.range(tf.shape(tensor1)[2])[:,None, None, None],tf.range(tf.shape(tensor1)[3])[:,None, None],tf.range(tf.shape(tensor1)[4])[:,None],tensor2])

There is probably no solution to 2. I will try pytorch.

Perm. Questiin
  • 429
  • 2
  • 9
  • 21