def get_mistakes(truth_mask: torch.ByteTensor, dataset: MorphoDataset,
                 inputs: torch.LongTensor, predictions: torch.LongTensor,
                 targets: torch.LongTensor) -> List[List[str]]:
    inputs, predictions, targets = inputs.numpy(), predictions.numpy(
    ), targets.numpy()
    mistakes = []

    for i in range(inputs.shape[0]):
        if truth_mask[i].item() == 1: continue
        before = ""
        for k in range(max(0, i - 4), i):
            before += get_example(inputs[k, :],
                                  dataset.data[dataset.FORMS].alphabet) + ' '

        form = get_example(inputs[i, :], dataset.data[dataset.FORMS].alphabet)
        gold_lemma = get_example(targets[i, :],
                                 dataset.data[dataset.LEMMAS].alphabet)
        system_lemma = get_example(predictions[i, :],
                                   dataset.data[dataset.LEMMAS].alphabet)

        after = ""
        for k in range(min(inputs.shape[0] - 1, i + 1),
                       min(inputs.shape[0] - 1, i + 5)):
            after += get_example(inputs[k, :],
                                 dataset.data[dataset.FORMS].alphabet) + ' '

        mistakes.append([before, form, gold_lemma, system_lemma, after])

    return mistakes
Пример #2
0
def resample(train, val, test : torch.LongTensor, path, idx_map, rewrite=True):
    if os.path.exists(path+'train_inductive.map'):
        rewrite = False
        filenames = ['train', 'unlabeled', 'vali', 'test']
        ans = []
        for file in filenames:
            with open(path+file+'_inductive.map', 'r') as f:
                cache = []
                for line in f:
                    cache.append(idx_map.get(int(line)))
            ans.append(torch.LongTensor(cache))
        return ans

    idx_train = train
    idx_test = val
    cache = list(test.numpy())
    shuffle(cache)
    idx_val = cache[: idx_train.shape[0]]
    idx_unlabeled = cache[idx_train.shape[0]: ]
    idx_val = torch.LongTensor(idx_val)
    idx_unlabeled = torch.LongTensor(idx_unlabeled)

    print("\n\ttrain: ", idx_train.shape[0],
          "\n\tunlabeled: ", idx_unlabeled.shape[0],
          "\n\tvali: ", idx_val.shape[0],
          "\n\ttest: ", idx_test.shape[0])
    if rewrite:
        idx_map_reverse = dict(map(lambda t: (t[1], t[0]), idx_map.items()))
        filenames = ['train', 'unlabeled', 'vali', 'test']
        ans = [idx_train, idx_unlabeled, idx_val, idx_test]
        for i in range(4):
            with open(path+filenames[i]+'_inductive.map', 'w') as f:
                f.write("\n".join(map(str, map(idx_map_reverse.get, ans[i].numpy()))))

    return idx_train, idx_unlabeled, idx_val, idx_test
Пример #3
0
    def decode(
        self,
        tokens: torch.LongTensor,
        skip_special_tokens: bool = True,
        remove_bpe: bool = True,
    ) -> Union[str, List]:
        assert tokens.dim() == 1
        tokens = tokens.numpy()

        if tokens[0] == self.task.source_dictionary.bos(
        ) and skip_special_tokens:
            tokens = tokens[1:]  # remove <s>

        eos_mask = tokens == self.task.source_dictionary.eos()
        doc_mask = eos_mask[1:] & eos_mask[:-1]
        sentences = np.split(tokens, doc_mask.nonzero()[0] + 1)

        if skip_special_tokens:
            sentences = [
                np.array(
                    [c for c in s if c != self.task.source_dictionary.eos()])
                for s in sentences
            ]

        sentences = [
            " ".join([self.task.source_dictionary.symbols[c] for c in s])
            for s in sentences
        ]
        if remove_bpe:
            sentences = [self.bpe.decode(s) for s in sentences]
        if len(sentences) == 1:
            return sentences[0]
        return sentences
