On 9/27/13 3:43 PM, Alexander Franks wrote:
> I've been having some trouble fitting multinomial logit in stan and have read a few things about issues with
> categorical(softmax()) here that suggested there may be a memory leak?
As far as we know, there's no memory leak for softmax
in 1.3 or in the coming 2.0 versions. Having said that,
it is memory intensive (more about that below).
> I have a very large multinomial model that I am trying to fit: about 50 categories and a couple thousand parameters.
> The model matrix is very sparse since I include "person-specific" random effect which each observation (with partial
> pooling on the params). Each row of the model matrix has roughly 30 non-zero entries (out of a few thousand). I have
> about 75,000 data points.
>
> When I tried to fit this model in stan everything compiled nicely but it hung once it began sampling. Eventually it
> segfaulted.
Segfaulted or just ran out of memory with a bad_alloc
exception? If it really was a segfault, could you send
us the output and some description of the platform you're
running on?
> I also have a few questions about implementing this efficiently in stan. With a very sparse model matrix, am I better
> off specifying response[n] ~ categorical(softmax(beta*x[n]) as in the documentation, or rather should I specify eta[n]
> in the transformed parameters section using a series of for loops to index and sum up the relevant parameters? E.g. in
> a loop something like
> eta[n,k] <- eta[n,k]+beta[index[n,k]]
I'm not exactly sure what the two alternatives are you're
considering or what the index[n,k] variable is doing. Could
you share the whole loop?
In general, you don't want to be multiplying by known
0 values if you can help it.
But rather than doing a loop like this:
real foo;
foo <- 0;
for (n in 1:N)
foo <- foo + bar[n];
it's better to do
foo <- sum(bar);
This is true even if everything's not in a simple array or vector
like bar to begin with (that is, if you have to allocate and
populate bar). The reason is because the sum operation uses
less memory than repeated application of + and performs fewer
(virtual) function applications for computing derivatives. The
allocation and loop for the array are relatively cheap.
> Finally, is it better at this stage to use softmax or to specify probs[n] <- exp(eta[n])/sum(eta[n]) in transformed
> parameters section? categorical_logit will be available in stan 2.0?
Use softmax. It should be have exactly the same way
in 1.3, which just defines softmax that way.
In Stan 2 (which we hope to launch a beta for next week),
has custom derivatives for softmax, which will be much faster
and much less memory intense. The issue with memory is that
Stan stores the whole expression tree for the log probability,
which can get big. For instance, softmax does a lot of work
for each n because it needs to be normalized.
The categorical_logit that's part of 2.0 gets around that
problem AND has a custom derivative. So it's much faster
and much less memory intensive.
We would've done this sooner if we'd known there were
so many multi-logit fitters out there. We're trying to
optimize according to perceived demand.
- Bob