@Override public String toString() { return MoreObjects.toStringHelper(RatingMatrixEntry.class) .add("index", getIndex()) .add("user", getUserId()) .add("item", getItemId()) .add("value", getValue()) .toString(); } }
double n = 0; for (RatingMatrixEntry r : ratings) { sum += r.getValue(); n += 1; final int iidx = r.getItemIndex(); final double p = mean + uoff[uidx] + ioff[iidx]; final double err = r.getValue() - p; uoff[uidx] += learningRate * (err - regularizationFactor * Math.abs(uoff[uidx])); ioff[iidx] += learningRate * (err - regularizationFactor * Math.abs(ioff[iidx]));
int user = ratingEntry.getUserIndex(); int item = ratingEntry.getItemIndex(); double rating = ratingEntry.getValue(); double eThetaBeta = 0.0; for (int k = 0; k < featureCount; k++) {
@Test public void testGetRatings() { List<RatingMatrixEntry> ratings = snap.getRatings(); assertEquals(20, ratings.size()); for (int i = 0; i < 20; i++) { RatingMatrixEntry entry = ratings.get(i); assertThat(entry.getIndex(), equalTo(i)); assertThat(ratingList, Matchers.<List<Rating>>anyOf(hasItem(rating(entry.getUserId(), entry.getItemId(), entry.getValue(), 1)), hasItem(rating(entry.getUserId(), entry.getItemId(), entry.getValue(), 2)))); } }
final int iidx = r.getItemIndex(); updater.prepare(0, r.getValue(), estimates.get(r), userFeatureVector.getEntry(uidx), itemFeatureVector.getEntry(iidx), trail);
@Override public synchronized Long2DoubleMap getUserRatingVector(long userId) { // FIXME Don't make this so locky if (cache == null) { cache = new Long2ObjectOpenHashMap<>(); } Long2DoubleMap data = cache.get(userId); if (data != null) { return data; } else { Collection<RatingMatrixEntry> prefs = this.getUserRatings(userId); Long2DoubleMap map = new Long2DoubleOpenHashMap(); for (RatingMatrixEntry e: prefs) { map.put(e.getItemId(), e.getValue()); } data = new Long2DoubleSortedArrayMap(map); cache.put(userId, data); return data; } }
@Test public void testAddPreference() { int idx = bld.add(10, 39, 3.5); assertThat(idx, equalTo(0)); assertThat(bld.size(), equalTo(1)); PackedRatingData data = bld.build(); assertThat(data.size(), equalTo(1)); RatingMatrixEntry p2 = data.getEntry(0); assertThat(p2.getIndex(), equalTo(0)); assertThat(p2.getUserIndex(), equalTo(0)); assertThat(p2.getItemIndex(), equalTo(0)); assertThat(p2.getUserId(), equalTo(10L)); assertThat(p2.getItemId(), equalTo(39L)); assertThat(p2.getValue(), equalTo(3.5)); }
RatingMatrixEntry entry = userRatings.next(); int item = entry.getItemIndex(); double rating = entry.getValue(); if (rating <= 0) { continue;
RatingMatrixEntry entry = itemRatings.next(); int user = entry.getUserIndex(); double rating = entry.getValue(); if (rating <= 0) { continue;
@Test public void testGetRatings() throws Exception { RandomDataSplitStrategyProvider splitData = new RandomDataSplitStrategyProvider(snapshot, new Random(), 0, 0.2); DataSplitStrategy splitStrategy = splitData.get(); KeyIndex userIndex = splitStrategy.getUserIndex(); KeyIndex itemIndex = splitStrategy.getItemIndex(); List<RatingMatrixEntry> validations = splitStrategy.getValidationRatings(); List<RatingMatrixEntry> trainingRatings = splitStrategy.getTrainRatings(); for (RatingMatrixEntry re : trainingRatings) { int user = re.getUserIndex(); int item = re.getItemIndex(); long userId = userIndex.getKey(user); long itemId = itemIndex.getKey(item); double rating = re.getValue(); assertThat(rating, equalTo(snapshot.getUserRatingVector(userId).get(itemId))); assertThat(rating, equalTo(data.get(itemId).get(userId))); } for (RatingMatrixEntry re : validations) { long userId = re.getUserId(); long itemId = re.getItemId(); double rating = re.getValue(); assertThat(rating, equalTo(snapshot.getUserRatingVector(userId).get(itemId))); assertThat(rating, equalTo(data.get(itemId).get(userId))); } }
int item = entry.getItemIndex(); int user = entry.getUserIndex(); double ratingUI = entry.getValue(); if (ratingUI <= 0) { continue; int user = ratingEntry.getUserIndex(); int item = ratingEntry.getItemIndex(); double rating = ratingEntry.getValue(); double eThetaBeta = 0.0; for (int k = 0; k < featureCount; k++) {
@Override public String toString() { return MoreObjects.toStringHelper(RatingMatrixEntry.class) .add("index", getIndex()) .add("user", getUserId()) .add("item", getItemId()) .add("value", getValue()) .toString(); } }
double n = 0; for (RatingMatrixEntry r : ratings) { sum += r.getValue(); n += 1; final int iidx = r.getItemIndex(); final double p = mean + uoff[uidx] + ioff[iidx]; final double err = r.getValue() - p; uoff[uidx] += learningRate * (err - regularizationFactor * Math.abs(uoff[uidx])); ioff[iidx] += learningRate * (err - regularizationFactor * Math.abs(ioff[iidx]));
@Override public synchronized Long2DoubleMap getUserRatingVector(long userId) { // FIXME Don't make this so locky if (cache == null) { cache = new Long2ObjectOpenHashMap<>(); } Long2DoubleMap data = cache.get(userId); if (data != null) { return data; } else { Collection<RatingMatrixEntry> prefs = this.getUserRatings(userId); Long2DoubleMap map = new Long2DoubleOpenHashMap(); for (RatingMatrixEntry e: prefs) { map.put(e.getItemId(), e.getValue()); } data = new Long2DoubleSortedArrayMap(map); cache.put(userId, data); return data; } }
/** * Do a single feature iteration. * * * * @param estimates The estimates. * @param ratings The ratings to train on. * @param userFeatureVector The user column vector for the current feature. * @param itemFeatureVector The item column vector for the current feature. * @param trail The sum of the remaining user-item-feature values. * @return The RMSE of the feature iteration. */ protected double doFeatureIteration(TrainingEstimator estimates, List<RatingMatrixEntry> ratings, RealVector userFeatureVector, RealVector itemFeatureVector, double trail) { // We'll create a fresh updater for each feature iteration // Not much overhead, and prevents needing another parameter FunkSVDUpdater updater = rule.createUpdater(); for (RatingMatrixEntry r : ratings) { final int uidx = r.getUserIndex(); final int iidx = r.getItemIndex(); updater.prepare(0, r.getValue(), estimates.get(r), userFeatureVector.getEntry(uidx), itemFeatureVector.getEntry(iidx), trail); // Step 3: Update feature values userFeatureVector.addToEntry(uidx, updater.getUserFeatureUpdate()); itemFeatureVector.addToEntry(iidx, updater.getItemFeatureUpdate()); } return updater.getRMSE(); }