2

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?

ti7
  • 16,375
  • 6
  • 40
  • 68
bruce
  • 23
  • 4

2 Answers2

3

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
Musleh
  • 31
  • 3
  • thank you for your reply ,now i got it and its working now – bruce May 03 '21 at 05:23
  • @bruce if this solved your Question, please mark it as the Answer with the green check to its left to help yourself (+2 rep) and the community (no need to review this; it's solved!)! – ti7 May 16 '21 at 21:40
0
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
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Dec 28 '21 at 00:44