Пример #4
0
 def xtrans(self, Xc: FloatTensor, Xe: LongTensor) -> FeaturesData:
     num_feature_data = Xc.numpy().astype(
         np.float32) if self.num_cont != 0 else None
     cat_feature_data = Xe.numpy().astype(str).astype(
         object) if self.num_enum != 0 else None
     return FeaturesData(num_feature_data=num_feature_data,
                         cat_feature_data=cat_feature_data)
Пример #5
0
 def decode(self, tokens: torch.LongTensor):
     assert tokens.dim() == 1
     tokens = tokens.numpy()
     if tokens[0] == self.task.source_dictionary.bos():
         tokens = tokens[1:]  # remove <s>
     eos_mask = (tokens == self.task.source_dictionary.eos())
     doc_mask = eos_mask[1:] & eos_mask[:-1]
     sentences = np.split(tokens, doc_mask.nonzero()[0] + 1)
     sentences = [self.bpe.decode(self.task.source_dictionary.string(s)) for s in sentences]
     if len(sentences) == 1:
         return sentences[0]
     return sentences
Пример #6
0
    def call(
            self,  # type: ignore
            tokens: Dict[str, torch.LongTensor],
            verb_indicator: torch.LongTensor,
            training: bool = False,
            tags: torch.LongTensor = None,
            metadata: List[Dict[str, Any]] = None) -> Dict[str, np.ndarray]:
        """
        x2_b is an array where each row is a one hot vector,
         where the hot index is the position of the verb in the sequence.
        this means x2_b should be embedded, retrieving either a vector with a single 1 or single 0.
        """

        # convert torch tensor to numpy
        x1_b = tokens['tokens'].numpy()
        x2_b = verb_indicator.numpy()

        # embed
        embedded1 = self.embedding1(x1_b)
        embedded2 = self.embedding1(
            x2_b
        )  # returns one of two trainable vectors of length params.binary_feature_dim
        mask = self.embedding1.compute_mask(x1_b)

        # encode
        encoded_lm0 = tf.concat(
            [embedded1, embedded2],
            axis=2)  # [batch_size, max_seq_len, embed_size + feature dim]
        encoded_lm1 = None  # encoding at layer minus 1
        for layer, lstm in enumerate(self.lstms):
            encoded_lm2 = encoded_lm1  # in current layer (loop), what was previously 1 layer back is now 2 layers back
            encoded_lm1 = encoded_lm0  # in current layer (loop), what was previously 0 layer back is now 1 layers back
            if encoded_lm2 is None or tf.shape(
                    encoded_lm2)[2] != self.params.hidden_size:
                encoded_lm2 = 0  # in layer 1 and 2, there should be no contribution from previous layers
            encoded_lm0 = lstm(encoded_lm1 + encoded_lm2,
                               mask=mask,
                               training=training)

        # output projection
        encoded_2d = tf.reshape(encoded_lm0 + encoded_lm1,
                                [-1, self.params.hidden_size])
        softmax_2d = self.dense_output(
            encoded_2d)  # [num_words_in_batch, num_labels]
        softmax_3d = np.reshape(
            softmax_2d,
            (*np.shape(x1_b), self.num_classes))  # 1st dim is batch_size

        output_dict = {'softmax_2d': softmax_2d, 'softmax_3d': softmax_3d}
        return output_dict
Пример #7
0
 def decode(self, tokens: torch.LongTensor, remove_underscore=True):
     assert tokens.dim() == 1
     tokens = tokens.numpy()
     if tokens[0] == self.dictionary.bos():
         tokens = tokens[1:]  # remove <s>
     eos_mask = (tokens == self.dictionary.eos())
     doc_mask = eos_mask[1:] & eos_mask[:-1]
     sentences = np.split(tokens, doc_mask.nonzero()[0] + 1)
     if remove_underscore:
         sentences = [self.bpe.decode(self.dictionary.string(s)).replace("_", " ") for s in sentences]
     else:
         sentences = [self.bpe.decode(self.dictionary.string(s)) for s in sentences]
         
     if len(sentences) == 1:
         return sentences[0]
     return sentences
