Optimise matrix factorisation algorithm using numpy matrix operations

Viewed 45
O = self.feedback_df_normalised.to_numpy() # original matrix
K = self.latent_feature_count
P = np.random.rand(len(O), K) # user embeddings
Q = np.random.rand(len(O[0]), K) # show embeddings
Q_T = np.transpose(Q)

for i in range(len(O)):
                print("i:", i)
                for j in range(len(O[0])):
                    print("j:", j)
                    A_ij = np.dot(P[i,:], Q_T[:,j])
                    dif_ij = O[i, j] - A_ij
                    dif_sqd += dif_ij ** 2

                    for k in range(K):
                        P[i, k] = P[i, k] + alpha * (2 * dif_ij * Q_T[k, j] - beta * P[i, k])
                        Q_T[k, j] = Q_T[k, j] + alpha * (2 * dif_ij * P[i, k] - beta * Q_T[k, j])

            print("dif_sqd:", dif_sqd)
            if dif_sqd < accepted_deviation:
                A = P @ Q_T
                break

have this algorithm implementing matrix factorisation via gradient based on this one: https://towardsdatascience.com/recommendation-system-matrix-factorization-d61978660b4b#:~:text=Collaborative%20filtering%20is%20the%20application,items'%20and%20users'%20entities.&text=Hence%2C%20from%20the%20matrix%20factorization,in%20user's%20preferences%20and%20interactions.

Iterative relationship the algorithm aims to implement

The general format of O is something like this:

O = [

     [5,3,0,1],

     [4,0,0,1],

     [1,1,0,5],

     [1,0,0,4],

     [0,1,5,4],
    
     [2,1,3,0],

    ]

When O becomes large this becomes veerrrrryyyy slow to execute though. I've been banging my head trying to think about how to do this sans looping, but I'm not good enough with matrices to figure it out. Any help would be appreciated.

0 Answers
Related