简体   繁体   中英

Multilabel text classification using BERT and Tensorflow 2

I am trying to build a simple multilabel text classification pipeline using BERT; the goal is to classify the content of social media posts and any post can have more than one label (ie, a post can be labeled both "Medications" and "Physical and Mental Health"). I am very new to BERT and was trying to follow this example I found: https://towardsdatascience.com/building-a-multi-label-text-classifier-using-bert-and-tensorflow-f188e0ecdc5d I have some questions on how to set it up for this task. In my Anaconda system I have previously installed Tensorflow version 2.0. I have ran the command "pip install bert-tensorflow" and then ran the following:

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

And got this error at the step "from bert import run_classifier":

ModuleNotFoundError: No module named 'tensorflow.contrib'

I did some research and found out that Tensorflow 2.0 indeed does not have the contrib module, but earlier versions do. I just wanted some ideas on how to resolve this issue? I do not want to downgrade my Tensorflow version. And, if anyone can please point me to other examples of multilabel text classification and BERT I would greatly appreciate it (so far the ones I've seen are not very easy to follow).

Thank you

You can avoid this error by selecting Tensorflow version 1.x before your code:

%tensorflow_version 1.x

import tensorflow as tf
tf.__version__   # to check the tensorflow version

Output:

TensorFlow 1.x selected.
'1.15.2' 

This code line will convert Tensorflow version to 1.15 for your kernel runtime and now you can import the libraries and run your code without error:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM