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

Function test_softmax_grad

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

Source from the content-addressed store, hash-verified

583
584
585def test_softmax_grad(N=15):
586 from numpy_ml.neural_nets.layers import Softmax
587 from functools import partial
588
589 np.random.seed(12345)
590
591 N = np.inf if N is None else N
592 p_soft = partial(F.softmax, dim=1)
593 gold = torch_gradient_generator(p_soft)
594
595 i = 0
596 while i < N:
597 mine = Softmax()
598 n_ex = np.random.randint(1, 3)
599 n_dims = np.random.randint(1, 50)
600 z = random_tensor((n_ex, n_dims), standardize=True)
601 out = mine.forward(z)
602
603 assert_almost_equal(
604 gold(z),
605 mine.backward(np.ones_like(out)),
606 err_msg="Theirs:\n{}\n\nMine:\n{}\n".format(
607 gold(z), mine.backward(np.ones_like(out))
608 ),
609 decimal=3,
610 )
611 print("PASSED")
612 i += 1
613
614
615def test_softplus_grad(N=15):

Callers

nothing calls this directly

Calls 5

forwardMethod · 0.95
backwardMethod · 0.95
SoftmaxClass · 0.90
random_tensorFunction · 0.90
torch_gradient_generatorFunction · 0.70

Tested by

no test coverage detected