AttributeError: ‘Adam’对象没有’get_updates’属性

huangapple go评论77阅读模式
英文:

AttributeError: 'Adam' object has no attribute 'get_updates'

问题

我正在使用 TensorFlow Keras 后端训练一个 VAE,并且我正在使用 Adam 作为优化器。我使用的代码如下:

def compile(self, learning_rate=0.0001):
    optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
    self.model.compile(optimizer=optimizer,
                       loss=self._calculate_combined_loss,
                       metrics=[_calculate_reconstruction_loss,
                                calculate_kl_loss(self)])

我使用的 TensorFlow 版本是 2.11.0。我遇到的错误是:

AttributeError: 'Adam' object has no attribute 'get_updates'

我怀疑这个问题是由于版本不匹配引起的。有人可以帮我解决这个问题吗?提前感谢。

英文:

I'm training a VAE with TensorFlow Keras backend and I'm using Adam as the optimizer. the code I used is attached below.

    def compile(self, learning_rate=0.0001):
    optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
    self.model.compile(optimizer=optimizer,
                       loss=self._calculate_combined_loss,
                       metrics=[_calculate_reconstruction_loss,
                                calculate_kl_loss(self)])

The TensorFlow version I'm using is 2.11.0. The error I'm getting is

AttributeError: 'Adam' object has no attribute 'get_updates'

I'm suspecting the issues arise because of the version mismatch. Can someone please help me to sort out the issue? Thanks in advance.

答案1

得分: 7

请尝试将你的第二行代码中的:

"optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)"

替换为:

"optimizer = tf.keras.optimizers.**legacy**.Adam(learning_rate=learning_rate)"

有关更多信息,请查看 https://github.com/tensorflow/tensorflow/releases 中的 tf 2.11.0 Release 11/28/2022 版本,特别指出:"tf.keras.optimizers.Optimizer 基类现在指向了新的 Keras 优化器,而旧的优化器已经移到了 tf.keras.optimizers.legacy 命名空间中。"

英文:

Try replacing your 2nd line
"optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)"
by
"optimizer = tf.keras.optimizers.legacy.Adam(learning_rate=learning_rate)"

For further information, check tf 2.11.0 Release 11/28/2022 in https://github.com/tensorflow/tensorflow/releases
It indicates in particular that:
"The tf.keras.optimizers.Optimizer base class now points to the new Keras optimizer, while the old optimizers have been moved to the tf.keras.optimizers.legacy namespace."

答案2

得分: 2

最近,我不得不使用tensorflow2.5,并将所有的"import keras"替换为"import tensorflow.keras"。现在我使用tensorflow2.12,遇到了这个错误,当我恢复了这些替换时,这个错误被解决了。谢谢!

英文:

Of late, I had to use the tensorflow2.5 and I replaced all "import keras" by "import tensorflow.keras".
Now I use tensorflow2.12 and I met this error and when I returned those replacements; this error was removed.
thank you!

答案3

得分: 2

  1. 通过使用 tf.keras.optimizers.legacy.SGD 而不是 tf.keras.optimizers.SGD,两种方法对我有效。

  2. 导入语句已更改为 import keras,从 import tensorflow.keras as kerasimport keras

英文:

Two ways worked for me,

  1. By using tf.keras.optimizers.legacy.SGD - instead of tf.keras.optimizers.SGD

  2. Importing statement is changed from
    import tensorflow.keras as keras to 'import keras'

huangapple
  • 本文由 发表于 2023年2月6日 10:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定