def test_trace_1(): B = np.ones((3, 3)) X = np.random.randn(100, 9) y = np.dot(X, B.ravel('F')) + .1 * np.random.randn(100) alpha = 10. B_, _ = mt.trace(X, y, alpha, 0., (3, 3), rtol=1e-10) # KKT conditions grad = - np.dot(X.T, y - np.dot(X, B_.ravel('F'))) M = (grad / alpha).reshape(B.shape, order='F') assert np.all(linalg.svdvals(M) < 1. + 1e-3) testing.assert_allclose(np.dot(M.ravel('F'), B_.ravel('F')), - linalg.svdvals(B_).sum())
import numpy as np import multitask as mt from scipy import linalg import pylab as pl n_samples, n_features = 1000, 100 X = np.random.randn(n_samples, n_features) y = np.random.randn(n_samples) res = [] for alpha in np.logspace(-1, 3, 100): print('alpha: %s' % alpha) B_, gap_ = mt.trace(X, y, alpha, 0., (10, 10), rtol=1e-10, verbose=True, max_iter=1000) s = linalg.svdvals(B_) pl.scatter(alpha * np.ones_like(s), s) ax = pl.gca() ax.set_xscale('log') pl.show()
loss.append(.5 * (linalg.norm(Y_train - X.dot(w)) ** 2)) timings.append((datetime.now() - start).total_seconds()) u0 = np.repeat(canonical, n_task).reshape((-1, n_task)) start = datetime.now() u = np.zeros((size_u, n_task)) for j in range(n_task): alpha_max = linalg.norm( X.T.dot(Y_train[:, j]).reshape((size_u, size_v)), 2) alpha = alpha_max / 2 print('Voxel %s' % j) while True: print('alpha: %s' % alpha) out = mt.trace( X, Y_train[:, j], alpha, 0., (size_u, size_v), rtol=1e-6, verbose=True) ss = linalg.svdvals(out[0]) print(ss[1:].sum()) if ss[1:].sum() < 1e-3: _u, _s, _vt = splinalg.svds(out[0], 1) break else: alpha_old = alpha alpha = (alpha_max + alpha) / 2. u[:, j] = _u.ravel() tmp = (u.T - u.mean(0)[:, None]).T sgn = np.sign(tmp.T.dot(u0.ravel('F')[:size_u] - u0.mean()))