0

I'm struggling to convert a simple Keras tensor into a Numpy array. This is the code I want to perform:

input = tf.keras.Input(shape=(100, 1), name='input')
inputs = np.array(input) 

Error: Cannot convert a symbolic Keras input/output to a numpy array.

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
  • What is your numpy version? try using numpy version less than 1.20, pip install numpy==1.19.5 – Sonia Samipillai Sep 05 '21 at 23:44
  • Why do you need to do this? I feel this is an XY problem. – Dr. Snoopy Sep 06 '21 at 00:30
  • I used numpy 1.19.5, here is a link to a colab : https://colab.research.google.com/gist/minh28/35597cc566b4797949142c7bcc5eedb2/50155.ipynb – Serge Nguyen Sep 06 '21 at 17:03
  • Need to use this because I need to re use a code written from a previous version of Keras and Tensorflow. However I need to use a more recent version of those librairies now but with the same code. – Serge Nguyen Sep 06 '21 at 17:04
  • Keras Tensor can not be converted to Numpy array directly, Convert Keras tensor to Tensor and from Tensor to numpy. Thanks! –  Sep 17 '21 at 11:41

1 Answers1

0

First things first. Your input is not a tensor, but a symbolic tensor.

  1. So, what is symbolic tensor? - You can consider it as tensor without any value associated with it.
  2. Now, what is the use of symbolic tensor? - it actually helps you build a model framework so that it is ready to accept the input anytime later.

More can be found in this answer

Now, coming back to answer your question. You need to convert the symbolic tensor to a tensor by first feeding the network with the data. Once you have the tensor, then you can proceed to get the numpy array out of it.

Harsha Y
  • 59
  • 1
  • 3