|
| 1 | +from ..utils import BaseTestClass, gen_test_data |
| 2 | +from typing import List |
| 3 | +import numpy as np |
| 4 | +import torch |
| 5 | + |
| 6 | + |
| 7 | +class TestClass(BaseTestClass): |
| 8 | + def test_compute_gradients_and_hessians(self, dist_class, loss_fn, stabilization): |
| 9 | + # Create data for testing |
| 10 | + params, target, weights, _ = gen_test_data(dist_class, weights=True) |
| 11 | + if dist_class.dist.univariate: |
| 12 | + target = torch.tensor(target) |
| 13 | + else: |
| 14 | + target = torch.tensor(target)[:, :dist_class.dist.n_targets] |
| 15 | + start_values = np.array([0.5 for _ in range(dist_class.dist.n_dist_param)]) |
| 16 | + |
| 17 | + # Set the loss function for testing |
| 18 | + dist_class.dist.loss_fn = loss_fn |
| 19 | + |
| 20 | + # Set the stabilization for testing |
| 21 | + dist_class.dist.stabilization = stabilization |
| 22 | + |
| 23 | + # Call the function |
| 24 | + predt, loss = dist_class.dist.get_params_loss(params, target, start_values, requires_grad=True) |
| 25 | + grad, hess = dist_class.dist.compute_gradients_and_hessians(loss, predt, weights) |
| 26 | + |
| 27 | + # Assertions |
| 28 | + assert isinstance(predt, List) |
| 29 | + for i in range(len(predt)): |
| 30 | + assert isinstance(predt[i], torch.Tensor) |
| 31 | + assert not torch.isnan(predt[i]).any() |
| 32 | + assert not torch.isinf(predt[i]).any() |
| 33 | + assert isinstance(loss, torch.Tensor) |
| 34 | + assert not torch.isnan(loss).any() |
| 35 | + assert not torch.isinf(loss).any() |
| 36 | + |
| 37 | + assert isinstance(grad, np.ndarray) |
| 38 | + assert isinstance(hess, np.ndarray) |
| 39 | + assert grad.shape == params.flatten().shape |
| 40 | + assert hess.shape == params.flatten().shape |
| 41 | + assert not np.isnan(grad).any() |
| 42 | + assert not np.isnan(hess).any() |
| 43 | + |
| 44 | + def test_compute_gradients_and_hessians_crps(self, dist_class_crps, stabilization): |
| 45 | + # Create data for testing |
| 46 | + params, target, weights, _ = gen_test_data(dist_class_crps, weights=True) |
| 47 | + if dist_class_crps.dist.univariate: |
| 48 | + target = torch.tensor(target) |
| 49 | + else: |
| 50 | + target = torch.tensor(target)[:, :dist_class_crps.dist.n_targets] |
| 51 | + start_values = np.array([0.5 for _ in range(dist_class_crps.dist.n_dist_param)]) |
| 52 | + |
| 53 | + # Set the loss function for testing |
| 54 | + dist_class_crps.dist.loss_fn = "crps" |
| 55 | + |
| 56 | + # Set the stabilization for testing |
| 57 | + dist_class_crps.dist.stabilization = stabilization |
| 58 | + |
| 59 | + # Call the function |
| 60 | + predt, loss = dist_class_crps.dist.get_params_loss(params, target, start_values, requires_grad=True) |
| 61 | + grad, hess = dist_class_crps.dist.compute_gradients_and_hessians(loss, predt, weights) |
| 62 | + |
| 63 | + # Assertions |
| 64 | + assert isinstance(predt, List) |
| 65 | + for i in range(len(predt)): |
| 66 | + assert isinstance(predt[i], torch.Tensor) |
| 67 | + assert not torch.isnan(predt[i]).any() |
| 68 | + assert not torch.isinf(predt[i]).any() |
| 69 | + assert isinstance(loss, torch.Tensor) |
| 70 | + assert not torch.isnan(loss).any() |
| 71 | + assert not torch.isinf(loss).any() |
| 72 | + |
| 73 | + assert isinstance(grad, np.ndarray) |
| 74 | + assert isinstance(hess, np.ndarray) |
| 75 | + assert grad.shape == params.flatten().shape |
| 76 | + assert hess.shape == params.flatten().shape |
| 77 | + assert not np.isnan(grad).any() |
| 78 | + assert not np.isnan(hess).any() |
| 79 | + |
| 80 | + def test_compute_gradients_and_hessians_nans(self, dist_class, loss_fn, stabilization): |
| 81 | + # Create data for testing |
| 82 | + params, target, weights, _ = gen_test_data(dist_class, weights=True) |
| 83 | + params[0, 0] = np.nan |
| 84 | + if dist_class.dist.univariate: |
| 85 | + target = torch.tensor(target) |
| 86 | + else: |
| 87 | + target = torch.tensor(target)[:, :dist_class.dist.n_targets] |
| 88 | + start_values = np.array([0.5 for _ in range(dist_class.dist.n_dist_param)]) |
| 89 | + |
| 90 | + # Set the loss function for testing |
| 91 | + dist_class.dist.loss_fn = loss_fn |
| 92 | + |
| 93 | + # Set the stabilization for testing |
| 94 | + dist_class.dist.stabilization = stabilization |
| 95 | + |
| 96 | + # Call the function |
| 97 | + predt, loss = dist_class.dist.get_params_loss(params, target, start_values, requires_grad=True) |
| 98 | + grad, hess = dist_class.dist.compute_gradients_and_hessians(loss, predt, weights) |
| 99 | + |
| 100 | + # Assertions |
| 101 | + assert isinstance(predt, List) |
| 102 | + for i in range(len(predt)): |
| 103 | + assert isinstance(predt[i], torch.Tensor) |
| 104 | + assert not torch.isnan(predt[i]).any() |
| 105 | + assert not torch.isinf(predt[i]).any() |
| 106 | + assert isinstance(loss, torch.Tensor) |
| 107 | + assert not torch.isnan(loss).any() |
| 108 | + assert not torch.isinf(loss).any() |
| 109 | + |
| 110 | + assert isinstance(grad, np.ndarray) |
| 111 | + assert isinstance(hess, np.ndarray) |
| 112 | + assert grad.shape == params.flatten().shape |
| 113 | + assert hess.shape == params.flatten().shape |
| 114 | + assert not np.isnan(grad).any() |
| 115 | + assert not np.isnan(hess).any() |
0 commit comments