Hướng dẫn dùng torch sqrt python

torch.sqrt[input, *, out=None]Tensor

Returns a new tensor with the square-root of the elements of input.

outi= inputi\text{out}_{i} = \sqrt{\text{input}_{i}}

Parameters

input [Tensor] – the input tensor.

Keyword Arguments

out [Tensor, optional] – the output tensor.

Example:

>>> a = torch.randn[4]
>>> a
tensor[[-2.0755,  1.0226,  0.0831,  0.4806]]
>>> torch.sqrt[a]
tensor[[    nan,  1.0112,  0.2883,  0.6933]]

The following are 30 code examples of torch.sqrt[]. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch, or try the search function

.

Example #1

def _attn[self, q, k, v, sequence_mask]:
        w = torch.matmul[q, k]
        if self.scale:
            w = w / math.sqrt[v.size[-1]]

        b_subset = self.b[:, :, :w.size[-2], :w.size[-1]]

        if sequence_mask is not None:
            b_subset = b_subset * sequence_mask.view[
                sequence_mask.size[0], 1, -1]
            b_subset = b_subset.permute[1, 0, 2, 3]

        w = w * b_subset + -1e9 * [1 - b_subset]
        w = nn.Softmax[dim=-1][w]
        w = self.attn_dropout[w]
        return torch.matmul[w, v] 

Example #2

def centerness_target[self, pos_bbox_targets]:
        """Compute centerness targets.

        Args:
            pos_bbox_targets [Tensor]: BBox targets of positive bboxes in shape
                [num_pos, 4]

        Returns:
            Tensor: Centerness target.
        """
        # only calculate pos centerness targets, otherwise there may be nan
        left_right = pos_bbox_targets[:, [0, 2]]
        top_bottom = pos_bbox_targets[:, [1, 3]]
        centerness_targets = [
            left_right.min[dim=-1][0] / left_right.max[dim=-1][0]] * [
                top_bottom.min[dim=-1][0] / top_bottom.max[dim=-1][0]]
        return torch.sqrt[centerness_targets] 

Example #3

def centerness_target[self, anchors, bbox_targets]:
        # only calculate pos centerness targets, otherwise there may be nan
        gts = self.bbox_coder.decode[anchors, bbox_targets]
        anchors_cx = [anchors[:, 2] + anchors[:, 0]] / 2
        anchors_cy = [anchors[:, 3] + anchors[:, 1]] / 2
        l_ = anchors_cx - gts[:, 0]
        t_ = anchors_cy - gts[:, 1]
        r_ = gts[:, 2] - anchors_cx
        b_ = gts[:, 3] - anchors_cy

        left_right = torch.stack[[l_, r_], dim=1]
        top_bottom = torch.stack[[t_, b_], dim=1]
        centerness = torch.sqrt[
            [left_right.min[dim=-1][0] / left_right.max[dim=-1][0]] *
            [top_bottom.min[dim=-1][0] / top_bottom.max[dim=-1][0]]]
        assert not torch.isnan[centerness].any[]
        return centerness 

Example #4

def map_roi_levels[self, rois, num_levels]:
        """Map rois to corresponding feature levels by scales.

        - scale < finest_scale * 2: level 0
        - finest_scale * 2 

Chủ Đề