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

Thank you for submitting a TensorFlow documentation issue. Per our GitHub policy, we only address code/doc bugs, performance issues, feature requests, and build/installation issues on GitHub.

The TensorFlow docs are open source! To get involved, read the documentation contributor guide: https://www.tensorflow.org/community/contribute/docs

URL(s) with the issue:

Please provide a link to the documentation entry, for example: https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/estimator/LinearClassifier

Description of issue (what needs changing):

FtrlOptimizer is not accessible from tf.train:

# Or estimator using the FTRL optimizer with regularization.
estimator = LinearClassifier(
    feature_columns=[categorical_column_a,
                     categorical_feature_a_x_categorical_feature_b],
    optimizer=tf.train.FtrlOptimizer(
      learning_rate=0.1,
      l1_regularization_strength=0.001
    )
    ### should be optimizer=tf.keras.optimizers.Ftrl(...)
)

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

Clear description

N/A

Correct links

N/A

Parameters defined

N/A

Returns defined

N/A

Raises listed and defined

N/A

Usage example

N/A

Request visuals, if applicable

N/A

Submit a pull request?

N/A

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I had the same problem here:

!pip install scikit-learn
!pip install tensorflow-gpu
!pip install tensorflow-hub
!pip install bert-tensorflow
from sklearn.model_selection import train_test_split
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from datetime import datetime

import bert
from bert import run_classifier
from bert import optimization
from bert import tokenization
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-1ccb11d8dffa> in <module>
      1 import bert
----> 2 from bert import run_classifier
      3 from bert import optimization
      4 from bert import tokenization

~/anaconda3/envs/py37/lib/python3.7/site-packages/bert/run_classifier.py in <module>
     23 import os
     24 from bert import modeling
---> 25 from bert import optimization
     26 from bert import tokenization
     27 import tensorflow as tf

~/anaconda3/envs/py37/lib/python3.7/site-packages/bert/optimization.py in <module>
     85 
     86 
---> 87 class AdamWeightDecayOptimizer(tf.train.Optimizer):
     88   """A basic Adam optimizer that includes "correct" L2 weight decay."""
     89 

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

while using

!pip install tensorflow-gpu==1.15.0

I get a deprecation warning only

WARNING:tensorflow:From /home/ubuntu/anaconda3/envs/py37/lib/python3.7/site-packages/bert/optimization.py:87: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

see this notebook: https://github.com/loretoparisi/bert-movie-reviews-sentiment-classifier/blob/master/src/bert_sentiment_classifier.ipynb

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.

@loretoparisi I’m getting a similar error trying to run the Bert example here in my own Jupyter notebook running on EC2 (using TF2.0 on an Ubuntu deep learning AMI):

https://colab.research.google.com/github/google-research/bert/blob/master/predicting_movie_reviews_with_bert_on_tf_hub.ipynb#scrollTo=IhJSe0QHNG7U

from bert import tokenization

AttributeError                            Traceback (most recent call last)
<ipython-input-24-f9c3f4b9c6d5> in <module>()
----> 1 from bert import optimization

~/bert/optimization.py in <module>()
     85 
     86 
---> 87 class AdamWeightDecayOptimizer(tf.train.Optimizer):
     88   """A basic Adam optimizer that includes "correct" L2 weight decay."""
     89 

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

Even running the code in the Colab notebook gives a similar warning (although not an error): WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/bert/optimization.py:87: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.