|
| 1 | +from copy import deepcopy |
| 2 | +from typing import Callable |
| 3 | + |
1 | 4 | import numpy
|
2 | 5 | import pytest
|
3 | 6 | import torch
|
4 |
| -from copy import deepcopy |
| 7 | + |
5 | 8 | from stochman import nnj
|
6 | 9 |
|
7 | 10 | _batch_size = 2
|
|
14 | 17 | _3d_conv_input = torch.randn(_batch_size, _features, _dims, _dims, _dims)
|
15 | 18 |
|
16 | 19 |
|
17 |
| -def _compare_jacobian(f, x): |
| 20 | +def _compare_jacobian(f: Callable, x: torch.Tensor) -> torch.Tensor: |
| 21 | + """ Use pytorch build-in jacobian function to compare for correctness of computations""" |
18 | 22 | out = f(x)
|
19 |
| - output = torch.autograd.functional.jacobian(f, x) |
| 23 | + output = torch.autograd.functional.jacobian(f, x) |
20 | 24 | m = out.ndim
|
21 |
| - output = output.movedim(m,1) |
22 |
| - res = torch.stack([output[i,i] for i in range(_batch_size)], dim=0) |
| 25 | + output = output.movedim(m, 1) |
| 26 | + res = torch.stack([output[i, i] for i in range(_batch_size)], dim=0) |
23 | 27 | return res
|
24 | 28 |
|
25 | 29 |
|
26 |
| -@pytest.mark.parametrize("model, input", |
| 30 | +@pytest.mark.parametrize( |
| 31 | + "model, input", |
27 | 32 | [
|
28 | 33 | (nnj.Sequential(nnj.Identity(), nnj.Identity()), _linear_input),
|
29 | 34 | (nnj.Linear(_features, 2), _linear_input),
|
30 |
| - (nnj.PosLinear(_features, 2), _linear_input), |
| 35 | + (nnj.Sequential(nnj.PosLinear(_features, 2), nnj.Reciprocal()), _linear_input), |
31 | 36 | (nnj.Sequential(nnj.Linear(_features, 2), nnj.Sigmoid(), nnj.ArcTanh()), _linear_input),
|
32 | 37 | (nnj.Sequential(nnj.Linear(_features, 5), nnj.Sigmoid(), nnj.Linear(5, 2)), _linear_input),
|
33 |
| - (nnj.Sequential( |
34 |
| - nnj.Linear(_features, 2), nnj.Softplus(beta=100, threshold=5), nnj.Linear(2, 4), nnj.Tanh() |
35 |
| - ), _linear_input), |
36 |
| - (nnj.Sequential( |
37 |
| - nnj.ELU(), nnj.Linear(_features, 2), nnj.Sigmoid(), nnj.ReLU(), nnj.Hardshrink(), nnj.LeakyReLU() |
38 |
| - ), _linear_input), |
| 38 | + ( |
| 39 | + nnj.Sequential( |
| 40 | + nnj.Linear(_features, 2), nnj.Softplus(beta=100, threshold=5), nnj.Linear(2, 4), nnj.Tanh() |
| 41 | + ), |
| 42 | + _linear_input, |
| 43 | + ), |
| 44 | + ( |
| 45 | + nnj.Sequential( |
| 46 | + nnj.ELU(), |
| 47 | + nnj.Linear(_features, 2), |
| 48 | + nnj.Sigmoid(), |
| 49 | + nnj.ReLU(), |
| 50 | + nnj.Sqrt(), |
| 51 | + nnj.Hardshrink(), |
| 52 | + nnj.LeakyReLU(), |
| 53 | + ), |
| 54 | + _linear_input, |
| 55 | + ), |
| 56 | + (nnj.Sequential(nnj.Linear(_features, 2), nnj.OneMinusX()), _linear_input), |
39 | 57 | (nnj.Sequential(nnj.Conv1d(_features, 2, 5), nnj.ConvTranspose1d(2, _features, 5)), _1d_conv_input),
|
40 | 58 | (nnj.Sequential(nnj.Conv2d(_features, 2, 5), nnj.ConvTranspose2d(2, _features, 5)), _2d_conv_input),
|
41 | 59 | (nnj.Sequential(nnj.Conv3d(_features, 2, 5), nnj.ConvTranspose3d(2, _features, 5)), _3d_conv_input),
|
42 |
| - (nnj.Sequential( |
43 |
| - nnj.Linear(_features, 8), nnj.Sigmoid(), nnj.Reshape(2, 4), nnj.Conv1d(2, 1, 2), |
44 |
| - ),_linear_input), |
45 |
| - (nnj.Sequential( |
46 |
| - nnj.Linear(_features, 32), nnj.Sigmoid(), nnj.Reshape(2, 4, 4), nnj.Conv2d(2, 1, 2), |
47 |
| - ),_linear_input), |
48 |
| - (nnj.Sequential( |
49 |
| - nnj.Linear(_features, 128), nnj.Sigmoid(), nnj.Reshape(2, 4, 4, 4), nnj.Conv3d(2, 1, 2), |
50 |
| - ),_linear_input), |
51 |
| - (nnj.Sequential( |
52 |
| - nnj.Conv1d(_features, 2, 3), nnj.Flatten(), nnj.Linear(8*2, 5), nnj.ReLU(), |
53 |
| - ),_1d_conv_input), |
54 |
| - (nnj.Sequential( |
55 |
| - nnj.Conv2d(_features, 2, 3), nnj.Flatten(), nnj.Linear(8*8*2, 5), nnj.ReLU(), |
56 |
| - ),_2d_conv_input), |
57 |
| - (nnj.Sequential( |
58 |
| - nnj.Conv3d(_features, 2, 3), nnj.Flatten(), nnj.Linear(8*8*8*2, 5), nnj.ReLU(), |
59 |
| - ),_3d_conv_input), |
60 |
| - (nnj.Sequential( |
61 |
| - nnj.Conv2d(_features, 2, 3), nnj.Hardtanh(), nnj.Upsample(scale_factor=2) |
62 |
| - ), _2d_conv_input) |
63 |
| - ] |
| 60 | + ( |
| 61 | + nnj.Sequential( |
| 62 | + nnj.Linear(_features, 8), |
| 63 | + nnj.Sigmoid(), |
| 64 | + nnj.Reshape(2, 4), |
| 65 | + nnj.Conv1d(2, 1, 2), |
| 66 | + ), |
| 67 | + _linear_input, |
| 68 | + ), |
| 69 | + ( |
| 70 | + nnj.Sequential( |
| 71 | + nnj.Linear(_features, 32), |
| 72 | + nnj.Sigmoid(), |
| 73 | + nnj.Reshape(2, 4, 4), |
| 74 | + nnj.Conv2d(2, 1, 2), |
| 75 | + ), |
| 76 | + _linear_input, |
| 77 | + ), |
| 78 | + ( |
| 79 | + nnj.Sequential( |
| 80 | + nnj.Linear(_features, 128), |
| 81 | + nnj.Sigmoid(), |
| 82 | + nnj.Reshape(2, 4, 4, 4), |
| 83 | + nnj.Conv3d(2, 1, 2), |
| 84 | + ), |
| 85 | + _linear_input, |
| 86 | + ), |
| 87 | + ( |
| 88 | + nnj.Sequential( |
| 89 | + nnj.Conv1d(_features, 2, 3), |
| 90 | + nnj.Flatten(), |
| 91 | + nnj.Linear(8 * 2, 5), |
| 92 | + nnj.ReLU(), |
| 93 | + ), |
| 94 | + _1d_conv_input, |
| 95 | + ), |
| 96 | + ( |
| 97 | + nnj.Sequential( |
| 98 | + nnj.Conv2d(_features, 2, 3), |
| 99 | + nnj.Flatten(), |
| 100 | + nnj.Linear(8 * 8 * 2, 5), |
| 101 | + nnj.ReLU(), |
| 102 | + ), |
| 103 | + _2d_conv_input, |
| 104 | + ), |
| 105 | + ( |
| 106 | + nnj.Sequential( |
| 107 | + nnj.Conv3d(_features, 2, 3), |
| 108 | + nnj.Flatten(), |
| 109 | + nnj.Linear(8 * 8 * 8 * 2, 5), |
| 110 | + nnj.ReLU(), |
| 111 | + ), |
| 112 | + _3d_conv_input, |
| 113 | + ), |
| 114 | + ( |
| 115 | + nnj.Sequential(nnj.Conv2d(_features, 2, 3), nnj.Hardtanh(), nnj.Upsample(scale_factor=2)), |
| 116 | + _2d_conv_input, |
| 117 | + ), |
| 118 | + ], |
64 | 119 | )
|
65 | 120 | class TestJacobian:
|
66 | 121 | @pytest.mark.parametrize("dtype", [torch.float, torch.double])
|
67 | 122 | def test_jacobians(self, model, input, dtype):
|
68 | 123 | """Test that the analytical jacobian of the model is consistent with finite
|
69 | 124 | order approximation
|
70 | 125 | """
|
71 |
| - model=deepcopy(model).to(dtype) |
72 |
| - input=deepcopy(input).to(dtype) |
| 126 | + model = deepcopy(model).to(dtype) |
| 127 | + input = deepcopy(input).to(dtype) |
73 | 128 | _, jac = model(input, jacobian=True)
|
74 | 129 | jacnum = _compare_jacobian(model, input)
|
75 | 130 | assert torch.isclose(jac, jacnum, atol=1e-7).all(), "jacobians did not match"
|
76 | 131 |
|
77 |
| - |
78 |
| - |
79 | 132 | @pytest.mark.parametrize("return_jac", [True, False])
|
80 | 133 | def test_jac_return(self, model, input, return_jac):
|
81 | 134 | """ Test that all models returns the jacobian output if asked for it """
|
82 | 135 | output = model(input, jacobian=return_jac)
|
83 | 136 | if return_jac:
|
84 | 137 | assert len(output) == 2, "expected two outputs when jacobian=True"
|
85 |
| - assert all(isinstance(o, torch.Tensor) for o in output), "expected all outputs to be torch tensors" |
| 138 | + assert all( |
| 139 | + isinstance(o, torch.Tensor) for o in output |
| 140 | + ), "expected all outputs to be torch tensors" |
86 | 141 | else:
|
87 | 142 | assert isinstance(output, torch.Tensor)
|
88 |
| - |
|
0 commit comments