tensorflow / hub

A library for transfer learning by reusing parts of TensorFlow models.
https://tensorflow.org/hub
Apache License 2.0
3.48k stars 1.66k forks source link

Cannot convert a symbolic Tensor ({}) to a numpy array #50353 #775

Closed dbl001 closed 3 years ago

dbl001 commented 3 years ago

Can you investigate this issue first reported in tensorflow? https://github.com/tensorflow/tensorflow/issues/50353

50353 has examples for reproducing the problem.

and also here?

https://stackoverflow.com/questions/68040079/how-to-convert-tensorflow-tensor-objects-to-numpy-arrays

WGierke commented 3 years ago

Hi @dbl001 Please note that the API of https://tfhub.dev/google/universal-sentence-encoder/3 is different from https://tfhub.dev/google/universal-sentence-encoder/4: As you can see in its documentation, USEv3 returns a dictionary so to access the actual embeddings you'll need to access the "outputs" element:

embeddings = model([
    "The quick brown fox jumps over the lazy dog.",
    "I am a sentence for which I would like to get its embedding"])["outputs"]

So instead of doing l2_normalize(np.array(m3(english_sentences))) in your notebook in https://github.com/tensorflow/tensorflow/issues/50353, running l2_normalize(np.array(m3(english_sentences)["outputs"])) outputs a valid numpy array. Note that I used a fresh Colab (TF: 2.5.0, tensorflow_hub: 0.12.0).

If that doesn't solve your problem I'm afraid that we'd need more details on what your expected output should be. Thanks!

dbl001 commented 3 years ago

The exception "Cannot convert a symbolic Tensor ({}) to a numpy array " was generated when I disabled eager execution, trying to utilize the GPU in tensorflow-macos.

from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()

When I commented out the line (e.g. - "disable_eager_execution()" , the code runs but no GPU.