0

I have tensor with shape (1, 3) and I'm using TensorFlow 2.4.1 but I have code that needs to run on the old version. So, I use

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior() 

predicted_id = tf.argmax(sample_decoder_output[-1]).numpy()

The problem is that I got the following error :

 predicted_id = tf.argmax(sample_decoder_output[-1]).numpy()
AttributeError: 'Tensor' object has no attribute 'numpy'

and I have found many answers [1][2] that I applied but I got the following error

predicted_id = tf.argmax(sample_decoder_output[-1]).numpy()   File "~/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
        return target(*args, **kwargs)   File "~/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 1036, in _slice_helper
        return strided_slice(   File "~/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
        return target(*args, **kwargs)   File "~/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 1209, in strided_slice
        op = gen_array_ops.strided_slice(   File "~/lib/python3.8/site-packages/tensorflow/python/ops/gen_array_ops.py", line 10451, in strided_slice
        return strided_slice_eager_fallback(   File "~/lib/python3.8/site-packages/tensorflow/python/ops/gen_array_ops.py", line 10521, in strided_slice_eager_fallback
        _result = _execute.execute(b"StridedSlice", 1, inputs=_inputs_flat,   File "~/lib/python3.8/site-packages/tensorflow/python/eager/execute.py", line 75, in quick_execute
        raise e   File "~/lib/python3.8/site-packages/tensorflow/python/eager/execute.py", line 59, in quick_execute
        tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, TypeError: An op outside of the function building code is being passed a "Graph" tensor. It is possible to have Graph tensors leak out of the function building context by including a tf.init_scope in your function building code. For example, the following function will fail:  
        @tf.function  
            def has_init_scope():
                my_constant = tf.constant(1.)
                with tf.init_scope():
                   added = my_constant * 2
 The graph tensor has name: decoder/dense/BiasAdd:0
I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37
  • 3
    You are disabling TF2 behavior... and then are using TF2 behavior. – xdurch0 Sep 27 '21 at 20:22
  • Yes, you are right but I need to get `sample_decoder_output` and I'm not sure if it is possible to do it. – I_Al-thamary Sep 27 '21 at 20:27
  • `.numpy` is only supported in Eager mode. If you disable Tf2 behavior, you lose eager execution. It will take graph mode default and need to use session to get the value of the tensor in numpy array. –  Oct 08 '21 at 07:03
  • I did that `s_ = sess.run(predicted_id, feed_dict={S2: s})` but the problem that I did not got the correct results. – I_Al-thamary Oct 08 '21 at 07:45

0 Answers0