从 Pytorch 模型输出权重

Output the weights from a Pytorch model

这是一个非常基本的模型:

class LinearDeepQNetwork(nn.Module):
    def __init__(self, lr, n_actions, input_dims):
        super(LinearDeepQNetwork, self).__init__()

        self.fc1 = nn.Linear(*input_dims, 128)
        self.fc2 = nn.Linear(128, n_actions)

        self.optimizer = optim.Adam(self.parameters(), lr=lr)
        self.loss = nn.MSELoss()
        self.device = T.device('cuda:0' if T.cuda.is_available() else 'cpu')
        self.to(self.device)

    def forward(self, state):
        layer1 = F.relu(self.fc1(state))
        actions = self.fc2(layer1)

        return actions

请注意,我使用的是 Pytorch,而不是 KerasTensorflow。在我的 Agent() class 中,我实例化了 self.Q_eval = LinearDeepQNetwork(self.lr, self.n_actions, self.input_dims)。一旦我训练了我的智能体几集,我需要输出 self.Q_eval 的权重。我怎样才能做到这一点?

我需要将权重从 Q_eval 网络注入到 Q_next 网络。我做了以下功能:

def replace_target_network(self):
        self.Q_next.load_state_dict(self.Q_eval.state_dict())
        self.Q_next.eval()

回答我可以用Q_eval.state_dict()得到权重。