Slicing a Tensor with indices from Argmax and Week-3 Ex.1

@ai_curious @paulinpaloalto

  1. How can I slice a tensor using the output (indices obtained )from the tf.math.argmax on another tensor of same shape?
  2. I’ve noticed that tf.math.argmax takes input a numpy nd array as well? I thought it should only work on tf.tensor objects

from exercise 1 in week 3 assignment-1

3.What does scores[2] represent? does masking return a 1-D array? and why do we have to do. scores[2].numpy() to print it? why can’t we print it directly?

Thanks and Happy New Year!!

argmax returns one or more values depending on the value of the axis parameter. The returned value(s) represent the position(s) in the the input where the largest value is found. Those position(s) can then be used to index into a different array, and pull put the value(s) in those location(s)

1 Like

TensorFlow interoperability

An ND array can be passed to TensorFlow APIs, since ND array is just an alias to tf.Tensor . As mentioned earlier, such interoperation does not do data copies, even for data placed on accelerators or remote devices.

scores is one of the 3 Tensors returned by yolo_filter_boxes() function. scores[2] is the 3^{rd} element in that multi-valued Tensor. Calling numpy() works with TensorFlow v2.x eager execution to allow output of values without evaluating an entire TensorFlow graph. In TensorFlow v1.x you would have used .eval() instead.

Hi thanks for the reply, How can I index Box scores using argmax output stored in box_classes
Basically an alternative to using reduce_max.

so for printing the value of an output tensor each time I have to call .numpy() method? What is Eager Execution?

"TensorFlow supports eager execution and graph execution. In eager execution, operations are evaluated immediately. In graph execution, a computational graph is constructed for later evaluation.

TensorFlow defaults to eager execution. "

# After eager execution is enabled, operations are executed as they are
# defined and Tensor objects hold concrete values, which can be accessed as
# numpy.ndarray`s through the numpy() method.

further exposition of the TensorFlow graph model and the difference between eager and graph mode execution is beyond the scope of this forum.

There is always more than one way to accomplish something in code. This is particularly true in Python. Here, you could use the indices returned by argmax() to slice some other array, which is what your question 1. above asked. In this case, however, at least in the version that I have access to, the inline hints suggest to use max() to directly access the maximum values in the parallel array. Always a good idea in these exercises to adhere to the hints and guidelines in the notebooks. Diverging risks incurring autograder wrath. BTW one of the mentors might point out that the Coursera honor code prohibits directly post your solutions. Enforcement seems to vary by course. Don’t be surprised, though, if your screen capture gets removed.

Here’s what my notebook says…
For each box, find:

  • the index of the class with the maximum box score
  • the corresponding box score
  • Useful references*

Hi thanks for the reply, Actually I followed the hints for the notebook exercise but When I tried to do it in alternate way for learning i’m unable to figure out how to supply these indices which are outputed from argmax to slice the box_scores tensor. Could you please tell me how can I use these indices box_classes to slice box_scores.


I don’t think it’s a good choice to use it to slice box_scores because max() is actually perfect for that given the 3D data structures. But you could use it to extract in other situations. Here’s one that is close to how it is used towards the end of the car detection exercise…

from keras import backend as K
import numpy as np

    #same as the class code.  NOTE: only the shapes are realistic here...the values include negative numbers which is not what you will get in real use where all probabilities will be 0 <= p <= 1.
box_confidence = K.random_normal([19, 19, 5, 1], mean=1, stddev=4, seed = 1)
boxes = K.random_normal([19, 19, 5, 4], mean=1, stddev=4, seed = 1)
box_class_probs = K.random_normal([19, 19, 5, 80], mean=1, stddev=4, seed = 1)

box_scores = box_class_probs * box_confidence
box_classes = K.argmax(box_scores, axis=-1)
box_class_scores = K.max(box_scores, axis=-1)

class_path = "./model_data/coco_classes.txt"

def read_classes(classes_path):
    with open(classes_path) as f:
        class_names = f.readlines()
        class_names = [c.strip() for c in class_names]

    return class_names

class_names = read_classes(class_path)


>tennis racket

the value of box_classes[0,0,1] is used to index into the class_names array so that you can see the name of the class, instead of just its index.