Skip to contents

as_dataloader is used internally by luz to convert input data and valid_data as passed to fit.luz_module_generator() to a torch::dataloader


as_dataloader(x, ...)

# S3 method for dataset
as_dataloader(x, ..., batch_size = 32)

# S3 method for list
as_dataloader(x, ...)

# S3 method for dataloader
as_dataloader(x, ...)

# S3 method for matrix
as_dataloader(x, ...)

# S3 method for numeric
as_dataloader(x, ...)

# S3 method for array
as_dataloader(x, ...)

# S3 method for torch_tensor
as_dataloader(x, ...)



the input object.


Passed to torch::dataloader().


(int, optional): how many samples per batch to load (default: 1).


as_dataloader methods should have sensible defaults for batch_size, parallel workers, etc.

It allows users to quickly experiment with fit.luz_module_generator() by not requiring to create a torch::dataset and a torch::dataloader in simple experiments.

Methods (by class)

  • dataset: Converts a torch::dataset() to a torch::dataloader().

  • list: Converts a list of tensors or arrays with the same size in the first dimension to a torch::dataloader()

  • dataloader: Returns the same dataloader

  • matrix: Converts the matrix to a dataloader

  • numeric: Converts the numeric vector to a dataloader

  • array: Converts the array to a dataloader

  • torch_tensor: Converts the tensor to a dataloader


You can implement your own as_dataloader S3 method if you want your data structure to be automatically supported by luz's fit.luz_module_generator(). The method must satisfy the following conditions:

  • The method should return a torch::dataloader().

  • The only required argument is x. You have good default for all other arguments.

It's better to avoid implementing as_dataloader methods for common S3 classes like data.frames. In this case, its better to assign a different class to the inputs and implement as_dataloader for it.