如何使用Pytorch DataLoader输出x和y值的较大2D网格的较小2D块?

时间:2019-01-23 20:28:07

标签: python pytorch

我正在尝试输出x和y值的“块”,它们是较大网格的较小部分。

因此,例如,我希望第一批具有0-250的x值和0-250的y值。那么下一批将是x = 0-250和y = 250-500。另一批将是x = 250-500,y = 250-500。最后,x = 250-500,y = 0-250。

因此,每次使用DataLoader类获取批处理的调用都将获取较大的2D网格中的一小块2D块。

这是我到目前为止所拥有的。

class inputDataset(Dataset):

  def __init__(self):
    self.z = 
    torch.Tensor([0.234,0.123,0.831,0.456,0.910,0.356,0.276,0.081])
    self.xmax = 1000
    self.xmin = 0
    self.ymax = 1000
    self.ymin = 0

    self.x = torch.linspace(self.xmin,self.xmax-1,self.xmax)
    self.y = torch.linspace(self.ymin,self.ymax-1,self.ymax)
    self.r = torch.sqrt(torch.mul(self.x,self.x) + /
    torch.mul(self.y,self.y))


  def __len__(self):
    return self.xmax**2 *  (len(self.z) + 3)

  def __getitem__(self, idx):

    out = /
    torch.cat((torch.cat((self.z,self.x[idx].unsqueeze(dim=0)/
    )),torch.cat((self.y[idx].unsqueeze(dim=0)/
    ,self.r[idx].unsqueeze(dim=0)))))

    return out

现在我可以输出x和y,但是它们是相同的数字而不是块。

2 个答案:

答案 0 :(得分:0)

我让自己删除了代码中大部分无关的部分,例如zr。下面是一个最小的数据加载器,该数据加载器返回了您定义的2d网格的连续区域:

import torch

class GridDataset:
    def __init__(self):
        self.chunk_size = 250

        self.x = torch.arange(500)
        self.y = torch.arange(500)

    def chunks_x(self):
        return self.x.size(0) // self.chunk_size

    def chunks_y(self):
        return self.y.size(0) // self.chunk_size

    def __len__(self):
        return self.chunks_x() * self.chunks_y()

    def __getitem__(self, idx):
        # integer division to get the id along the first axis
        x_idx = idx // self.chunks_x()
        # modulo division to get the id along the other axis
        y_idx = idx % self.chunks_x()

        cs = self.chunk_size # to make lines shorter

        # grab the actual slices using the computed values of x_idx and y_idx
        x_chunk = self.x[cs * x_idx:cs * (1+x_idx)]
        y_chunk = self.y[cs * y_idx:cs * (1+y_idx)]

        return x_chunk, y_chunk

请注意,我不完全了解您的__getitem__中级联的目的-我只是返回两个张量,其张量分别来自xy。请让我知道该方法是否可以解决您的问题。

答案 1 :(得分:0)

这是到目前为止我要提出的...

class GridDataset:
    def __init__(self):
        self.chunk_size = 5

        self.x = torch.arange(100)
        self.y = torch.arange(100)
        self.z = torch.tensor([0.22,0.22,0.45,0.788,0.013])

    def chunks_x(self):
        return self.x.size(0) // self.chunk_size

    def chunks_y(self):
        return self.y.size(0) // self.chunk_size

    def __len__(self):
        return self.chunks_x() * self.chunks_y()

    def __getitem__(self, idx):
        if idx >= len(self): raise IndexError()
        # integer division to get the id along the first axis
        x_idx = idx // self.chunks_x()
        # modulo division to get the id along the other axis
        y_idx = idx % self.chunks_x()

        cs = self.chunk_size # to make lines shorter

        # grab the actual slices using the computed values of x_idx and y_idx
        x_chunk = self.x[cs * x_idx:cs * (1+x_idx)]
        y_chunk = self.y[cs * y_idx:cs * (1+y_idx)]
        print(x_chunk.shape)

        x_chunk = x_chunk.unsqueeze(dim=1).double()
        y_chunk = y_chunk.unsqueeze(dim=1).double()

        xytotal = torch.cat((x_chunk,y_chunk),dim=1)
        r = torch.sqrt(x_chunk**2 + y_chunk**2).float()
        new = torch.zeros((len(xytotal),len(xytotal[0]) + len(self.z)))
        for i in range(len(xytotal)):
          new[i] = torch.cat((xytotal[i].double(),self.z.double()))
        new = torch.cat((new,r),dim=1)


        return new

如果我显示一个值,它将输出:

torch.Size([5])
tensor([[0.0000, 5.0000, 0.2200, 0.2200, 0.4500, 0.7880, 0.0130, 5.0000],
        [1.0000, 6.0000, 0.2200, 0.2200, 0.4500, 0.7880, 0.0130, 6.0828],
        [2.0000, 7.0000, 0.2200, 0.2200, 0.4500, 0.7880, 0.0130, 7.2801],
        [3.0000, 8.0000, 0.2200, 0.2200, 0.4500, 0.7880, 0.0130, 8.5440],
        [4.0000, 9.0000, 0.2200, 0.2200, 0.4500, 0.7880, 0.0130, 9.8489]])