Package ai.djl.training.dataset
Class BatchSampler
- java.lang.Object
-
- ai.djl.training.dataset.BatchSampler
-
- All Implemented Interfaces:
Sampler
public class BatchSampler extends java.lang.Object implements Sampler
BatchSampler
is aSampler
that returns a single epoch over the data.BatchSampler
wraps anotherSampler.SubSampler
to yield a mini-batch of indices.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface ai.djl.training.dataset.Sampler
Sampler.SubSampler
-
-
Constructor Summary
Constructors Constructor Description BatchSampler(Sampler.SubSampler subSampler, int batchSize)
Creates a new instance ofBatchSampler
that samples from the givenSampler.SubSampler
, and yields a mini-batch of indices.BatchSampler(Sampler.SubSampler subSampler, int batchSize, boolean dropLast)
Creates a new instance ofBatchSampler
that samples from the givenSampler.SubSampler
, and yields a mini-batch of indices.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getBatchSize()
Returns the batch size of theSampler
.java.util.Iterator<java.util.List<java.lang.Long>>
sample(RandomAccessDataset dataset)
Fetches an iterator that iterates through the givenRandomAccessDataset
in mini-batches of indices.
-
-
-
Constructor Detail
-
BatchSampler
public BatchSampler(Sampler.SubSampler subSampler, int batchSize)
Creates a new instance ofBatchSampler
that samples from the givenSampler.SubSampler
, and yields a mini-batch of indices.The last batch will not be dropped. The size of the last batch maybe smaller than batch size in case the size of the dataset is not a multiple of batch size.
- Parameters:
subSampler
- theSampler.SubSampler
to sample frombatchSize
- the required batch size
-
BatchSampler
public BatchSampler(Sampler.SubSampler subSampler, int batchSize, boolean dropLast)
Creates a new instance ofBatchSampler
that samples from the givenSampler.SubSampler
, and yields a mini-batch of indices.- Parameters:
subSampler
- theSampler.SubSampler
to sample frombatchSize
- the required batch sizedropLast
- whether theBatchSampler
should drop the last few samples in case the size of the dataset is not a multiple of batch size
-
-
Method Detail
-
sample
public java.util.Iterator<java.util.List<java.lang.Long>> sample(RandomAccessDataset dataset)
Fetches an iterator that iterates through the givenRandomAccessDataset
in mini-batches of indices.- Specified by:
sample
in interfaceSampler
- Parameters:
dataset
- theRandomAccessDataset
to sample from- Returns:
- an iterator that iterates through the given
RandomAccessDataset
in mini-batches of indices
-
getBatchSize
public int getBatchSize()
Returns the batch size of theSampler
.- Specified by:
getBatchSize
in interfaceSampler
- Returns:
- the batch size of the
Sampler
, -1 if batch size is not fixed
-
-