Question on C5W1A3Ex3 predict_and_sample function, indices data type

The expected output for indices is
list(indices[12:18]) =[array([26]), array([18]), array([53]), array([27]), array([40]), array([7])]

what I got is a bit difference
list(indices[12:18]) = [<tf.Tensor: shape=(1,), dtype=int64, numpy=array([68])>, <tf.Tensor: shape=(1,), dtype=int64, numpy=array([45])>, <tf.Tensor: shape=(1,), dtype=int64, numpy=array([20])>, <tf.Tensor: shape=(1,), dtype=int64, numpy=array([17])>, <tf.Tensor: shape=(1,), dtype=int64, numpy=array([73])>, <tf.Tensor: shape=(1,), dtype=int64, numpy=array([86])>]

I checked the type of indices:
print(“indices type:”, type(indices)) and It says
indices type: <class ‘tensorflow.python.framework.ops.EagerTensor’>

do we expect “EagerTensor” for indices?

1 Like

Doesn’t make much difference. Use np.argmax for array data type:

Here’s an example:

>>> a = tf.random.normal((2,3,4))
>>> list(tf.argmax(a, axis=-1))
[<tf.Tensor: shape=(3,), dtype=int64, numpy=array([0, 2, 0])>, <tf.Tensor: shape=(3,), dtype=int64, numpy=array([2, 1, 1])>]
>>> list(np.argmax(a, axis=-1))
[array([0, 2, 0]), array([2, 1, 1])]

See this note in the markdown:

  • Your results may likely differ because Keras’ results are not completely predictable.
1 Like

No. Should be a numpy.ndarray.

1 Like

@TMosh
Is there a test code that asserts this behavior?

1 Like

There is no unit test with asserts.

The notebook just has some vague wording about your exact results may vary because “Keras is not predictable”.

1 Like

Thanks for confirming. I suppose we could return tensors / numpy arrays then.

1 Like

Thank you for the explanation.
Yes. When I replace tf.math.argmax (suggested to be used in Exercise2 2D) with np.argmax, in Execise3 step2 then I get the expected array representation

<class ‘numpy.ndarray’>
list(indices[12:18]) = [array([56]), array([85]), array([24]), array([45]), array([64]), array([48])]

I then go back to Exerciser2 2D and try to replace tf.math.argmax with np.argmax, I get this error:

“NotImplementedError: Cannot convert a symbolic Tensor (dense/Softmax_132:0) to a numpy array. This error may indicate that you’re trying to pass a Tensor to a NumPy call, which is not supported”

So it looks like that if inputs to the argmax is a tf tensor, then we should use tf.math.argmax instead of np.argmax

1 Like

Another option is to convert a tensor to numpy as needed.
eg:

>>> import tensorflow as tf
>>> a = tf.random.normal((2,3))
>>> type(a)
<class 'tensorflow.python.framework.ops.EagerTensor'>
>>> type(a.numpy())
<class 'numpy.ndarray'>

1 Like

Thanks. It works well too converting a tensor to numpy as needed.

1 Like