(N=15)
| 366 | |
| 367 | |
| 368 | def 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 | ####################################################################### |
nothing calls this directly
no test coverage detected