(N=15)
| 337 | |
| 338 | |
| 339 | def test_squared_error_grad(N=15): |
| 340 | from numpy_ml.neural_nets.losses import SquaredError |
| 341 | from numpy_ml.neural_nets.activations import Tanh |
| 342 | |
| 343 | np.random.seed(12345) |
| 344 | |
| 345 | N = np.inf if N is None else N |
| 346 | |
| 347 | mine = SquaredError() |
| 348 | gold = torch_mse_grad |
| 349 | act = Tanh() |
| 350 | |
| 351 | i = 1 |
| 352 | while i < N: |
| 353 | n_dims = np.random.randint(2, 100) |
| 354 | n_examples = np.random.randint(1, 1000) |
| 355 | y = random_tensor((n_examples, n_dims)) |
| 356 | |
| 357 | # raw inputs |
| 358 | z = random_tensor((n_examples, n_dims)) |
| 359 | y_pred = act.fn(z) |
| 360 | |
| 361 | assert_almost_equal( |
| 362 | mine.grad(y, y_pred, z, act), 0.5 * gold(y, z, torch.tanh), decimal=4 |
| 363 | ) |
| 364 | print("PASSED") |
| 365 | i += 1 |
| 366 | |
| 367 | |
| 368 | def test_cross_entropy_grad(N=15): |
nothing calls this directly
no test coverage detected