question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gradient of QR fails

See original GitHub issue

There seems to be a major bug in the gradient of the QR decomposition. The forward-mode derivative is accurate for Q but very far off for R. There also seems to be no check in the tests making sure the gradient is accurate.

This reproduces the error:

import jax
import jax.numpy as jnp
import numpy as np

x = np.random.randn(3, 3)
dx = np.random.randn(3, 3)

primals, tangents = jax.jvp(jnp.linalg.qr, (x,), (dx,))
q, r = primals
dq, dr = tangents

dt = 1e-6
dq_ = (np.linalg.qr(x + dt * dx)[0] - np.linalg.qr(x)[0]) / dt
dr_ = (np.linalg.qr(x + dt * dx)[1] - np.linalg.qr(x)[1]) / dt

assert jnp.allclose(x, q @ r, atol=1e-5, rtol=1e-5)  # passes
assert jnp.allclose(dq, dq_, atol=1e-5, rtol=1e-5)  # passes
assert jnp.allclose(dx, q @ dr_ + dq_ @ r, atol=1e-5, rtol=1e-5)  # passes
assert jnp.allclose(dr, dr_, atol=1e-5, rtol=1e-5)  # fails

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:23 (23 by maintainers)

github_iconTop GitHub Comments

1reaction
dpfaucommented, Apr 28, 2020

Thanks. Yes, I saw the derivation, and checked it manually. The problem is perhaps deeper than the derivation itself and has more to do with something weird happening in the back end if the exact same function implemented as a jvp rule for a primitive gives different results than executing the bare function.

On Tue, Apr 28, 2020, 10:18 AM Jamie Townsend notifications@github.com wrote:

Interesting, I’ll try to take a look at this today. FYI the derivation I wrote is here https://j-towns.github.io/papers/qr-derivative.pdf.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/google/jax/issues/2863#issuecomment-620485555, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABDACBLLSFY2EGRJ5JEXFDRO2NPFANCNFSM4MSDXDMA .

0reactions
dpfaucommented, Apr 28, 2020

Thanks! And glad this was able to flush out the testing rng seed issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scanning QR code with color gradient - Apple Developer
When scanning a QR code with Apple's camera app, it has no problem scanning the QR code that has a color gradient. But...
Read more >
Problems adding Qr field in externalWallHeatFluxTemperature ...
The problem is that the solver usually crashes when Qr is added to the patch's balance, I don't know why, I'm actually testing...
Read more >
Qr Code Gradients - Osiset
This created a problem… how do I create a gradient on the Qr code? I came up with a solution: colorize the Qr...
Read more >
8. Least squares
the problem is also called the linear least squares problem. Least squares ... gradient of is ... rewrite least squares solution using QR...
Read more >
Conjugate Gradient Method - Stanford University
same as direct methods. • get advantage over dense if matrix-vector multiply is cheaper than n2. • with roundoff error, CG can work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found