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

Method forward

numpy_ml/neural_nets/layers/layers.py:1528–1570  ·  view source on GitHub ↗

Compute the layer output on a single minibatch. Notes ----- Equations [train & test]:: Y = scaler * norm(X) + intercept norm(X) = (X - mean(X)) / sqrt(var(X) + epsilon) Parameters ---------- X : :py:class:`ndarray <n

(self, X, retain_derived=True)

Source from the content-addressed store, hash-verified

1526 }
1527
1528 def forward(self, X, retain_derived=True):
1529 """
1530 Compute the layer output on a single minibatch.
1531
1532 Notes
1533 -----
1534 Equations [train & test]::
1535
1536 Y = scaler * norm(X) + intercept
1537 norm(X) = (X - mean(X)) / sqrt(var(X) + epsilon)
1538
1539 Parameters
1540 ----------
1541 X : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, in_rows, in_cols, in_ch)`
1542 Input volume containing the `in_rows` by `in_cols`-dimensional
1543 features for a minibatch of `n_ex` examples.
1544 retain_derived : bool
1545 Whether to retain the variables calculated during the forward pass
1546 for use later during backprop. If False, this suggests the layer
1547 will not be expected to backprop through wrt. this input. Default
1548 is True.
1549
1550 Returns
1551 -------
1552 Y : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, in_rows, in_cols, in_ch)`
1553 Layer output for each of the `n_ex` examples.
1554 """ # noqa: E501
1555 if not self.is_initialized:
1556 self.in_ch = self.out_ch = X.shape[3]
1557 self._init_params(X.shape)
1558
1559 scaler = self.parameters["scaler"]
1560 ep = self.hyperparameters["epsilon"]
1561 intercept = self.parameters["intercept"]
1562
1563 if retain_derived:
1564 self.X.append(X)
1565
1566 X_var = X.var(axis=(1, 2, 3), keepdims=True)
1567 X_mean = X.mean(axis=(1, 2, 3), keepdims=True)
1568 lnorm = (X - X_mean) / np.sqrt(X_var + ep)
1569 y = scaler * lnorm + intercept
1570 return y
1571
1572 def backward(self, dLdy, retain_grads=True):
1573 """

Callers 1

test_LayerNorm2DFunction · 0.95

Calls 1

_init_paramsMethod · 0.95

Tested by 1

test_LayerNorm2DFunction · 0.76