Class Batch
- All Implemented Interfaces:
AutoCloseable
Batch is used to hold multiple items (data and label pairs) from a Dataset.
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 Batch class.
In a Batch, data and label are each an NDList. The data NDList
represents the data for each input in the batch. The number of NDArrays in
the NDList is based on the number of different kinds of inputs, not the batch size. Similarly,
the label NDList represents 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 Batch will be an NDList containing an NCHW image
NDArray and an NTC question NDArray. The label will
be an NDList containing only an NTC answer NDArray.
In order to differentiate a batch vs a single record (despite them both consisting of two
NDLists), we have the Batch and the Record respectively.
-
Constructor Summary
ConstructorsConstructorDescriptionBatch(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, List<?> indices) Creates a new instance ofBatchwith the given manager, data and labels. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()getData()Gets the data of thisBatch.List<?>Returns the indices used to extract the data and labels from theDataset.Gets the labels corresponding to the data of thisBatch.Gets theNDManagerthat is attached to thisBatch.longReturns the progress of the batch if it is part of some kind of iteration like a dataset iteration.longReturns 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[]Splits the data and labels in theBatchacross the given devices.
-
Constructor Details
-
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, 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 Details
-
getManager
Gets theNDManagerthat is attached to thisBatch.- Returns:
- the
NDManagerattached to thisBatch
-
getData
Gets the data of thisBatch.- Returns:
- an
NDListthat contains the data
-
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 interfaceAutoCloseable
-
split
Splits the data and labels in theBatchacross the given devices.if
evenSplitisfalse, that last device may have a smaller batch than the rest. -
getIndices
Returns the indices used to extract the data and labels from theDataset.- Returns:
- a list of
Longif theDatasetis aRandomAccessDataset, otherwise may returnnull.
-