def __init__(self, label_map, max_size, polarity, opinion_offset, opinion_direction): #label map is a dict #max_size is the max length of the sentence in train valid and test dataset super().__init__() self.labels = ['x'] * len(label_map) self.label2id = label_map for key in self.label2id: self.labels[self.label2id[key]] = key self.MAX_OPINION_OFFSET = opinion_offset self.OPINION_DIRECTION = opinion_direction self.POLARITY = polarity NetworkIDMapper.set_capacity( np.asarray([ max_size, 10, self.POLARITY, self.OPINION_DIRECTION, self.MAX_OPINION_OFFSET, self.MAX_OPINION_OFFSET, 3 ], dtype=np.int64)) #pos, label_id, opinion_direction, opinion_start, opinion_end, node_type self._all_nodes = None self._all_children = None self._max_size = max_size print('The max size: ', self._max_size) print("Building generic network..") self.build_generic_network()
def to_node(self, pos, label_id, target_sentiment, target_dir, target_s, target_e, node_type): return NetworkIDMapper.to_hybrid_node_ID( np.asarray([ pos, label_id, target_sentiment, target_dir, target_s, target_e, node_type ]))
def to_node(self, pos, label_id, opinion_sentiment, opinion_dir, opinion_s, opinion_e, node_type): #TODO RESHAPE ARRAY return NetworkIDMapper.to_hybrid_node_ID( np.asarray([ pos, label_id, opinion_sentiment, opinion_dir, opinion_s, opinion_e, node_type ]))
def check_link_validity(self, parent, children): for child in children: if child < 0: continue if child >= parent: eprint(NetworkIDMapper.to_hybrid_node_array(parent)) eprint(NetworkIDMapper.to_hybrid_node_array(child)) eprint() raise Exception( "In an edge, the parent needs to have larger node ID in order to have a proper schedule for inference. Violation: ", parent, "\t", children) self.check_node_validity(parent) for child in children: if child < 0: continue self.check_node_validity(child)
def __init__(self, network_id, inst, nodes, children, node_count, param, compiler, num_stage=-1, num_row=-1, num_hyperedge=-1, staged_nodes=None): super().__init__(network_id, inst, param, node_count, num_stage, num_row, num_hyperedge, staged_nodes) self.nodes = nodes self.children = children # self.num_stage = num_stage # self.num_row = num_row self.nodeid2arr = [None] * self.size for k in range(len(self.nodeid2arr)): node_long = self.get_node(k) self.nodeid2arr[k] = NetworkIDMapper.to_hybrid_node_array( node_long)
def get_node_array(self, k): node = self.get_node(k) return NetworkIDMapper.to_hybrid_node_array(node)
def visualize(self, network: TensorTableLookupNetwork, input: list): self.input = input G = self.G label_dict = {} for node in network.nodes: node_arr = NetworkIDMapper.to_hybrid_node_array(node) node_arr = tuple(node_arr) G.add_node(node_arr, pos=self.nodearr2coord(node_arr)) label_dict[node_arr] = self.nodearr2label(node_arr) color_values = [self.nodearr2color(node_arr) for node_arr in G.nodes()] for stage_idx in range(network.num_stage): children_list_k = network.get_children( stage_idx ) ## numpy type, num_row[stage_idx] x num_hyper_edge x 2 for idx in range(len(children_list_k)): node_id = network.staged_nodes[stage_idx][idx] node_arr = network.get_node_array(node_id) node_arr = tuple(node_arr) for children_k_index in range(len(children_list_k[idx])): children_k = children_list_k[idx][children_k_index] for child_k in children_k: if child_k < network.size: child_k_arr = network.get_node_array(child_k) child_k_arr = tuple(child_k_arr) G.add_edge(node_arr, child_k_arr) nx.draw(G, nx.get_node_attributes(G, 'pos'), labels=label_dict, with_labels=True, node_color=color_values, font_size=self.font_size) #, node_size = 60 plt.show() # def test(): # G = nx.Graph() # # G.add_edges_from( # # [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'), # # ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')]) # # # # val_map = {'A': 1.0, # # 'D': 0.5714285714285714, # # 'H': 0.0} # # # # values = [val_map.get(node, 0.25) for node in G.nodes()] # # # # nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values) # # plt.show() # # def map_arr2color(node_arr): # return node_arr[0] / 4.0 # # def map_arr2label(node_arr): # return str(node_arr[1]) + '----' + str(node_arr[0]) # # def map_arr2coord(node_arr): # return (node_arr[0], node_arr[1]) # # nodes = [(0,0), (1,5), (1,3), (3,1)] # # labeldict = {} # # for node in nodes: # G.add_node(node, pos = map_arr2coord(node)) # labeldict[node] = map_arr2label(node) # # # G.add_edge(nodes[0], nodes[1]) # # # color_values = [ map_arr2color(node) for node in G.nodes()] # # # nx.draw(G, nx.get_node_attributes(G, 'pos'), labels=labeldict, with_labels=True, node_color = color_values) # plt.show() #test()
def check_node_validity(self, node): if node not in self._children_tmp: raise Exception("Node not found:", NetworkIDMapper.to_hybrid_node_array(node))