-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsvd.py
102 lines (81 loc) · 2.53 KB
/
svd.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project :Awesome-DL-Models
@File :svd.py
@Author :JackHCC
@Date :2022/2/8 13:10
@Desc :Implement SVD
'''
import numpy as np
from numpy.linalg import norm
from random import normalvariate
from math import sqrt
def random_unit_vector(n):
unnormalized = [normalvariate(0, 1) for _ in range(n)]
theNorm = sqrt(sum(x * x for x in unnormalized))
return [x / theNorm for x in unnormalized]
def svd_1d(A, epsilon=1e-10):
''' The one-dimensional SVD '''
n, m = A.shape
x = random_unit_vector(min(n,m))
lastV = None
currentV = x
if n > m:
B = np.dot(A.T, A)
else:
B = np.dot(A, A.T)
iterations = 0
while True:
iterations += 1
lastV = currentV
currentV = np.dot(B, lastV)
currentV = currentV / norm(currentV)
if abs(np.dot(currentV, lastV)) > 1 - epsilon:
print("converged in {} iterations!".format(iterations))
return currentV
def svd(A, k=None, epsilon=1e-10):
'''
Compute the singular value decomposition of a matrix A
using the power method. A is the input matrix, and k
is the number of singular values you wish to compute.
If k is None, this computes the full-rank decomposition.
'''
A = np.array(A, dtype=float)
n, m = A.shape
svdSoFar = []
if k is None:
k = min(n, m)
for i in range(k):
matrixFor1D = A.copy()
for singularValue, u, v in svdSoFar[:i]:
matrixFor1D -= singularValue * np.outer(u, v)
if n > m:
v = svd_1d(matrixFor1D, epsilon=epsilon) # next singular vector
u_unnormalized = np.dot(A, v)
sigma = norm(u_unnormalized) # next singular value
u = u_unnormalized / sigma
else:
u = svd_1d(matrixFor1D, epsilon=epsilon) # next singular vector
v_unnormalized = np.dot(A.T, u)
sigma = norm(v_unnormalized) # next singular value
v = v_unnormalized / sigma
svdSoFar.append((sigma, u, v))
singularValues, us, vs = [np.array(x) for x in zip(*svdSoFar)]
return singularValues, us.T, vs
if __name__ == "__main__":
print("开始测试SVD……")
movieRatings = np.array([
[2, 5, 3],
[1, 2, 1],
[4, 1, 1],
[3, 5, 2],
[5, 3, 1],
[4, 5, 5],
[2, 4, 2],
[2, 2, 5],
], dtype='float64')
# v1 = svd_1d(movieRatings)
# print(v1)
theSVD = svd(movieRatings)
print(theSVD)