我正在尝试使用BERT建立一个简单的多标签文本分类管道;目标是对社交媒体帖子的内容进行分类,任何帖子都可以有多个标签(即,一篇文章可以被标记为“药物”和“身心健康”)。我对伯特非常陌生,并试图效仿我发现的这个例子:https://towardsdatascience.com/building-a-multi-label-text-classifier-using-bert-and-tensorflow-f188e0ecdc5d,我有一些关于如何为这个任务设置它的问题。在我的Anaconda系统中,我以前安装了Tensorflow 2.0版本。我已经运行了命令"pip install bert-tensorflow“,然后运行以下命令:
import tensorflow as tf
import tensorflow_hub as hub
import bert
from bert import run_classifier
from bert import optimization
from bert import tokenization
from bert import modeling并在“从伯特导入run_classifier”的步骤中获得了此错误:
ModuleNotFoundError: No module named 'tensorflow.contrib'我做了一些研究,发现Tensorflow 2.0确实没有contrib模块,但早期版本确实有。我只是想知道如何解决这个问题?我不想贬低我的Tensorflow版本。而且,如果有人能告诉我其他多标签文本分类的例子,我会非常感激的(到目前为止,我所看到的还不是很容易理解)。
谢谢!
发布于 2022-07-02 14:23:19
您可以通过在代码之前选择Tensorflow version 1.x来避免此错误:
%tensorflow_version 1.x
import tensorflow as tf
tf.__version__ # to check the tensorflow version输出:
TensorFlow 1.x selected.
'1.15.2' 对于内核运行时,此代码行将Tensorflow版本转换为1.15,现在您可以导入库并运行代码,而不会出错:
import tensorflow as tf
import tensorflow_hub as hub
import bert
from bert import run_classifier
from bert import optimization
from bert import tokenization
from bert import modelinghttps://stackoverflow.com/questions/72610356
复制相似问题