You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python深度学习模型中TensorFlow/Keras配置及CartPole模型报错排查

问题解决:平衡杆DQN模型报错及TensorFlow/Keras按需运行指南

一、ModuleNotFoundError: No module named 'keras.utils.generic_utils' 报错解决

报错详情

/home/nate/Desktop/00_Temp/02_Coding/DeepReinforcementLearningBalanceGame.py/.venv/bin/python /home/nate/Desktop/00_Temp/02_Coding/DeepReinforcementLearningBalanceGame.py/BalancingMl.py
2024-06-29 11:13:59.059897: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2024-06-29 11:13:59.101402: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2024-06-29 11:13:59.102021: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-06-29 11:13:59.750624: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Traceback (most recent call last):
  File "/home/nate/Desktop/00_Temp/02_Coding/DeepReinforcementLearningBalanceGame.py/BalancingMl.py", line 6, in <module>
    from rl.agents import DQNAgent
  File "/home/nate/Desktop/00_Temp/02_Coding/DeepReinforcementLearningBalanceGame.py/.venv/lib/python3.8/site-packages/rl/agents/__init__.py", line 2, in <module>
    from .dqn import DQNAgent, NAFAgent, ContinuousDQNAgent
  File "/home/nate/Desktop/00_Temp/02_Coding/DeepReinforcementLearningBalanceGame.py/.venv/lib/python3.8/site-packages/rl/agents/dqn.py", line 8, in <module>
    from rl.core import Agent
  File "/home/nate/Desktop/00_Temp/02_Coding/DeepReinforcementLearningBalanceGame.py/.venv/lib/python3.8/site-packages/rl/core.py", line 8, in <module>
    from rl.callbacks import (
  File "/home/nate/Desktop/00_Temp/02_Coding/DeepReinforcementLearningBalanceGame.py/.venv/lib/python3.8/site-packages/rl/callbacks.py", line 12, in <module>
    from keras.utils.generic_utils import Progbar
ModuleNotFoundError: No module named 'keras.utils.generic_utils'

原因分析

你使用的rl包是旧版keras-rl,仅兼容TensorFlow 1.x和独立Keras包。当前环境安装的是TensorFlow 2.x,其中Keras以tf.keras形式集成,不再存在独立的keras.utils.generic_utils路径,导致模块找不到。

解决步骤

  1. 卸载旧版keras-rl
pip uninstall -y keras-rl
  1. 安装适配TensorFlow 2.x的keras-rl2
pip install keras-rl2

代码额外修正

你的代码还有几处小问题,修正后才能正常运行:

  • BoltzmannQPolicy需要实例化,传入类对象会报错,改为policy=BoltzmannQPolicy()
  • np.mea是拼写错误,应为np.mean
  • 新版TensorFlow中,Adam优化器的学习率参数建议用learning_rate替代lr(兼容旧版但更规范)

修正后的完整代码:

import random
import gym
import numpy as np
import tensorflow as tf

from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory

env = gym.make("CartPole-v1")

states = env.observation_space.shape[0]
actions = env.action_space.n

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(1, states)))
model.add(tf.keras.layers.Dense(24, activation="relu"))
model.add(tf.keras.layers.Dense(24, activation="relu"))
model.add(tf.keras.layers.Dense(actions, activation="linear"))

agent = DQNAgent(
    model=model,
    memory=SequentialMemory(limit=50000, window_length=1),
    policy=BoltzmannQPolicy(),  # 修正:实例化策略
    nb_actions=actions,
    nb_steps_warmup=10,
    target_model_update=0.01
)

# 修正:使用learning_rate替代lr
agent.compile(tf.keras.optimizers.Adam(learning_rate=0.001), metrics=["mae"])
agent.fit(env, nb_steps=100000, visualize=False, verbose=1)

results = agent.test(env, nb_episodes=10, visualize=True)

# 修正:np.mean拼写正确
print(np.mean(results.history["episode_reward"]))

env.close()

二、Python深度学习中按需运行TensorFlow和Keras

1. 延迟导入模块

将TensorFlow/Keras的导入语句放在具体使用的函数内部,而非脚本开头,避免程序启动时就加载整个框架:

def train_model():
    import tensorflow as tf
    from rl.agents import DQNAgent
    # 后续模型定义、训练代码

2. 控制TensorFlow内存使用

避免TensorFlow启动时占用全部GPU内存,开启内存增长模式:

import tensorflow as tf

gpus = tf.config.list_physical_devices('GPU')
if gpus:
    try:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)
    except RuntimeError as e:
        print(e)

如果使用CPU,可设置软设备放置,让TensorFlow自动选择可用设备:

tf.config.set_soft_device_placement(True)

3. 及时释放资源

训练/推理完成后,清理Keras会话释放内存:

tf.keras.backend.clear_session()

对于不再使用的模型,可手动删除对象并触发垃圾回收:

import gc

del model, agent
gc.collect()

4. 按需加载模型

如果模型已保存,仅在需要推理时加载:

def predict_with_model():
    model = tf.keras.models.load_model("saved_model.h5")
    # 推理逻辑
    model.predict(...)
    tf.keras.backend.clear_session()

内容的提问来源于stack exchange,提问作者ndh.py

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:44:51