1

q_pred = self.Q.forward(states) gives me the following output :

tensor([[-4.4713e-02,  4.2878e-03],
        [-2.2801e-01,  2.2295e-01],
        [-9.8098e-03, -1.0766e-01],
        [-1.4654e-01,  1.2742e-01],
        [-1.6224e-01,  1.6565e-01],
        [-3.6515e-02,  3.1022e-02],
        [-4.5094e-02,  1.4848e-02],
        [-1.4157e-01,  1.3974e-01],
        [-3.0593e-03, -4.2342e-02],
        [-4.1689e-02,  2.9376e-02],
        [-9.3629e-02,  1.0297e-01],
        [-5.2163e-04, -7.4799e-02],
        [-2.8944e-02, -1.2417e-01]], grad_fn=<AddmmBackward>)

and actions gives me the the following output

tensor([1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1], dtype=torch.int32)

From the two above output, how can I can the following result :

tensor([4.2878e-03, -2.2801e-01, -1.0766e-01, 1.2742e-01, -1.6224e-01,  3.1022e-02, 1.4848e-02,
        -1.4157e-01, -4.2342e-02, -4.1689e-02, -9.3629e-02, -7.4799e-02,, -1.2417e-01], grad_fn=<AddmmBackward>)

?

UPDATE

Here is how self.Q is implemented :

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
jgauth
  • 195
  • 1
  • 6
  • 14

1 Answers1

0

you can use:

q_pred[torch.arange(q_pred.size(0)), actions.type(torch.LongTensor)]
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • `*** IndexError: tensors used as indices must be long, byte or bool tensors`. This is the error from your answer. How can I fix that? – jgauth Mar 29 '20 at 00:01
  • @Louis-Andre did you try `q_pred[torch.arange(q_pred.size(0)), actions]` ? – kederrac Mar 29 '20 at 00:02
  • Yes, I got the above error. Keep in mind that `'torch.FloatTensor'` for `q_pred.type()` and `'torch.IntTensor'` for `actions.type()` – jgauth Mar 29 '20 at 00:04
  • @Louis-Andre this one? `q_pred[torch.arange(q_pred.size(0)), actions.type(torch.LongTensor)]`? – kederrac Mar 29 '20 at 00:14