在pip安装bert

时间:2019-06-12 03:43:26

标签: python nlp bert-language-model

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

但是,在使用pip install bert在终端中安装bert之后,我无法从bert导入文件(例如run_classifier,优化等)。我试图在木星笔记本中运行以下代码:

import bert
from bert import run_classifier

错误是:

ImportError: cannot import name 'run_classifier'

然后我在\anaconda3\lib\python3.6\site-packages中找到了名为'bert'的文件,并且里面没有名为'run_classifier','optimization'等的python文件。因此,我从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'

对此我真的很困惑。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

尝试像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