示例成品 · 平台演示,按左边这组点选真跑出来的
按你给的段落补全如下;`get_candidate_set` 和 `update_state` 是原文没贴出来的部分,先按常见实现占位,你替换成论文对应公式即可。
````markdown
```pseudocode
# ===== 输入 =====
# state_s 当前状态,作为网络输入
# candidate_set_S_t 第 t 轮候选动作集,来自论文 S_t
# net_model 策略网络,输出未归一化 logits
# max_round_T 最大迭代轮数,论文未给出时按实验设置
# ===== 输出 =====
# action_seq_A 每轮采样到的动作序列
# ===== 中间量 =====
# logits_all 网络对全部动作的原始输出
# logits_cand 候选动作对应的 logits
# probs_P softmax 后的概率 P(a|s)
# local_idx 候选集内的局部采样位置
action_seq_A = [] # 初始化输出动作序列
for t = 1 to max_round_T do # 外层循环:对应“每轮迭代”
candidate_set_S_t = get_candidate_set(state_s, t) # 获取当前轮候选集 S_t
if candidate_set_S_t is empty then # 易错点:空候选集做 softmax 会报错
break # 无候选动作,直接终止循环
else # 候选集非空,进入采样
logits_all = net_model(state_s) # 网络输出所有动作 logits
logits_cand = logits_all[candidate_set_S_t] # 筛出候选动作对应的 logits
logits_cand = logits_cand - max(logits_cand) # 易错点:减去最大值防止 exp 上溢
probs_P = softmax(logits_cand) # 原文:P(a|s) 由 logits 经 softmax 归一化得到
local_idx = categorical_sample(probs_P) # 按概率 P(a|s) 从候选集中采样一个位置
action_a = candidate_set_S_t[local_idx] # 易错点:别返回局部索引,要映射回全局动作 ID
action_seq_A.append(action_a) # 记录本轮采样结果
state_s = update_state(state_s, action_a) # 若论文只采样不更新状态,删除此行
end if
end for
return action_seq_A # 返回每轮采样得到的动作序列
```
````
点左边「开工 · 直接出成品」,出一份你自己的版本(文字免费)