torch icon indicating copy to clipboard operation
torch copied to clipboard

examples for sampler

Open tdhock opened this issue 7 months ago • 2 comments

https://github.com/mlverse/torch/blob/main/man/sampler.Rd#L23 references an examples section which does not exist on that man page. Can it be added please? Maybe based on https://docs.pytorch.org/docs/stable/data.html#torch.utils.data.Sampler ?

tdhock avatar Jul 23 '25 02:07 tdhock

the simplest "example" I can find of a usage of sampler, is in the source code: https://github.com/mlverse/torch/blob/main/R/utils-data-sampler.R#L57

SequentialSampler <- sampler(
  name = "utils_sampler_sequential",
  initialize = function(data_source) {
    self$data_source <- data_source
  },
  .iter_batch = function(batch_size) {
    n <- length(self$data_source)
    as_batch_iterator(seq_len(n), batch_size)
  },
  .iter = function() {
    self$.iter_batch(1L)
  },
  .length = function() {
    length(self$data_source)
  }
)

however this uses as_batch_iterator, which is not exported. @dfalbel can you please explain what would be an acceptable way forward, to allow users to define their own samplers? Can as_batch_iterator be exported? Or can users make it work without using that?

tdhock avatar Jul 23 '25 11:07 tdhock

here is an example that seems to work. https://github.com/mlr-org/mlr3torch/issues/417#issuecomment-3109001613 would it be acceptable to submit a PR which adds this as an example?

tdhock avatar Jul 24 '25 19:07 tdhock