Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import net.librec.common.LibrecException;
import net.librec.math.algorithm.Randoms;
import net.librec.recommender.MatrixFactorizationRecommender;
import net.librec.recommender.AbstractRecommender;
import net.librec.util.Lists;
import net.librec.util.ZeroSetter;

Expand All @@ -38,7 +38,7 @@
*
* @author bin wu(Email:wubin@gs.zzu.edu.cn)
*/
public class RBMRecommender extends MatrixFactorizationRecommender {
public class RBMRecommender extends AbstractRecommender {
int featureNumber;
int softmax;
int maxIter;
Expand All @@ -53,9 +53,9 @@ public class RBMRecommender extends MatrixFactorizationRecommender {
double[][] visbiases;
double[] hidbiases;

double[][][] CDpos;
double[][][] CDneg;
double[][][] CDinc;
double[][][] cDpos;
double[][][] cDneg;
double[][][] cDinc;

double[] poshidact;
double[] neghidact;
Expand All @@ -72,33 +72,32 @@ public class RBMRecommender extends MatrixFactorizationRecommender {

char[] negvissoftmax;
int[] moviecount;
String PredictionType;
String predictionType;

public RBMRecommender() {

}

protected void setup() throws LibrecException {
super.setup();

softmax = ratingScale.size();
this.maxIter = numIterations;
featureNumber = conf.getInt("-fn", 100);
epsilonw = conf.getDouble("-ew", 0.001);
epsilonvb = conf.getDouble("-evb", 0.001);
epsilonhb = conf.getDouble("-ehb", 0.001);
tSteps = conf.getInt("-t", 1);
momentum = conf.getDouble("-m", 0.0d);
lamtaw = conf.getDouble("-lw", 0.001);
lamtab = conf.getDouble("-lb", 0.0d);
PredictionType = conf.get("-p", "mean");
this.maxIter = conf.getInt("rec.iterator.maximum", 10);
featureNumber = conf.getInt("rec.factor.number", 500);
epsilonw = conf.getDouble("rec.epsilonw", 0.001);
epsilonvb = conf.getDouble("rec.epsilonvb", 0.001);
epsilonhb = conf.getDouble("rec.epsilonhb", 0.001);
tSteps = conf.getInt("rec.tstep", 1);
momentum = conf.getDouble("rec.momentum", 0.0d);
lamtaw = conf.getDouble("rec.lamtaw", 0.001);
lamtab = conf.getDouble("rec.lamtab", 0.0d);
predictionType = conf.get("rec.predictiontype", "mean");
weights = new double[numItems][softmax][featureNumber];
visbiases = new double[numItems][softmax];
hidbiases = new double[featureNumber];

CDpos = new double[numItems][softmax][featureNumber];
CDneg = new double[numItems][softmax][featureNumber];
CDinc = new double[numItems][softmax][featureNumber];
cDpos = new double[numItems][softmax][featureNumber];
cDneg = new double[numItems][softmax][featureNumber];
cDinc = new double[numItems][softmax][featureNumber];

poshidact = new double[featureNumber];
neghidact = new double[featureNumber];
Expand Down Expand Up @@ -157,7 +156,7 @@ protected void trainModel() throws LibrecException {
Random randn = new Random();
while (loopcount < maxIter) {
loopcount++;
Zero();
zero();
int[] visitingSeq = new int[numUsers];
for (int i = 0; i < visitingSeq.length; i++) {
visitingSeq[i] = i;
Expand Down Expand Up @@ -270,9 +269,9 @@ else if ((randval -= negvisprobs[m][3]) <= 0.0)

for (int h = 0; h < featureNumber; h++) {
if (poshidstates[h] == 1) {
CDpos[m][r][h] += 1.0;
cDpos[m][r][h] += 1.0;
}
CDneg[m][negvissoftmax[m]][h] += (double) neghidstates[h];
cDneg[m][negvissoftmax[m]][h] += (double) neghidstates[h];
}
}
update(u, num);
Expand All @@ -294,14 +293,14 @@ private void update(int user, int num) {
for (int h = 0; h < featureNumber; h++) {

for (int r = 0; r < softmax; r++) {
double CDp = CDpos[m][r][h];
double CDn = CDneg[m][r][h];
double CDp = cDpos[m][r][h];
double CDn = cDneg[m][r][h];
if (CDp != 0.0 || CDn != 0.0) {
CDp /= ((double) moviecount[m]);
CDn /= ((double) moviecount[m]);
CDinc[m][r][h] = momentum * CDinc[m][r][h]
cDinc[m][r][h] = momentum * cDinc[m][r][h]
+ epsilonw * ((CDp - CDn) - lamtaw * weights[m][r][h]);
weights[m][r][h] += CDinc[m][r][h];
weights[m][r][h] += cDinc[m][r][h];
}
}
}
Expand All @@ -324,13 +323,13 @@ private void update(int user, int num) {
hidbiases[h] += hidbiasinc[h];
}
}
Zero();
zero();
}
}

private void Zero() {
CDpos = new double[numItems][softmax][featureNumber];
CDneg = new double[numItems][softmax][featureNumber];
private void zero() {
cDpos = new double[numItems][softmax][featureNumber];
cDneg = new double[numItems][softmax][featureNumber];
poshidact = new double[featureNumber];
neghidact = new double[featureNumber];
posvisact = new double[numItems][softmax];
Expand All @@ -341,11 +340,11 @@ private void Zero() {
protected double predict(int u, int m) throws LibrecException {
double[][] negvisprobs = new double[numItems][softmax];
double[] poshidprobs = new double[featureNumber];
int trainNumber = trainMatrix.rowSize(u);
int trainNumber = testMatrix.rowSize(u);
double[] sumW = new double[featureNumber];
for (int i = 0; i < trainNumber; i++) {
int item = trainMatrix.row(u).getIndex()[i];
int rate = (int) trainMatrix.get(u, item);
int item = testMatrix.row(u).getIndex()[i];
int rate = (int) testMatrix.get(u, item);

for (int h = 0; h < featureNumber; h++) {
sumW[h] += weights[item][rate][h];
Expand All @@ -357,7 +356,8 @@ protected double predict(int u, int m) throws LibrecException {
}

for (int i = 0; i < trainNumber; i++) {
int item = trainMatrix.row(u).getIndex()[i];

int item = testMatrix.row(u).getIndex()[i];
for (int h = 0; h < featureNumber; h++) {
for (int r = 0; r < softmax; r++) {
negvisprobs[item][r] += poshidprobs[h] * weights[item][r][h];
Expand All @@ -380,7 +380,7 @@ protected double predict(int u, int m) throws LibrecException {
}
}
double predict = 0;
if (PredictionType.equals("max")) {
if (predictionType.equals("max")) {

int max_index = 0;
double max_value = negvisprobs[m][0];
Expand All @@ -391,7 +391,7 @@ protected double predict(int u, int m) throws LibrecException {
}
}
predict = max_index + 1;
} else if (PredictionType.equals("mean")) {
} else if (predictionType.equals("mean")) {
double mean = 0.0;
for (int r = 0; r < softmax; r++) {
mean += negvisprobs[m][r] * (r + 1);
Expand Down
19 changes: 11 additions & 8 deletions core/src/main/resources/rec/cf/rating/rbm-test.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
rec.recommender.class=rbm
rec.iterator.learnrate=0.01
rec.iterator.learnrate.maximum=0.01
rec.iterator.maximum=100
rec.user.regularization=0.01
rec.item.regularization=0.01
rec.factor.number=10
rec.learnrate.bolddriver=false
rec.learnrate.decay=1.0
rec.iterator.maximum=20
data.input.path=movielens/ml-100k/ratings.txt
rec.factor.number=500
rec.epsilonw=0.001
rec.epsilonvb=0.01
rec.epsilonhb=0.01
rec.tstep=1
rec.momentum=0.1
rec.lamtaw=0.01
rec.lamtab=0.0
rec.predictiontype=mean
Loading