'Why can't I import functions in bert after pip install bert

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 \anaconda3\lib\python3.6\site-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.



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Proyag
Solution 2 Shaina Raza
Solution 3 procrastinator