SGD algorithm from scratch to predict movie rating

648 views Asked by At

Based on this equation I have to compute derivative w.r.t b which I did below

optimization equation optimization equation

def derivative_db(user_id,item_id,rating,U,V,mu,alpha):
    '''In this function, we will compute dL/db_i'''
    return (2*alpha*np.sum(user_id))-(2*np.sum((rating-mu-user_id-item_id-np.dot(U,V))))

but for the query

U1, Sigma, V1 = randomized_svd(adjacency_matrix, n_components=2,n_iter=5, random_state=24)
U1.shape = (943,2)
V1.shape = (2,1681)

alpha=0.01
mu = 3.529

value=derivative_db(312,98,4,U1,V1,mu,alpha)

I should get answer = -0.931
but I am getting a huge number.
what correction should i need to make in my function?

2

There are 2 answers

1
Shivam Baldha On BEST ANSWER
def derivative_db(user_id,item_id,rating,U,V,mu,alpha):
'''In this function, we will compute dL/db_i'''
   U1 = U[user_id]
   V1 = V.T[item_id]
   a = alpha * 2 *(b_i[user_id]) - 2 * np.sum((rating - mu - b_i[user_id] - c_j[item_id] - np.dot(U1 , V1)))
   return a
2
Musleh On

You have actually misunderstood it. Try with the following code it will work for your assignment.

def derivative_db(user_id,item_id,rating,U,V,mu,alpha):
    '''In this function, we will compute dL/db_i'''
    db=2*alpha*(b_i[user_id])-2*(rating-mu-b_i[user_id]-c_j[item_id]-np.dot(U[user_id],V[:,item_id].T))
    return db