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

Function test_Embedding

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

Source from the content-addressed store, hash-verified

697
698
699def test_Embedding(N=15):
700 from numpy_ml.neural_nets.layers import Embedding
701
702 np.random.seed(12345)
703
704 N = np.inf if N is None else N
705
706 i = 1
707 while i < N + 1:
708 vocab_size = np.random.randint(1, 2000)
709 n_ex = np.random.randint(1, 100)
710 n_in = np.random.randint(1, 100)
711 emb_dim = np.random.randint(1, 100)
712
713 X = np.random.randint(0, vocab_size, (n_ex, n_in))
714
715 # initialize Embedding layer
716 L1 = Embedding(n_out=emb_dim, vocab_size=vocab_size)
717
718 # forward prop
719 y_pred = L1.forward(X)
720
721 # backprop
722 dLdy = np.ones_like(y_pred)
723 # dLdX = L1.backward(dLdy)
724 L1.backward(dLdy)
725
726 # get gold standard gradients
727 gold_mod = TorchEmbeddingLayer(vocab_size, emb_dim, L1.parameters)
728 golds = gold_mod.extract_grads(X)
729
730 params = [
731 (L1.X[0], "X"),
732 (y_pred, "y"),
733 (L1.parameters["W"], "W"),
734 (dLdy, "dLdy"),
735 (L1.gradients["W"], "dLdW"),
736 # (dLdX, "dLdX"),
737 ]
738
739 print("\nTrial {}".format(i))
740 for ix, (mine, label) in enumerate(params):
741 assert_almost_equal(
742 mine, golds[label], err_msg=err_fmt(params, golds, ix), decimal=3
743 )
744 print("\tPASSED {}".format(label))
745 i += 1
746
747
748def test_BatchNorm1D(N=15):

Callers

nothing calls this directly

Calls 6

forwardMethod · 0.95
backwardMethod · 0.95
extract_gradsMethod · 0.95
EmbeddingClass · 0.90
TorchEmbeddingLayerClass · 0.85
err_fmtFunction · 0.70

Tested by

no test coverage detected