MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / eval_batch

Method eval_batch

python/paddle/hapi/model.py:1291–1351  ·  view source on GitHub ↗
(self, inputs, labels=None)

Source from the content-addressed store, hash-verified

1289 )
1290
1291 def eval_batch(self, inputs, labels=None):
1292 self.model.network.eval()
1293 self.mode = 'eval'
1294 inputs = to_list(inputs)
1295 self._input_info = _update_input_info(inputs)
1296 labels = labels or []
1297 labels = [paddle.to_tensor(l) for l in to_list(labels)]
1298
1299 outputs = self.model.network(*[paddle.to_tensor(x) for x in inputs])
1300
1301 # Transform data to expected device
1302 expected_device = paddle.device.get_device()
1303 for o in to_list(outputs):
1304 o._to(device=expected_device)
1305
1306 for l in labels:
1307 l._to(device=expected_device)
1308
1309 if self.model._loss:
1310 losses = self.model._loss(*(to_list(outputs) + labels))
1311 losses = to_list(losses)
1312
1313 if self._nranks > 1:
1314 outputs = [_all_gather(o) for o in to_list(outputs)]
1315 labels = [_all_gather(l) for l in labels]
1316
1317 if self.model._test_dataloader is not None and isinstance(
1318 self.model._test_dataloader, DataLoader
1319 ):
1320 total_size = len(self.model._test_dataloader.dataset)
1321 samples = outputs[0].shape[0]
1322 current_count = self._merge_count.get(self.mode + '_total', 0)
1323
1324 if current_count + samples >= total_size:
1325 outputs = [
1326 o[: int(total_size - current_count)] for o in outputs
1327 ]
1328 labels = [
1329 l[: int(total_size - current_count)] for l in labels
1330 ]
1331 self._merge_count[self.mode + '_total'] = 0
1332 self._merge_count[self.mode + '_batch'] = int(
1333 total_size - current_count
1334 )
1335 else:
1336 self._merge_count[self.mode + '_total'] += samples
1337 self._merge_count[self.mode + '_batch'] = samples
1338
1339 metrics = []
1340 for metric in self.model._metrics:
1341 # cut off padding value.
1342 metric_outs = metric.compute(*(to_list(outputs) + labels))
1343 m = metric.update(*[to_numpy(m) for m in to_list(metric_outs)])
1344 metrics.append(m)
1345
1346 if self.model._loss and len(metrics):
1347 return [to_numpy(l) for l in losses], metrics
1348 elif self.model._loss:

Callers

nothing calls this directly

Calls 12

_update_input_infoFunction · 0.85
to_tensorMethod · 0.80
to_listFunction · 0.70
_all_gatherFunction · 0.70
to_numpyFunction · 0.70
evalMethod · 0.45
networkMethod · 0.45
get_deviceMethod · 0.45
getMethod · 0.45
computeMethod · 0.45
updateMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected