def __init__(self, training_sampler: Sampler, test_sampler: Sampler, graph: Graph, hidden_layer_sizes: List[int], concatenate_features: bool): super().__init__(training_sampler, test_sampler, graph, hidden_layer_sizes, concatenate_features) self._sum_cache: List[List[NDArray]] = \ [[]] + [[nd.zeros(x.shape) for x in v] for v in self._feature_layers[:-1]] """ Contains sum of features on the previous layer """ # Initialize and fill the cache using all features with current weights for layer in range(1, self._num_layers): for vertex in graph.vertices: subgraph = Subgraph(vertex.id, len(vertex.neighbors), [ Subgraph(v.id, len(v.neighbors), []) for v in vertex.neighbors ]) self._feature_layers[layer][ vertex.id] = self.compute_vertex_layer( layer, vertex.id, subgraph)
def __collect(self, vertices: List[Vertex], layer: int) -> List[Subgraph]: res: List[Subgraph] = [] for v in vertices: if v in self.cache[layer]: res.append(self.cache[layer][v]) else: n_subgraphs = [] if layer == 0 else self.__collect( v.neighbors, layer - 1) subgraph = Subgraph(v.id, v.degree, n_subgraphs) self.cache[layer][v] = subgraph res.append(subgraph) return res
def __collect(self, vertices: List[Vertex], layer: int) -> List[Subgraph]: res: List[Subgraph] = [] for v in vertices: if layer == 0: neighbors_subgraphs = [] else: neighbors = v.neighbors \ if len(v.neighbors) <= self._neighbors_count \ else random.sample(v.neighbors, self._neighbors_count) neighbors_subgraphs = self.__collect(neighbors, layer - 1) res.append(Subgraph(v.id, v.degree, neighbors_subgraphs)) return res
def __collect(self, vertices: List[Vertex], layer: int) -> List[Subgraph]: collected_vertices: List[List[Vertex]] = [vertices] for layer in range(0, self._layers_count - 1): cur_vertices = collected_vertices[layer] for v in vertices: if layer == 0: neighbors_subgraphs = [] else: neighbors = v.neighbors \ if len(v.neighbors) <= self._neighbors_count \ else random.sample(v.neighbors, self._neighbors_count) neighbors_subgraphs = self.__collect(neighbors, layer - 1) res.append(Subgraph(v.id, v.degree, neighbors_subgraphs)) return res
def sample(self, root_vertices: List[Vertex]) -> List[Subgraph]: return [Subgraph(v.id, v.degree, []) for v in root_vertices]
def __collect(self, vertices: List[Vertex], layer: int) -> List[Subgraph]: return [ Subgraph(v.id, v.degree, [] if layer == 0 else self.__collect([v], layer - 1)) for v in vertices ]
def testTensorNetwork(self): A_indices = (2,3,4,6) B_indices = (7,2,8) C_indices = (9,8,3,10) D_indices = (11,7,9,4) E_indices = (6,10,11) A = rand(2,5,3,7) B = rand(4,2,6) C = rand(6,6,5,8) D = rand(4,4,6,3) E = rand(7,8,4) g = make_graph( (A,A_indices), (B,B_indices), (C,C_indices), (D,D_indices), (E,E_indices), ) self.assertTrue(allclose( g.fully_contract()[0], make_contractor_from_implicit_joins([A_indices,B_indices,C_indices,D_indices,E_indices],[])(A,B,C,D,E), rtol=1e-10 )) tensors = (A,B,C,D,E) specifications = (A_indices,B_indices,C_indices,D_indices,E_indices) for selected in xrange(1,1<<5): selected_tensors = [i for i in xrange(5) if ((selected >> i) % 2) == 1] selected_specifications = [specifications[i] for i in selected_tensors] excluded_tensors = [i for i in xrange(5) if ((selected >> i) % 2) == 0] result_indices = [] connected_tensors = [] for i in excluded_tensors: tensor_is_connected = True for index in specifications[i]: found = False for j in selected_tensors: if index in specifications[j]: found = True break if found: # i.e., if this index is connected to the network result_indices.append(index) tensor_is_connected = True if tensor_is_connected: connected_tensors.append(i) f_eval = make_contractor_from_implicit_joins(selected_specifications,result_indices)(*[tensors[i] for i in selected_tensors]) s = Subgraph(g) for i in selected_tensors: s.add_node(i) s.merge_all() g_eval = s.get_resulting_matrices(connected_tensors)[0] self.assertTrue(allclose(f_eval,g_eval,rtol=1e-10))