Belkacem Thiziri
Belkacem Thiziri

Reputation: 665

Error importing BERT: module 'tensorflow._api.v2.train' has no attribute 'Optimizer'

I tried to use bert-tensorflow in Google Colab, but I got the following error:

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 1 import bert ----> 2 from bert import run_classifier_with_tfhub # run_classifier 3 from bert import optimization 4 from bert import tokenization

1 frames /usr/local/lib/python3.6/dist-packages/bert/optimization.py in () 85 86 ---> 87 class AdamWeightDecayOptimizer(tf.train.Optimizer): 88 """A basic Adam optimizer that includes "correct" L2 weight decay.""" 89

AttributeError: module 'tensorflow._api.v2.train' has no attribute 'Optimizer'

Here is the code I tried:

  1. Install the libraries:

!pip install --upgrade --force-reinstall tensorflow !pip install --upgrade --force-reinstall tensorflow-gpu !pip install tensorflow_hub !pip install sentencepiece !pip install bert-tensorflow

  1. Run this code:

from sklearn.model_selection import train_test_split import pandas as pd from datetime import datetime from tensorflow.keras import optimizers import bert from bert import run_classifier from bert import optimization from bert import tokenization

I've also tried import tensorflow.compat.v1 as tf tf.disable_v2_behavior()

But got the same error.

Upvotes: 10

Views: 16403

Answers (4)

Shima Foolad
Shima Foolad

Reputation: 86

I fixed the issue in google colab by installing tensorflow 1.15 instead of 2.0. I get a warning only.

!pip install tensorflow-gpu==1.15.0

Upvotes: 3

LALIT SINGH
LALIT SINGH

Reputation: 29

import tensorflow as tf

print(tf.__version__)

!pip uninstall tensorflow==2.2.0

!pip install tensorflow==1.15.0

!pip install bert-tensorflow

try this. it worked for me for the same issue

Upvotes: 3

mrk
mrk

Reputation: 10366

This issue has been reported and discussed on Github as well,

Try to change the code of line 87 (see your error message: /usr/local/lib/python3.6/dist-packages/bert/optimization.py), from

tf.train.Optimizer
# change to
tf.keras.optimizers.Optimizer

Are you in TF 1.x or TF 2.0? In general, tf.train.Optimizer has been deprecated in TF 2.0, and you need to use tf.compat.v1.Optimizer (then the deprecation message shows up but it's a warning only). In TF 2.0, the Keras optimziers tf.keras.optimizers.* are recommended to use.

Upvotes: 3

neel g
neel g

Reputation: 1255

I did some experimentation in my own colab notebook (please provide a link next time) and I found that in the error message, there was

class AdamWeightDecayOptimizer(tf.train.Optimizer):

this being the header of the class. But there is nothing like tf.train.optimizer instead it should be :

class AdamWeightDecayOptimizer(tf.compat.v1.train.Optimizer):

The link where there is exact issue with (lol) exact same line is here

Upvotes: 9

Related Questions