Why can't I import functions in bert after pip install bert
Question:
I am a beginner for bert, and I am trying to use files of bert given on the GitHub:https://github.com/google-research/bert
However I cannot import files(such as run_classifier, optimisation and so on) from bert after using pip install bert
to install bert in terminal. I tried to run following codes in jupiter notebook:
import bert
from bert import run_classifier
And the error is:
ImportError: cannot import name 'run_classifier'
Then I found the file named ‘bert’ in anaconda3libpython3.6site-packages
, and there were no python files named ‘run_classifier’, ‘optimization’ etc inside it. So I downloaded those files from GitHub and put them into file ‘bert’ by myself. After doing this I could import run_classifier.
However, another problem occurred. I couldn’t use the functions inside the files although I could import them.
For example, there’s a function convert_to_unicode
in tokenization.py:
Help on module bert.tokenization in bert:
NAME
bert.tokenization - Tokenization classes.
FUNCTIONS
convert_to_unicode(text)
Converts `text` to Unicode (if it's not already), assuming utf-8 input.
Then I tried this:
import tokenization from bert
convert_to_unicode('input.txt')
And the error is:
NameError: name 'convert_to_unicode' is not defined
Then I tried:
from tokenization import convert_to_unicode
And the error is:
ModuleNotFoundError: No module named 'tokenization'
I am really confused about this.
Answers:
The package you’re looking for is bert-tensorflow
, not bert
.
bert-tensorflow is the Python package for Google’s BERT implementation.
bert is a serialization library.
try adding these lines of code as in https://colab.research.google.com/drive/1hMLd5-r82FrnFnBub-B-fVW78Px4KPX1#scrollTo=2IjSWx7-O8yY
issue is that the BERT embedding is now using TensorFlow 2.0. As TensorFlow 2.0 has been released recently.
!pip install tensorflow==2.0
!pip install tensorflow_hub
!pip install bert-for-tf2
!pip install sentencepiece
import tensorflow_hub as hub
import tensorflow as tf
from bert import tokenization
from tensorflow.keras.models import Model # Keras is the new high level API for TensorFlow
import math
Try this, its a new change actually.
pip install bert-tensorflow
from bert import bert_tokenization
tokenizer=bert_tokenization.FullTokenizer(vocab_file,do_lower_case)
I’m using Python – 3.8, tensorflow – 2.9.2.
Getting below error after using bert-tensorflow.
27 import six
28 import tensorflow.compat.v1 as tf
—> 29 from tensorflow.contrib import layers as contrib_layers
30
31
ModuleNotFoundError: No module named ‘tensorflow.contrib’
I am a beginner for bert, and I am trying to use files of bert given on the GitHub:https://github.com/google-research/bert
However I cannot import files(such as run_classifier, optimisation and so on) from bert after using pip install bert
to install bert in terminal. I tried to run following codes in jupiter notebook:
import bert
from bert import run_classifier
And the error is:
ImportError: cannot import name 'run_classifier'
Then I found the file named ‘bert’ in anaconda3libpython3.6site-packages
, and there were no python files named ‘run_classifier’, ‘optimization’ etc inside it. So I downloaded those files from GitHub and put them into file ‘bert’ by myself. After doing this I could import run_classifier.
However, another problem occurred. I couldn’t use the functions inside the files although I could import them.
For example, there’s a function convert_to_unicode
in tokenization.py:
Help on module bert.tokenization in bert:
NAME
bert.tokenization - Tokenization classes.
FUNCTIONS
convert_to_unicode(text)
Converts `text` to Unicode (if it's not already), assuming utf-8 input.
Then I tried this:
import tokenization from bert
convert_to_unicode('input.txt')
And the error is:
NameError: name 'convert_to_unicode' is not defined
Then I tried:
from tokenization import convert_to_unicode
And the error is:
ModuleNotFoundError: No module named 'tokenization'
I am really confused about this.
The package you’re looking for is bert-tensorflow
, not bert
.
bert-tensorflow is the Python package for Google’s BERT implementation.
bert is a serialization library.
try adding these lines of code as in https://colab.research.google.com/drive/1hMLd5-r82FrnFnBub-B-fVW78Px4KPX1#scrollTo=2IjSWx7-O8yY
issue is that the BERT embedding is now using TensorFlow 2.0. As TensorFlow 2.0 has been released recently.
!pip install tensorflow==2.0
!pip install tensorflow_hub
!pip install bert-for-tf2
!pip install sentencepiece
import tensorflow_hub as hub
import tensorflow as tf
from bert import tokenization
from tensorflow.keras.models import Model # Keras is the new high level API for TensorFlow
import math
Try this, its a new change actually.
pip install bert-tensorflow
from bert import bert_tokenization
tokenizer=bert_tokenization.FullTokenizer(vocab_file,do_lower_case)
I’m using Python – 3.8, tensorflow – 2.9.2.
Getting below error after using bert-tensorflow.
27 import six
28 import tensorflow.compat.v1 as tf
—> 29 from tensorflow.contrib import layers as contrib_layers
30
31
ModuleNotFoundError: No module named ‘tensorflow.contrib’