Live data from Hacker News

Softmax, can you derive the Jacobian? And should you care?

idlemachines.co.uk

41–50 of 50 posts

Re: Softmax, can you derive the Jacobian? And should you care?

#41

Earlier quoted context omitted.

They’re not. Cross entropy loss is E[-log q] where q is a probability. You could convert the model outputs x into probabilities using some other function like q = 1/Z x^2, and compute cross entropy loss just fine.

Behold the softmax: https://docs.pytorch.org/docs/2.11/generated/torch.nn.CrossE...

Behold the actual definition of cross entropy: https://en.wikipedia.org/wiki/Cross-entropy

It's true that the PyTorch API conflates cross entropy and softmax, but they are separate concepts.

Re: Softmax, can you derive the Jacobian? And should you care?

#42
post #5

"This transforms a vector of arbitrary real numbers into values between 0 and 1 that sum to 1" Not really, softmax transforms logits (logariths of probabilities) into probabilities. Probabilities → logits → back again. Start with p = [0.6, 0.3, 0.1]. Logits = log(p) = [-0.51, -1.20, -2.30]. Softmax(logits) = original p. NN prefer to output logits because they are linear and go from -inf to +inf.

Softmax is defined over an arbitrary vector of raw real numbers. Stating that those inputs are "logits" is applying post-hoc semantics to what the model is learning. One of the key properties of a softmax is scale invariance, (e.g. softmax([-1, 1, 3, 5]) == softmax([9, 11, 13, 15])) and so it is easiest to just think of it as operating on a vector of unnormalized raw scores, which is the more colloquial definition of…

(meant to say, scale-invariance of probability ratios, or shift-invariance of the inputs)

Re: Softmax, can you derive the Jacobian? And should you care?

#43

Something that really helped me grasp the foundational relevance of the softmax is to justify from first principles why e^x shows up in the preferred mapping function in the numerator (1). The stated problem of mapping raw inputs/scores/logits to a probability distribution can be solved by a bunch of arbitrary functions, and the usual justification given for a softmax is "it has nice derivatives" which is empirically…

The reason for exp(x) is that its derivative is exp(x), which makes it possible to express the gradient of s(x) in terms of s(x), or both in terms of exp(x). This simplifies the computation of backward pass.

Re: Softmax, can you derive the Jacobian? And should you care?

#44
post #43

Something that really helped me grasp the foundational relevance of the softmax is to justify from first principles why e^x shows up in the preferred mapping function in the numerator (1). The stated problem of mapping raw inputs/scores/logits to a probability distribution can be solved by a bunch of arbitrary functions, and the usual justification given for a softmax is "it has nice derivatives" which is empirically…

The reason for exp(x) is that its derivative is exp(x), which makes it possible to express the gradient of s(x) in terms of s(x), or both in terms of exp(x). This simplifies the computation of backward pass.

I agree that "it has nice derivatives" is a great empirical reason to use a specific function in ML, but it doesn't sufficiently prove that it's the best function to use. And even if a derivative term looks more complex, that doesn't necessarily imply that it is more computationally expensive to compute, so that can't be the only criteria to select a function.

Luckily, there are more axiomatic reasons for why softmax is the preferred way to map inputs to a probability distribution.

Re: Softmax, can you derive the Jacobian? And should you care?

#45
post #9

Earlier quoted context omitted.

Mathematically, it is literally a probability distribution, because it fits the definition of a measure whose total mass is one, so I think the language is just imprecise. What they may be trying to say is that semantically it doesn't arise in a principled way from an uncertainty model, such as from Bayesian or frequentist statistics.

Hogwash. If you get into deriving maximum entropy distributions via the calculus of variations, the multinomial is the maximum entropy distribution among categorical distributions. This is exactly the sense that it comes up for old school LMs and why it appears in thermodynamics. Of course it is entirely possible that newfangled ML people use it without understanding that it is derived from first principles - i.e. se…

