def draw_clustered_mlp(weights_path, clustering_result, n_clusters=4, is_first_square=True, ax=None): """Draw MLP with its spectral clustering.""" weights = load_weights(weights_path) layer_widths = extact_layer_widths(weights) labels, metrics = clustering_result G = nx.from_scipy_sparse_matrix(weights_to_graph(weights)) pos = set_nodes_positions(G.nodes, layer_widths, labels, is_first_square) color_mapper = get_color_mapper(n_clusters) color_map = [color_mapper[label] for label in labels] if ax is None: _, ax = plt.subplots(1) with warnings.catch_warnings(): warnings.simplefilter('ignore') nx.draw(G, pos=pos, node_color=color_map, width=0, node_size=10, ax=ax) draw_metrics(metrics, ax) return ax, labels, metrics
def plot_eigenvalues_old(weights_path, n_eigenvalues=None, ax=None, **kwargs): warnings.warn('deprecated', DeprecationWarning) loaded_weights = load_weights(weights_path) G = nx.from_scipy_sparse_matrix(weights_to_graph(loaded_weights)) G_nn = G.subgraph(max(nx.connected_components(G), key=len)) assert nx.is_connected(G_nn) nrom_laplacian_matrics = nx.normalized_laplacian_matrix(G_nn) eigen_values = np.sort(np.linalg.eigvals(nrom_laplacian_matrics.A)) if n_eigenvalues == None: start, end = 0, len(G_nn) elif isinstance(n_eigenvalues, int): start, end = 0, n_eigenvalues elif isinstance(n_eigenvalues, tuple): start, end = n_eigenvalues else: raise TypeError( 'n_eigenvalues should be either None or int or tuple or slice.') eigen_values = eigen_values[start:end] if ax is None: _, ax = plt.subplots(1) ax.xaxis.set_major_locator(MaxNLocator(integer=True)) if 'linestyle' not in kwargs: kwargs['linestyle'] = 'none' kwargs['marker'] = '*' kwargs['markersize'] = 5 return ax.plot(range(start + 1, end + 1), eigen_values, **kwargs)
def plot_eigenvalues(weights_path, n_eigenvalues=None, ax=None, **kwargs): weights = load_weights(weights_path) if 'cnn' in str(weights_path): weights, _ = extract_cnn_weights( weights, with_avg=True) #(max_weight_convention=='one_on_n')) print([w.shape for w in weights]) # TODO: take simpler solution from delete_isolated_ccs_refactored adj_mat = weights_to_graph(weights) _, components = sparse.csgraph.connected_components(adj_mat) most_common_component_counts = Counter(components).most_common(2) main_component_id = most_common_component_counts[0][0] assert (len(most_common_component_counts) == 1 or most_common_component_counts[1][1] == 1) main_component_mask = (components == main_component_id) selected_adj_mat = adj_mat[main_component_mask, :][:, main_component_mask] nrom_laplacian_matrix = sparse.csgraph.laplacian(selected_adj_mat, normed=True) if n_eigenvalues == None: start, end = 0, selected_adj_mat.shape[0] - 2 elif isinstance(n_eigenvalues, int): start, end = 0, n_eigenvalues elif isinstance(n_eigenvalues, tuple): start, end = n_eigenvalues else: raise TypeError( 'n_eigenvalues should be either None or int or tuple or slice.') """ eigen_values, _ = sparse.linalg.eigs(nrom_laplacian_matrix, k=end, which='SM') """ sigma = 1 OP = nrom_laplacian_matrix - sigma * sparse.eye( nrom_laplacian_matrix.shape[0]) OPinv = sparse.linalg.LinearOperator( matvec=lambda v: sparse.linalg.minres(OP, v, tol=1e-5)[0], shape=nrom_laplacian_matrix.shape, dtype=nrom_laplacian_matrix.dtype) eigen_values, _ = sparse.linalg.eigsh(nrom_laplacian_matrix, sigma=sigma, k=end, which='LM', tol=1e-5, OPinv=OPinv) eigen_values = np.sort(eigen_values) eigen_values = eigen_values[start:end] if ax is None: _, ax = plt.subplots(1) ax.xaxis.set_major_locator(MaxNLocator(integer=True)) if 'linestyle' not in kwargs: kwargs['linestyle'] = 'none' kwargs['marker'] = '*' kwargs['markersize'] = 5 return ax.plot(range(start + 1, end + 1), eigen_values, **kwargs)
from visualization import run_spectral_cluster, build_cluster_graph, plot_eigenvalues dataset = "Cora" file_constraited = dataset + "Convergence" for epoch in range(0, 200, 40): weights_path0 = dataset + "Convergence/WeightChanges-Cora-GCN-param_512_2_0.99_0.2-monte_0-" + str( epoch) + ".pt" weights_array0 = torch.load(weights_path0) weights_array = [] for (i, weight) in enumerate(weights_array0): weight = weight[:800, :800].cpu().detach().numpy() weights_array.append(weight) weights_path = dataset + "Convergence/WeightChanges-Cora-GCN-param_512_2_0.99_0.2-monte_0-" + str( epoch) + ".pckl" pickle.dump(weights_array, open(weights_path, 'wb')) adj_mat = weights_to_graph(weights_array) new_weight_array, new_adj_mat = delete_isolated_ccs(weights_array, adj_mat) num_clusters = 6 assign_labels = 'kmeans' eigen_solver = 'amg' epsilon = 1e-8 #[ncut_val, clustering_labels]=weights_array_to_cluster_quality(weights_array, adj_mat, num_clusters,eigen_solver, assign_labels, epsilon,is_testing=False) # weights_array_to_cluster_quality() #[labels, metrics]=run_spectral_cluster(weights_path) plot_eigenvalues(weights_path) #labels=cluster_net(num_clusters, adj_mat, eigen_solver, assign_labels) #build_cluster_graph(weights_path,labels,normalize_in_out=True)