MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / aggregate_batch

Method aggregate_batch

tensorpack/dataflow/common.py:158–190  ·  view source on GitHub ↗

Aggregate a list of datapoints to one batched datapoint. Args: data_holder (list[dp]): each dp is either a list or a dict. use_list (bool): whether to batch data into a list or a numpy array. Returns: dp: either a list or

(data_holder, use_list=False)

Source from the content-addressed store, hash-verified

156
157 @staticmethod
158 def aggregate_batch(data_holder, use_list=False):
159 """
160 Aggregate a list of datapoints to one batched datapoint.
161
162 Args:
163 data_holder (list[dp]): each dp is either a list or a dict.
164 use_list (bool): whether to batch data into a list or a numpy array.
165
166 Returns:
167 dp:
168 either a list or a dict, depend on the inputs.
169 Each item is a batched version of the corresponding inputs.
170 """
171 first_dp = data_holder[0]
172 if isinstance(first_dp, (list, tuple)):
173 result = []
174 for k in range(len(first_dp)):
175 data_list = [x[k] for x in data_holder]
176 if use_list:
177 result.append(data_list)
178 else:
179 result.append(BatchData._batch_numpy(data_list))
180 elif isinstance(first_dp, dict):
181 result = {}
182 for key in first_dp.keys():
183 data_list = [x[key] for x in data_holder]
184 if use_list:
185 result[key] = data_list
186 else:
187 result[key] = BatchData._batch_numpy(data_list)
188 else:
189 raise ValueError("Data point has to be list/tuple/dict. Got {}".format(type(first_dp)))
190 return result
191
192
193class BatchDataByShape(BatchData):

Callers 3

__iter__Method · 0.80
__iter__Method · 0.80
runMethod · 0.80

Calls 3

appendMethod · 0.80
_batch_numpyMethod · 0.80
formatMethod · 0.80

Tested by

no test coverage detected