I'm very new to PyTorch, and I have encountered the "Index tensor must have the same number of dimensions as input tensor" error when running my neural network. It happens with I call an instance of torch.gather(). Could someone help me understand torch.gather() and explain the cause of this error?
Here is the code where the error occurs:
def learn(batch, optim, net, target_net, gamma, global_step, target_update):
my_loss = []
optim.zero_grad()
state, action, next_state, reward, done, next_action = batch
qval = net(state.float())
loss_a = torch.gather(qval, 3, action.view(-1,1,1,1)).squeeze() #Error happens here!
loss_b = reward + gamma * torch.max(target_net(next_state.float()).cuda(), dim=3).values * (1 - done.int())
loss_val = torch.sum(( torch.abs(loss_a-loss_b) ))
loss_val /= 128
my_loss.append(loss_val.item())
loss_val.backward()
optim.step()
if global_step % target_update == 0:
target_network.load_state_dict(q_network.state_dict())
In case it is helpful, here is the batch function that creates the batch that the action comes from:
def sample_batch(memory,batch_size):
indices = np.random.randint(0,len(memory), (batch_size,))
state = torch.stack([memory[i][0] for i in indices])
action = torch.tensor([memory[i][1] for i in indices], dtype = torch.long)
next_state = torch.stack([memory[i][2] for i in indices])
reward = torch.tensor([memory[i][3] for i in indices], dtype = torch.float)
done = torch.tensor([memory[i][4] for i in indices], dtype = torch.float)
next_action = torch.tensor([memory[i][5] for i in indices], dtype = torch.long)
return state,action,next_state,reward,done,next_action
When I print out the different shapes of 'qvals', 'action', and 'action.view(-1,1,1,1)' this is the output:
qval torch.Size([10, 225])
act view torch.Size([10, 1, 1, 1])
action shape torch.Size([10])
Any explanation as to what is causing this error is appreciated! I want to understand more what is going on in the code as well as how to fix the problem. Thanks!