// Z pliku binarnego, jeśli istnieje public ALS(int iloscFaktorow, String FileName) { R = Serializer.UnpackRMatrix(FileName); U = new Matrix(iloscFaktorow, R.u); U.FillRandom(); P = new Matrix(iloscFaktorow, R.p); P.FillRandom(); countOfFactors = iloscFaktorow; }
// Z pliku txt, jeśli istnieje public ALS(int iloscFaktorow, int iloscProduktow, int iloscUserow) { R = Extractor.createR(iloscProduktow, iloscUserow); Serializer.PackRMatrix(R); U = new Matrix(iloscFaktorow, R.u); U.FillRandom(); P = new Matrix(iloscFaktorow, R.p); P.FillRandom(); countOfFactors = iloscFaktorow; }
public static void Calculate(int d, RMatrix R, Matrix U, Matrix P, double lambda) { double sum1 = 0; foreach (var kvp in R.ratings) { double r_up = kvp.Value; int u = kvp.Key.Item1; int p = kvp.Key.Item2; double vectorSum = 0; for (int row = 0; row < d; row++) { vectorSum += U.Data[row, u] * P.Data[row, p]; } sum1 += Math.Pow(r_up + vectorSum, 2); } double sum2 = 0; for (int col = 0; col < U.ColumnCount; col++) { double vectorSum = 0; for (int row = 0; row < d; row++) { vectorSum += Math.Pow(U.Data[row, col], 2); } sum2 += vectorSum; } double sum3 = 0; for (int col = 0; col < P.ColumnCount; col++) { double vectorSum = 0; for (int row = 0; row < d; row++) { vectorSum += Math.Pow(P.Data[row, col], 2); } sum3 += vectorSum; } double total = sum1 + lambda * (sum2 + sum3); //Console.WriteLine($"sum1 = ${sum1}, sum2 = ${sum2}, sum3 = ${sum3}, total = ${total}"); Console.WriteLine(total); }
public RMatrix extractToR() { RMatrix r = new RMatrix(); int standardItemID = 0; foreach (Item item in items) { foreach (Tuple <string, int> ratingTuple in item.Rates) { r.Add(users[ratingTuple.Item1], standardItemID, ratingTuple.Item2); } standardItemID++; } r.setSize(users.Count, items.Count); return(r); }
public static void PackRMatrix(RMatrix rMatrix) { Pack(rMatrix, "p" + rMatrix.p + "u" + rMatrix.u); }