如何使用Pytorch中的数据加载程序访问下一步数据?

发布于 2025-02-12 00:38:58 字数 702 浏览 0 评论 0原文

我正在使用训练神经网络的代码。该代码使用Pytorch的数据加载器为每次迭代加载数据。代码看起来如下所示

for step, data in enumerate(dataloader, 0):
      ............................................................
      output = neuralnetwork_model(data)
      .............................................................

,步骤是一个整数,可在每个步骤中给出值0、1、2、3的整数。数据给出了一批样本。代码在每个步骤中将相应的批次传递给神经网络。

我需要在步骤n处访问步骤n+1的数据。我需要这样的东西

for step, data in enumerate(dataloader, 0):
      ............................................................
      output = neuralnetwork_model(data)
      access = data_of_next_step
      .............................................................

我该如何实现?

I am using a code that trains neural networks. The code uses the DataLoader of PyTorch to load the data for every iteration. The code looks as follows

for step, data in enumerate(dataloader, 0):
      ............................................................
      output = neuralnetwork_model(data)
      .............................................................

Here the step is an integer that gives values 0, 1, 2, 3, ....... and data gives a batch of samples at each step. The code passes corresponding batches to the neural network at each step.

I need to just access the data of step n+1 at step n. I need something like this

for step, data in enumerate(dataloader, 0):
      ............................................................
      output = neuralnetwork_model(data)
      access = data_of_next_step
      .............................................................

How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇 2025-02-19 00:38:58

在迭代级别上执行此类操作似乎更加方便,而不必更改数据加载器实现。查看 n连续元素具有重叠 您可以实现这使用 itertools.tees.tees.tees.tee

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

因此,您只需要在包装的数据加载程序上迭代以下方式:

>>> for batch1, batch2 pairwise(dataloader)
...     # batch1 is current batch
...     # batch2 is batch of following step

It seems to be handier to perform such manipulation at the iteration level rather than having to change the data loaders implementation. Looking at Iterate over n successive elements with overlap you can achieve this using itertools.tee:

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

Therefore you simply have to iterate over your wrapped data loader with:

>>> for batch1, batch2 pairwise(dataloader)
...     # batch1 is current batch
...     # batch2 is batch of following step
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文