How to speed up my stan code.

69 views
Skip to first unread message

シ YUASA

unread,
Mar 10, 2017, 4:47:51 AM3/10/17
to Stan users mailing list
Hello
I want to know how can I speeding up my code.
I tried ADVI, but it takes long time.

This is my stan code.
data {
  int<lower=2> K;
  int<lower=2> V;
  int<lower=1> M;
  int<lower=1> N; 
  int<lower=1> S;
  int<lower=1,upper=V> w[N];
  int<lower=1,upper=M> word_doc[N];
  vector[S] epu_mu;
  simplex[K] zeta0[S];
  simplex[V] xi0[K];
}
parameters {
  vector[K] Alpha[S];
  vector[V] Beta[K];
  vector[K] Eta[M,S];
  vector[S] Epu;
  vector[S] Epu2[M];
}
transformed parameters {
  simplex[K] Theta[M,S];
  simplex[V] PHi[K];
  simplex[S] Mu[M];
 
  for (m in 1:M){
    Mu[m] = softmax(Epu2[m]);
    for(s in 1:S){
    Theta[m,s] = softmax(Eta[m,s]);
  }
}
    for (k in 1:K) {
      PHi[k] = softmax(Beta[k]);
    }
  }
model {
  for(s in 1:S){
  Alpha[s] ~ normal(0,1);
}
  for (k in 1:K) {
      Beta[k] ~ normal(xi0[k],1);
    }
    Epu ~ normal(epu_mu,1);
  for (m in 1:M) {
    Epu2[m] ~ normal(Epu,1);
    for (s in 1:S) {
      Eta[m,s] ~ normal(Alpha[s] .* zeta0[s],1);
    }
  }
 
  for (n in 1:N) {
    real gamma2[S];
    for (s in 1:S) {
      real gamma1[K];
      for (k in 1:K)
      gamma1[k] = log(Theta[word_doc[n],s,k]) + log(PHi[k,w[n]]);
      gamma2[s] = log(Mu[word_doc[n],s]) + log_sum_exp(gamma1);
    }
    target += log_sum_exp(gamma2);
}
}

Thank you.

Bob Carpenter

unread,
Mar 23, 2017, 3:41:22 PM3/23/17
to stan-...@googlegroups.com
Sorry--- missed this before. YOU want to avoid recomputing
shared subexpressions (such as log(Mu), log(Theta), log(Mu)
to be saved in log_Mu, log_Theta, and log_Mu and then be
used in vectorized form in the expressions).

For example,

> for (k in 1:K)
> gamma1[k] = log(Theta[word_doc[n],s,k]) + log(PHi[k,w[n]]);

can be

gamma1 = log_Theta[word_doc, s] + log_Phi[ , w];

And vectorize the densities wherever you can.

Otherwise, Stan's not going to be fast at these kinds
of models, and to boot, you won't be able to do full Bayes
on them anyway because they're combinatorially intractable.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages