TensorTonic
Problems
Matrix Factorization SGD Step
Recommender Systems
Easy

Matrix factorization is the backbone of modern recommender systems (popularized by the Netflix Prize). It approximates the user-item rating matrix as a product of two low-rank matrices: user factors U and item factors V. Each user and item is represented by a latent vector, and the predicted rating is their dot product.

Given a user latent vector U, an item latent vector V, a known rating r, a learning rate lr, and a regularization strength reg, perform one step of stochastic gradient descent to update both vectors.

Algorithm

  1. Compute the prediction error:
e=r−U⋅Ve = r - U \cdot Ve=r−U⋅V
  1. Update both vectors simultaneously using the old values:
Ui′=Ui+lr⋅(e⋅Vi−reg⋅Ui)U_i' = U_i + \text{lr} \cdot (e \cdot V_i - \text{reg} \cdot U_i)Ui′​=Ui​+lr⋅(e⋅Vi​−reg⋅Ui​) Vi′=Vi+lr⋅(e⋅Ui−reg⋅Vi)V_i' = V_i + \text{lr} \cdot (e \cdot U_i - \text{reg} \cdot V_i)Vi′​=Vi​+lr⋅(e⋅Ui​−reg⋅Vi​)

Both U and V must be updated using their original (pre-update) values.

Return [U_new, V_new]. You do not need to round the returned vectors; answers that match the expected values to four decimal places are accepted.

Loading visualization...

Examples

Input: U = [1, 0], V = [0, 1], r = 5, lr = 0.1, reg = 0

Output: [[1.0, 0.5], [0.5, 1.0]]

Explanation: The zero dot product gives error 5, and both vectors are updated from their original values.

Input: U = [0.5, 0.3], V = [0.4, 0.6], r = 4, lr = 0.01, reg = 0.02

Output: [[0.5144, 0.3217], [0.418, 0.6107]]

Hint 1

Compute the rating error from the original vectors before either update.

Hint 2

Build both new vectors from the original U and V. Rounding each coordinate is optional.

Requirements

  • Compute the prediction error as r minus the dot product of U and V
  • Update U and V simultaneously using original (pre-update) values
  • Apply L2 regularization with strength reg to prevent overfitting
  • Return [U_new, V_new] where each item is a list of floats
  • You do not need to round the returned numbers; answers that match the expected values to 4 decimal places are accepted.

Constraints

  • U and V have the same length (latent dimension k >= 1)
  • lr > 0, reg >= 0
  • r is a number
  • Return a two-item list containing two lists of floats
  • Time limit: 300 ms
Try Similar Problems
Mean Rating ImputationMediumItem-Based CF PredictionMediumUser-Based CF PredictionMediumBaseline PredictorHardImplement Gradient Descent for a 1D QuadraticEasyImplement Precision and Recall at K in PythonEasyRating NormalizationEasyTop-K RecommendationsEasyCatalog CoverageEasyJaccard Similarity in PythonEasyHit Rate at KEasyPopularity RankingEasy
Scratchpad

Accepts: array

Accepts: array

Accepts: number

Accepts: number

Accepts: number

You must run your code first.
Matrix Factorization SGD Step

Matrix Factorization SGD Step

Recommender Systems
Easy

Matrix factorization is the backbone of modern recommender systems (popularized by the Netflix Prize). It approximates the user-item rating matrix as a product of two low-rank matrices: user factors U and item factors V. Each user and item is represented by a latent vector, and the predicted rating is their dot product.

Given a user latent vector U, an item latent vector V, a known rating r, a learning rate lr, and a regularization strength reg, perform one step of stochastic gradient descent to update both vectors.

Algorithm

  1. Compute the prediction error:
e=r−U⋅Ve = r - U \cdot Ve=r−U⋅V
  1. Update both vectors simultaneously using the old values:
Ui′=Ui+lr⋅(e⋅Vi−reg⋅Ui)U_i' = U_i + \text{lr} \cdot (e \cdot V_i - \text{reg} \cdot U_i)Ui′​=Ui​+lr⋅(e⋅Vi​−reg⋅Ui​) Vi′=Vi+lr⋅(e⋅Ui−reg⋅Vi)V_i' = V_i + \text{lr} \cdot (e \cdot U_i - \text{reg} \cdot V_i)Vi′​=Vi​+lr⋅(e⋅Ui​−reg⋅Vi​)

Both U and V must be updated using their original (pre-update) values.

Return [U_new, V_new]. You do not need to round the returned vectors; answers that match the expected values to four decimal places are accepted.

Loading visualization...

Examples

Input: U = [1, 0], V = [0, 1], r = 5, lr = 0.1, reg = 0

Output: [[1.0, 0.5], [0.5, 1.0]]

Explanation: The zero dot product gives error 5, and both vectors are updated from their original values.

Input: U = [0.5, 0.3], V = [0.4, 0.6], r = 4, lr = 0.01, reg = 0.02

Output: [[0.5144, 0.3217], [0.418, 0.6107]]

Hint 1

Compute the rating error from the original vectors before either update.

Hint 2

Build both new vectors from the original U and V. Rounding each coordinate is optional.

Requirements

  • Compute the prediction error as r minus the dot product of U and V
  • Update U and V simultaneously using original (pre-update) values
  • Apply L2 regularization with strength reg to prevent overfitting
  • Return [U_new, V_new] where each item is a list of floats
  • You do not need to round the returned numbers; answers that match the expected values to 4 decimal places are accepted.

Constraints

  • U and V have the same length (latent dimension k >= 1)
  • lr > 0, reg >= 0
  • r is a number
  • Return a two-item list containing two lists of floats
  • Time limit: 300 ms
Try Similar Problems
Mean Rating ImputationMediumItem-Based CF PredictionMediumUser-Based CF PredictionMediumBaseline PredictorHardImplement Gradient Descent for a 1D QuadraticEasyImplement Precision and Recall at K in PythonEasyRating NormalizationEasyTop-K RecommendationsEasyCatalog CoverageEasyJaccard Similarity in PythonEasyHit Rate at KEasyPopularity RankingEasy
Scratchpad

Accepts: array

Accepts: array

Accepts: number

Accepts: number

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1