tensorly icon indicating copy to clipboard operation
tensorly copied to clipboard

tl. diag function supports main diagonal only with Tensorflow backend

Open caglayantuna opened this issue 5 years ago • 2 comments

Describe the bug

Tensorly diag function has an input k to select diagonal. However, this option doesn't work properly with Tensorflow backend.

Steps or Code to Reproduce

Selected vector is always being on the main diagonal. For instance:

tl.diag([1,1,1], 1)

array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]], dtype=int32)>

Same function with tensorflow gives an expected output:

tf.linalg.diag([1,1,1], k=1)

array([[0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1],
       [0, 0, 0, 0]], dtype=int32)>

Most probably, reason is the place of k in the Tensorflow function. But I couldn't find how to fix it in Tensorly.

caglayantuna avatar Jul 22 '21 11:07 caglayantuna

I think the problem is the use of tf.linalg.tensor_diag instead of tf.linalg.diag in tensorly. The former only supports the main diagonal. So I guess an easy fix would be to change this in: https://github.com/tensorly/tensorly/blob/10d0babc24ceb689ad59b19d2a794ebd8bbf10f8/tensorly/backend/tensorflow_backend.py#L175

merajhashemi avatar Aug 25 '21 04:08 merajhashemi

Tkanks @merajhashemi . I submitted a PR #321 to solve this issue.

caglayantuna avatar Aug 31 '21 09:08 caglayantuna