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

Function test_LayerNorm2D

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

Source from the content-addressed store, hash-verified

844
845
846def test_LayerNorm2D(N=15):
847 from numpy_ml.neural_nets.layers import LayerNorm2D
848
849 N = np.inf if N is None else N
850
851 np.random.seed(12345)
852
853 i = 1
854 while i < N + 1:
855 n_ex = np.random.randint(2, 10)
856 in_rows = np.random.randint(1, 10)
857 in_cols = np.random.randint(1, 10)
858 n_in = np.random.randint(1, 3)
859
860 # initialize LayerNorm2D layer
861 X = random_tensor((n_ex, in_rows, in_cols, n_in), standardize=True)
862 L1 = LayerNorm2D()
863
864 # forward prop
865 y_pred = L1.forward(X)
866
867 # standard sum loss
868 dLdy = np.ones_like(X)
869 dLdX = L1.backward(dLdy)
870
871 # get gold standard gradients
872 gold_mod = TorchLayerNormLayer(
873 [n_in, in_rows, in_cols], L1.parameters, mode="2D", epsilon=L1.epsilon
874 )
875 golds = gold_mod.extract_grads(X, Y_true=None)
876
877 params = [
878 (L1.X[0], "X"),
879 (L1.hyperparameters["epsilon"], "epsilon"),
880 (L1.parameters["scaler"], "scaler"),
881 (L1.parameters["intercept"], "intercept"),
882 (y_pred, "y"),
883 (L1.gradients["scaler"], "dLdScaler"),
884 (L1.gradients["intercept"], "dLdIntercept"),
885 (dLdX, "dLdX"),
886 ]
887
888 print("Trial {}".format(i))
889 for ix, (mine, label) in enumerate(params):
890 assert_almost_equal(
891 mine, golds[label], err_msg=err_fmt(params, golds, ix), decimal=3
892 )
893
894 print("\tPASSED {}".format(label))
895
896 i += 1
897
898
899def test_MultiplyLayer(N=15):

Callers

nothing calls this directly

Calls 7

forwardMethod · 0.95
backwardMethod · 0.95
extract_gradsMethod · 0.95
random_tensorFunction · 0.90
LayerNorm2DClass · 0.90
TorchLayerNormLayerClass · 0.85
err_fmtFunction · 0.70

Tested by

no test coverage detected