我是bert的初学者,我正在尝试使用GitHub上给出的bert文件:https://github.com/google-research/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‘的文件,里面没有名为'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'我对此真的很困惑。
发布于 2019-06-12 16:36:16
发布于 2020-04-06 19:53:27
尝试添加这些代码行,因为在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 mathhttps://stackoverflow.com/questions/56554380
复制相似问题