-
Notifications
You must be signed in to change notification settings - Fork 311
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
Inconsistency in into_shape
with identity
#766
Comments
Good catch! The first paragraph of the docs is incorrect. The correct condition is specified in the second paragraph ("Errors if the input array is not c- or f-contiguous."). In the example you've provided, the array is contiguous but not in standard or Fortran layout; that's why If you actually want to do what your example illustrates (re-interpreting an array with contiguous but not standard or Fortran layout) using the current version of The |
Got it! If you don't mind me asking, what makes reshaping an existing array nontrivial? To my knowledge the reshape is essentially "virtual"; no memory is actually moved, right? For example, I've seen implementations that only check if the product of the shape (i.e. n*m) remains the same and don't do anything other than that. |
This approach works well if the implementation forces a particular memory layout of the arrays. For example, MATLAB arrays are always column-major, so For implementations like NumPy and The current implementation of
Consider this example which just flattens various arrays into 1-D arrays: use ndarray::prelude::*;
fn main() {
let a = Array2::from_shape_vec((2, 3), vec![0, 1, 2, 3, 4, 5]).unwrap();
let b = Array2::from_shape_vec((2, 3).f(), vec![0, 3, 1, 4, 2, 5]).unwrap();
let c = (&a + &b) / 2;
let d = (&b + &a) / 2;
assert_eq!(a, b);
assert_eq!(a, c);
assert_eq!(a, d);
println!("a_flat = {}", a.into_shape(6).unwrap());
println!("b_flat = {}", b.into_shape(6).unwrap());
println!("c_flat = {}", c.into_shape(6).unwrap());
println!("d_flat = {}", d.into_shape(6).unwrap());
} It has the following output: a_flat = [0, 1, 2, 3, 4, 5]
b_flat = [0, 3, 1, 4, 2, 5]
c_flat = [0, 1, 2, 3, 4, 5]
d_flat = [0, 3, 1, 4, 2, 5] Observe that all the arrays are equal (see the I would hate for a project to use So, IMO it's very important to change |
Got it, thanks! |
In addition to the limits of
into_shape()
described in #390, I think the following is a bug with the current implementation.I'm trying to reshape a tensor into its own shape (which should obviously always succeed).
This use case is purely hypothetical (why would anyone want to reshape into the current shape?), but I still think it might be a bug.
The next step would be to support reshaping from (A...) to (1, A...), which I assume would also be possible once this bug is fixed.
The text was updated successfully, but these errors were encountered: