def test_median_relative_absolute_error_2(): np.random.seed(1) size = 105 np_y_pred = np.random.rand(size, 1) np_y = np.random.rand(size, 1) np.random.shuffle(np_y) np_median_absolute_relative_error = np.median( np.abs(np_y - np_y_pred) / np.abs(np_y - np_y.mean())) m = MedianRelativeAbsoluteError() y_pred = torch.from_numpy(np_y_pred) y = torch.from_numpy(np_y) m.reset() batch_size = 16 n_iters = size // batch_size + 1 for i in range(n_iters + 1): idx = i * batch_size m.update((y_pred[idx:idx + batch_size], y[idx:idx + batch_size])) assert np_median_absolute_relative_error == pytest.approx(m.compute())
def test_wrong_input_shapes(): m = MedianRelativeAbsoluteError() with pytest.raises(ValueError): m.update((torch.rand(4, 1, 2), torch.rand(4, 1))) with pytest.raises(ValueError): m.update((torch.rand(4, 1), torch.rand(4, 1, 2))) with pytest.raises(ValueError): m.update((torch.rand(4, 1, 2), torch.rand(4,))) with pytest.raises(ValueError): m.update((torch.rand(4,), torch.rand(4, 1, 2)))
def test_wrong_input_shapes(): m = MedianRelativeAbsoluteError() with pytest.raises(ValueError, match=r"Predictions should be of shape"): m.update((torch.rand(4, 1, 2), torch.rand(4, 1))) with pytest.raises(ValueError, match=r"Targets should be of shape"): m.update((torch.rand(4, 1), torch.rand(4, 1, 2))) with pytest.raises(ValueError, match=r"Predictions should be of shape"): m.update(( torch.rand(4, 1, 2), torch.rand(4, ), )) with pytest.raises(ValueError, match=r"Targets should be of shape"): m.update(( torch.rand(4, ), torch.rand(4, 1, 2), ))