That definitely could be the case. I was also a bit surprised by what the article said, so I was simply trying to interpret it, but I'm not extremely well versed in ML so I could be missing some details. My main point was that contrary to what the article said, they do in fact have a probability distribution on their hands.

Re: Softmax, can you derive the Jacobian? And should you care?

#46

Earlier quoted context omitted.

Hogwash. If you get into deriving maximum entropy distributions via the calculus of variations, the multinomial is the maximum entropy distribution among categorical distributions. This is exactly the sense that it comes up for old school LMs and why it appears in thermodynamics. Of course it is entirely possible that newfangled ML people use it without understanding that it is derived from first principles - i.e. se…

That definitely could be the case. I was also a bit surprised by what the article said, so I was simply trying to interpret it, but I'm not extremely well versed in ML so I could be missing some details. My main point was that contrary to what the article said, they do in fact have a probability distribution on their hands.

This is literally the probability distribution ML models are trained on.

https://docs.pytorch.org/docs/2.11/generated/torch.nn.CrossE...

You have a relatively small dictionary of tokens, each prediction has a neural network score that goes into the final token prediction layer, and they are trained based on a log-softmax (i.e. the above function) to predict their next token.

This is exactly how anyone in any field does conditional multinomial/categorical (i.e. one of a bunch of distinct tokens) distributions, and AFAIK what LLMs generally use as their loss functions on the output layer, though I have not deeply investigated all of them, since this has been how you do that since time immemorial.

I am extremely confused by all of the people screaming it's not a probability distribution?!?!?

I have seen computer vision tasks use binomial training objectives (one-vs-all) and then use the multinomial only at inference time, and that could be fair that that is not a probability distribution induced by training (while technically a probability distribution only in the sense it is \ge 0 and sums to 1).

But afaik token prediction LLMs that I am aware of use the softmax for the probability in their loss function, i.e. the maximize log softmax.

Re: Softmax, can you derive the Jacobian? And should you care?

#48
post #5

"This transforms a vector of arbitrary real numbers into values between 0 and 1 that sum to 1" Not really, softmax transforms logits (logariths of probabilities) into probabilities. Probabilities → logits → back again. Start with p = [0.6, 0.3, 0.1]. Logits = log(p) = [-0.51, -1.20, -2.30]. Softmax(logits) = original p. NN prefer to output logits because they are linear and go from -inf to +inf.

Softmax is defined over an arbitrary vector of raw real numbers. Stating that those inputs are "logits" is applying post-hoc semantics to what the model is learning. One of the key properties of a softmax is scale invariance, (e.g. softmax([-1, 1, 3, 5]) == softmax([9, 11, 13, 15])) and so it is easiest to just think of it as operating on a vector of unnormalized raw scores, which is the more colloquial definition of…

[dead]

Re: Softmax, can you derive the Jacobian? And should you care?

#49

Earlier quoted context omitted.

I would love to see more work on beam search/Viterbi decodes rather than just greedy next token output.

Regret analysis in bandit and similar algorithms shows how inference is connected to loss function. If your loss function is good, greedy inference is as good as joint inference. Training on cost-to-go loss is good enough. Perfect cost-to-go eliminates the need for global algorithms and allows local decision making. Given “natural” datasets it is probably the best thing to attempt to learn. The fact that probabilisti…

Are there any good papers on this you would suggest/specific search terms?

I am vaguely aware of some stuff, but would love to study more, I don't quite understand what this is all about (but I do see how LLMs can do attention to all prior tokens so you don't have the single-point-of-failure HMMs do which more necessitates Viterbi decodes)

Re: Softmax, can you derive the Jacobian? And should you care?

#50
post #21

So softmax is e^x projection followed by l1 norm. Why is e^x projection useful?

It maps (-inf, inf) to (0, inf) in about as nice a way as you could expect (addition turns into multiplication). When you want to constrain a value to be positive, parameterizing it with exp is usually a good option.

And importantly it's got nice properties like being differentiable and monotonic, unlike eg. taking |x|.
Post reply on HN