def get_steps(n_dim, n_atoms, n_samples, n_test, n_layers, reg, rng, max_iter): lista_kwargs = dict(n_layers=n_layers, max_iter=max_iter) x, D, z = make_coding(n_samples=n_samples + n_test, n_atoms=n_atoms, n_dim=n_dim, random_state=rng) x_test = x[n_samples:] x = x[:n_samples] c_star = Lista(D, 1000).score(x_test, reg) L = np.linalg.norm(D, ord=2)**2 # Lipschitz constant network = Lista(D, **lista_kwargs, parametrization='step', per_layer='oneshot') init_score = network.score(x_test, reg) print(init_score) network.fit(x, reg) print(network.score(x_test, reg)) steps = network.get_parameters(name='step_size') L_s = np.zeros((n_layers, n_samples)) for layer in range(1, n_layers + 1): z_ = network.transform(x, reg, output_layer=layer) supports = z_ != 0 S_pca = [] for support in supports: idx = np.where(support)[0] D_s = D[idx] G_s = D_s.T.dot(D_s) S_pca.append(np.linalg.eigvalsh(G_s)[-1]) L_s[layer - 1] = np.array(S_pca) return steps, L, L_s, S_pca
def get_steps(n_dim, n_atoms, n_samples, n_test, n_layers, reg, rng, max_iter): lista_kwargs = dict( n_layers=n_layers, max_iter=max_iter) x, D, z = make_coding(n_samples=n_samples + n_test, n_atoms=n_atoms, n_dim=n_dim, random_state=rng) x_test = x[n_samples:] x = x[:n_samples] c_star = Lista(D, 1000).score(x_test, reg) L = np.linalg.norm(D, ord=2) ** 2 # Lipschitz constant network = Lista(D, **lista_kwargs, parametrization='step', per_layer='oneshot') init_score = network.score(x_test, reg) print(init_score) network.fit(x, reg) print(network.score(x_test, reg)) steps = network.get_parameters(name='step_size') z_ = network.transform(x, reg) supports = np.unique(z_ != 0, axis=0) S_pca = [] for support in supports: D_s = D[support] G_s = D_s.T.dot(D_s) S_pca.append(np.linalg.eigvalsh(G_s)[-1]) L_s = np.max(S_pca) return steps, L, L_s, S_pca
def get_params(n_samples, n_atoms, n_dim, rng, max_iter, training, n_layers): x, D, z = make_coding(n_samples=n_samples, n_atoms=n_atoms, n_dim=n_dim, random_state=rng) lista = Lista(D, n_layers, parametrization='coupled', max_iter=max_iter, per_layer=training, verbose=1) lista.fit(x, reg) W_list = lista.get_parameters('W_coupled') thresholds = lista.get_parameters('threshold') return D, W_list, thresholds
for support in supports: D_s = D[support] G_s = D_s.T.dot(D_s) S_pca.append(np.linalg.eigvalsh(G_s)[-1]) L_s_list.append(np.max(S_pca)) L_s_list = np.array(L_s_list) network_hack = deepcopy(network) network_hack.set_parameters('step_size', np.array(2 / L_s_list[-1]), offset=25) ####################################################################### # Retrieve the step size values # steps = network.get_parameters(name='step_size') steps_hack = network_hack.get_parameters(name='step_size') if parametrization == "first_step": steps_hack[0] = steps[0] = network.get_parameters('threshold')[0] ####################################################################### # Retrieve the losses # losses = np.array([ network.score(x_test, reg, output_layer=layer + 1) for layer in range(n_layers) ]) losses_hack = np.array([ network_hack.score(x_test, reg, output_layer=layer + 1) for layer in range(n_layers) ])