Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/openfermion/resource_estimates/thc/utils/adagrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ def init(x0):
m = np.zeros_like(x0)
return x0, g_sq, m

def update(i, g, state):
def update(i, g, state, eps=1e-9):
x, g_sq, m = state
g_sq += np.square(g)
g_sq_inv_sqrt = np.where(g_sq > 0, 1.0 / np.sqrt(g_sq), 0.0)
# Add a small number to avoid division by zero
g_sq_safe = g_sq + eps
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about doing this only if abs(g_sq<eps)?

Copy link
Contributor Author

@mhucka mhucka Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may impact performance, but probably not too badly. I've rewritten it like this:

if np.abs(g_sq < eps):
    # Add a small number to avoid division by zero.
    g_sq_inv_sqrt = 1.0 / np.sqrt(g_sq + eps)
else:
    g_sq_inv_sqrt = 1.0 / np.sqrt(g_sq)

Does that seem okay?

I forgot that g_sq is an array, so a test like x < y means testing every value, and that's going to be more time-consuming.

My reading of to handle cases like this is that the original is not a bad approach in a case where gradients are being done, because ultimately the values are not used in the results. @dstrain115 do you think it's going to lead to errors if left in the original form?

g_sq_inv_sqrt = 1.0 / np.sqrt(g_sq_safe)
m = (1.0 - momentum) * (g * g_sq_inv_sqrt) + momentum * m
x = x - step_size(i) * m
return x, g_sq, m
Expand Down
Loading