Run net forward + backward in batches. Parameters ---------- blobs: list of blobs to extract as in forward() diffs: list of diffs to extract as in backward() kwargs: Keys are input (for forward) and output (for backward) blob names and values are ndarrays. Refer
(self, blobs=None, diffs=None, **kwargs)
| 214 | |
| 215 | |
| 216 | def _Net_forward_backward_all(self, blobs=None, diffs=None, **kwargs): |
| 217 | """ |
| 218 | Run net forward + backward in batches. |
| 219 | |
| 220 | Parameters |
| 221 | ---------- |
| 222 | blobs: list of blobs to extract as in forward() |
| 223 | diffs: list of diffs to extract as in backward() |
| 224 | kwargs: Keys are input (for forward) and output (for backward) blob names |
| 225 | and values are ndarrays. Refer to forward() and backward(). |
| 226 | Prefilled variants are called for lack of input or output blobs. |
| 227 | |
| 228 | Returns |
| 229 | ------- |
| 230 | all_blobs: {blob name: blob ndarray} dict. |
| 231 | all_diffs: {blob name: diff ndarray} dict. |
| 232 | """ |
| 233 | # Batch blobs and diffs. |
| 234 | all_outs = {out: [] for out in set(self.outputs + (blobs or []))} |
| 235 | all_diffs = {diff: [] for diff in set(self.inputs + (diffs or []))} |
| 236 | forward_batches = self._batch({in_: kwargs[in_] |
| 237 | for in_ in self.inputs if in_ in kwargs}) |
| 238 | backward_batches = self._batch({out: kwargs[out] |
| 239 | for out in self.outputs if out in kwargs}) |
| 240 | # Collect outputs from batches (and heed lack of forward/backward batches). |
| 241 | for fb, bb in izip_longest(forward_batches, backward_batches, fillvalue={}): |
| 242 | batch_blobs = self.forward(blobs=blobs, **fb) |
| 243 | batch_diffs = self.backward(diffs=diffs, **bb) |
| 244 | for out, out_blobs in six.iteritems(batch_blobs): |
| 245 | all_outs[out].extend(out_blobs.copy()) |
| 246 | for diff, out_diffs in six.iteritems(batch_diffs): |
| 247 | all_diffs[diff].extend(out_diffs.copy()) |
| 248 | # Package in ndarray. |
| 249 | for out, diff in zip(all_outs, all_diffs): |
| 250 | all_outs[out] = np.asarray(all_outs[out]) |
| 251 | all_diffs[diff] = np.asarray(all_diffs[diff]) |
| 252 | # Discard padding at the end and package in ndarray. |
| 253 | pad = len(six.next(six.itervalues(all_outs))) - len(six.next(six.itervalues(kwargs))) |
| 254 | if pad: |
| 255 | for out, diff in zip(all_outs, all_diffs): |
| 256 | all_outs[out] = all_outs[out][:-pad] |
| 257 | all_diffs[diff] = all_diffs[diff][:-pad] |
| 258 | return all_outs, all_diffs |
| 259 | |
| 260 | |
| 261 | def _Net_set_input_arrays(self, data, labels): |