Skip to content

tensor.mttkrp does not work with d-way tensors when d > 3 #35

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

Closed
dmdunla opened this issue Jul 11, 2022 · 0 comments · Fixed by #36
Closed

tensor.mttkrp does not work with d-way tensors when d > 3 #35

dmdunla opened this issue Jul 11, 2022 · 0 comments · Fixed by #36

Comments

@dmdunla
Copy link
Collaborator

dmdunla commented Jul 11, 2022

Below are the current outputs from tensor.mttkrp in pyttb and the Matlab Tensor Toolbox. For dimensions 0, 3, 4, pyttb works, but the other dimensions throw a ValueError during the reshape of the khatrirao product.

pyttb:

>>> T = ttb.tensor.from_data(np.arange(1,np.prod(shape)+1), shape)
>>> U = [];
>>> for s in shape: U.append(np.ones((s,2)))
...
>>> T.mttkrp(U,0)
array([[129600., 129600.],
       [129960., 129960.]])
>>> T.mttkrp(U,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\dmdunla\dev\github.com\pyttb\pyttb\tensor.py", line 603, in mttkrp
    Ur = np.reshape(ttb.khatrirao(U[0:self.ndims - 2], reverse=True), (szl, 1, R), order='F')
  File "<__array_function__ internals>", line 5, in reshape
  File "C:\Users\dmdunla\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 299, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "C:\Users\dmdunla\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 58, in _wrapfunc
    return bound(*args, **kwds)
ValueError: cannot reshape array of size 48 into shape (2,1,2)
>>> T.mttkrp(U,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\dmdunla\dev\github.com\pyttb\pyttb\tensor.py", line 603, in mttkrp
    Ur = np.reshape(ttb.khatrirao(U[0:self.ndims - 2], reverse=True), (szl, 1, R), order='F')
  File "<__array_function__ internals>", line 5, in reshape
  File "C:\Users\dmdunla\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 299, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "C:\Users\dmdunla\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 58, in _wrapfunc
    return bound(*args, **kwds)
ValueError: cannot reshape array of size 48 into shape (6,1,2)
>>> T.mttkrp(U,3)
array([[45000., 45000.],
       [48456., 48456.],
       [51912., 51912.],
       [55368., 55368.],
       [58824., 58824.]])
>>> T.mttkrp(U,4)
array([[ 7260.,  7260.],
       [21660., 21660.],
       [36060., 36060.],
       [50460., 50460.],
       [64860., 64860.],
       [79260., 79260.]])

Matlab:

>> T = tensor(1:prod(shape),shape);
>> U = {}; for i = 1:length(shape), U{i} = ones(shape(i),2); end
>> mttkrp(T,U,1)

ans =

      129600      129600
      129960      129960

>> mttkrp(T,U,2)

ans =

       86040       86040
       86520       86520
       87000       87000

>> mttkrp(T,U,3)

ans =

       63270       63270
       64350       64350
       65430       65430
       66510       66510

>> mttkrp(T,U,4)

ans =

       45000       45000
       48456       48456
       51912       51912
       55368       55368
       58824       58824

>> mttkrp(T,U,5)

ans =

        7260        7260
       21660       21660
       36060       36060
       50460       50460
       64860       64860
       79260       79260
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant