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

Method train_batch

python/paddle/hapi/model.py:1236–1289  ·  view source on GitHub ↗
(self, inputs, labels=None, update=True)

Source from the content-addressed store, hash-verified

1234
1235 # TODO multi device in dygraph mode not implemented at present time
1236 def train_batch(self, inputs, labels=None, update=True):
1237 assert self.model._optimizer, (
1238 "model not ready, please call `model.prepare()` first"
1239 )
1240 self.model.network.train()
1241 self.mode = 'train'
1242 inputs = to_list(inputs)
1243 self._input_info = _update_input_info(inputs)
1244 labels = labels or []
1245 labels = [paddle.to_tensor(l) for l in to_list(labels)]
1246
1247 # scaler should be initialized only once
1248 if self._amp_level != "O0" and self.model._scaler is None:
1249 self.model._scaler = paddle.amp.GradScaler(**self._amp_configs)
1250
1251 with paddle.amp.auto_cast(
1252 enable=self._amp_level != 'O0',
1253 **self._amp_custom_lists,
1254 level=self._amp_level,
1255 ):
1256 if self._nranks > 1:
1257 outputs = self.ddp_model(*[paddle.to_tensor(x) for x in inputs])
1258 else:
1259 outputs = self.model.network(
1260 *[paddle.to_tensor(x) for x in inputs]
1261 )
1262
1263 losses = self.model._loss(*(to_list(outputs) + labels))
1264 losses = to_list(losses)
1265 final_loss = paddle.add_n(losses)
1266
1267 if self._amp_level != "O0":
1268 scaled = self.model._scaler.scale(final_loss)
1269 scaled.backward()
1270 if update:
1271 self.model._scaler.minimize(self.model._optimizer, scaled)
1272 self.model.network.clear_gradients()
1273 else:
1274 final_loss.backward()
1275 if update:
1276 self.model._optimizer.minimize(final_loss)
1277 self.model.network.clear_gradients()
1278
1279 metrics = []
1280 for metric in self.model._metrics:
1281 metric_outs = metric.compute(*(to_list(outputs) + labels))
1282 m = metric.update(*[to_numpy(m) for m in to_list(metric_outs)])
1283 metrics.append(m)
1284
1285 return (
1286 ([to_numpy(l) for l in losses], metrics)
1287 if len(metrics) > 0
1288 else [to_numpy(l) for l in losses]
1289 )
1290
1291 def eval_batch(self, inputs, labels=None):
1292 self.model.network.eval()

Callers

nothing calls this directly

Calls 13

_update_input_infoFunction · 0.85
to_tensorMethod · 0.80
to_listFunction · 0.70
to_numpyFunction · 0.70
trainMethod · 0.45
networkMethod · 0.45
scaleMethod · 0.45
backwardMethod · 0.45
minimizeMethod · 0.45
clear_gradientsMethod · 0.45
computeMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected