Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lmntrx-sys
Copy link

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

   state = tf.convert_to_tensor(obs)

   state = tf.expand_dims(state, 0)

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

Copy link

google-cla bot commented Aug 4, 2025

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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 with gymnasium to ensure compatibility and address potential issues, as gymnasium is the maintained successor to gym.
  • Tensor Operation Correction: The tensor conversion and expansion operations (convert_to_tensor and expand_dims) were updated to use tf (TensorFlow) directly instead of keras.ops, which resolves a ValueError as described by the author.
  • Environment Reset Signature Adjustment: The call to env.reset() was modified to obs, _ = env.reset() to correctly handle the new return signature of gymnasium, 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

  1. 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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.

Suggested change
obs, _ = env.reset()
state, _ = env.reset()

Comment on lines +106 to +107
state = tf.convert_to_tensor(state)
state = tf.expand_dims(state, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
state = tf.convert_to_tensor(state)
state = tf.expand_dims(state, 0)
state = ops.convert_to_tensor(state)
state = ops.expand_dims(state, 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants