Skip to content

Commit df68461

Browse files
geoffroylecontedpo
andauthored
Improve DiagonalQN stability when s is close to zero (#271)
* improve update stability when s is close to zero * Update src/DiagonalHessianApproximation.jl Co-authored-by: Dominique <dominique.orban@gmail.com> * Auto stash before merge of "qn-stability" and "origin/qn-stability" --------- Co-authored-by: Dominique <dominique.orban@gmail.com>
1 parent 106f083 commit df68461

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/DiagonalHessianApproximation.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,27 @@ positive definite.
4747
"""
4848
function DiagonalQN(d::AbstractVector{T}, psb::Bool = false) where {T <: Real}
4949
prod = (res, v, α, β) -> mulSquareOpDiagonal!(res, d, v, α, β)
50-
DiagonalQN(d, length(d), length(d), true, true, prod, prod, prod, 0, 0, 0, true, true, true, psb)
50+
n = length(d)
51+
DiagonalQN(d, n, n, true, true, prod, prod, prod, 0, 0, 0, true, true, true, psb)
5152
end
5253

5354
# update function
5455
# s = x_{k+1} - x_k
5556
# y = ∇f(x_{k+1}) - ∇f(x_k)
5657
function push!(
5758
B::DiagonalQN{T, I, V, F},
58-
s::V,
59-
y::V,
59+
s0::V,
60+
y0::V,
6061
) where {T <: Real, I <: Integer, V <: AbstractVector{T}, F}
62+
s0Norm = norm(s0, 2)
63+
if s0Norm == 0
64+
error("Cannot update DiagonalQN operator with s=0")
65+
end
66+
# sᵀBs = sᵀy can be scaled by ||s||² without changing the update
67+
s = (si / s0Norm for si s0)
6168
s2 = (si^2 for si s)
69+
y = (yi / s0Norm for yi y0)
6270
trA2 = dot(s2, s2)
63-
if trA2 == 0
64-
error("Cannot divide by zero and trA2 = 0")
65-
end
6671
sT_y = dot(s, y)
6772
sT_B_s = dot(s2, B.d)
6873
q = sT_y - sT_B_s

0 commit comments

Comments
 (0)