MCPcopy
hub / github.com/ddbourgin/numpy-ml / backward

Method backward

numpy_ml/neural_nets/layers/layers.py:1158–1190  ·  view source on GitHub ↗

Backprop from layer outputs to inputs. Parameters ---------- dLdY : :py:class:`ndarray ` of shape `(n_ex, in_rows, in_cols, in_ch)` The gradient of the loss wrt. the layer output `Y`. retain_grads : bool Whether to incl

(self, dLdy, retain_grads=True)

Source from the content-addressed store, hash-verified

1156 return y
1157
1158 def backward(self, dLdy, retain_grads=True):
1159 """
1160 Backprop from layer outputs to inputs.
1161
1162 Parameters
1163 ----------
1164 dLdY : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, in_rows, in_cols, in_ch)`
1165 The gradient of the loss wrt. the layer output `Y`.
1166 retain_grads : bool
1167 Whether to include the intermediate parameter gradients computed
1168 during the backward pass in the final parameter update. Default is
1169 True.
1170
1171 Returns
1172 -------
1173 dX : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, in_rows, in_cols, in_ch)`
1174 The gradient of the loss wrt. the layer input `X`.
1175 """ # noqa: E501
1176 assert self.trainable, "Layer is frozen"
1177 if not isinstance(dLdy, list):
1178 dLdy = [dLdy]
1179
1180 dX = []
1181 X = self.X
1182 for dy, x in zip(dLdy, X):
1183 dx, dScaler, dIntercept = self._bwd(dy, x)
1184 dX.append(dx)
1185
1186 if retain_grads:
1187 self.gradients["scaler"] += dScaler
1188 self.gradients["intercept"] += dIntercept
1189
1190 return dX[0] if len(X) == 1 else dX
1191
1192 def _bwd(self, dLdy, X):
1193 """Computation of gradient of loss wrt. X, scaler, and intercept"""

Callers 1

test_BatchNorm2DFunction · 0.95

Calls 1

_bwdMethod · 0.95

Tested by 1

test_BatchNorm2DFunction · 0.76