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

Function test_BatchNorm1D

numpy_ml/tests/test_nn.py:748–797  ·  view source on GitHub ↗
(N=15)

Source from the content-addressed store, hash-verified

746
747
748def test_BatchNorm1D(N=15):
749 from numpy_ml.neural_nets.layers import BatchNorm1D
750
751 np.random.seed(12345)
752
753 N = np.inf if N is None else N
754
755 np.random.seed(12345)
756
757 i = 1
758 while i < N + 1:
759 n_ex = np.random.randint(2, 1000)
760 n_in = np.random.randint(1, 1000)
761 X = random_tensor((n_ex, n_in), standardize=True)
762
763 # initialize BatchNorm1D layer
764 L1 = BatchNorm1D()
765
766 # forward prop
767 y_pred = L1.forward(X)
768
769 # backprop
770 dLdy = np.ones_like(y_pred)
771 dLdX = L1.backward(dLdy)
772
773 # get gold standard gradients
774 gold_mod = TorchBatchNormLayer(
775 n_in, L1.parameters, "1D", epsilon=L1.epsilon, momentum=L1.momentum
776 )
777 golds = gold_mod.extract_grads(X)
778
779 params = [
780 (L1.X[0], "X"),
781 (y_pred, "y"),
782 (L1.parameters["scaler"].T, "scaler"),
783 (L1.parameters["intercept"], "intercept"),
784 (L1.parameters["running_mean"], "running_mean"),
785 # (L1.parameters["running_var"], "running_var"),
786 (L1.gradients["scaler"], "dLdScaler"),
787 (L1.gradients["intercept"], "dLdIntercept"),
788 (dLdX, "dLdX"),
789 ]
790
791 print("Trial {}".format(i))
792 for ix, (mine, label) in enumerate(params):
793 assert_almost_equal(
794 mine, golds[label], err_msg=err_fmt(params, golds, ix), decimal=1
795 )
796 print("\tPASSED {}".format(label))
797 i += 1
798
799
800def test_LayerNorm1D(N=15):

Callers

nothing calls this directly

Calls 7

forwardMethod · 0.95
backwardMethod · 0.95
extract_gradsMethod · 0.95
random_tensorFunction · 0.90
BatchNorm1DClass · 0.90
TorchBatchNormLayerClass · 0.85
err_fmtFunction · 0.70

Tested by

no test coverage detected