r-Rstudio MCMC上的Rstan运行时间过长(可用CPU和RAM的使用有限)



我是Rstan世界的新手,但我的论文真的需要它。事实上,我使用的是纽约大学一个人的脚本和类似的数据集,他报告了大约18小时的类似DS的估计时间。然而,当我尝试运行我的模型时,它在18小时内不会超过10%。因此,我请求一些小帮助来了解我做错了什么以及如何提高效率。

我正在运行一个500 iter、100个预热2链模型,该模型具有5个参数的伯努利_位函数,试图通过No U Turn MC程序估计其中的2个参数。(在每个步骤中,它从随机正态中提取每个参数,然后估计y并将其与实际数据进行比较,以查看新参数是否更适合数据)

y[n] ~ bernoulli_logit( alpha[kk[n]] + beta[jj[n]] - gamma * square( theta[jj[n]] - phi[kk[n]] ) );

(n约为10mln)我的数据是一个由0和1组成的10.000x1004矩阵。总而言之,这是一个关于人们在推特上关注政客的矩阵,我想根据他们关注的人来估计他们的政治想法。我在带有R x64 3.1.1的RStudio上运行该模型,该模型位于Win8 Professional、6位、I7四核、16 GB ram上。检查性能,rsession使用不超过14%的CPU和6GB的ram,尽管还有7 GB是免费的。在尝试对10.000x250矩阵进行子采样时,我注意到它将使用低于1.5GB的数据。然而,我已经用50x50数据集尝试了这个过程,它运行得很好,所以这个过程中没有错误。Rsession打开了8个线程,我看到每个核心上都有活动,但没有一个线程被完全占用。我想知道为什么我的电脑不能尽其所能工作,是否可能有一些瓶颈、上限或设置阻止它这样做。R是64位(刚刚检查),所以Rstan应该是(尽管我在安装时遇到了一些困难,这可能会打乱一些参数)

这就是我编译时发生的情况

Iteration: 1 / 1 [100%]  (Sampling)
#  Elapsed Time: 0 seconds (Warm-up)
#                11.451 seconds (Sampling)
#                11.451 seconds (Total)
SAMPLING FOR MODEL 'stan.code' NOW (CHAIN 2).
Iteration: 1 / 1 [100%]  (Sampling)
#  Elapsed Time: 0 seconds (Warm-up)
#                12.354 seconds (Sampling)
#                12.354 seconds (Total)

而当我运行它时,它只工作了几个小时,但它永远不会超过第一个链的10%(主要是因为我在电脑即将融化后中断了它)。

Iteration:   1 / 500 [  0%]  (Warmup)

并具有以下设置:

stan.model <- stan(model_code=stan.code, data = stan.data, init=inits, iter=1, warmup=0, chains=2)
## running modle
stan.fit <- stan(fit=stan.model, data = stan.data, iter=500, warmup=100, chains=2, thin=thin, init=inits)

请帮我找到是什么减缓了程序的速度(如果没有发生任何事情,我可以操作什么来在更短的时间内获得一些合理的结果?)。

我提前感谢你,

ML

这是的模型

n.iter <- 500
n.warmup <- 100
thin <- 2 ## this will give up to 200 effective samples for each chain and par
Adjmatrix <- read.csv("D:/TheMatrix/Adjmatrix_1004by10000_20150424.txt", header=FALSE)  
##10.000x1004 matrix of {0, 1} with the relationship "user i follows politician j"
StartPhi <- read.csv("D:/TheMatrix/StartPhi_20150424.txt", header=FALSE)  
##1004 vector of values [-1, 1] that should be a good prior for the Phi I want to estimate
start.phi<-ba<-c(do.call("cbind",StartPhi))
y<-Adjmatrix
J <- dim(y)[1]
K <- dim(y)[2]
N <- J * K
jj <- rep(1:J, times=K)
kk <- rep(1:K, each=J)
stan.data <- list(J=J, K=K, N=N, jj=jj, kk=kk, y=c(as.matrix(y)))
## rest of starting values
colK <- colSums(y)
rowJ <- rowSums(y)
normalize <- function(x){ (x-mean(x))/sd(x) }
inits <- rep(list(list(alpha=normalize(log(colK+0.0001)), 
beta=normalize(log(rowJ+0.0001)),
theta=rnorm(J), phi=start.phi,mu_beta=0, sigma_beta=1, 
gamma=abs(rnorm(1)), mu_phi=0, sigma_phi=1, sigma_alpha=1)),2)
##alpha and beta are the popularity of the politician j and the propensity to follow people of user i;
##phi and theta are the position on the political spectrum of pol j and user i; phi has a prior given by expert surveys
##gamma is just a weight on the importance of political closeness
library(rstan)
stan.code <- '
data {
int<lower=1> J; // number of twitter users
int<lower=1> K; // number of elite twitter accounts
int<lower=1> N; // N = J x K
int<lower=1,upper=J> jj[N]; // twitter user for observation n
int<lower=1,upper=K> kk[N]; // elite account for observation n
int<lower=0,upper=1> y[N]; // dummy if user i follows elite j
}
parameters {
vector[K] alpha;
vector[K] phi;
vector[J] theta;
vector[J] beta;
real mu_beta;
real<lower=0.1> sigma_beta;
real mu_phi;
real<lower=0.1> sigma_phi;
real<lower=0.1> sigma_alpha;
real gamma;
}
model {
alpha ~ normal(0, sigma_alpha);
beta ~ normal(mu_beta, sigma_beta);
phi ~ normal(mu_phi, sigma_phi);
theta ~ normal(0, 1); 
for (n in 1:N)
y[n] ~ bernoulli_logit( alpha[kk[n]] + beta[jj[n]] - 
gamma * square( theta[jj[n]] - phi[kk[n]] ) );
}
'
## compiling model
stan.model <- stan(model_code=stan.code, 
data = stan.data, init=inits, iter=1, warmup=0, chains=2)
## running modle
stan.fit <- stan(fit=stan.model, data = stan.data, 
iter=n.iter, warmup=n.warmup, chains=2, 
thin=thin, init=inits)
samples <- extract(stan.fit, pars=c("alpha", "phi", "gamma", "mu_beta",
"sigma_beta", "sigma_alpha"))

首先,我很抱歉:我本想把这作为一条评论来介绍,但我没有足够的声誉。

这是你问的问题:"我能操纵什么在更短的时间内仍然有一些合理的结果?">

答案是,这取决于情况。您是否尝试过使用计数来缩小矩阵的大小,而不是将事物表示为二进制矩阵?根据你试图运行的模型类型,我认为后部存在一些不可识别性。你能试着重新参数化吗?

此外,如果R导致内存管理出现问题,您可能希望在CmdStan中运行。

最新更新