Repository navigation
Commit 9c3b441
Use compact [1,-2,1] kernel for BendingEnergyLoss second derivatives (#8918)
Fixes #5939.
### Summary
`BendingEnergyLoss` previously computed second-order derivatives by
applying the central first-order helper `spatial_gradient` twice, which
yields a wide `[1, 0, -2, 0, 1] / 4` stencil for pure second derivatives
that spans four voxels per axis. That stencil is less accurate than the
standard compact one and forced the validation "all spatial dims > 4".
This PR replaces the second-order computation with compact central
stencils evaluated directly on `pred`:
- **Pure** ``d^2/dx_i^2``: ``x[i+1] - 2 * x[i] + x[i-1]`` (the standard
`[1, -2, 1]` kernel).
- **Mixed** ``d^2/(dx_i dx_j)``: ``(x[i+1,j+1] - x[i+1,j-1] - x[i-1,j+1]
+ x[i-1,j-1]) / 4`` (compact 4-point central scheme).
Both span three voxels per axis, so the spatial-size validation is
relaxed from `> 4` to `> 2`, matching `DiffusionLoss`. The public API
(`__init__(normalize, reduction)`, `forward(pred)`) and `normalize`
semantics are unchanged. The existing `spatial_gradient` helper is left
untouched because `DiffusionLoss` still uses it.
### Why TEST_CASES expected values do not change
For ``f(x) = x^2``, the analytical second derivative is the constant
`2`. Both the previous central-of-central stencil ``(f[i+2] - 2*f[i] +
f[i-2]) / 4`` and the new compact ``[1, -2, 1]`` stencil ``f[i+1] -
2*f[i] + f[i-1]`` are exact for quadratics, so both return `2` at every
interior voxel. Mixed-partial test inputs are constant in at least one
of the two axes, so both stencils return `0` for mixed terms on these
cases. Squared and reduced by ``mean``, the existing `TEST_CASES`
expected values (``0.0``, ``4.0``, ``100.0``) are therefore invariant
under this change.
What does change in the tests:
- `test_ill_shape` is updated to trigger on shape `2` (was `4`) so it
still exercises the spatial-size guard.
- A new `TEST_CASES` row covers shape `(1, 3, 3, 3, 3)` of ones
(previously rejected by the `> 4` guard) → expected `0.0`, validating
the relaxed guard.
Reference for the compact mixed-partial scheme: Pavel Holoborodko's
finite-difference notes cited in the original issue.
---------
Signed-off-by: Vishnu Kannaujia <vishnu.kannaujia@gmail.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>1 parent b7d14c8 commit 9c3b441
2 files changed
Lines changed: 70 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
47 | 92 | | |
48 | 93 | | |
49 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
50 | 99 | | |
51 | 100 | | |
52 | 101 | | |
| |||
79 | 128 | | |
80 | 129 | | |
81 | 130 | | |
82 | | - | |
| 131 | + | |
83 | 132 | | |
84 | 133 | | |
85 | 134 | | |
86 | 135 | | |
87 | 136 | | |
88 | 137 | | |
89 | | - | |
90 | | - | |
| 138 | + | |
| 139 | + | |
91 | 140 | | |
92 | 141 | | |
93 | 142 | | |
94 | 143 | | |
95 | 144 | | |
96 | 145 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | 146 | | |
101 | 147 | | |
102 | 148 | | |
103 | 149 | | |
104 | | - | |
105 | | - | |
106 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
107 | 158 | | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
| 159 | + | |
| 160 | + | |
112 | 161 | | |
| 162 | + | |
113 | 163 | | |
114 | | - | |
115 | | - | |
116 | | - | |
| 164 | + | |
| 165 | + | |
117 | 166 | | |
118 | 167 | | |
119 | 168 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
67 | | - | |
| 68 | + | |
68 | 69 | | |
69 | | - | |
| 70 | + | |
70 | 71 | | |
71 | | - | |
| 72 | + | |
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| |||
0 commit comments