Python 为什么可以';在pip安装bert后,我在bert中导入函数

Python 为什么可以';在pip安装bert后,我在bert中导入函数,python,nlp,bert-language-model,Python,Nlp,Bert Language Model,我是bert的初学者,我正在尝试使用GitHub上提供的bert文件: 但是,在使用pip install bert在终端中安装bert后,我无法从bert导入文件(如run_分类器、优化等)。我尝试在jupiter笔记本中运行以下代码: import bert from bert import run_classifier 错误是: ImportError: cannot import name 'run_classifier' NameError: name 'convert_to_un

我是bert的初学者,我正在尝试使用GitHub上提供的bert文件:

但是,在使用
pip install bert
在终端中安装bert后,我无法从bert导入文件(如run_分类器、优化等)。我尝试在jupiter笔记本中运行以下代码:

import bert
from bert import run_classifier
错误是:

ImportError: cannot import name 'run_classifier'
NameError: name 'convert_to_unicode' is not defined
ModuleNotFoundError: No module named 'tokenization'
然后我在
\anaconda3\lib\python3.6\site packages
中找到了名为“bert”的文件,里面没有名为“run\u classifier”、“optimization”等python文件。所以我从GitHub下载了这些文件,并自己把它们放到文件“bert”中。完成此操作后,我可以导入run_分类器

然而,出现了另一个问题。我无法使用文件中的函数,尽管我可以导入它们。 例如,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')
错误是:

ImportError: cannot import name 'run_classifier'
NameError: name 'convert_to_unicode' is not defined
ModuleNotFoundError: No module named 'tokenization'
然后我试着:

from tokenization import convert_to_unicode
错误是:

ImportError: cannot import name 'run_classifier'
NameError: name 'convert_to_unicode' is not defined
ModuleNotFoundError: No module named 'tokenization'

我真的很困惑

您要查找的软件包是
bert tensorflow
,而不是
bert

是谷歌BERT实现的Python包。

是序列化库。

请尝试添加这些代码行,如中所示 问题是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安装bert tensorflow之后,您就可以使用这些功能了。非常感谢你!顺便说一下,我还想知道为什么我不能使用
从标记化导入convert_to_unicode
而不是
从bert.tokenization导入convert_to_unicode
?非常感谢你!您还可以使用git clonehttps://github.com/google-research/bert.git以便从他们的git存储库中克隆!