Torch.sum - too many indices for tensor of dimension 1

please I have a “targets” and “outputs” tensor which have a size [batch-size,slate_length]. “targets” contains values: True or False and “outputs” contains values in [0; 1]
when I execute these lines:

outputs_order = torch.argsort(outputs, descending=True, dim=-1)
targets_sorted_by_outputs = torch.gather(targets, dim=-1, index=outputs_order)
hits_score = torch.sum(targets_sorted_by_outputs[:,:k], dim=1) / torch.sum(targets, dim=1)

I get this error message

13    outputs_order = torch.argsort(outputs, descending=True, dim=-1)
     14    targets_sorted_by_outputs = torch.gather(targets, dim=-1, index=outputs_order)
---> 15    hits_score = torch.sum(targets_sorted_by_outputs[:,:k], dim=1) / torch.sum(targets[:,:k], dim=1)

IndexError: too many indices for tensor of dimension 1

how i can solve this problem? and thank you

I don’t think this is the case, as targets_sorted_by_output seems to have a single dimension and raises the IndexError as seen here:

k = 5
targets_sorted_by_outputs = torch.randn(10)
targets_sorted_by_outputs[:, :k] 
# IndexError: too many indices for tensor of dimension 1
1 Like

Thank you very much for this valuable help