使用“ ...”的张量索引操纵

时间:2020-06-18 04:06:40

标签: python numpy pytorch tensor

嗨,我是Pytorch和火炬张量的新手。我正在阅读yolo_v3代码并遇到此问题。我认为这与使用...进行张量索引有关,但是很难通过Google搜索...,因此我想在这里提出。代码是:

        prediction = (
            x.view(num_samples, self.num_anchors, self.num_classes + 5, grid_size, grid_size)
            .permute(0, 1, 3, 4, 2)
            .contiguous()
        )
        print (prediction.shape)
        # Get outputs
        x = torch.sigmoid(prediction[..., 0])  # Center x
        y = torch.sigmoid(prediction[..., 1])  # Center y
        w = prediction[..., 2]  # Width
        h = prediction[..., 3]  # Height
        pred_conf = torch.sigmoid(prediction[..., 4])  # Conf
        pred_cls = torch.sigmoid(prediction[..., 5:])  # Cls pred.

我的理解是,预测将是具有[batch,anchor,x_grid,y_grid,class]形状的张量。但是预测[...,x]有什么作用(x = 0,1,2,3,4,5)?它类似于[:,x]的numpy索引吗?如果是这样,则x,y,w,h,pred_conf和pred_cls的计算就没有意义。

1 个答案:

答案 0 :(得分:0)

它叫Ellipsis。它指示ndarraytensor的未指定尺寸。

在这里,如果prediction的形状为[batch, anchor, x_grid, y_grid, class],那么

prediction[..., 0] # is equivalent to prediction[:,:,:,:,0]
prediction[..., 1] # is equivalent to prediction[:,:,:,:,1]

更多

prediction[0, ..., 0] # equivalent to prediction[0,:,:,:,0]

您也可以将...写为Ellipsis

prediction[Ellipsis, 0]