在 Python 3.11.6 中导入 Keras 和 TensorFlow 时 CartPole 游戏中的语法错误

SyntaxError in CartPole Game when importing Keras and TensorFlow in Python 3.11.6

提问人:CodeCracker 提问时间:10/6/2023 更新时间:10/6/2023 访问量:14

问:

import gym
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam

# Create the CartPole environment
env = gym.make('CartPole-v1')

# Define the neural network model
model = Sequential()
model.add(Dense(24, input_dim=4, activation='relu'))
model.add(Dense(24, activation='relu'))
model.add(Dense(2, activation='linear'))
model.compile(loss='mse', optimizer=Adam(lr=0.001))

# Training parameters
EPISODES = 1000
MAX_STEPS = 500
GAMMA = 0.99

# Training loop
for episode in range(EPISODES):
    state = env.reset()
    state = np.reshape(state, [1, 4])
    total_reward = 0
    
    for step in range(MAX_STEPS):
        env.render()
        
        # Choose an action using epsilon-greedy policy
        epsilon = 0.1
        if np.random.rand() < epsilon:
            action = env.action_space.sample()  # Explore
        else:
            Q_values = model.predict(state)
            action = np.argmax(Q_values[0])  # Exploit

        # Take the chosen action
        next_state, reward, done, _ = env.step(action)
        next_state = np.reshape(next_state, [1, 4])

        # Update the Q-values using the Bellman equation
        target = reward + GAMMA * np.amax(model.predict(next_state)[0])
        target_f = model.predict(state)
        target_f[0][action] = target
        model.fit(state, target_f, epochs=1, verbose=0)

        total_reward += reward
        state = next_state

        if done:
            print(f"Episode: {episode + 1}, Total Reward: {total_reward}")
            break

env.close()

我的模块版本是否正确。如果是,我的问题的解决方案是什么

我已经在我的系统中安装了这些

  • Python版本:3.11.6

  • Keras 版本:2.12.0

  • 健身房版本:0.25.2

  • Numpy版本:1.23.5

这是运行代码的输出

PS C:\Users\hp> python -u "rl_01.py"
DeprecationWarning: the imp module is deprecated in favour of importlib and slated for removal in Python 3.12; see the module's documentation for alternative uses
Traceback (most recent call last):
  File "rl_01.py", line 3, in <module>
    from keras.models import Sequential
  File "path_to_keras\__init__.py", line 21, in <module>
    from keras import models
  File "path_to_keras\models\__init__.py", line 18, in <module>
    from keras.engine.functional import Functional
  File "path_to_keras\engine\functional.py", line 24, in <module>
    import tensorflow.compat.v2 as tf
  File "path_to_tensorflow\__init__.py", line 37, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "path_to_tensorflow\python\__init__.py", line 45, in <module>
    from tensorflow.python.feature_column import feature_column_lib as feature_column
  File "path_to_tensorflow\python\feature_column\feature_column_lib.py", line 18, in <module>
    from tensorflow.python.feature_column.feature_column import *
  File "path_to_tensorflow\python\feature_column\feature_column.py", line 143, in <module>
    from tensorflow.python.layers import base
  File "path_to_tensorflow\python\layers\base.py", line 16, in <module>
    from tensorflow.python.keras.legacy_tf_layers import base
  File "path_to_tensorflow\python\keras\__init__.py", line 25, in <module>
    from tensorflow.python.keras import models
  File "path_to_tensorflow\python\keras\models.py", line 20, in <module>
    from tensorflow.python.keras import metrics as metrics_module
  File "path_to_tensorflow\python\keras\metrics.py", line 34, in <module>
    from tensorflow.python.keras import activations
  File "path_to_tensorflow\python\keras\activations.py", line 18, in <module>
    from tensorflow.python.keras.layers import advanced_activations
  File "path_to_tensorflow\python\keras\layers\__init__.py", line 22, in <module>
    from tensorflow.python.keras.engine.input_layer import Input
  File "path_to_tensorflow\python\keras\engine\input_layer.py", line 24, in <module>
    from tensorflow.python.keras.engine import base_layer
  File "path_to_tensorflow\python\keras\engine\base_layer.py", line 48, in <module>
    from tensorflow.python.keras.engine import base_layer_utils
  File "path_to_tensorflow\python\keras\engine\base_layer_utils.py", line 31, in <module>
    from tensorflow.python.keras.utils import tf_utils
  File "path_to_tensorflow\python\keras\utils\tf_utils.py", line 22, in <module>
    from tensorflow.python.distribute.coordinator import cluster_coordinator as coordinator_lib
  File "path_to_tensorflow\python\distribute\coordinator\cluster_coordinator.py", line 30, in <module>
    from tensorflow.python.distribute import parameter_server_strategy_v2
  File "path_to_tensorflow\python\distribute\parameter_server_strategy_v2.py", line 31, in <module>
    from tensorflow.python.distribute import parameter_server_strategy
  File "path_to_tensorflow\python\distribute\parameter_server_strategy.py", line 31, in <module>
    from tensorflow.python.distribute.cluster_resolver import SimpleClusterResolver
  File "path_to_tensorflow\python\distribute\cluster_resolver\__init__.py", line 27, in <module>
    from tensorflow.python.distribute.cluster_resolver.gce_cluster_resolver import GCEClusterResolver
  File "path_to_cluster_resolver.py", line 24, in <module>
    from googleapiclient import discovery  # pylint: disable=g-import-not-at-top
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "path_to_discovery.py", line 57, in <module>
    from googleapiclient import _auth, mimeparse
  File "path_to_auth.py", line 34, in <module>
    import oauth2client.client
  File "path_to_oauth2client\client.py", line 47, in <module>
    from oauth2client import crypt
  File "path_to_oauth2client\crypt.py", line 55, in <module>
    from oauth2client import _pycrypto_crypt
  File "path_to_pycrypto_crypt.py", line 17, in <module>
    from Crypto.PublicKey import RSA
  File "path_to_crypto\PublicKey\__init__.py", line 29, in <module>
    from Crypto.Util.asn1 import (DerSequence, DerInteger, DerBitString,
  File "path_to_crypto\Util\asn1.py", line 33, in <module>
    from Crypto.Util.number import long_to_bytes, bytes_to_long
  File "path_to_crypto\Util\number.py", line 398
    s = pack('>I', n & 0xffffffffL) + s
                                ^
SyntaxError: invalid hexadecimal literal

我尝试在网上搜索解决方案,但还没有找到明确的答案。任何帮助将不胜感激!

python-3.x keras 强化-学习 openai-gym

评论


答: 暂无答案