tensorflow/text

AttributeError: 'Tensor' object has no attribute 'numpy'

Closed this issue · 3 comments

code:
#getting Vocab file
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()

error:

AttributeError Traceback (most recent call last)
in
1 #getting Vocab file
----> 2 vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
3 do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()

AttributeError: 'Tensor' object has no attribute 'numpy'

Hi @ratnasankeerthanreddy, that AttributeError suggests you might not be running in TensorFlow 2's default mode of eager execution: in eager mode, every tensor has already been evaluated to a numpy value, in graph mode (the opposite of eager mode), a tensor is a handle on a value to be computed in the future.

I don't know enough about the structure of your program and what you're trying to achieve to recommend a specific workaround. Typically, users of TF2 would call bert_layer.resolved_object.vocab_file.asset_path.numpy() to build a model in the default context (what you get after import tensorflow as tf) and leave use of non-eager execution (if at all) to the speed-critical internals of their training loop.

In any case, this does not look at all like an issue with the tensorflow_text library.

i have resolved this error
just import the following two lines

import tensorflow  as tf 
tf.enable_eager_execution()

@ratnasankeerthanreddy @arnoegw
I tried to enable eager execution, but it still didn't work for me.

Traceback (most recent call last):
File "data_preprocessing_bert_based.py", line 301, in
tokenizer = create_tokenizer_from_hub_module()
File "data_preprocessing_bert_based.py", line 45, in create_tokenizer_from_hub_module
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
File "python3.8/site-packages/tensorflow/python/framework/ops.py", line 446, in getattr
self.getattribute(name)
AttributeError: 'Tensor' object has no attribute 'numpy'