Class Batch
- java.lang.Object
-
- ai.djl.training.dataset.Batch
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class Batch extends java.lang.Object implements java.lang.AutoCloseableABatchis used to hold multiple items (data and label pairs) from aDataset.When training and performing inference, it is often more efficient to run multiple items through a network simultaneously rather than one at a time. For this reason, much of the API is oriented around the
Batchclass.In a
Batch, data and label are each anNDList. The dataNDListrepresents the data for each input in the batch. The number ofNDArrays in the NDList is based on the number of different kinds of inputs, not the batch size. Similarly, the labelNDListrepresents the labels for each kind of output.For example, an Image Question and Answer dataset has two inputs: an image and a question. In this case, the data in the
Batchwill be anNDListcontaining an NCHW imageNDArrayand an NTC questionNDArray. The label will be anNDListcontaining only an NTC answerNDArray.In order to differentiate a batch vs a single record (despite them both consisting of two
NDLists), we have theBatchand theRecordrespectively.
-
-
Constructor Summary
Constructors Constructor Description Batch(NDManager manager, NDList data, NDList labels, int size, Batchifier dataBatchifier, Batchifier labelBatchifier, long progress, long progressTotal)Creates a new instance ofBatchwith the given manager, data and labels.Batch(NDManager manager, NDList data, NDList labels, int size, Batchifier dataBatchifier, Batchifier labelBatchifier, long progress, long progressTotal, java.util.List<?> indices)Creates a new instance ofBatchwith the given manager, data and labels.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()NDListgetData()Gets the data of thisBatch.java.util.List<?>getIndices()Returns the indices used to extract the data and labels from theDataset.NDListgetLabels()Gets the labels corresponding to the data of thisBatch.NDManagergetManager()Gets theNDManagerthat is attached to thisBatch.longgetProgress()Returns the progress of the batch if it is part of some kind of iteration like a dataset iteration.longgetProgressTotal()Returns the total or end value for the progress of the batch if it is part of some kind of iteration like a dataset iteration.intgetSize()Returns the batchSize.Batch[]split(Device[] devices, boolean evenSplit)Splits the data and labels in theBatchacross the given devices.
-
-
-
Constructor Detail
-
Batch
public Batch(NDManager manager, NDList data, NDList labels, int size, Batchifier dataBatchifier, Batchifier labelBatchifier, long progress, long progressTotal)
Creates a new instance ofBatchwith the given manager, data and labels.- Parameters:
manager- the manager for theBatchdata- theNDListcontaining the datalabels- theNDListcontaining the labelssize- (batchSize) the number ofRecords in the batchdataBatchifier- theBatchifierthat is used to split datalabelBatchifier- theBatchifierthat is used for split labelsprogress- the progress of the batch if it is part of some kind of iteration like a dataset iteration. Returns 0 if there is no iteration.progressTotal- the total or end value for the progress of the batch if it is part of
-
Batch
public Batch(NDManager manager, NDList data, NDList labels, int size, Batchifier dataBatchifier, Batchifier labelBatchifier, long progress, long progressTotal, java.util.List<?> indices)
Creates a new instance ofBatchwith the given manager, data and labels.- Parameters:
manager- the manager for theBatchdata- theNDListcontaining the datalabels- theNDListcontaining the labelssize- (batchSize) the number ofRecords in the batchdataBatchifier- theBatchifierthat is used to split datalabelBatchifier- theBatchifierthat is used for split labelsprogress- the progress of the batch if it is part of some kind of iteration like a dataset iteration. Returns 0 if there is no iteration.progressTotal- the total or end value for the progress of the batch if it is part ofindices- the indices used to extract the data and labels
-
-
Method Detail
-
getManager
public NDManager getManager()
Gets theNDManagerthat is attached to thisBatch.- Returns:
- the
NDManagerattached to thisBatch
-
getData
public NDList getData()
Gets the data of thisBatch.- Returns:
- an
NDListthat contains the data
-
getLabels
public NDList getLabels()
Gets the labels corresponding to the data of thisBatch.- Returns:
- an
NDListthat contains the labels
-
getSize
public int getSize()
Returns the batchSize.- Returns:
- the batchSize or number of
Records in the batch
-
getProgress
public long getProgress()
Returns the progress of the batch if it is part of some kind of iteration like a dataset iteration.- Returns:
- the progress of the batch if it is part of some kind of iteration like a dataset iteration. Returns 0 if there is no iteration
-
getProgressTotal
public long getProgressTotal()
Returns the total or end value for the progress of the batch if it is part of some kind of iteration like a dataset iteration.- Returns:
- the total or end value for the progress of the batch if it is part of some kind of iteration like a dataset iteration. Returns 0 if there is no iteration
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
split
public Batch[] split(Device[] devices, boolean evenSplit)
Splits the data and labels in theBatchacross the given devices.if
evenSplitisfalse, that last device may have a smaller batch than the rest.
-
getIndices
public java.util.List<?> getIndices()
Returns the indices used to extract the data and labels from theDataset.- Returns:
- a list of
Longif theDatasetis aRandomAccessDataset, otherwise may returnnull.
-
-