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

Method forward

numpy_ml/neural_nets/layers/layers.py:1714–1748  ·  view source on GitHub ↗

Compute the layer output on a single minibatch. Parameters ---------- X : :py:class:`ndarray ` of shape `(n_ex, n_in)` Layer input, representing the `n_in`-dimensional features for a minibatch of `n_ex` examples. retain

(self, X, retain_derived=True)

Source from the content-addressed store, hash-verified

1712 }
1713
1714 def forward(self, X, retain_derived=True):
1715 """
1716 Compute the layer output on a single minibatch.
1717
1718 Parameters
1719 ----------
1720 X : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_in)`
1721 Layer input, representing the `n_in`-dimensional features for a
1722 minibatch of `n_ex` examples.
1723 retain_derived : bool
1724 Whether to retain the variables calculated during the forward pass
1725 for use later during backprop. If False, this suggests the layer
1726 will not be expected to backprop through wrt. this input. Default
1727 is True.
1728
1729 Returns
1730 -------
1731 Y : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_in)`
1732 Layer output for each of the `n_ex` examples.
1733 """
1734 if not self.is_initialized:
1735 self.n_in = self.n_out = X.shape[1]
1736 self._init_params()
1737
1738 scaler = self.parameters["scaler"]
1739 ep = self.hyperparameters["epsilon"]
1740 intercept = self.parameters["intercept"]
1741
1742 if retain_derived:
1743 self.X.append(X)
1744
1745 X_mean, X_var = X.mean(axis=1, keepdims=True), X.var(axis=1, keepdims=True)
1746 lnorm = (X - X_mean) / np.sqrt(X_var + ep)
1747 y = scaler * lnorm + intercept
1748 return y
1749
1750 def backward(self, dLdy, retain_grads=True):
1751 """

Callers 1

test_LayerNorm1DFunction · 0.95

Calls 1

_init_paramsMethod · 0.95

Tested by 1

test_LayerNorm1DFunction · 0.76