Пример #8
0
    def select_action(self, state):
        state = torch.from_numpy(state)
        sample = random.random()

        # Set the present epsilon threshold and anneal
        self.eps_threshold = self.eps_end + (self.eps_start - self.eps_end) * \
            math.exp(-1. * self.steps_done / self.eps_decay)
        self.steps_done += 1

        if sample > self.eps_threshold:
            # Choose Max(Q|s)
            action = self.model(
                Variable(
                    state,
                    volatile=True).type(FloatTensor))[0].data.max(1)[1].view(
                        1, 1)
        else:
            # Choose a random action
            action = LongTensor([[random.randrange(self.nActions)]])

        return action.numpy()
    def _sample_edges_for_layer(
        self, __current_layer_target_nodes_indexes: torch.LongTensor,
        __top_layer_target_nodes_indexes: torch.LongTensor,
        layer_argument: _typing.Any, *args, **kwargs
    ) -> _typing.Tuple[torch.LongTensor, _typing.Optional[torch.Tensor]]:
        """
        Sample edges for one specific layer, expected to be implemented in subclass.

        Parameters
        ------------
        __current_layer_target_nodes_indexes:
            target nodes for current layer
        __top_layer_target_nodes_indexes:
            target nodes for top layer
        layer_argument:
            argument for current layer
        args:
            remaining positional arguments
        kwargs:
            remaining keyword arguments

        Returns
        --------
        edge_id_in_integral_graph:
            the corresponding positional indexes for the `edge_index` of integral graph
        edge_weight:
            the optional `edge_weight` for aggregation
        """
        __wrapped_result: _typing.Tuple[
            np.ndarray, np.ndarray, np.ndarray] = self.__sample_edges(
                __current_layer_target_nodes_indexes.numpy(),
                __top_layer_target_nodes_indexes.numpy(),
                layer_argument,
            )
        _sampled_edges_indexes: torch.Tensor = torch.from_numpy(
            __wrapped_result[0])
        _selected_source_nodes: torch.Tensor = torch.from_numpy(
            __wrapped_result[1])
        _selected_source_nodes_probabilities: torch.Tensor = torch.from_numpy(
            __wrapped_result[2])
        """ Multiply corresponding discount weights """
        __selected_source_node_probability_mapping: _typing.Dict[
            int, float] = dict(
                zip(
                    _selected_source_nodes.tolist(),
                    _selected_source_nodes_probabilities.tolist(),
                ))
        _selected_edges_weight: torch.Tensor = self.__edge_weight[
            _sampled_edges_indexes]
        _selected_edges_weight: torch.Tensor = _selected_edges_weight / torch.tensor(
            [
                __selected_source_node_probability_mapping.get(
                    _current_source_node_index) for _current_source_node_index
                in self._edge_index[0, _sampled_edges_indexes].tolist()
            ])
        """ Normalize edge weight for selected edges by target nodes """
        for _current_target_node_index in (
                self._edge_index[1, _sampled_edges_indexes].unique().tolist()):
            _current_mask_for_selected_edges: torch.BoolTensor = (
                self._edge_index[
                    1, _sampled_edges_indexes] == _current_target_node_index)
            _selected_edges_weight[
                _current_mask_for_selected_edges] = _selected_edges_weight[
                    _current_mask_for_selected_edges] / torch.sum(
                        _selected_edges_weight[
                            _current_mask_for_selected_edges])

        _sampled_edges_indexes: _typing.Union[
            torch.LongTensor, torch.Tensor] = _sampled_edges_indexes
        return _sampled_edges_indexes, _selected_edges_weight
Пример #10
0
 def torch_indices_to_tokens(self, indices: torch.LongTensor) -> np.array:
     """Converts arbitrary shape long tensor of indices into an array of vocab type"""
     return self.itos[indices.numpy()]
Пример #11
0
 def torch_indices_to_tokens(self, indices: torch.LongTensor) -> np.array:
     return self.itos[indices.numpy()]