long duration = System.currentTimeMillis() - start;
/* a hold out test would be better, but this is just a toy example so we only check that the
* factorization is close to the original matrix */
RunningAverage avg = new FullRunningAverage();
LongPrimitiveIterator userIDs = dataModel.getUserIDs();
LongPrimitiveIterator itemIDs;
while (userIDs.hasNext()) {
long userID = userIDs.nextLong();
for (Preference pref : dataModel.getPreferencesFromUser(userID)) {
double rating = pref.getValue();
Vector userVector = new DenseVector(factorization.getUserFeatures(userID));
Vector itemVector = new DenseVector(factorization.getItemFeatures(pref.getItemID()));
double estimate = userVector.dot(itemVector);
double err = rating - estimate;
avg.addDatum(err * err);
}
}
double sum = 0.0;
userIDs = dataModel.getUserIDs();
while (userIDs.hasNext()) {
long userID = userIDs.nextLong();
Vector userVector = new DenseVector(factorization.getUserFeatures(userID));
double regularization=userVector.dot(userVector);
sum += regularization;
}
itemIDs = dataModel.getItemIDs();
while (itemIDs.hasNext()) {
long itemID = itemIDs.nextLong();
Vector itemVector = new DenseVector(factorization.getUserFeatures(itemID));
double regularization = itemVector.dot(itemVector);
sum += regularization;
}
double rmse = Math.sqrt(avg.getAverage());
double loss = avg.getAverage() / 2 + lambda / 2 * sum;
logger.info("RMSE: " + rmse + ";\tLoss: " + loss + ";\tTime Used: " + duration + "ms");
assertTrue(rmse < 0.2);
}