Skip to content
Back to skills

Tf Agents Lstm Multi Stock Training

ASecurity

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

  • 17 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added May 30, 2026
ai-agentsapi

Works with

  • api

Security analysis

A100/100

Scanned May 30, 2026

npx -y skills add gabrielmoreira/agent-skills-mirror --skill tf_agents_lstm_multi_stock_training --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Tf Agents Lstm Multi Stock Training?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Tf Agents Lstm Multi Stock Training
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/gabrielmoreira-tf-agents-lstm-multi-stock-training/badge)](https://www.skillsdirectory.com/skills/gabrielmoreira-tf-agents-lstm-multi-stock-training)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "6b87d923-c9f7-4084-b2cd-5bb83ffa909c"
name: "tf_agents_lstm_multi_stock_training"
description: "配置TF-Agents的DQN代理使用自定义LSTM网络处理多只股票的时间序列数据,涵盖环境批量打包、维度适配、网络初始化避坑以及完整的训练与评估循环,兼容TensorFlow 2.10.1。"
version: "0.1.1"
tags:
  - "TF-Agents"
  - "LSTM"
  - "DQN"
  - "强化学习"
  - "股票交易"
  - "TensorFlow"
triggers:
  - "TF-Agents LSTM网络配置"
  - "多股票强化学习训练"
  - "DQN LSTM维度适配"
  - "TF 2.10.1兼容性配置"
  - "BatchedPyEnvironment使用"
---

# 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使用

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.