Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

In Week 3 Lab - reinforcement Learning I got this error message “Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)” when running the code in a latest python version in this line of code:

state_qn = np.expand_dims(state, axis=0) # state needs to be the right shape for the q_network
→ q_values = q_network(state_qn) # HERE
action = utils.get_action(q_values, epsilon)

Any coaching how to solve this issue?

Thanks

Eduardo

Hello Eduardo @Eduardo_Saldarriaga,

Would you mind copy and paste the whole Error Traceback here?

Raymond

Hi. Thanks for helping. I´m copying below the whole message. In the Jupyter course platform code works well. I got this error when running in VS Code. Thanks for your help.

ValueError Traceback (most recent call last) c:\Users\eduardos\OneDrive - Microsoft\Documents\Microsoft\Documentos\My Code\Pyone\MachileLearningSpecialization\UnsupervisedLearning\C3_W3_A1_Assignment.ipynb Cell 36 in <cell line: 17>() 
23 for t in range(max_num_timesteps): 
24 
25 # From the current state S choose an action A using an ε-greedy policy 
26 state_qn = np.expand_dims(state, axis=0) # state needs to be the right shape for the q_network 
—> 27 q_values = q_network(state_qn) 
28 action = utils.get_action(q_values, epsilon) 

30 # Take action A and receive reward R and the next state S’ 

File c:\Users\eduardos\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py:67 , in filter_traceback..error_handler**(args, kwargs) 
65 except Exception as e: # pylint: disable=broad-except 
66 filtered_tb = _process_traceback_frames(e.traceback) 
—> 67 raise e.with_traceback(filtered_tb) from None 
68 finally: 
69 del filtered_tb 

File c:\Users\eduardos\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py:102 , in convert_to_eager_tensor*(value, ctx, dtype)** 
100 dtype = dtypes.as_dtype(dtype).as_datatype_enum 
101 ctx.ensure_initialized()
 → 102 return ops.EagerTensor(value, ctx.device_name, dtype) ValueError : Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

Hello @Eduardo_Saldarriaga,

Please forget about the VS Code for now and focus on solving the problem on Coursera.

  1. Go to coursera, and open the lab from there.
  2. Follow these instructions to get a new copy of the lab. The instructions will ask you to rename the current lab’s name in order for the system to get you a new copy. If you successfully get a new copy, besides the renamed file, a new lab notebook named “C3_W3_A1_Assignment.ipynb” will show up.
  3. Open the new lab notebook.
  4. Copy your work for exercise 1 & 2 to the new lab notebook. Please only change code in between "### START CODE HERE ### " and “### END CODE HERE ###”. Again please don’t change any other part of the notebook.
  5. Run all the cells from the top of the new lab notebook on coursera
  6. Report here the whole error traceback if any.

Our future discussion will be based on the new copy. Please make sure you successfully get a new copy of the notebook.

Raymond

With the steps you provide it worked! thanks!

1 Like