Error: module 'tensorflow' has no attribute 'gfile' even after importing tensorflow as tensorflow.compat.v1

Viewed 318

I have read solutions to this problem where tensorflow version change to 1.14 worked for people. I was wondering if there are any other solutions without doing the version change. I am working off of Jupyter and I have programs running that uses version 2.4.1. Other reason I am trying to avoid the version change is there are other functions like sentencepiece and others that gives subsequent errors once I change it to 1.14.

I am trying to run the BERT model using their code from the github. https://github.com/google-research/bert

The error is in the tokenization.py function

~\bert\tokenization.py in load_vocab(vocab_file)
123   """Loads a vocabulary file into a dictionary."""
124   vocab = collections.OrderedDict()
125   index = 0
126   with tf.io.gfile.GFile(vocab_file, "r") as reader:

AttributeError: module 'tensorflow' has no attribute 'gfile'

Any suggestions is highly appreciated!

Update: Even when I dont use tf.io.gfile.GFile and try to use "with open(json_file, 'r') as reader:", weirdly I get the same error. This is strange.

 \bert\modeling.py in from_json_file(cls, json_file)
 91   def from_json_file(cls, json_file):
 92     """Constructs a `BertConfig` from a json file of 
 parameters."""
 93     print(cls)
 94     print(json_file)
 95     with open(json_file, 'r') as reader:

AttributeError: module 'tensorflow' has no attribute 'gfile'. Please let me know if you see something that I am completely not seeing.

0 Answers
Related