为什么pip install bert后不能导入bert中的函数

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

我是 bert 的初学者,我正在尝试使用 GitHub:https://github.com/google-research/bert

上给出的 bert 文件

但是在使用pip install bert在终端中安装bert后,我无法从bert导入文件(例如run_classifier、优化等)。我尝试 运行 在 jupiter notebook 中遵循以下代码:

import bert
from bert import run_classifier

错误是:

ImportError: cannot import name 'run_classifier'

然后在\anaconda3\lib\python3.6\site-packages中找到'bert'的文件,里面没有python个'run_classifier'、'optimization'等文件。所以我从 GitHub 下载了这些文件,然后自己将它们放入文件 'bert' 中。这样做之后我可以导入 run_classifier.

但是,又出现了一个问题。尽管我可以导入它们,但我无法使用文件中的函数。 比如tokenization.py中有一个函数convert_to_unicode:

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.

然后我试了这个:

import tokenization from bert
convert_to_unicode('input.txt')

错误是:

NameError: name 'convert_to_unicode' is not defined

然后我尝试了:

from tokenization import convert_to_unicode

错误是:

ModuleNotFoundError: No module named 'tokenization'

我真的很困惑。

您要查找的包裹是 bert-tensorflow,而不是 bert

bert-tensorflow 是 Google BERT 实现的 Python 包。
bert 是一个序列化库。

尝试添加这些代码行,如 https://colab.research.google.com/drive/1hMLd5-r82FrnFnBub-B-fVW78Px4KPX1#scrollTo=2IjSWx7-O8yY 问题是 BERT 嵌入现在使用的是 TensorFlow 2.0。由于最近发布了 TensorFlow 2.0。

!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

试试这个,它实际上是一个新的变化。

pip install bert-tensorflow

from bert import bert_tokenization
tokenizer=bert_tokenization.FullTokenizer(vocab_file,do_lower_case)