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

1D tensor not displayed correctly #340

Closed
tgkolda opened this issue Nov 13, 2024 · 6 comments
Closed

1D tensor not displayed correctly #340

tgkolda opened this issue Nov 13, 2024 · 6 comments
Labels
not-a-bug Won't fix because isn't a bug

Comments

@tgkolda
Copy link
Collaborator

tgkolda commented Nov 13, 2024

When I display a 1D tensor, it has a spurious comma in the shape and is printed in row rather than column orientation:
Code:

data = np.array([5,7,8,1,3,9,5,3,5,2,8,4,5,1,7,2,9,4,7,7,8,1,3,5]);
X = ttb.tensor(data, (3, 4, 2));
print("X[1, :, 1] =\n", X[1, :, 1])

Output:

X[1, :, 1] =
 tensor of shape (4,)
data[:] =
[1 9 7 3]

Compare to MATLAB:

X = tensor([5,7,8,1,3,9,5,3,5,2,8,4,5,1,7,2,9,4,7,7,8,1,3,5],[3 4 2]);
X(2,:,2)

Output:

ans is a tensor of size 4
	ans(:) = 
	     1
	     9
	     7
	     3
@ntjohnson1
Copy link
Collaborator

This is consistent with our documentation and numpy. We note that we follow the numpy convention for 1D tensors to be a row.

Numpy has three flavors of 1D arrays of length N with shape (N,1), (1,N), and (N,) corresponding to explicit row, explicit column and a flexible 1D vector that can serve either.

So I think this is conceptually aligned but visually different. Have you run into functional issues because of this or just stylistic?

@ntjohnson1 ntjohnson1 added the not-a-bug Won't fix because isn't a bug label Nov 14, 2024
@tgkolda
Copy link
Collaborator Author

tgkolda commented Nov 14, 2024

In MATLAB, we ensure any 1D tensor (or fiber of a larger tensor) is explicitly a column array. This is partly because MATLAB has no concept of a 1D array. But it's also consistent with the ordering of tensor modes. A row vector is technically a 1 x N tensor, whereas a column vector is an N or N x 1 tensor. This contrast to what you're saying, so I'm a bit confused.

@tgkolda
Copy link
Collaborator Author

tgkolda commented Nov 14, 2024

Also, where is this in the documentation?

@ntjohnson1
Copy link
Collaborator

RE: Confusion
I think my ordering wasn't consistent. What you described matches for numpy as well.

>>> N = 4
>>> np.ones((N,))
array([1., 1., 1., 1.])
>>> np.ones((N,1))
array([[1.],
       [1.],
       [1.],
       [1.]])
>>> np.ones((1,4))
array([[1., 1., 1., 1.]])

I think unless there is a strong functional reason we've tried to align design choices to numpy style since that is by far the most popular array/scientific computing package in python. We can see if the true 1-D array causes any issues for use or if its mostly just a style difference (unless you already have an example to review)

@ntjohnson1
Copy link
Collaborator

Re: Documentation

https://pyttb.readthedocs.io/en/stable/tensor.html#pyttb.tensor Above parameters describing the default constructor or search for Numpy convention of being a row vector. We probably should make it under a Notes heading rather than just saying note to make it clearer.

@ntjohnson1
Copy link
Collaborator

Discussion on #350 appears to subsume this so closing here so only tracking in one location

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not-a-bug Won't fix because isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants