addons icon indicating copy to clipboard operation
addons copied to clipboard

AdamW weight_decay cannot be model.saved() TypeError: Unable to serialize

Open mw66 opened this issue 3 years ago • 0 comments

TypeError: Unable to serialize 1.0000000656873453e-05 to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.

https://stackoverflow.com/questions/69882263/tf-keras-callbacks-modelcheckpoint-type-error-unable-to-serialize-1-0000000656

wd is directly copy-&-pasted from:

https://www.tensorflow.org/addons/api_docs/python/tfa/optimizers/AdamW

step = tf.Variable(0, trainable=False)
schedule = tf.optimizers.schedules.PiecewiseConstantDecay(
    [10000, 15000], [1e-0, 1e-1, 1e-2])
# lr and wd can be a function or a tensor
lr = 1e-1 * schedule(step)
wd = lambda: 1e-4 * schedule(step)

# ...

optimizer = tfa.optimizers.AdamW(learning_rate=lr, weight_decay=wd)

On stackoverflow, there is a workaround (kind of hackish). But I'm wondering:

  1. can this be fixed directly here?
  2. is there a better proper workaround?

Thanks.

mw66 avatar May 16 '22 06:05 mw66