neel-tech
neel-tech

Reputation: 9

Why is pytorch softmax function not working?

so this is my code

import torch.nn.functional as F
import torch

inputs = [1,2,3]
input = torch.tensor(inputs)
output = F.softmax(input, dim=1)
print(output)

is the reason why the code not working because of the dim? the error here:

  File "c:\Users\user\Desktop\AI\pytorch_jovian\linear_reg.py", line 19, in <module>
    output = F.softmax(input, dim=1)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\functional.py", line 1583, in softmax
    ret = input.softmax(dim)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

Upvotes: 0

Views: 4032

Answers (1)

Innat
Innat

Reputation: 17219

Apart from dim=0, there is another issue in your code. Softmax doesn't work on a long tensor, so it should be converted to a float or double tensor first

>>> input = torch.tensor([1, 2, 3])
>>> input
tensor([1, 2, 3])

>>> F.softmax(input.float(), dim=0)
tensor([0.0900, 0.2447, 0.6652])

Upvotes: 1

Related Questions