開始使用免費開始

Q-Network 架構

你已經快要可以訓練你的第一個深度強化學習代理了!在開始第一個完整的訓練迴圈之前,你需要一個神經網路架構,來驅動代理的決策並讓它能學習。

你將修改先前練習中定義的通用架構。 torch 與 torch.nn 已在本練習中匯入。

本練習屬於課程

Python 的深度強化學習

檢視課程

練習說明

  • 建立第一個隱藏層;它的輸入是環境的 state,維度為 state_size。
  • 建立輸出層;它會為每個動作提供 Q-value,維度為 action_size。
  • 完成 forward() 方法;在這個例子中使用 torch.relu 作為啟用函式。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

class QNetwork(nn.Module):
    def __init__(self, state_size, action_size):
        super(QNetwork, self).__init__()
        # Instantiate the first hidden layer
        self.fc1 = nn.Linear(____, ____)
        self.fc2 = nn.Linear(64, 64)
        # Instantiate the output layer
        self.fc3 = nn.Linear(____, ____)
    def forward(self, state):
        # Ensure the ReLU activation function is used
        x = ____(self.fc1(torch.tensor(state)))
        x = ____(self.fc2(x))
        return self.fc3(x)
編輯並執行程式碼