def _get_accuracy(self, predictions: torch.FloatTensor,
                   y: torch.FloatTensor) -> torch.FloatTensor:
     threshold = 0.5
     flat_pred = predictions.contiguous().view(-1, 1)
     flat_y = y.contiguous().view(-1, 1)
     guess = (flat_pred > threshold).float()
     is_eq = guess.eq(flat_y).float()
     return is_eq.mean()
 def forward(self, input: torch.FloatTensor,
             target: torch.FloatTensor) -> float:
     smooth = 1.
     iflat = input.contiguous().view(-1)
     tflat = target.contiguous().view(-1)
     intersection = (iflat * tflat).sum()
     A_sum = torch.sum(iflat * iflat)
     B_sum = torch.sum(tflat * tflat)
     return 1 - ((2. * intersection + smooth) / (A_sum + B_sum + smooth))
Example #3
0
    def _get_loss(
            logits: torch.FloatTensor, targets: torch.LongTensor, target_mask: torch.FloatTensor
    ) -> torch.Tensor:
        logits = logits.contiguous()
        # shape: (batch_size, num_decoding_steps)
        relevant_targets = targets.contiguous()

        # shape: (batch_size, num_decoding_steps)
        relevant_mask = target_mask.contiguous()

        return util.sequence_cross_entropy_with_logits(logits, relevant_targets, relevant_mask)
Example #4
0
    def forward(self, input: torch.FloatTensor, target: torch.LongTensor):
        """
        :param input: (N, C) where C = number of classes.
        :param target: (N) where each value is 0 <= targets[i] <= C-1
        :return: Scaler.
        """
        if input.dim() > 2:
            input = input.view(input.size(0), input.size(1),
                               -1)  # N,C,H,W => N,C,H*W
            input = input.transpose(1, 2)  # N,C,H*W => N,H*W,C
            input = input.contiguous().view(
                -1, input.size(2))  # N,H*W,C => N*H*W,C

        target = target.view(-1, 1)
        logpt = F.log_softmax(input, dim=1)
        logpt = logpt.gather(1, target)
        logpt = logpt.view(-1)
        pt = logpt.exp()

        if self.alpha is not None:
            if self.alpha.type() != input.data.type():
                self.alpha = self.alpha.type_as(input.data)

            at = self.alpha.gather(0, target.data.view(-1))
            logpt = logpt * at

        loss = -1 * (1 - pt)**self.gamma * logpt

        if self.size_average:
            return loss.mean()
        else:
            return loss.sum()
Example #5
0
    def forward(
        self,
        X: torch.FloatTensor,
        edge_index: torch.LongTensor,
        edge_weight: torch.FloatTensor,
    ) -> torch.FloatTensor:
        """
        Making a forward pass through the whole architecture.

        Arg types:
            * **X** *(PyTorch FloatTensor)* - Node features.
            * **edge_index** *(PyTorch LongTensor)* - Graph edge indices.
            * **edge_weight** *(PyTorch LongTensor, optional)* - Edge weight vector.

        Return types:
            *  **H** *(PyTorch FloatTensor)* - The hidden representation of size 2*nhid+in_channels+window-1 for each node.
        """
        R = list()

        S = X.view(-1, self.window, self.num_nodes, self.in_channels)
        S = torch.transpose(S, 1, 2)
        S = S.reshape(-1, self.window, self.in_channels)
        O = [S[:, 0, :]]

        for l in range(1, self.window):
            O.append(S[:, l, self.in_channels - 1].unsqueeze(1))

        S = torch.cat(O, dim=1)

        X = self._graph_convolution_1(X, edge_index, edge_weight)
        R.append(X)

        X = self._graph_convolution_2(X, edge_index, edge_weight)
        R.append(X)

        X = torch.cat(R, dim=1)

        X = X.view(-1, self.window, self.num_nodes, X.size(1))
        X = torch.transpose(X, 0, 1)
        X = X.contiguous().view(self.window, -1, X.size(3))

        X, (H_1, _) = self._recurrent_1(X)
        X, (H_2, _) = self._recurrent_2(X)

        H = torch.cat([H_1[0, :, :], H_2[0, :, :], S], dim=1)
        return H
Example #6
0
    def _get_edge_type_score(self, query: torch.FloatTensor,
                             key: torch.FloatTensor,
                             edge_head: torch.Tensor) -> torch.Tensor:
        """
        Compute the edge type scores.
        :param query:  [batch_size, query_length, query_vector_dim]
        :param key: [batch_size, key_length, key_vector_dim]
        :param edge_head: [batch_size, query_length]
        :return:
            label_score: None or [batch_size, query_length, num_labels]
        """
        batch_size = key.size(0)
        batch_index = torch.arange(0, batch_size).view(batch_size,
                                                       1).type_as(edge_head)
        # [batch_size, query_length, hidden_size]
        selected_key = key[batch_index, edge_head].contiguous()
        query = query.contiguous()
        edge_type_score = self.edge_type_bilinear(query, selected_key)

        return edge_type_score
 def _get_cost(self, predictions: torch.FloatTensor,
               y: torch.FloatTensor) -> torch.FloatTensor:
     flat_pred = predictions.contiguous().view(-1, 1)
     flat_y = y.contiguous().view(-1, 1)
     cost = self.loss_fn(flat_pred, flat_y)
     return cost
