将两个1 * k向量相乘,但没有匹配:pystan中的real〜normal(matrix,real)错误 [英] Multiply two 1*k vectors but got No matches for: real ~ normal(matrix, real) error in pystan

查看:177
本文介绍了将两个1 * k向量相乘,但没有匹配:pystan中的real〜normal(matrix,real)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在按以下方式实现pmf模型:

Now I am implementing the pmf model as below:

pmf_code = """

data {

int<lower=0> K; //number of factors
int<lower=0> N; //number of user
int<lower=0> M; //number of item
int<lower=0> D; //number of observation
int<lower=0> D_new; //number of pridictor 
int<lower=0, upper=N> ii[D]; //item 
int<lower=0, upper=M> jj[D]; //user
int<lower=0, upper=N> ii_new[D_new]; // item
int<lower=0, upper=N> jj_new[D_new]; // user
real<lower=0, upper=5> r[D]; //rating
real<lower=0, upper=5> r_new[D_new]; //pridict rating

}

parameters {
row_vector[K] i[M]; // item profile
row_vector[K] u[N]; // user profile
real<lower=0> alpha;
real<lower=0> alpha_i;
real<lower=0> alpha_u;

}
transformed parameters {
matrix[N,M] I; // indicator variable
I <- rep_matrix(0, N, M);
for (d in 1:D){
    I[ii[d]][jj[d]] <- 1;
}
}
model {
for (d in 1:D){
    r[d] ~ normal(u[jj[d]]' * i[ii[d]], 1/alpha);
}

for (n in 1: N){
   u[n] ~ normal(0,(1/alpha_u) * I);
}
for (m in 1:M){
   i[m] ~ normal(0,(1/alpha_i) * I);
}
}
generated_quantities{
for (d in 1:D_new){
   r_new[d] <- normal(u[jj_new[d]]' * i[ii_new[d]], 1/alpha);
}
}
"""     

但在以下代码行中出现了No matches for: real ~ normal(matrix, real)错误:

but got an No matches for: real ~ normal(matrix, real) error in this line of code:

for (d in 1:D){
   r[d] ~ normal(u[jj[d]]' * i[ii[d]], 1/alpha);
}

但是jj[d]应该是int,表示user的ID.并且u[int]应该是row_vector具有k因子的i[ii[d]].它们的乘积应该是单个实际值,为什么斯坦说它是矩阵?

But the jj[d] should be a int, denoting the id of user. And u[int] should be a row_vector has k factors and so is i[ii[d]]. The product of them should be a single real value, why stan said it was a matrix?

提前谢谢!

最初的问题已通过添加产品的sum解决.但是现在又发生了另一个错误:

The initial problem has been solved by adding a sum of the product. But now another error occurs:

No matches for: 

row vector ~ normal(int, matrix) 

for (n in 1: N){
   u[n] ~ normal(0,(1/alpha_u) * I);
}

在pmf模型中,I是单位矩阵.因此,(1/alpha_u) * I的乘积也是一个矩阵.但是斯坦只接受向量或实值作为方差.我想知道如何将其转换为向量或单个值.谢谢.

In the pmf model, I is unit matrix. So the product of (1/alpha_u) * I is also a matrix. But stan just accept vector or real value as variance. I wonder how to transform it to a vector or a single value. Thanks.

推荐答案

Stan是一种静态类型的语言. vector乘以row_vector将得到matrix.在这种情况下,它是一个1x1矩阵.有关operator*的返回类型,请参见手册v2.9.0版的355页.

Stan is a statically typed language. A vector multiplied by a row_vector will result in a matrix. In this case, it's a 1x1 matrix. For the return type of operator*, see page 355 of the v2.9.0 version of the manual.

而不是相乘,而是使用dot_product().

Rather than multiplying, use dot_product() instead.

这篇关于将两个1 * k向量相乘,但没有匹配:pystan中的real〜normal(matrix,real)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