@Override public void run() { double dot = 0; for (int k = 0; k < numIndexes; k++) { Vector row = Y.get(indexes.getQuick(k)); dot += row.getQuick(ii) * row.getQuick(jj); } YtY[ii][jj] = dot; if (ii != jj) { YtY[jj][ii] = dot; } } });
/** Y' Cu p(u) */ private Matrix getYtransponseCuPu(Vector userRatings) { Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!"); Vector YtransponseCuPu = new DenseVector(numFeatures); for (Element e : userRatings.nonZeroes()) { YtransponseCuPu.assign(Y.get(e.index()).times(confidence(e.get())), Functions.PLUS); } return columnVectorAsMatrix(YtransponseCuPu); }
/** Y' (Cu - I) Y + λ I */ private Matrix getYtransponseCuMinusIYPlusLambdaI(Vector userRatings) { Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!"); /* (Cu -I) Y */ OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<>(userRatings.getNumNondefaultElements()); for (Element e : userRatings.nonZeroes()) { CuMinusIY.put(e.index(), Y.get(e.index()).times(confidence(e.get()) - 1)); } Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures); /* Y' (Cu -I) Y by outer products */ for (Element e : userRatings.nonZeroes()) { for (Vector.Element feature : Y.get(e.index()).all()) { Vector partial = CuMinusIY.get(e.index()).times(feature.get()); YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS); } } /* Y' (Cu - I) Y + λ I add lambda on the diagonal */ for (int feature = 0; feature < numFeatures; feature++) { YtransponseCuMinusIY.setQuick(feature, feature, YtransponseCuMinusIY.getQuick(feature, feature) + lambda); } return YtransponseCuMinusIY; }
@Test public void testClear() { OpenIntObjectHashMap<TestClass> map = new OpenIntObjectHashMap<TestClass>(); for (int i = 0; i < 100; i++) { map.put((int) i, item); assertEquals(1, map.size()); map.clear(); assertEquals(0, map.size()); assertFalse("Contains: " + i, map.containsKey((int) i)); assertSame(null, map.get((int) i)); } }
public FPNode child(int attribute) { return childMap.get(attribute); }
@Override public double getQuick(int row, int column) { Vector r = rowVectors.get(row); return r == null ? 0.0 : r.getQuick(column); }
@Override protected void map(LongWritable key, Text value, Context ctx) throws IOException, InterruptedException { String[] tokens = TasteHadoopUtils.splitPrefTokens(value.toString()); int userID = TasteHadoopUtils.readID(tokens[TasteHadoopUtils.USER_ID_POS], usesLongIDs); int itemID = TasteHadoopUtils.readID(tokens[TasteHadoopUtils.ITEM_ID_POS], usesLongIDs); double rating = Double.parseDouble(tokens[2]); if (U.containsKey(userID) && M.containsKey(itemID)) { double estimate = U.get(userID).dot(M.get(itemID)); error.set(rating - estimate); ctx.write(error, NullWritable.get()); } } }
@Override protected void map(LongWritable key, Text value, Context ctx) throws IOException, InterruptedException { String[] tokens = TasteHadoopUtils.splitPrefTokens(value.toString()); int userID = TasteHadoopUtils.readID(tokens[TasteHadoopUtils.USER_ID_POS], usesLongIDs); int itemID = TasteHadoopUtils.readID(tokens[TasteHadoopUtils.ITEM_ID_POS], usesLongIDs); double rating = Double.parseDouble(tokens[2]); if (U.containsKey(userID) && M.containsKey(itemID)) { double estimate = U.get(userID).dot(M.get(itemID)); error.set(rating - estimate); ctx.write(error, NullWritable.get()); } } }
@Override protected void map(LongWritable key, Text value, Context ctx) throws IOException, InterruptedException { String[] tokens = TasteHadoopUtils.splitPrefTokens(value.toString()); int userID = TasteHadoopUtils.readID(tokens[TasteHadoopUtils.USER_ID_POS], usesLongIDs); int itemID = TasteHadoopUtils.readID(tokens[TasteHadoopUtils.ITEM_ID_POS], usesLongIDs); double rating = Double.parseDouble(tokens[2]); if (U.containsKey(userID) && M.containsKey(itemID)) { double estimate = U.get(userID).dot(M.get(itemID)); error.set(rating - estimate); ctx.write(error, NullWritable.get()); } } }
public static Vector solveExplicit(VectorWritable ratingsWritable, OpenIntObjectHashMap<Vector> uOrM, double lambda, int numFeatures) { Vector ratings = ratingsWritable.get(); List<Vector> featureVectors = Lists.newArrayListWithCapacity(ratings.getNumNondefaultElements()); for (Vector.Element e : ratings.nonZeroes()) { int index = e.index(); featureVectors.add(uOrM.get(index)); } return AlternatingLeastSquaresSolver.solve(featureVectors, ratings, lambda, numFeatures); } }
public static Vector solveExplicit(VectorWritable ratingsWritable, OpenIntObjectHashMap<Vector> uOrM, double lambda, int numFeatures) { Vector ratings = ratingsWritable.get(); List<Vector> featureVectors = new ArrayList<>(ratings.getNumNondefaultElements()); for (Vector.Element e : ratings.nonZeroes()) { int index = e.index(); featureVectors.add(uOrM.get(index)); } return AlternatingLeastSquaresSolver.solve(featureVectors, ratings, lambda, numFeatures); } }
@Override public void run() { double dot = 0; for (int k = 0; k < numIndexes; k++) { Vector row = Y.get(indexes.getQuick(k)); dot += row.getQuick(ii) * row.getQuick(jj); } YtY[ii][jj] = dot; if (ii != jj) { YtY[jj][ii] = dot; } } });
public static Vector solveExplicit(VectorWritable ratingsWritable, OpenIntObjectHashMap<Vector> uOrM, double lambda, int numFeatures) { Vector ratings = ratingsWritable.get(); List<Vector> featureVectors = Lists.newArrayListWithCapacity(ratings.getNumNondefaultElements()); for (Vector.Element e : ratings.nonZeroes()) { int index = e.index(); featureVectors.add(uOrM.get(index)); } return AlternatingLeastSquaresSolver.solve(featureVectors, ratings, lambda, numFeatures); } }
@Override protected MatrixSlice computeNext() { if (slice >= rowVectors.size()) { return endOfData(); } int i = keys.get(slice); Vector row = rowVectors.get(i); slice++; return new MatrixSlice(row, i); } };
@Override public void setQuick(int row, int column, double value) { Vector r = rowVectors.get(row); if (r == null) { r = new RandomAccessSparseVector(columnSize()); rowVectors.put(row, r); } r.setQuick(column, value); }
/** Y' Cu p(u) */ private Matrix YtransponseCuPu(Vector userRatings) { Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!"); Vector YtransponseCuPu = new DenseVector(numFeatures); Iterator<Vector.Element> ratings = userRatings.iterateNonZero(); while (ratings.hasNext()) { Vector.Element e = ratings.next(); YtransponseCuPu.assign(Y.get(e.index()).times(confidence(e.get())), Functions.PLUS); } return columnVectorAsMatrix(YtransponseCuPu); }
@Override public Vector viewRow(int row) { if (row < 0 || row >= rowSize()) { throw new IndexException(row, rowSize()); } Vector res = rowVectors.get(row); if (res == null) { res = new RandomAccessSparseVector(columnSize()); } return res; }
/** Y' Cu p(u) */ private Matrix getYtransponseCuPu(Vector userRatings) { Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!"); Vector YtransponseCuPu = new DenseVector(numFeatures); for (Element e : userRatings.nonZeroes()) { YtransponseCuPu.assign(Y.get(e.index()).times(confidence(e.get())), Functions.PLUS); } return columnVectorAsMatrix(YtransponseCuPu); }
private Matrix YtransposeY(OpenIntObjectHashMap<Vector> Y) { Matrix compactedY = new DenseMatrix(Y.size(), numFeatures); IntArrayList indexes = Y.keys(); indexes.quickSort(); int row = 0; for (int index : indexes.elements()) { compactedY.assignRow(row++, Y.get(index)); } return compactedY.transpose().times(compactedY); }