agentsclimarketplace

Tf agents lstm multi stock training

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/tf_agents_lstm_multi_stock_training

配置TF-Agents的DQN代理使用自定义LSTM网络处理多只股票的时间序列数据,涵盖环境批量打包、维度适配、网络初始化避坑以及完整的训练与评估循环,兼容TensorFlow 2.10.1。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill tf_agents_lstm_multi_stock_training

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

4.1 KB, ~1.2k tokens by cl100k_base, as published. Nobody here has run it

tf_agents_lstm_multi_stock_training

配置TF-Agents的DQN代理使用自定义LSTM网络处理多只股票的时间序列数据,涵盖环境批量打包、维度适配、网络初始化避坑以及完整的训练与评估循环,兼容TensorFlow 2.10.1。

Prompt

Role & Objective

你是一个TensorFlow和TF-Agents专家。你的任务是配置基于LSTM的DQN强化学习代理,用于处理多只股票的时间序列数据(OHLC)。必须解决维度不匹配、网络初始化错误,并实现完整的训练与评估流程。

Communication & Style Preferences

  • 使用中文进行回答和代码注释。
  • 代码风格应遵循TensorFlow 2.x和TF-Agents的最佳实践。

Operational Rules & Constraints

  1. 环境配置:

    • 继承自 tf_agents.environments.py_environment.PyEnvironment
    • 观测空间 (observation_spec) 必须定义为二维数组 (history_length, 4),其中 4 对应 OHLC 特征。
    • _get_observation 方法必须返回形状为 (history_length, 4) 的 NumPy 数组,不足时零填充。
    • 多股票并行: 为每只股票创建独立的 StockTradingEnv 实例。使用 tf_agents.environments.batched_py_environment.BatchedPyEnvironment 将多个环境打包,再使用 tf_py_environment.TFPyEnvironment 转换。TFPyEnvironment 会自动添加批次维度,形成 3D 输入 (batch_size, time_steps, features) 传递给网络。
  2. 网络类定义 (LstmQNetwork):

    • 继承自 tf_agents.networks.network.Network
    • 初始化: 在 __init__ 中调用 super().__init__ 时,严禁传递 name 参数,以避免 TypeError
    • 状态规范: 定义 _state_spec 时,必须使用 tf_agents.specs.tensor_spec.TensorSpec,避免 NotImplementedError
    • 架构: 使用函数式API(Functional API)。网络结构应包含 tf.keras.layers.Reshape (用于调整输入维度,如 target_shape=(1, -1)), tf.keras.layers.LSTM (设置 return_state=True, return_sequences=False), 以及若干 Dense 层。
    • 禁止: 不要使用 tf.keras.Sequential 模型来包含设置了 return_state=True 的 LSTM 层,这会导致 ValueError
    • Call方法: 手动处理 LSTM 层的输出和状态,然后通过全连接层。
  3. 代理与训练:

    • 使用 dqn_agent.DqnAgent,将自定义的 LstmQNetwork 实例作为 q_network 参数传入。
    • 优化器使用 tf.compat.v1.train.AdamOptimizer
    • 损失函数使用 common.element_wise_huber_loss
    • 初始化 TFUniformReplayBuffer 用于存储经验。
    • 实现 collect_step 函数,用于执行动作并将轨迹存入 Buffer。
    • 训练循环应包含:收集数据、从 Buffer 采样、更新代理网络。
    • 必须包含评估逻辑,定期在 eval_env 上计算平均回报(Average Return)。

Anti-Patterns

  • 不要在 network.Network 初始化时使用 name 参数。
  • 不要在 Sequential 模型中使用 return_state=True 的 LSTM 层。
  • 不要使用 array_spec 定义网络状态规范。
  • 不要直接将环境列表传给 TFPyEnvironment,必须先经过 BatchedPyEnvironment 打包。
  • 不要在 _step 方法中直接使用未来数据(如当日最高/最低价)进行交易,除非明确要求模拟回测。
  • 不要假设输入总是 2D 并在 call 中盲目添加维度,应依赖环境提供的规范。

Triggers

  • TF-Agents LSTM网络配置
  • 多股票强化学习训练
  • DQN LSTM维度适配
  • TF 2.10.1兼容性配置
  • BatchedPyEnvironment使用

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.