def update_model(self, y):
y = self.proj.reduce(np.array([y]).T)
y = np.ravel(preprocessing.normalize(y, norm='l2', axis=0))
if not hasattr(self, 'E'):
self.E = np.zeros((self.k, self.ell))
# combine current sketched matrix with input at time t
zero_cols = np.nonzero([np.isclose(s_col, 0.0) for s_col in np.sum(self.E, axis=0)])[0]
j = zero_cols[0] if zero_cols.size != 0 else self.ell - 1 # left-most all-zero column in B
self.E[:, j] = y
Gaussian = np.random.normal(0., 0.1, (self.k, 100 * self.ell))
MM = np.dot(self.E, self.E.T)
Q, R = ln.qr(np.dot(MM, Gaussian))
# eig() returns eigen values/vectors with unsorted order
s, A = ln.eig(np.dot(np.dot(Q.T, MM), Q))
order = np.argsort(s)[::-1]
s = s[order]
A = A[:, order]
U = np.dot(Q, A)
# update the tracked orthonormal bases
self.U_r = U[:, :self.r]
# update ell orthogonal bases
U_ell = U[:, :self.ell]
s_ell = s[:self.ell]
# shrink step in the Frequent Directions algorithm
delta = s_ell[-1]
s_ell = np.sqrt(s_ell - delta)
self.E = np.dot(U_ell, np.diag(s_ell))
评论列表
文章目录