pyACA: Documentation 0.3.1
Source Code for Audio Content Analysis
Loading...
Searching...
No Matches
Functions
pyACA.ToolSimpleNmf Namespace Reference

Functions

 ToolSimpleNmf (X, iRank, numMaxIter=300, fSparsity=0)
 helper function: non-negative matrix factorization
 
 KlDivergence_I (p, q)
 

Function Documentation

◆ KlDivergence_I()

KlDivergence_I ( p,
q )

Definition at line 64 of file ToolSimpleNmf.py.

64def KlDivergence_I(p, q):
65 return np.sum(np.sum(p * (np.log(p + 1e-30) - np.log(q + 1e-30)) - p + q))
Here is the caller graph for this function:

◆ ToolSimpleNmf()

ToolSimpleNmf ( X,
iRank,
numMaxIter = 300,
fSparsity = 0 )

helper function: non-negative matrix factorization

Parameters
Xnon-negative matrix to factorize (dimension FFTLength X Observations)
iRanknmf rank
numMaxItermaximum number of iterations (stop if not converged before, default: 300)
fSparsitysparsity weight (default: 0)
Returns
W: dictionary matrix
H: activation matrix
err: loss function result

Definition at line 16 of file ToolSimpleNmf.py.

16def ToolSimpleNmf(X, iRank, numMaxIter=300, fSparsity=0):
17
18 # avoid zero input
19 X = X + 1e-30
20
21 # initialization
22 [iFreq, iFrames] = X.shape
23 err = np.zeros(numMaxIter)
24 bUpdateW = True
25 bUpdateH = True
26
27 W = np.random.rand(iFreq, iRank)
28 H = np.random.rand(iRank, iFrames)
29
30 # normalize W / H matrix
31 for r in range(iRank):
32 W[:, r] = W[:, r] / np.linalg.norm(W[:, r], 1)
33
34 count = 0
35 rep = np.ones([iFreq, iFrames])
36
37 # iteration
38 for count in range(numMaxIter):
39
40 # current estimate
41 X_hat = np.matmul(W, H)
42
43 # update
44 if bUpdateH:
45 H = H * np.matmul(W.T, (X / X_hat)) / np.matmul(W.T, rep)
46 if bUpdateW:
47 W = W * np.matmul((X / X_hat), H.T) / np.matmul(rep, H.T)
48
49 # normalize
50 for r in range(iRank):
51 W[:, r] = W[:, r] / np.linalg.norm(W[:, r], 1)
52
53 # calculate variation between iterations
54 err[count] = KlDivergence_I(X, np.matmul(W, H)) + fSparsity * np.linalg.norm(H, 1)
55
56 if count >= 1:
57 if (np.abs(err[count] - err[count - 1]) / (err[0] - err[count] + 1e-30)) < 0.001:
58 break
59 count = count + 1
60
61 return W, H, err[0:count]
62
63
Here is the call graph for this function: