MatlabNewb
MatlabNewb

Reputation: 787

Tensorflow._api.v2.train has no attribute 'AdamOptimizer'

When using

model.compile(optimizer = tf.train.AdamOptimizer(),
              loss = 'sparse_categorical_crossentropy',
              metrics=['accuracy'])

in my Jupyter Notebook the following Error pops up:

module 'tensorflow._api.v2.train' has no attribute 'AdamOptimizer'

Tensorflow Version: 2.0.0-alpha0


Do you think the only possibility is to downgrade the TF version?

Upvotes: 58

Views: 76003

Answers (7)

Faisal Buzaid
Faisal Buzaid

Reputation: 81

It should be:

tf.compat.v1.train.AdamOptimizer()

Upvotes: 8

Adi
Adi

Reputation: 51

It's a minor change in upgraded version.

Please use:

model.compile(optimizer=tf.optimizers.Adam(), loss="sparse_categorical_crossentropy")

Thanks!

Upvotes: 4

user12424147
user12424147

Reputation:

I had the same error. I removed

tf.train.AdamOptimizer() 

And I wrote

tf.optimizers.Adam()

Instead.

Upvotes: 8

ShaneCalder
ShaneCalder

Reputation: 346

model.compile(optimizer = tf.keras.optimizers.Adam(),
              loss = 'sparse_categorical_crossentropy',
              metrics=['accuracy'])

Upvotes: 27

Rriskit
Rriskit

Reputation: 565

tf.optimizers.Adam()

Is the way to go. No reason to downgrade.
There are lots of changes in tf 2.0 compared to 1.14.
Note that the parameter-names of Adam have changed, too. e.g. beta1 is now beta_1, check the documentation in Meixu Songs link.

Upvotes: 11

Meixu Song
Meixu Song

Reputation: 1046

tf.train.AdamOptimizer() => tf.optimizers.Adam()

From https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/optimizers

Upvotes: 93

William G
William G

Reputation: 159

I haven't tried 2.0 yet, but from what I've seen on the dev submit videos, you can use

model.compile(optimizer = 'adam',
           loss = 'sparse_categorical_crossentropy',
           metrics=['accuracy'])

Upvotes: 15

Related Questions