Skip to content

Instantly share code, notes, and snippets.

@KeAWang
Created November 11, 2022 20:41
Show Gist options
  • Save KeAWang/e1baa939bf5bfe83502ab068fa224221 to your computer and use it in GitHub Desktop.
Save KeAWang/e1baa939bf5bfe83502ab068fa224221 to your computer and use it in GitHub Desktop.
Pytorch data inheriting namedtuples
from collections import namedtuple
class Data(namedtuple("Data", ("x", "y"))):
def to(self, device, non_blocking=False):
x = self.x.to(device, non_blocking=non_blocking)
y = self.y.to(device, non_blocking=non_blocking)
return Data(x, y)
def contiguous(self):
x = self.x.contiguous()
y = self.y.contiguous()
return Data(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment