Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 790
Vicky Ding Avatar asked Jun 12 '19 03:06

Vicky Ding


People also ask

How do I install Python Bert?

Installation with conda On Linux platforms, the most comfortable way to install bert is via the conda package manager contained in the Anaconda distribution. Anaconda is a scientific Python distribution with more than 100 Python packages included (~400 Mb).


2 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.

like image 194
Proyag Avatar answered Nov 14 '22 23:11

Proyag


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
like image 29
Shaina Raza Avatar answered Nov 14 '22 22:11

Shaina Raza