Skip to content

tf-2.0: AttributeError: module 'tensorflow' has no attribute 'optimizers' #27929

Closed
@LGTrader

Description

@LGTrader

System information

  • OS Platform and Distribution Ubuntu 18.04 LTS
  • TensorFlow installed from source or binary): source
  • TensorFlow version (use command below): v2.0.0-alpha0-4-g2c2d508 2.0.0-alpha0
  • Python version: 3.5.7
  • Bazel version (if compiling from source): From docker image
  • GCC/Compiler version (if compiling from source): From docker image
  • CUDA/cuDNN version: NA
  • GPU model and memory: NA

Describe the current behavior
This documentation suggests there are 4 ways to load an optimizer:
https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/optimizers

However:

(tf_35) mark@science:~$ python tf_optimizers.py
Traceback (most recent call last):
File "tf_optimizers.py", line 7, in
opt4 = tf.optimizers.Adagrad
AttributeError: module 'tensorflow' has no attribute 'optimizers'

Describe the expected behavior

opt = tf.optimizers.Adagrad doesn't work. The first 3 methods do.

MINIMAL code to reproduce the issue

import tensorflow as tf
tf.version

opt1 = tf.compat.v2.keras.optimizers.Adagrad
opt2 = tf.compat.v2.optimizers.Adagrad
opt3 = tf.keras.optimizers.Adagrad
opt4 = tf.optimizers.Adagrad

I don't know if this is a documentation issue or a tensorflow bug.

Activity

jvishnuvardhan

jvishnuvardhan commented on Apr 24, 2019

@jvishnuvardhan
Contributor

@LGTrader I don't see any issue. Here is the gist.

Please make sure you have installed TF2.0. When I tried the above gist with TF1.13.1, it has thrown an error. So those four links work well for TF2.0 only. Please let me know what you think. If you agree, please close the issue. Thanks!

LGTrader

LGTrader commented on Apr 25, 2019

@LGTrader
Author

@jvishnuvardhan

I reworked the code and ran it in Jupyter Notebook on my system. I cannot download the same TF2.0 alpha version that you use because my CPU doesn't have an instruction set (AVX I think) that is now required so my version is built locally from the same code as of a couple of weeks ago. It does identify as 2.0-alpha-0

Also, a simple call to help(tf) actually does not show tf.optimizers as an option but does show tf.compat and tf.keras so I think everything is consistent.

For now I'm going to assume that possibly I caught the code in a weird state when I built this and will close the report. If I get a chance to rebuild newer code and still see it I think I can reopen it and we'll try again. If you think it's important to keep looking at this I assume you can reopen also?

Thanks!

import tensorflow as tf
print(tf.version)
opt1 = tf.compat.v2.keras.optimizers.Adagrad
opt2 = tf.compat.v2.optimizers.Adagrad
opt3 = tf.keras.optimizers.Adagrad
print(opt1)
print(opt2)
print(opt3)
2.0.0-alpha0
<class 'tensorflow.python.keras.optimizer_v2.adagrad.Adagrad'>
<class 'tensorflow.python.keras.optimizer_v2.adagrad.Adagrad'>
<class 'tensorflow.python.keras.optimizer_v2.adagrad.Adagrad'>
opt4 = tf.optimizers.Adagrad
print(opt4)

AttributeError Traceback (most recent call last)
in
----> 1 opt4 = tf.optimizers.Adagrad
2 print(opt4)

AttributeError: module 'tensorflow' has no attribute 'optimizers'

Help on package tensorflow:

NAME
tensorflow - Bring in all of the public TensorFlow interface into this module.

PACKAGE CONTENTS
_api (package)
app (package)
audio (package)
autograph (package)
bitwise (package)
compat (package)
compiler (package)
config (package)
contrib (package)
core (package)
data (package)
debugging (package)
distribute (package)
distributions (package)
dtypes (package)
errors (package)
estimator (package)
examples (package)
experimental (package)
feature_column (package)
gfile (package)
graph_util (package)
image (package)
initializers (package)
io (package)
keras (package)
layers (package)
libtensorflow_framework
linalg (package)
lite (package)
logging (package)
lookup (package)
losses (package)
manip (package)
math (package)
metrics (package)
nest (package)
nn (package)
profiler (package)
python (package)
python_io (package)
quantization (package)
queue (package)
ragged (package)
random (package)
raw_ops (package)
resource_loader (package)
saved_model (package)
sets (package)
signal (package)
sparse (package)
spectral (package)
strings (package)
summary (package)
sysconfig (package)
test (package)
tools (package)
tpu (package)
train (package)
user_ops (package)
v1
version (package)

jvishnuvardhan

jvishnuvardhan commented on Apr 25, 2019

@jvishnuvardhan
Contributor

@LGTrader I am reopening it as I noticed Attribute error with tf-nightly. I don't see any issue with TF2.0.0-alpha0. We need to check whether we need to update tf-nightly or the docs on website. Thanks for finding this issue.

AttributeError Traceback (most recent call last)
in ()
3 opt2 = tf.compat.v2.optimizers.Adagrad
4 opt3 = tf.keras.optimizers.Adagrad
----> 5 opt4 = tf.optimizers.Adagrad

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation_wrapper.py in getattr(self, name)
104 if name.startswith('dw'):
105 raise AttributeError('Accessing local variables before they are created.')
--> 106 attr = getattr(self._dw_wrapped_module, name)
107 if (self._dw_warning_count < _PER_MODULE_WARNING_LIMIT and
108 name not in self._dw_deprecated_printed):

AttributeError: module 'tensorflow' has no attribute 'optimizers'

LGTrader

LGTrader commented on Apr 25, 2019

@LGTrader
Author

Thanks. You saved me building again. (about 4 hours on my machine) I'll look for feedback in this thread and rebuild to verify fixes later if appropriate.

tanzhenyu

tanzhenyu commented on Apr 26, 2019

@tanzhenyu
Contributor

I think the latest change to enable tf.optimizers.Adagrad might not be in the alpha0 release, can you install nightly and try it? pip install tf-nightly-gpu-2.0-preview

12 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

TF 2.0Issues relating to TensorFlow 2.0comp:kerasKeras related issuesstat:awaiting tensorflowerStatus - Awaiting response from tensorflowertype:docs-bugDocument issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @LGTrader@tanzhenyu@jvishnuvardhan@lvenugopalan

      Issue actions

        tf-2.0: AttributeError: module 'tensorflow' has no attribute 'optimizers' · Issue #27929 · tensorflow/tensorflow