kerastensorflow2.0eager-execution

Converting KerasTensor to numpy array


I am trying to convert "KerasTensor" into numpy array. I have tried converting KerasTensor to tf.Tensor (with no luck). I have also tried using tensor.numpy(), tensor.eval() and keras.backend.eval(tensor) all of that have not worked. Trying ".numpy()" and ".eval()" I am getting AttributeError: 'KerasTensor' object has no attribute 'numpy' error. How do I convert extracted KerasTensor to numpy array or to EagerTensor so I can use .numpy() method ?

Tensorflow version: 2.8.0 Keras version: 2.8.0

Thanks for help

Edit (Additional info): Model is build using keras functional API. After fit() I am extracting encoded input by: encoded = model.get_layer("encoder_output").output After that I've tried converting the "encoded" KerasTensor like I've described above and it does not work.


Solution

  • There is no value to convert to numpy.

    You need an input to have an output.

    In keras, the best to do is to build a submodel.

    submodel = Model(original_model.inputs, original_model.get_layer("encoder_output").output)   
    results = submodel.predict(numpy_input)