Why does nn.Softmax, F.softmax will turn output as Variable?

I want to apply plain softmax in pytorch, so I used that way:

softmax = F.softmax # or softmax = nn.Softmax()
k = torch.randn([1,2])
print(k) # a tensor
print(softmax(k)) # a variable container

and interesting that, softmax(k) becomes a Variable container, which makes my next code torch.matmul(w, k) fail for the reason that “w” is a plain tensor, and every function of pytorch cannot has mixed inputs.

In my view, the softmax function of pytorch do too many things
And I also tested other function in nn package like nn.CosineSimilarity, it will not do this redundant thing.

Could someone tell me the reason? Thanks a lot


add,
I found that every nn activation will do this thing.
So is that a good coding style to make everything into Variable?

v = torch.autograd.Variable(torch.randn([1,2]))
v_activation = torch.nn.functional.softmax(v)

print(v.data)
print(v_activation.data)

You need to take a look at the documentation and tutorials to learn how Pytorch works, then you will understand why Variable is required. Use .data to access variable’s underlying data.

2 Likes