Skip to content

Commit 584f29a

Browse files
committed
SGD: fix numerical stability issues
1 parent 41e584d commit 584f29a

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/gradient_based_learning.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function apply_gradients_cpu(pbc::ParamBitCircuit, param_grads::Vector; lr::Floa
207207
end
208208
sum_params = -Inf
209209
@inbounds for ele_id = ele_start_id : ele_end_id
210-
params[ele_id] += lr * param_grads[ele_id] / sum_grads
210+
params[ele_id] += lr * param_grads[ele_id] / (sum_grads + 1e-8)
211211
sum_params = logaddexp(sum_params, params[ele_id])
212212
end
213213
@inbounds for ele_id = ele_start_id : ele_end_id
@@ -317,7 +317,7 @@ function apply_gradients_cuda(layer, nodes, param_grads, params, lr::Float64)
317317
end
318318
sum_params = -Inf
319319
@inbounds for ele_id = ele_start_id : ele_end_id
320-
params[ele_id] += lr * param_grads[ele_id] / sum_grads
320+
params[ele_id] += lr * param_grads[ele_id] / (sum_grads + 1e-8)
321321
sum_params = logsumexp_cuda(sum_params, params[ele_id])
322322
end
323323
@inbounds for ele_id = ele_start_id : ele_end_id

0 commit comments

Comments
 (0)