Merged
Conversation
Contributor
Author
wang-chen
requested changes
Dec 7, 2023
wang-chen
requested changes
Dec 8, 2023
Member
There was a problem hiding this comment.
This test doesn't follow the existing style. Refer to this test or this test with predefined data. Use class style.
pypose/optim/solver.py
Outdated
Comment on lines
+304
to
+332
| if self.maxiter is None: | ||
| maxiter = n*10 | ||
| else: | ||
| maxiter = self.maxiter | ||
| r = b - bmv(A, x) if x.any() else b.clone() | ||
| rho_prev, p = None, None | ||
|
|
||
| for iteration in range(maxiter): | ||
| if (torch.linalg.norm(r, dim=-1) < atol).all(): | ||
| return x | ||
|
|
||
| z = bmv(M, r) if M is not None else r | ||
| rho_cur = vecdot(r, z) | ||
| if iteration > 0: | ||
| beta = rho_cur / rho_prev | ||
| p = p * beta.unsqueeze(-1) + z | ||
| else: # First spin | ||
| p = torch.empty_like(r) | ||
| p[:] = z[:] | ||
|
|
||
| q = bmv(A, p) | ||
| alpha = rho_cur / vecdot(p, q) | ||
| x += alpha.unsqueeze(-1)*p | ||
| r -= alpha.unsqueeze(-1)*q | ||
| rho_prev = rho_cur | ||
|
|
||
| else: # for loop exhausted | ||
| # Return incomplete progress | ||
| return x |
Member
There was a problem hiding this comment.
how about
if self.maxiter is None:
maxiter = n * 10
else:
maxiter = self.maxiter
r = b - bmv(A, x) if x.any() else b.clone()
p = torch.empty_like(r)
rho_prev, p[:]= None, z[:]
for iteration in range(maxiter):
if (torch.linalg.norm(r, dim=-1) < atol).all():
return x
z = bmv(M, r) if M is not None else r
rho_cur = vecdot(r, z)
beta = rho_cur / rho_prev
p = p * beta.unsqueeze(-1) + z
q = bmv(A, p)
alpha = rho_cur / vecdot(p, q)
x += alpha.unsqueeze(-1)*p
r -= alpha.unsqueeze(-1)*q
rho_prev = rho_cur
return x
Contributor
Author
There was a problem hiding this comment.
z has to be defined before it is used in rho_prev, p[:]= None, z[:]
wang-chen
approved these changes
Dec 8, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add cg solver
Solve #311