How to return max value and it’s neighbors from tensor

Viewed 163

I created a neural network model and would like to customize the loss function.

I was wondering how to return a max value and its neighbors from a tensor?

I know the tf.argmax can return the index of max value from a tensor. But is it possible to get the a new tensor that includes a range of [3 values before max, max, and 3 values after max]

1 Answers

Just saw that you have said in comments, you want to run it in non-eager mode (i.e. inside loss function), thus tensor.numpy() is not available.

Let's create a random tensor, and find the maximum value and it's neighbors, without using numpy and any other function which is unavailable in non-eager mode:

a = tf.random.uniform((20,), minval=0, maxval=100,dtype=tf.int32)
tf.print(a,summarize=20)
# [5 42 25 18 15 95 1 51 47 42 36 72 92 11 21 32 1 68 84 24]

start_index = tf.maximum(0,tf.subtract(tf.argmax(a,output_type=tf.int32),3))
end_index   = tf.minimum(a.shape[0],tf.add(start_index,7))

max_neighbors = a[start_index: end_index]
tf.print(max_neighbors,summarize=7)
# [25 18 15 95 1 51 47]

Above code, can be run in non-eager mode. Note that you can pass run_eagerly=True argument to the model.compile(), to write your code without limitation of non-eager mode (e.g. able to convert to numpy), but training will not be efficient.

Related