-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
53 lines (37 loc) · 1.21 KB
/
test.py
File metadata and controls
53 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# import necessary libraries import pandas as pd
from unicodedata import ucd_3_2_0
import numpy as np
from pandas import NA
import scipy as sp
from scipy import sparse
from scipy.sparse.linalg import norm
import random
import matplotlib.pyplot as plt
import time
M = np.array([[1, 0, 0],[0, 1, 0],[0, 0, 1]]) # sample dataset
# print(M)
max_iterations = 50
step = 0.1
gamma = 0.01
k=1
# randomly initialize U and V
U = np.random.normal(size = (len(M), k), scale = 1/k)
V = np.random.normal(size = (len(M[0]), k), scale = 1/k)
print(U)
print(V)
local_gradient = np.zeros(shape = (len(M), len(M[0]))) # what should the dimensions be?
for itr in range(max_iterations):
for i in range(len(M)):
for j in range(len(M[0])):
# compute difference
difference = np.dot(U[i, :], V[j, :].T)
# if data exists at i,j
if not M[i, j]:
continue
# complete partial calculation
error = difference - M[i, j]
# update U
U[i, :] -= step * (error*V[j, :] + gamma*U[i, :])
# calculate some component of the gradient for V
local_gradient[i, j] = error * U[i, :] + gamma * V[i, :]
print(local_gradient)