Example #8
0
    def expected_log_likelihood(
            self,
            logits: torch.Tensor,
            mask: torch.ByteTensor,
            unary_marginals: torch.FloatTensor,
            pairwise_marginals: torch.Tensor = None) -> torch.Tensor:
        """
        Computes the expected log likelihood of CRFs defined by batch of logits
        with respect to a reference distribution over the random variables.

        Parameters
        ----------
        logits : torch.Tensor, required
            The logits that define the CRF distribution, of shape
            ``(batch_size, seq_len, num_tags)``.
        unary_marginals : torch.Tensor, required
            Marginal probability that each sequence element is a particular tag,
            according to the reference distribution. Shape is
            ``(batch_size, seq_len, num_tags)``.
        pairwise_marginals : torch.Tensor, optional (default = ``None``)
            Marginal probability that each pair of sequence elements is a
            particular pair of tags, according to the reference distribution.
            Shape is ``(batch_size, seq_len - 1, num_tags, num_tags)``, so
            pairwise_marginals[:, 0, 0, 0] is the probability that the first
            and second tags in each sequence are both 0,
            pairwise_marginals[:, 1, 0, 0] is the probability that the second
            and and third tags in each sequence are both 0, etc. If None,
            pairwise_marginals will be computed from unary_marginals assuming
            that they are independent in the reference distribution.
        mask : ``torch.ByteTensor``
            The text field mask for the input tokens of shape
            ``(batch_size, seq_len)``.
        """
        batch_size, seq_len, num_tags = logits.size()

        # We compute the partition function before rearranging the inputs
        partition = self._input_likelihood(logits, mask)
        # Transpose batch size and sequence dimensions
        logits = logits.transpose(
            0, 1).contiguous()  # (seq_len, batch_size, num_tags)
        mask = mask.float().transpose(0,
                                      1).contiguous()  # (seq_len, batch_size)

        unary_marginals = unary_marginals.transpose(
            0, 1)  # (seq_len, batch_size, num_tags)
        unary_marginals = unary_marginals.contiguous()
        if pairwise_marginals is not None:
            pairwise_marginals = pairwise_marginals.transpose(
                0, 1)  # (seq_len - 1, batch_size, num_tags, num_tags)
            pairwise_marginals = pairwise_marginals.contiguous()
        else:
            pairwise_marginals = torch.zeros(
                (seq_len - 1, batch_size, num_tags, num_tags),
                device=logits.device.type)

            for i in range(seq_len - 1):
                for j in range(batch_size):
                    temp1 = unary_marginals[i, j]
                    temp2 = unary_marginals[i + 1, j]
                    temp = torch.ger(temp1, temp2)
                    pairwise_marginals[i, j, :, :] = temp

        # Start with the transition scores from start_tag to the
        # first tag in each input
        if self.include_start_end_transitions:
            temp = self.start_transitions.unsqueeze(0)  # (1, num_tags)
            temp = temp * unary_marginals[0]  # (batch_size, num_tags)
            score = temp.sum(dim=1)  # (batch_size,)
        else:
            score = torch.zeros((batch_size, ),
                                device=logits.device.type)  # (batch_size,)

        # Add up the scores for the expected transitions and all
        # the inputs but the last
        for i in range(seq_len - 1):
            # Adds contributions from logits
            temp = logits[i] * unary_marginals[i]  # (batch_size, num_tags)
            temp = temp.sum(dim=1)  # (batch_size,)
            score += temp * mask[i]

            # Adds contributions from transitions from i to i+1
            temp = self.transitions.unsqueeze(0)  # (1, num_tags, num_tags)
            temp = temp * pairwise_marginals[
                i]  # (batch_size, num_tags, num_tags)
            temp = temp.sum(dim=2).sum(dim=1)  # (batch_size,)
            score += temp * mask[i + 1]

        # Transition from last state to "stop" state.
        # Computes score of transitioning to `stop_tag` from
        # each last token.
        if self.include_start_end_transitions:
            # To start, we need to find the last token for
            # each instance.
            index0 = mask.sum(dim=0).long() - 1  # (batch_size,)
            index1 = torch.arange(0, batch_size,
                                  dtype=torch.long)  # (batch_size,)
            last_marginals = unary_marginals[
                index0, index1, :]  # (batch_size, num_tags)

            temp = self.end_transitions.unsqueeze(0)  # (1, num_tags)
            temp = temp * last_marginals  # (batch_size, num_tags)
            temp = temp.sum(dim=1)  # (batch_size,)
            score += temp

        # Adds the last input if it's not masked.
        last_scores = logits[-1] * unary_marginals[
            -1]  # (batch_size, num_tags)
        last_scores = last_scores.sum(dim=1)  # (batch_size,)
        score += last_scores * mask[-1]

        # Finally we subtract partition function and return sum
        return torch.sum(score - partition)
Example #9
0
 def forward(self, input : torch.FloatTensor, hidden : torch.FloatTensor) \
     -> Tuple[torch.FloatTensor, torch.FloatTensor]:
     input = input.contiguous()
     hidden = hidden.expand(self.num_layers, -1, -1).contiguous()
     output, hidden = self.gru(input, hidden)
     return self.softmax(input), hidden[0]