本文整理汇总了Java中librec.data.MatrixEntry类的典型用法代码示例。如果您正苦于以下问题:Java MatrixEntry类的具体用法?Java MatrixEntry怎么用?Java MatrixEntry使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MatrixEntry类属于librec.data包,在下文中一共展示了MatrixEntry类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeMatrix
import librec.data.MatrixEntry; //导入依赖的package包/类
/**
* write a matrix data into a file
*/
private void writeMatrix(SparseMatrix data, String filePath) throws Exception {
// delete old file first
FileIO.deleteFile(filePath);
List<String> lines = new ArrayList<>(1500);
for (MatrixEntry me : data) {
int u = me.row();
int j = me.column();
double ruj = me.get();
if (ruj <= 0)
continue;
String user = rateDao.getUserId(u);
String item = rateDao.getItemId(j);
String timestamp = timeMatrix != null ? " " + timeMatrix.get(u, j) : "";
lines.add(user + " " + item + " " + (float) ruj + timestamp);
if (lines.size() >= 1000) {
FileIO.writeList(filePath, lines, true);
lines.clear();
}
}
if (lines.size() > 0)
FileIO.writeList(filePath, lines, true);
Logs.debug("Matrix data is written to: {}", filePath);
}
示例2: initModel
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
public void initModel() throws Exception {
super.initModel();
userExp = new DenseVector(numUsers);
for (MatrixEntry me : trainMatrix) {
int u = me.row(); // user
double ruj = me.get();
userExp.add(u, Math.exp(ruj));
}
}
示例3: initModel
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
public void initModel() throws Exception {
PukSum = new DenseMatrix(numUsers, numFactors);
PkiSum = new DenseMatrix(numFactors, numItems);
// initialize count variables.
Nuk = new DenseMatrix(numUsers, numFactors);
Nu = new DenseVector(numUsers);
Nki = new DenseMatrix(numFactors, numItems);
Nk = new DenseVector(numFactors);
alpha = new DenseVector(numFactors);
alpha.setAll(initAlpha);
beta = new DenseVector(numItems);
beta.setAll(initBeta);
// The z_u,i are initialized to values in [0, K-1] to determine the initial state of the Markov chain.
z = HashBasedTable.create();
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
int t = (int) (Math.random() * numFactors); // 0 ~ k-1
// assign a topic t to pair (u, i)
z.put(u, i, t);
// number of items of user u assigned to topic t.
Nuk.add(u, t, 1);
// total number of items of user u
Nu.add(u, 1);
// number of instances of item i assigned to topic t
Nki.add(t, i, 1);
// total number of words assigned to topic t.
Nk.add(t, 1);
}
}
示例4: isConverged
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
protected boolean isConverged(int iter) throws Exception {
loss = 0;
// get params
estimateParams();
// compute likelihood
int count = 0;
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
double rui = me.get();
int r = ratingScale.indexOf(rui);
double prob = 0;
for (int k = 0; k < numFactors; k++) {
prob += Puk.get(u, k) * Pki.get(k, i) * Pkir[k][i][r];
}
loss += -Math.log(prob);
count++;
}
loss /= count;
float delta = (float) (loss - lastLoss); // loss gets smaller, delta <= 0
// Logs.debug("{}{} iter {} achieves log likelihood = {}, delta_LogLLH = {}", algoName, foldInfo, iter, (float) loss, delta);
if (numStats > 1 && delta > 0) {
//Logs.debug("{}{} has converged at iter {}", algoName, foldInfo, iter);
return true;
}
lastLoss = loss;
return false;
}
示例5: isConverged
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
protected boolean isConverged(int iter) throws Exception {
// get the parameters
estimateParams();
// compute the perplexity
int N = 0;
double sum = 0;
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int v = me.column();
double ruv = me.get();
sum += perplexity(u, v, ruv);
N++;
}
double perp = Math.exp(sum / N);
double delta = perp - loss; // perplexity should get smaller and smaller --> delta<0
if (numStats > 1 && delta > 0)
return true;
loss = perp;
return false;
}
示例6: buildModel
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
public void buildModel() throws Exception {
for (int i = 1; i <= numIters; i++) {
loss = 0;
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int j = me.column();
double ruj = me.get();
double pred = predict(u, j);
double euj = ruj - pred;
loss += euj * euj;
double userWeight = userWeights.get(u) + lRate * (euj - regU * userWeights.get(u));
userWeights.set(u, userWeight);
// Gradient-Step on item weights.
double itemWeight = itemWeights.get(j) + lRate * (euj - regI * itemWeights.get(j));
itemWeights.set(j, itemWeight);
loss += regU * userWeights.get(u) * userWeights.get(u) + regI * itemWeights.get(j) * itemWeights.get(j);
}
loss *= 0.5;
if (isConverged(i))
break;
}
}
示例7: initModel
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
public void initModel() throws Exception {
Nuk = new DenseMatrix(numUsers, K);
Nu = new DenseVector(numUsers);
Nkl = new DenseMatrix(K, L);
Nk = new DenseVector(K);
Nklr = new int[K][L][numLevels];
Nkli = new int[K][L][numItems];
Zk = HashBasedTable.create();
Zl = HashBasedTable.create();
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
double rate = me.get();
int r = ratingScale.indexOf(rate);
int k = (int) (K * Math.random()); // user's topic k
int l = (int) (L * Math.random()); // item's topic l
Nuk.add(u, k, 1);
Nu.add(u, 1);
Nkl.add(k, l, 1);
Nk.add(k, 1);
Nklr[k][l][r]++;
Nkli[k][l][i]++;
Zk.put(u, i, k);
Zl.put(u, i, l);
}
// parameters
PukSum = new DenseMatrix(numUsers, K);
PklSum = new DenseMatrix(K, L);
PklrSum = new double[K][L][numLevels];
Pklr = new double[K][L][numLevels];
PkliSum = new double[K][L][numItems];
Pkli = new double[K][L][numItems];
}
示例8: eStep
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
protected void eStep() {
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
double rate = me.get();
int r = ratingScale.indexOf(rate);
int k = Zk.get(u, i);
int l = Zl.get(u, i);
Nuk.add(u, k, -1);
Nu.add(u, -1);
Nkl.add(k, l, -1);
Nk.add(k, -1);
Nklr[k][l][r]--;
Nkli[k][l][i]--;
DenseMatrix Pzw = new DenseMatrix(K, L);
double sum = 0;
for (int z = 0; z < K; z++) {
for (int w = 0; w < L; w++) {
double v1 = (Nuk.get(u, k) + initAlpha) / (Nu.get(u) + K * initAlpha);
double v2 = (Nkl.get(k, l) + initBeta) / (Nk.get(k) + L * initBeta);
double v3 = (Nklr[k][l][r] + initGamma) / (Nkl.get(k, l) + numLevels * initGamma);
double v4 = (Nkli[k][l][i] + initSigma) / (Nkl.get(k, l) + numItems * initSigma);
double val = v1 * v2 * v3 * v4;
Pzw.set(z, w, val);
sum += val;
}
}
// normalization
Pzw = Pzw.scale(1.0 / sum);
// resample k
double[] Pz = new double[K];
for (int z = 0; z < K; z++)
Pz[z] = Pzw.sumOfRow(z);
for (int z = 1; z < K; z++)
Pz[z] += Pz[z - 1];
double rand = Math.random();
for (k = 0; k < K; k++) {
if (rand < Pz[k])
break;
}
// resample l
double[] Pw = new double[L];
for (int w = 0; w < L; w++)
Pw[w] = Pzw.sumOfColumn(w);
for (int w = 1; w < L; w++)
Pw[w] += Pw[w - 1];
rand = Math.random();
for (l = 0; l < L; l++) {
if (rand < Pw[l])
break;
}
// add statistic
Nuk.add(u, k, 1);
Nu.add(u, 1);
Nkl.add(k, l, 1);
Nk.add(k, 1);
Nklr[k][l][r]++;
Nkli[k][l][i]++;
Zk.put(u, i, k);
Zl.put(u, i, l);
}
}
示例9: buildModel
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
public void buildModel() throws Exception {
for (int iter = 1; iter <= numIters; iter++) {
loss = 0;
for (MatrixEntry me : trainMatrix) {
int u = me.row(); // user
int j = me.column(); // item
double ruj = me.get();
double pred = DenseMatrix.rowMult(P, u, Q, j);
double uexp = 0;
List<Integer> items = trainMatrix.getColumns(u);
for (int i : items) {
uexp += Math.exp(DenseMatrix.rowMult(P, u, Q, i));
}
loss -= Math.exp(ruj) / userExp.get(u) * Math.log(Math.exp(pred) / uexp);
// update factors
for (int f = 0; f < numFactors; f++) {
double puf = P.get(u, f);
double qjf = Q.get(j, f);
double delta_u = (Math.exp(ruj) / userExp.get(u) - Math.exp(pred) / uexp) * gd(pred) * qjf - regU * puf;
double delta_j = (Math.exp(ruj) / userExp.get(u) - Math.exp(pred) / uexp) * gd(pred) * puf - regI * qjf;
P.add(u, f, lRate * delta_u);
Q.add(j, f, lRate * delta_j);
loss += 0.5 * regU * puf * puf + 0.5 * regI * qjf * qjf;
}
}
if (isConverged(iter))
break;
}// end of training
}
示例10: eStep
import librec.data.MatrixEntry; //导入依赖的package包/类
protected void eStep() {
double sumAlpha = alpha.sum();
double sumBeta = beta.sum();
// Gibbs sampling from full conditional distribution
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
int t = z.get(u, i); // topic
Nuk.add(u, t, -1);
Nu.add(u, -1);
Nki.add(t, i, -1);
Nk.add(t, -1);
// do multinomial sampling via cumulative method:
double[] p = new double[numFactors];
for (int k = 0; k < numFactors; k++) {
p[k] = (Nuk.get(u, k) + alpha.get(k)) / (Nu.get(u) + sumAlpha) * (Nki.get(k, i) + beta.get(i))
/ (Nk.get(k) + sumBeta);
}
// cumulating multinomial parameters
for (int k = 1; k < p.length; k++) {
p[k] += p[k - 1];
}
// scaled sample because of unnormalized p[], randomly sampled a new topic t
double rand = Math.random() * p[numFactors - 1];
for (t = 0; t < p.length; t++) {
if (rand < p[t])
break;
}
// add newly estimated z_i to count variables
Nuk.add(u, t, 1);
Nu.add(u, 1);
Nki.add(t, i, 1);
Nk.add(t, 1);
z.put(u, i, t);
}
}
示例11: initModel
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
public void initModel() throws Exception {
// cumulative parameters
PukSum = new DenseMatrix(numUsers, numFactors);
PkiSum = new DenseMatrix(numFactors, numItems);
PkirSum = new double[numFactors][numItems][numLevels];
// initialize count variables
Nuk = new DenseMatrix(numUsers, numFactors);
Nu = new DenseVector(numUsers);
Nki = new DenseMatrix(numFactors, numItems);
Nk = new DenseVector(numFactors);
Nkir = new int[numFactors][numItems][numLevels];
alpha = new DenseVector(numFactors);
alpha.setAll(initAlpha);
beta = new DenseVector(numItems);
beta.setAll(initBeta);
gamma = new DenseVector(numLevels);
// initGamma = algoOptions.getFloat("-gamma", 1.0f / numLevels);
gamma.setAll(initGamma);
// initialize topics
z = HashBasedTable.create();
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
double rui = me.get();
int r = ratingScale.indexOf(rui); // rating level 0 ~ numLevels
int t = (int) (Math.random() * numFactors); // 0 ~ k-1
// assign a topic t to pair (u, i)
z.put(u, i, t);
// for users
Nuk.add(u, t, 1);
Nu.add(u, 1);
// for items
Nki.add(t, i, 1);
Nk.add(t, 1);
// for ratings
Nkir[t][i][r]++;
}
}
示例12: eStep
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
protected void eStep() {
double sumAlpha = alpha.sum();
double sumBeta = beta.sum();
double sumGamma = gamma.sum();
// collapse Gibbs sampling
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
double rui = me.get();
int r = ratingScale.indexOf(rui); // rating level 0 ~ numLevels
int t = z.get(u, i);
Nuk.add(u, t, -1);
Nu.add(u, -1);
Nki.add(t, i, -1);
Nk.add(t, -1);
Nkir[t][i][r]--;
// do multinomial sampling via cumulative method:
double[] p = new double[numFactors];
double v1, v2, v3;
for (int k = 0; k < numFactors; k++) {
v1 = (Nuk.get(u, k) + alpha.get(k)) / (Nu.get(u) + sumAlpha);
v2 = (Nki.get(k, i) + beta.get(i)) / (Nk.get(k) + sumBeta);
v3 = (Nkir[k][i][r] + gamma.get(r)) / (Nki.get(k, i) + sumGamma);
p[k] = v1 * v2 * v3;
}
// cumulate multinomial parameters
for (int k = 1; k < p.length; k++) {
p[k] += p[k - 1];
}
// scaled sample because of unnormalized p[], randomly sampled a new topic t
double rand = Math.random() * p[numFactors - 1];
for (t = 0; t < p.length; t++) {
if (rand < p[t])
break;
}
// new topic t
z.put(u, i, t);
// add newly estimated z_i to count variables
Nuk.add(u, t, 1);
Nu.add(u, 1);
Nki.add(t, i, 1);
Nk.add(t, 1);
Nkir[t][i][r]++;
}
}
示例13: buildModel
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
public void buildModel() throws Exception {
for (int iter = 1; iter <= numIters; iter++) {
loss = 0;
// temp data
DenseMatrix PS = new DenseMatrix(numUsers, numFactors);
DenseMatrix QS = new DenseMatrix(numItems, numFactors);
// ratings
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int j = me.column();
double ruj = me.get();
double pred = predict(u, j);
double euj = pred - ruj;
loss += euj * euj;
for (int f = 0; f < numFactors; f++) {
double puf = P.get(u, f);
double qjf = Q.get(j, f);
PS.add(u, f, euj * qjf + regU * puf);
QS.add(j, f, euj * puf + regI * qjf);
loss += regU * puf * puf + regI * qjf * qjf;
}
}
// friends
for (int u = 0; u < numUsers; u++) {
// out links: F+
SparseVector uos = socialMatrix.row(u);
for (int k : uos.getIndex()) {
double suk = similarity(u, k);
if (!Double.isNaN(suk)) {
for (int f = 0; f < numFactors; f++) {
double euk = P.get(u, f) - P.get(k, f);
PS.add(u, f, beta * suk * euk);
loss += beta * suk * euk * euk;
}
}
}
// in links: F-
SparseVector uis = socialMatrix.column(u);
for (int g : uis.getIndex()) {
double sug = similarity(u, g);
if (!Double.isNaN(sug)) {
for (int f = 0; f < numFactors; f++) {
double eug = P.get(u, f) - P.get(g, f);
PS.add(u, f, beta * sug * eug);
}
}
}
} // end of for loop
P = P.add(PS.scale(-lRate));
Q = Q.add(QS.scale(-lRate));
loss *= 0.5;
if (isConverged(iter))
break;
}
}
示例14: initModel
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
public void initModel() throws Exception {
// cumulative parameters
PukSum = new DenseMatrix(numUsers, numFactors);
PkirSum = new double[numFactors][numItems][numLevels];
// initialize count variables
Nuk = new DenseMatrix(numUsers, numFactors);
Nu = new DenseVector(numUsers);
Nkir = new int[numFactors][numItems][numLevels];
Nki = new DenseMatrix(numFactors, numItems);
alpha = new DenseVector(numFactors);
alpha.setAll(initAlpha);
beta = new DenseVector(numLevels);
beta.setAll(initBeta);
// initialize topics
z = HashBasedTable.create();
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
double rui = me.get();
int r = ratingScale.indexOf(rui); // rating level 0 ~ numLevels
int t = (int) (Math.random() * numFactors); // 0 ~ k-1
// assign a topic t to pair (u, i)
z.put(u, i, t);
// number of pairs (u, t) in (u, i, t)
Nuk.add(u, t, 1);
// total number of items of user u
Nu.add(u, 1);
// number of pairs (t, i, r)
Nkir[t][i][r]++;
// total number of words assigned to topic t
Nki.add(t, i, 1);
}
}
示例15: eStep
import librec.data.MatrixEntry; //导入依赖的package包/类
@Override
protected void eStep() {
double sumAlpha = alpha.sum();
double sumBeta = beta.sum();
// collapse Gibbs sampling
for (MatrixEntry me : trainMatrix) {
int u = me.row();
int i = me.column();
double rui = me.get();
int r = (int) (rui / minRate - 1); // rating level 0 ~ numLevels
int t = z.get(u, i);
Nuk.add(u, t, -1);
Nu.add(u, -1);
Nkir[t][i][r]--;
Nki.add(t, i, -1);
// do multinomial sampling via cumulative method:
double[] p = new double[numFactors];
for (int k = 0; k < numFactors; k++) {
p[k] = (Nuk.get(u, k) + alpha.get(k)) / (Nu.get(u) + sumAlpha) * (Nkir[k][i][r] + beta.get(r))
/ (Nki.get(k, i) + sumBeta);
}
// cumulate multinomial parameters
for (int k = 1; k < p.length; k++) {
p[k] += p[k - 1];
}
// scaled sample because of unnormalized p[], randomly sampled a new topic t
double rand = Math.random() * p[numFactors - 1];
for (t = 0; t < p.length; t++) {
if (rand < p[t])
break;
}
// new topic t
z.put(u, i, t);
// add newly estimated z_i to count variables
Nuk.add(u, t, 1);
Nu.add(u, 1);
Nkir[t][i][r]++;
Nki.add(t, i, 1);
}
}