Skip to content
DeepForgeby svx
All problems
HardML Fundamentalsml-220

Matrix Factorization SGD Step

Problem

Perform one SGD step for matrix factorization. With err = rating - dot(p, q): p_j += lr*(err*q_j - reg*p_j) and q_j += lr*(err*p_j - reg*q_j) using the old p and q.

Return [new_p, new_q].

Hint

The regularization term shrinks the factors while the error term pulls them toward the rating.

Test cases

case 1
in: [5,[0.5,0.5],[0.2,0.4],0.1,0.01]
expected: [[0.5935,0.6875],[0.4348000000000001,0.6346]]
case 2
in: [4,[1,0],[0.5,0.5],0.05,0]
expected: [[1.0875,0.08750000000000001],[0.675,0.5]]
case 3
in: [0,[0.5,0.5],[0.5,0.5],0.1,0.01]
expected: [[0.4745,0.4745],[0.4745,0.4745]]
case 4
in: [3,[],[],0.1,0.01]
expected: [[],[]]
Starter codePython
def matrix_factorization_sgd(rating, p, q, lr, reg):
    # Returns [new_p, new_q]
    # Your code here
    pass

4 test cases · run your code against them in the editor