Torch DataLoader for a custom requirement

I have an ordered dataset(shuffle=False) that is categorised into “bins”. I shall present an example on smaller scale that helps to clarify. Let’s say the size of dataset is 60 with bins of sizes 10,20,30. I want to train my model in the order of bins. (first with 10 then 20 and 30). I want my DataLoader to get data in batch_sizes of 8. In this case, after getting the first 8 datapoints, I don’t want to get the 2 remaining from bin-1 and get 6 from next one. What I want is to get only 2 and in the next iteration, get the 8 from bin-2. In short, I want to complete training in one bin first before moving to other. Also if batch_size happens to be greater than bin size, I want to get data in solely one bin before moving to next.

Can I please get some advice on how to do this? I could think of two ways: implementing a custom DataLoader(need advice on this too) or just create separate DataLoaders for each bin and while iterating with bins in the outermost loop, grab the corresponding DataLoader and do training. Will the latter method have some serious downsides?

I could think of two ways: implementing a custom DataLoader(need advice on this too) or just create separate DataLoaders for each bin and while iterating with bins in the outermost loop, grab the corresponding DataLoader and do training. Will the latter method have some serious downsides?

  • Second option definitely is easier, the only problem I can see is if you use several workers ie if you use multiprocessing:
    what is going to happen is that each of those workers will spawn its own worker processes. Thus you will end up with a bunch of processes, most of them idle (those from the dataloader you are not using). If you have a lot of these dataloaders, then it might be a performance problem.
    If you are not planning on using multiprocessing though, I would probably go with that

  • First option is going to be way harder: here is the code for dataloader, you would want to make a subclass of that, but it gets pretty technical

  • There is also a third option, which is much simpler, although somewhat hacky to use: *make your Dataset class handle the batching, instead of using the Dataloader‘s collate capability:
    Since your Dataset is ordered, just make it iterate over batches, and set collate=None in the dataloader.

  • Finally a fourth option, the cleanest option is to use the batch_sampler capability of the torch dataloader (see documentation). Just build a Sampler yielding the indices of the batches: here is a version that follows your example:

from torch.utils.data import Sampler
from typing import List
from copy import copy

class BinSampler(torch.utils.data.Sampler[List[int]]):
   bin_sizes: List[int]
   batch_size: int
   def __init__(self, bin_sizes: List[int], batch_size: int):
      self.bin_sizes = bin_sizes
      self.batch_size = batch_size
   
   def __len__(self):
      return sum(self.bin_sizes)

   def __iter__(self):
      bin_sizes = copy(self.bin_sizes)
      remaining_in_bin = bin_sizes.pop()
      current_index = 0
      while bin_sizes or remaining_in_bin:
         if remaining_in_bin == 0:
            remaining_in_bin = bin_sizes.pop()
         if remaining_in_bin < self.batch_size:
            n_to_yield = remaining_in_bin
         else: n_to_yield = self.batch_size

         next_index = current_index + n_to_yield

         yield range(current_index, next_index)
         remaining_in_bin -= n_to_yield
         current_index = next_index



dataloader = torch.utils.data.DataLoader(
   dataset = your_dataset,
   batch_sampler = BinSampler([10, 20, 30,], batch_size=8)
)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật