-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Update actor_critic_cartpole.py #2154
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
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @lmntrx-sys, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
I've updated the actor_critic_cartpole.py
example to resolve a ValueError
. This was primarily achieved by migrating the environment dependency from gym
to gymnasium
, which is the actively maintained successor. Additionally, I've adjusted the tensor manipulation calls to directly use TensorFlow's convert_to_tensor
and expand_dims
functions, and updated the env.reset()
call to correctly handle the new return signature of gymnasium
.
Highlights
- Environment Library Update: The
gym
library has been replaced withgymnasium
to ensure compatibility and address potential issues, asgymnasium
is the maintained successor togym
. - Tensor Operation Correction: The tensor conversion and expansion operations (
convert_to_tensor
andexpand_dims
) were updated to usetf
(TensorFlow) directly instead ofkeras.ops
, which resolves aValueError
as described by the author. - Environment Reset Signature Adjustment: The call to
env.reset()
was modified toobs, _ = env.reset()
to correctly handle the new return signature ofgymnasium
, which now returns both the observation and an info dictionary.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request updates the actor-critic example to use gymnasium
instead of gym
, which is a good modernization step. It also attempts to fix a ValueError
by changing some keras.ops
calls to direct tf
calls. My review identifies a critical bug where a variable was renamed incorrectly after updating the env.reset()
call, which would cause a NameError
. I've also pointed out an inconsistency introduced by mixing keras.ops
and tf
calls and suggested sticking to keras.ops
for better code style and consistency within the file.
@@ -98,13 +98,13 @@ | |||
episode_count = 0 | |||
|
|||
while True: # Run until solved | |||
state = env.reset()[0] | |||
obs, _ = env.reset() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The env.reset()
call now correctly unpacks two values, but it assigns the observation to a new variable obs
. The following code on line 106 expects the variable state
, which is now undefined in this scope. This will lead to a NameError
. To fix this, you should assign the observation to state
.
obs, _ = env.reset() | |
state, _ = env.reset() |
state = tf.convert_to_tensor(state) | ||
state = tf.expand_dims(state, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines have been changed to use tf
directly, instead of the backend-agnostic keras.ops
. The rest of the file uses keras.ops
(e.g., on lines 116 and 160), so this change introduces an inconsistency. For better code style and to keep the example aligned with Keras best practices, it's recommended to use keras.ops
here as well.
state = tf.convert_to_tensor(state) | |
state = tf.expand_dims(state, 0) | |
state = ops.convert_to_tensor(state) | |
state = ops.expand_dims(state, 0) |
I have pushed a fix for the value error.
i think the fix comes from using gymnasium instead of gym and changing two specific operations which are being handled by keras.ops module
i have realized the fix that is in my repo has a small issue and that is one with the size of the model it's too small but, the correction comes by using gymnasium instead of gym
Also in this line which is found in the training loopobs, _ = env.reset() i have added another variable which is ignored to avoid another error