Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce the parameters of FMFTRL algorithm #216

Merged
merged 1 commit into from
Nov 15, 2017
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 @@ -17,37 +17,30 @@ public class FMFTRLRecommender extends FactorizationMachineRecommender {
/**
* lambda1 is the truncated threshold
*/
private double lambda1W0, lambda1W, lambda1V;
private double lambda1;

/**
* lambda2 is the L2 regularization
*/
private double lambda2W0, lambda2W, lambda2V;
private double lambda2;

/**
* alpha and beta are used to compute learning rate.
* The learning rate n = alpha / ( beta + sqrt(sum(g_i^2)) )
*/
private double alphaW0, alphaW, alphaV;
private double betaW0, betaW, betaV;
private double alpha;
private double beta;

@Override
protected void setup() throws LibrecException {
super.setup();
lambda1W0 = conf.getDouble("rec.regularization.lambda1W0");
lambda1W = conf.getDouble("rec.regularization.lambda1W");
lambda1V = conf.getDouble("rec.regularization.lambda1V");

lambda2W0 = conf.getDouble("rec.regularization.lambda2W0");
lambda2W = conf.getDouble("rec.regularization.lambda2W");
lambda2V = conf.getDouble("rec.regularization.lambda2V");

alphaW0 = conf.getDouble("rec.alphaW0");
alphaW = conf.getDouble("rec.alphaW");
alphaV = conf.getDouble("rec.alphaV");
betaW0 = conf.getDouble("rec.betaW0");
betaW = conf.getDouble("rec.betaW");
betaV = conf.getDouble("rec.betaV");
lambda1 = conf.getDouble("rec.regularization.lambda1");

lambda2 = conf.getDouble("rec.regularization.lambda2");

alpha = conf.getDouble("rec.learningRate.alpha");

beta = conf.getDouble("rec.learningRate.beta");
}

@Override
Expand Down Expand Up @@ -99,31 +92,31 @@ private void buildRatingModel() throws LibrecException {
// compute w0 gradient
double hW0 = 1;
gW0 = gradLoss * hW0;
thetaW0 = 1 / alphaW0 * (Math.sqrt(nW0 + Math.pow(gW0, 2)) - Math.sqrt(nW0));
thetaW0 = 1 / alpha * (Math.sqrt(nW0 + Math.pow(gW0, 2)) - Math.sqrt(nW0));
zW0 += gW0 - thetaW0 * w0;
nW0 += Math.pow(gW0, 2);

// update w0
if (Math.abs(zW0) <= lambda1W0) {
if (Math.abs(zW0) <= lambda1) {
w0 = 0;
} else {
w0 = -1 / ((betaW0 + Math.sqrt(nW0)) / alphaW0 + lambda2W0) * (zW0 - sgn(zW0) * lambda1W0);
w0 = -1 / ((beta + Math.sqrt(nW0)) / alpha + lambda2) * (zW0 - sgn(zW0) * lambda1);
}

for(VectorEntry ve: x){
int l = ve.index();
// compute W gradient
double hWl = ve.get();
gW.set(l, gradLoss * hWl);
thetaW.set(l, 1 / alphaW * (Math.sqrt(nW.get(l) + Math.pow(gW.get(l), 2)) - Math.sqrt(nW.get(l))));
thetaW.set(l, 1 / alpha * (Math.sqrt(nW.get(l) + Math.pow(gW.get(l), 2)) - Math.sqrt(nW.get(l))));
zW.add(l, gW.get(l) - thetaW.get(l) * W.get(l));
nW.add(l, Math.pow(gW.get(l), 2));

// update W
if (Math.abs(zW.get(l)) <= lambda1W) {
if (Math.abs(zW.get(l)) <= lambda1) {
W.set(l, 0);
} else {
double value = -1 / ((betaW + Math.sqrt(nW.get(l))) / alphaW + lambda2W) * (zW.get(l) - sgn(zW.get(l)) * lambda1W);
double value = -1 / ((beta + Math.sqrt(nW.get(l))) / alpha + lambda2) * (zW.get(l) - sgn(zW.get(l)) * lambda1);
W.set(l, value);
}

Expand All @@ -141,15 +134,15 @@ private void buildRatingModel() throws LibrecException {
double gradVlf = gradLoss * hVlf;

gV.set(l, f, gradVlf);
thetaV.set(l, f, 1 / alphaV * (Math.sqrt(nV.get(l, f) + Math.pow(gV.get(l, f), 2)) - Math.sqrt(nV.get(l, f))));
thetaV.set(l, f, 1 / alpha * (Math.sqrt(nV.get(l, f) + Math.pow(gV.get(l, f), 2)) - Math.sqrt(nV.get(l, f))));
zV.add(l, f, gV.get(l, f) - thetaV.get(l, f) * V.get(l, f));
nV.add(l, f, Math.pow(gV.get(l, f), 2));

// update V
if (Math.abs(zV.get(l, f)) <= lambda1V) {
if (Math.abs(zV.get(l, f)) <= lambda1) {
V.set(l, f, 0);
} else {
double value = -1 / ((betaV + Math.sqrt(nV.get(l, f))) / alphaV + lambda2V) * (zV.get(l, f) - sgn(zV.get(l, f)) * lambda1V);
double value = -1 / ((beta + Math.sqrt(nV.get(l, f))) / alpha + lambda2) * (zV.get(l, f) - sgn(zV.get(l, f)) * lambda1);
V.set(l, f, value);
}
}
Expand Down
20 changes: 5 additions & 15 deletions core/src/main/resources/rec/cf/rating/fmftrl-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@ data.model.format=arff

rec.recommender.class=fmftrl

rec.iterator.maximum=50
rec.iterator.maximum=30
rec.factor.number=10

rec.regularization.lambda1W0=0.05
rec.regularization.lambda1W=0.05
rec.regularization.lambda1V=0.05

rec.regularization.lambda2W0=5.0
rec.regularization.lambda2W=5.0
rec.regularization.lambda2V=5.0

rec.alphaW0=0.01
rec.alphaW=0.01
rec.alphaV=0.01
rec.betaW0=1
rec.betaW=1
rec.betaV=1
rec.regularization.lambda1=0.05
rec.regularization.lambda2=1.0
rec.learningRate.alpha=0.015
rec.learningRate.beta=1