【问题标题】:PyTorch softmax returnPyTorch softmax 返回
【发布时间】:2020-08-03 01:29:57
【问题描述】:

我是 PyTorch 的新手,一直在关注 this tutorial 进行强化学习。我的环境是一个不使用健身房环境的自定义 Pacman 游戏。游戏循环得到照顾。这个吃豆人游戏中的一个对象允许访问状态数据。我使用这些数据将输入发送到我的 Deep Q 网络。 首先,我将 python 列表中的输入更改为张量,以便我的 Deep Q Network 可以将其作为输入。以下是我如何将我的 python 列表转换为张量:

#self.getFeatures() returns a dictionary, so I grab the values and convert it to a list
input = torch.FloatTensor(list(self.getFeatures(gameState, bestAction).values())) \
             .unsqueeze(0) \
             .to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))

然后我将此输入传递给我的策略 Deep Q 网络:

test_net = self.policy_net(input).max(1)[1].view(1, 1)

下面是我的 Deep Q 网络:

class DQN(nn.Module):

def __init__(self, feature_size, action_size):
    super(DQN, self).__init__()
    self.input = nn.Linear(feature_size, 12)
    self.hidden1 = nn.Linear(12, 5)
    self.hidden2 = nn.Linear(5, action_size)

# Called with either one element to determine next action, or a batch
# during optimization. Returns tensor([[left0exp,right0exp]...]).
def forward(self, x):
    x = F.relu(self.input(x))
    x = F.relu(self.hidden1(x))
    x = F.softmax(self.hidden2(x), dim=1)
    return x

输入tensor([[0., 1., 1., 0., 1.]]) 这个test_net 返回这个tensor([[0]])。我不知道能从中得到什么。我的印象是 softmax 返回每个动作的概率。我的操作空间中有 5 个可用的操作。我不知道如何处理来自test_net 的输出。我想从这个test_net 中获得一个动作选择,但我得到的是一个整数。

我的问题是,输入应该是不同的形状吗?我是否正确地将我的 python 输入列表转换为张量?我有 5 个功能,它们是 tensor([[0., 1., 1., 0., 1.]])。输出 tensor([[0]]) 应该是浮点数而不是 0 吗?

【问题讨论】:

    标签: python pytorch softmax


    【解决方案1】:

    Softmax 确实为每个动作分配了一个概率,但是您在从 DQN 获得结果后调用.max(1)[1],它沿轴 1 (.max(1)) 计算 max 和 argmax 并选择 argmax ([1])。之后,您还将其查看为(1,1) 形状,这就是为什么最终您有一个只有一个单元格的二维张量,其中包含网络给出的概率最大的索引。

    尝试直接调用 DQN 实例,它将返回完整的 softmax 输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2018-08-08
      • 2021-03-19
      相关资源
      最近更新 更多