def decode(self, s_arc, mask): lens = mask.sum(1) # prevent self-loops s_arc.diagonal(0, 1, 2).fill_(float('-inf')) arc_preds = mst(s_arc, mask) return arc_preds
def parse(self, pretrained_word_tokens=None, word_tokens=None, pos_tokens=None): if word_tokens is not None: self.forward(pretrained_word_tokens, word_tokens, pos_tokens) ROOT = self._ROOT_LABEL arcs_batch, labels_batch = [], [] arc_logits = self._arc_logits.data.cpu().numpy() label_logits = self._label_logits.data.cpu().numpy() for arc_logit, label_logit, length in \ zip(arc_logits, np.transpose(label_logits, (0, 2, 1, 3)), self._lengths): arc_probs = softmax2d(arc_logit[:length, :length]) arcs = mst(arc_probs) label_probs = softmax2d(label_logit[np.arange(length), arcs]) labels = np.argmax(label_probs, axis=1) labels[0] = ROOT tokens = np.arange(1, length) roots = np.where(labels[tokens] == ROOT)[0] + 1 if len(roots) < 1: root_arc = np.where(arcs[tokens] == 0)[0] + 1 labels[root_arc] = ROOT elif len(roots) > 1: label_probs[roots, ROOT] = 0 new_labels = \ np.argmax(label_probs[roots], axis=1) root_arc = np.where(arcs[tokens] == 0)[0] + 1 labels[roots] = new_labels labels[root_arc] = ROOT arcs_batch.append(arcs) labels_batch.append(labels) return arcs_batch, labels_batch
def __init__(self, arr, genotype=None): self.rows = arr.shape[0] self.cols = arr.shape[1] # Generate initial phenotype if genotype is None: self.genotype = mst(arr, self.rows, self.cols) else: self.genotype = genotype self.segments = find_segments(self.genotype) self.edge_value = edge_value(arr, self.segments, self.rows, self.cols) self.connectivity = connectivity(arr, self.segments, self.rows, self.cols) self.deviation = overall_deviation(arr, self.segments, self.cols) self.fitness = 10 * self.edge_value - 8500 * self.connectivity - 2 * self.deviation
def __init__(self, arr, genotype=None): self.rows = arr.shape[0] self.cols = arr.shape[1] # Generate initial phenotype if genotype is None: self.genotype = mst(arr, self.rows, self.cols) else: self.genotype = genotype self.segments = find_segments(self.genotype) self.num_segments = max(self.segments) + 1 self.edge_value = -edge_value(arr, self.segments, self.rows, self.cols) # negative to minimize self.connectivity = connectivity(arr, self.segments, self.rows, self.cols) self.deviation = overall_deviation(arr, self.segments, self.cols)