-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsparse_coding.py
More file actions
29 lines (21 loc) · 816 Bytes
/
sparse_coding.py
File metadata and controls
29 lines (21 loc) · 816 Bytes
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
import os
import numpy as np
import logging
from featuresign import l1ls_featuresign
from bases import l2ls_learn_basis_dual
def sparse_coding(X, num_bases, beta, num_iters, iter_callback):
B = np.random.random((X.shape[0], num_bases)) - 0.5
B = B / np.sqrt(np.sum(B**2, 0))
S = np.zeros((num_bases, X.shape[1]))
for t in xrange(num_iters):
# shuffle samples
np.random.shuffle(X.T)
logging.info("basis %i %s" % (t, B))
for j in xrange(X.shape[1]):
logging.info("t %i sample %i %s" % (t, j, X[:, j]))
S[:, j] = l1ls_featuresign(B, X[:, j], beta, S[:, j])
logging.info("t %i coding %i %s" % (t, j, S[:, j]))
S[np.isnan(S)] = 0
B = l2ls_learn_basis_dual(X, S, 1.0)
iter_callback(B, S)
return (B, S)