Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QR-decomposition fails on a specific matrix. #1058

Closed
jarno-r opened this issue Dec 28, 2018 · 4 comments
Closed

QR-decomposition fails on a specific matrix. #1058

jarno-r opened this issue Dec 28, 2018 · 4 comments
Assignees
Labels
type:bug Something isn't working type:support user support questions

Comments

@jarno-r
Copy link
Contributor

jarno-r commented Dec 28, 2018

To get help from the community, check out our Google group.

TensorFlow.js version

0.14.1

Browser version

Node.js v8.12.0 without tfjs-node.

Describe the problem or feature request

QR decomposition returns NaNs for the invertible matrix [[0,2,2],[1,1,1],[0,1,2]], but works for some other matrices.

Code to reproduce the bug

"use strict";
exports.__esModule = true;
var tf = require("@tensorflow/tfjs");
var a = tf.tensor2d([[0, 2, 2], [1, 1, 1], [0, 1, 2]]);
var b = tf.tensor2d([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
{
    var _a = tf.linalg.qr(a), q = _a[0], r = _a[1];
    q.print();
    r.print();
}
{
    var _b = tf.linalg.qr(b), q = _b[0], r = _b[1];
    q.print();
    r.print();
}
@jarno-r
Copy link
Contributor Author

jarno-r commented Dec 28, 2018

The bug is in linalg_ops.ts:214: const s = rjj.sign().neg() as Tensor2D;
The sign() function returns 0 for a zero value, which is not desirable here. Instead either 1 or -1 should be used.
It could be fixed like this:

const sPre = rjj.sign().neg() as Tensor2D;
const s = sPre.where(sPre.notEqual(tensor2d([[0]])), tensor2d([[1]]));

However, it would be desirable to have positive diagonal for the resulting R and the sign selected accordingly.

@rthadur rthadur added type:bug Something isn't working type:support user support questions labels Dec 28, 2018
@caisq
Copy link
Collaborator

caisq commented Dec 28, 2018

@jarno-r Do you want to send us a PR (to https://github.com/tensorflow/tfjs-core) to fix the issue?

@jarno-r
Copy link
Contributor Author

jarno-r commented Jan 2, 2019

PR: tensorflow/tfjs-core#1473

astojilj added a commit to astojilj/tfjs-core that referenced this issue Feb 3, 2019
tensorflow/tfjs#1058

Use packed matmul program always, in batchMatMul and fusedMatMul.
Remove non packed mat mul implementation.

Modified [matmul benchmark](https://gist.github.com/astojilj/b6cc855e708bb2d77c7f892ef8489137)
shows performance improvement for matmul of tensors with shape
[3, 400, 400] and [3, 1000, 1000]:

master:

    "N=400": {
      "averageTimeMs": 25.751999999629334
    },
    "N=1000": {
      "averageTimeMs": 140.3969999976107
    }

with this patch:

    "N=400": {
      "averageTimeMs": 9.409999999043066
    },
    "N=1000": {
      "averageTimeMs": 54.652999998943415
    }

Benchmark run on discrete GPU on [ASUS ROG-GL702VM](https://www.asus.com/Laptops/ROG-GL702VM-7th-Gen-Intel-Core/)
annxingyuan pushed a commit to tensorflow/tfjs-core that referenced this issue Feb 5, 2019
PERF

tensorflow/tfjs#1058

Use packed matmul program always, in batchMatMul and fusedMatMul.
Remove non packed mat mul implementation.

Modified [matmul benchmark](https://gist.github.com/astojilj/b6cc855e708bb2d77c7f892ef8489137)
shows performance improvement for matmul of tensors with shape
[3, 400, 400] and [3, 1000, 1000]:

master:

    "N=400": {
      "averageTimeMs": 25.751999999629334
    },
    "N=1000": {
      "averageTimeMs": 140.3969999976107
    }

with this patch:

    "N=400": {
      "averageTimeMs": 9.409999999043066
    },
    "N=1000": {
      "averageTimeMs": 54.652999998943415
    }

Benchmark run on discrete GPU on [ASUS ROG-GL702VM](https://www.asus.com/Laptops/ROG-GL702VM-7th-Gen-Intel-Core/)
dsmilkov pushed a commit to tensorflow/tfjs-core that referenced this issue Aug 9, 2019
tensorflow/tfjs#1058
The sign() function returns 0 on 0, which causes a division by zero in the QR decomposition function qr() if there is a zero on the diagonal.

BUG
@dsmilkov
Copy link
Contributor

dsmilkov commented Aug 9, 2019

Fixed in tensorflow/tfjs-core#1473

@dsmilkov dsmilkov closed this as completed Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working type:support user support questions
Projects
None yet
Development

No branches or pull requests

5 participants