BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Recommender systems10 of 22 in topic

203. Take one SGD step of matrix factorisation

MediumRecommender systems Pro
Asked atMeta

Matrix factorisation learns a vector for each user and each item so that their dot product predicts the rating. Training is plain SGD over observed ratings.

Write mf_step(U, V, ratings, lr, reg) where ratings is a list of (user, item, value). Process them in order. For each one:
err = value - U[u] @ V[i]
U[u] += lr * (err * V[i] - reg * U[u])
V[i] += lr * (err * U[u]_old - reg * V[i])
Use the user vector from BEFORE its update when updating V[i]. Return new (U, V) without modifying the inputs.

Functiondef mf_step(U, V, ratings, lr, reg)

Example 1

Input: mf_step([[1.0, 0.0]], [[1.0, 1.0]], [(0, 0, 3.0)], 0.1, 0.0)

Output: ([[1.2, 0.2]], [[1.2, 1.0]])

Example 2

Input: mf_step(np.array([[0.1, 0.2], [0.3, -0.1]]), np.array([[0.2, 0.1], [-0.2, 0.4], [0.5, 0.5]]), [(0, 0, 4.0), (1, 2, 2.0), (0, 2, 5.0)], 0.1, 0.01)[0]

Output: [[0.445223, 0.469094], [0.3947, -0.0049]]

Example 3

Input: mf_step(np.array([[0.1, 0.2], [0.3, -0.1]]), np.array([[0.2, 0.1], [-0.2, 0.4], [0.5, 0.5]]), [(0, 0, 4.0), (1, 2, 2.0), (0, 2, 5.0)], 0.1, 0.01)[1]

Output: [[0.2394, 0.1791], [-0.2, 0.4], [0.641648, 0.59458]]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Copy U and V first.

Hint 2

Inside the loop save u_old = U[u].copy() before updating it.

Common mistakes
  • Using the freshly updated user vector for the item update, as the buggy version does, is a subtle bug that changes the training path.
Understand the idea

Matrix factorisation gives every user and every item a short vector, and their dot product should predict the rating. Training walks through observed ratings and nudges both vectors to shrink the error, with a small pull toward zero (regularisation). Both nudges come from the same error, so both must use the parameter values from before this rating's update. Worked example, lr = 0.1, reg = 0: U[0] = [1, 0], V[0] = [1, 1], rating 3. Prediction = 1, err = 2. New U[0] = [1, 0] + 0.1 x 2 x [1, 1] = [1.2, 0.2]. New V[0] = [1, 1] + 0.1 x 2 x [1, 0] (old U) = [1.2, 1.0]. Using the new U by mistake would give [1.24, 1.04].

err = r - U[u] 

V[i]; U[u] += lr (err V[i] - reg U[u]); V[i] += lr (err U_old - reg V[i]). Process ratings in order and work on copies. The Netflix Prize era recommenders were built on this.

Topics
Recommender systems
Companies1
  • Meta the topic appears in interview reports Machine Learning interview questions, page 10 (KatKat)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Predict a rating from similar usersMedium
  • Recommend unseen items with item-item cosine similarityMedium
  • Sample negative items a user has not interacted withMedium
  • Compute mean average precision at kMedium
All Recommender systems problems
Report
203 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def mf_step(U, V, ratings, lr, reg):
# your code here
pass
​
mf_step(np.array([[0.1, 0.2], [0.3, -0.1]]), np.array([[0.2, 0.1], [-0.2, 0.4], [0.5, 0.5]]), [(0, 0, 4.0), (1, 2, 2.0), (0, 2, 5.0)], 0.1, 0.01)[0]

This case uses generated data, so it runs as written. Press + to add your own case.

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS