New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot train canned estimators in multiple estimator.train() calls when using tf.keras.optimizers or tf.optimizers #33358
Comments
@JoshEZiegler , |
I'm not sure what you mean by a gist of colab. The gist I provided was saved from colab and shows to me a link to open the ipynb in colab. It may have been still processing something when you saw it? I'm a bit unfamiliar with particulars of colab/gists so I could certainly be making a mistake. After checking the traceback, it appears that the issue I had occurred with 2.0rc2 so this could be specific to that version (maybe that's what you're saying here). In that case maybe this is solved in the most recent release? |
I tried versions 2.0.0-rc1 and 2.0.0 with this same ipynb in colab but they both gave me the same error. Changing to tf.optimizers rather than keras does not change it either. |
@JoshEZiegler , |
Here is a link directly to the code in colab with TF 2.0.0: TF 2.0.0-rc2: TF 2.0.0-rc1: |
I tried running the linked code and I confirm I also see an error:
|
With some investigation, it looks like running estimator.train() multiple times is ok with the default optimizer, or by specifying the optimizer with a string. I believe that this is because the estimator.train() actually creates a new instance of the string-specified optimizer with each call, but retains the optimizer object if one was specified in the train call (see below). A possible workaround for the above error could be to modify this function to return a fresh optimizer with the same parameters as the opt instance specified. However, I'm not sure if this is easily achievable or if there are any use cases that this would break... From /tensorflow/estimator/blob/master/tensorflow_estimator/python/estimator/canned/optimizers.py
|
@JoshEZiegler I'm not sure if this is applicable here, but I got the same error in different context and solution for me was to pass a callable instead of the instance as |
@awolant I believe that workaround ends up being equivalent to what I mentioned just above your comment: passing a string to specify the optimizer. The key use case excluded by these workarounds is manual tuning of the hyperparams of the optimizer. It's unclear to me if there's a way to do that without passing an instance of |
@JoshEZiegler Right. My use case was a bit different and I missed that. But since we can pass any callable, then maybe something like this will work for you: from functools import partial
AdamWithParams = partial(Adam, learning_rate = 0.1)
...
optimizer = AdamWithParams More on what |
@awolant Nice! Thanks, that sounds like the perfect workaround. I'll leave this issue open because I don't believe that is the intended way to use |
Can you try with the latest tf-nightly? This should be already fixed. |
@yhliang2018 This issue still shows up with tf-nightly-2.2.0.dev20200306. See this colab notebook. Was there a specific version where you believe it should work? |
Have you resolved it in TF-v2 |
I have got the same issue here too |
Experienced the same issue in the latest RuntimeError: Cannot set `iterations` to a new Variable after the Optimizer weights have been created |
@mustafa-qamaruddin @JoshEZiegler Could you provide your use case of calling I'm working on a fix, and would like to check it in if more context on the use cases are provided. Thanks! |
@yhliang2018 Sure, my use was to manually log loss/accuracy/metrics for plotting, etc within a notebook. To get around this error I went ahead and switched to using tensorboard for achieving this same thing so I'm no longer affected by this. Hopefully this is the type of context you're looking for?
|
@JoshEZiegler Thanks a lot for providing your use case. Yes, I think it's still good to support such use cases when you prefer to log/check the model related info manually. I will have the fix submitted soon, and let you know when it's available in |
More thoughts on this issue: if people create an optimizer instance for canned estimator, it's natural that people think the created optimizer object is the one used in model optimization process. However, if different optimizer instances are created to support
@JoshEZiegler given this, how about let's hold to check in the fix for now, and wait for more feedbacks? |
i got the same error, have you solved it ? |
This workaround could be an option, but possibly not for your use case. |
The same error occurs when running this same colab notebook using the latest tf-nightly. |
Instead of linear_regressor = tf.estimator.LinearRegressor(
feature_columns=feature_columns,
optimizer=lambda:tf.keras.optimizers.SGD(learning_rate=0.0000001, clipnorm=5.0),
) |
We are checking to see if this is still an issue, Can you take a look at this workaround proposed by @simnalamburt and let us know if it helps? Thanks! |
This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you. |
@sanatmpa1 Hi it looks like the workaround suggested by @simnalamburt works as well. Without a workaround it still appears to be an issue. (Tested with version 2.8.0-dev20211019) |
Thanks for the confirmation @JoshEZiegler |
Hi There, This is a stale issue. As you are using an older version of tensorflow, we are checking to see if you still need help on this issue. Please test the issue with the latest TensorFlow (TF2.7 and tf-nightly). If the issue still persists with the newer versions of TF, please feel free to open it in keras-team/keras repository by providing details about the issue and a standalone code to reproduce the issue. Thanks! Please note that Keras development has moved to a separate Keras-team/keras repository to focus entirely on only Keras. Thanks! |
Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template
System information
Describe the current behavior
When training a canned estimator with multiple tf.train calls while using any tf.keras.optimizer the optimizer raises an exception.
Describe the expected behavior
Repeated tf.train calls train for the given amount of steps.
Code to reproduce the issue
Lightly edited example using canned estimators:
https://gist.github.com/JoshEZiegler/2a923a707d831ca7efd33dbfbf9779c9
Other info / logs
RuntimeError Traceback (most recent call last)
in ()
5 classifier.train(
6 input_fn=lambda: input_fn(train, train_y, training=True),
----> 7 steps=500)
7 frames
/tensorflow-2.0.0-rc2/python3.6/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py in iterations(self, variable)
660 def iterations(self, variable):
661 if self._iterations is not None:
--> 662 raise RuntimeError("Cannot set
iterations
to a new Variable after "663 "the Optimizer weights have been created")
664 self._iterations = variable
RuntimeError: Cannot set
iterations
to a new Variable after the Optimizer weights have been createdThe text was updated successfully, but these errors were encountered: