精簡版 DQN 的動作選擇
select_action() 函式會讓代理在每一步都選擇 Q 值最高的動作。
這個函式的引數是 Q 網路與當前 state,並回傳 Q 值最高之動作的索引。
Q 網路已以 q_network 建立,且環境中已載入一個隨機的 state:state = torch.rand(8),作為你可以操作的範例資料。
本練習屬於課程
Python 的深度強化學習
練習說明
- 計算作為引數提供之 state 中,各個動作對應的 Q 值。
- 取得 Q 值最高之動作所對應的索引。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def select_action(q_network, state):
# Calculate the Q-values
q_values = ____
print("Q-values:", [round(x, 2) for x in q_values.tolist()])
# Obtain the action index with highest Q-value
action = torch.____.item()
print(f"Action selected: {action}, with q-value {q_values[action]:.2f}")
return action
select_action(q_network, state)