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

Function test_cross_entropy_grad

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

Source from the content-addressed store, hash-verified

366
367
368def test_cross_entropy_grad(N=15):
369 from numpy_ml.neural_nets.losses import CrossEntropy
370 from numpy_ml.neural_nets.layers import Softmax
371
372 np.random.seed(12345)
373
374 N = np.inf if N is None else N
375
376 mine = CrossEntropy()
377 gold = torch_xe_grad
378 sm = Softmax()
379
380 i = 1
381 while i < N:
382 n_classes = np.random.randint(2, 100)
383 n_examples = np.random.randint(1, 1000)
384
385 y = random_one_hot_matrix(n_examples, n_classes)
386
387 # the cross_entropy_gradient returns the gradient wrt. z (NOT softmax(z))
388 z = random_tensor((n_examples, n_classes))
389 y_pred = sm.forward(z)
390
391 assert_almost_equal(mine.grad(y, y_pred), gold(y, z), decimal=5)
392 print("PASSED")
393 i += 1
394
395
396#######################################################################

Callers

nothing calls this directly

Calls 6

forwardMethod · 0.95
gradMethod · 0.95
CrossEntropyClass · 0.90
SoftmaxClass · 0.90
random_one_hot_matrixFunction · 0.90
random_tensorFunction · 0.90

Tested by

no test coverage detected