x_test = np.random.randn(*x.shape) x_test /= np.max(abs(x_test.dot(D.T)), axis=1, keepdims=True) format_cost = "{}: {} cost = {:.3e}" c_star = get_c_star(x, D, z, reg, device=device) saved_model = {} for parametrization in ['alista', 'hessian', 'coupled']: lista = Lista(D, n_layers, parametrization=parametrization, max_iter=5000, device=device) z_hat_test = lista.transform(x_test, reg) cost_test = cost_lasso(z_hat_test, D, x_test, reg) print( format_cost.format("Un-trained[{}]".format(parametrization), "test", cost_test)) # Train and evaluate the network lista.fit(x, reg, tol=0) z_hat_test = lista.transform(x_test, reg) cost_test = cost_lasso(z_hat_test, D, x_test, reg) print( format_cost.format("Trained[{}]".format(parametrization), "test", cost_test)) saved_model[parametrization] = lista z_hat = lista.transform(x, reg)
x_list = np.linspace(x_min, x_max, n_samples) y_list = np.linspace(y_min, y_max, n_samples) grid = np.meshgrid(x_list, y_list) x_plot = np.c_[grid[0].ravel(), grid[1].ravel()] n_reg = 100 for enum, reg in enumerate(np.linspace(0, 1, n_reg)[1:]): print(enum / n_reg) x_r = x_d * reg z_lasso = Lista(D, 1000).transform(x_plot, reg) loss_fit = loss_lasso(z_lasso, x_plot, reg) ista = Lista(D, n_layers) z_ista = ista.transform(x_plot, reg) z_ista_train = ista.transform(x, reg) loss_ista = loss_lasso(z_ista, x_plot, reg) avg_loss = np.mean(loss_lasso(z_ista_train, x, reg)) print('Ista training loss: {}'.format(avg_loss)) # @mem.cache def get_trained_lista(D, x, reg, n_layers, max_iter): lista = Lista(D, n_layers, max_iter=max_iter) lista.fit(x, reg) return lista lista = get_trained_lista(D, x, reg, n_layers, max_iter=1000) z_lista = lista.transform(x_plot, reg) z_lista_train = lista.transform(x, reg)
network.fit(x, reg) losses = np.array([ network.score(x_test, reg, output_layer=layer + 1) for layer in range(n_layers) ]) final_score = network.score(x_test, reg) print(final_print.format(final_score, init_score - final_score)) networks[name] = network ####################################################################### # Compute maximal sparsity eigenvalue # L_s_list = [] for layer in range(n_layers): z_ = network.transform(x, reg, output_layer=layer + 1) supports = np.unique(z_ != 0, axis=0) # Compute the sparse pcas of D 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_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)