Hey guys,
I was following exactly the same as the tutorial says
which official PyTorch.org had given on their site.
However, I got stuck on the softmax function which shows no warning according to the tutorial, but my python gives me a warning message it says,
UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument.
return F.log_softmax(self.linear(bow_vec))
Can anyone of you guys knows what ‘dim’ variable do?
do you mean if the input of a softmax function is a variable that is comprised of, let’s say, 6 elements then we should clarify that it is dimension of 6
with variable dim=6 ???
I don’t think I’m aware of what ‘dim’ actually means.
On the example you wrote, python says the expected range
of dim is [-3, 2].
Think I’m confused with the definition of what dim actually is in that context since so many meaning exist as a word dim in machine learning.
Could you explain more specifically what that means?
/OpenNMT-py/onmt/modules/GlobalAttention.py:177: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
Can anyone please help as to how can this be resolved?
You could add the dim parameter to the softmax function call, to get rid of this warning.
Have a look at the dimensions of the input to softmax and try to figure out, in which dimension the softmax should be calculated.
On the first case (using dim=1) the softmax function is applied along the axis 1 . That’s why all rows add up to 1. On the second case (using dim=0) the softmax function is applied along the axis 0. Making all the columns add up to 1.
If your batch size is say=64, and number of classes you want the CNN/NN to classify is say=10. Then the dimension of output tensor will be 64*10.
Set dim=0 to apply softmax along the rows.
hmmm might it be a good idea to just throw an error rather a warning and have the code actually stop running? Might save ppl headaches on debugging. I couldn’t even see the warning cuz I was printing a bunch of other stuff…if the code had just failed it would have saved me a bunch of time.
main_classifier.py:86: UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument.
logpt = F.log_softmax(input)
train: True test: False
preparing datasets and dataloaders......
creating models......
=>Epoches 1, learning rate = 0.0010000, previous best = 0.0000
training...
feats shape: torch.Size([64, 419, 512])
labels shape: torch.Size([64])
Traceback (most recent call last):
File "main_classifier.py", line 347, in <module>
loss = criterion(m(output[:,1]-output[:,0]), labels.float())
File "/home/jalal/research/venv/dpcc/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, **kwargs)
File "main_classifier.py", line 87, in forward
logpt = F.log_softmax(input, dim=1)
File "/home/jalal/research/venv/dpcc/lib/python3.8/site-packages/torch/nn/functional.py", line 1769, in log_softmax
ret = input.log_softmax(dim)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
As given in the new error message, you are trying to specify dim=1 while input has only a single dimension.
Use dim=0 instead and make sure that’s really the expected shape.
Common use cases use at least two dimensions as [batch_size, feature_dim] and use then the log_softmax in the feature dimension, but I’m also not familiar with your use case so input having a single dimension might be alright.
Thanks for the link.
Based on both errors I don’t think you should fix the issues by changing the dim argument in log_softmax and gather, but should instead debug why the input has a single dimension (while more are expected).
My pytorch version is 2.1.1
I also got the warning:
input
tensor([[ 0.5228, -1.4060, -0.8637],
[-0.4285, -1.1788, 0.6950]])
m = nn.Softmax()
m(input)
/home/clock/Work/venvs/torchgpu/lib/python3.10/site-packages/torch/nn/modules/module.py:1518: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
return self._call_impl(*args, **kwargs)
tensor([[0.7167, 0.1042, 0.1792],
[0.2199, 0.1038, 0.6763]])
torch.version
‘2.1.1+cu121’
But according pytorch official document here (for 2.1.0): https://pytorch.org/docs/stable/generated/torch.nn.Softmax.html
nn.Softmax() still accept implicit dimension choice as dim=None, so the document is not synched to the latest code yet, we need fix it in document explanation.