PyTorch 软最大 return
PyTorch softmax return
我是 PyTorch 的新手,一直在关注 this tutorial 进行强化学习。我的环境是一个不使用健身房环境的自定义 Pacman 游戏。游戏循环得到处理。此 Pacman 游戏中的对象允许访问状态数据。我使用这些数据将输入发送到我的深度 Q 网络。
首先,我将 python 列表中的输入更改为张量,以便我的深度 Q 网络可以将其作为输入。以下是我如何将 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 Network:
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
returns 这个 tensor([[0]])
。我不知道从中得到什么。我的印象是 softmax 返回了每个动作的概率。我的操作 space 中有 5 个可用操作。我不知道如何处理 test_net
的输出。我想从此 test_net
中获得一个动作选择,但得到的是一个整数。
我的问题是,输入是否应该采用不同的形状?我是否正确地将 python 输入列表转换为张量?我有 5 个特征,它们是 tensor([[0., 1., 1., 0., 1.]])
。输出 tensor([[0]])
应该是浮点数而不是 0 吗?
Softmax 确实为每个动作分配了一个概率,但是您在从 DQN 获得结果后调用 .max(1)[1]
,它计算轴 1 (.max(1)
) 上的 max 和 argmax 并选择 argmax ( [1]
)。之后,你也把它看成(1,1)
形状,这就是为什么最后你有一个只有一个单元格的二维张量,包含网络给出的概率最大的索引。
尝试直接调用 DQN 实例,它将 return 完整的 softmax 输出。
我是 PyTorch 的新手,一直在关注 this tutorial 进行强化学习。我的环境是一个不使用健身房环境的自定义 Pacman 游戏。游戏循环得到处理。此 Pacman 游戏中的对象允许访问状态数据。我使用这些数据将输入发送到我的深度 Q 网络。 首先,我将 python 列表中的输入更改为张量,以便我的深度 Q 网络可以将其作为输入。以下是我如何将 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 Network:
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
returns 这个 tensor([[0]])
。我不知道从中得到什么。我的印象是 softmax 返回了每个动作的概率。我的操作 space 中有 5 个可用操作。我不知道如何处理 test_net
的输出。我想从此 test_net
中获得一个动作选择,但得到的是一个整数。
我的问题是,输入是否应该采用不同的形状?我是否正确地将 python 输入列表转换为张量?我有 5 个特征,它们是 tensor([[0., 1., 1., 0., 1.]])
。输出 tensor([[0]])
应该是浮点数而不是 0 吗?
Softmax 确实为每个动作分配了一个概率,但是您在从 DQN 获得结果后调用 .max(1)[1]
,它计算轴 1 (.max(1)
) 上的 max 和 argmax 并选择 argmax ( [1]
)。之后,你也把它看成(1,1)
形状,这就是为什么最后你有一个只有一个单元格的二维张量,包含网络给出的概率最大的索引。
尝试直接调用 DQN 实例,它将 return 完整的 softmax 输出。