-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
reshape should be consistent re copying or sharing data #4211
Comments
See #2279. I'm against calling it |
A possible use case for reshape is neural network optimization code. I am trying to reimplement Ng's deep learning tutorial code in Julia (http://ufldl.stanford.edu/wiki/index.php/UFLDL_Tutorial). The optimizer (e.g. NLopt) expects a flat parameter vector. The neural net code is more readable if parameters are interpreted as several matrices. One would like to view the same data in both ways without duplicating, large models are memory bound. When I try to interpret a vector as several matrices I seem to get a "copy": c = rand(100) If I try to interpret the whole vector as a matrix I seem to get a "share": d = reshape(c, 10, 10) It seems the culprit is c[1:10]: d = c[1:10] Which can be solved using subarray: d = sub(c, 1:10) However not with reshape: d = reshape(sub(c, 1:10), 2, 5) Of course I do not know if data duplication happens when I define d or when I try to write on d. If it is the latter, than memory cost would not be an issue with read-only code. |
I don't believe subarrays can be reshaped in-place in general, but it is possible in 1d. That raises the question of whether it's ok for reshape of a subarray to copy the data "sometimes". |
Is there a technical reason that a function |
When the shape is part of the type, you can't change the shape without changing the type. If the type of arrays are prone to change, all code that dispatches on array shape will have to do runtime checks to see if someone changed the shape/type of the array. That is exactly the problem we have that makes global variables so slow. You could of course make |
Ah yes, thanks for the explanation! |
I do think we should make |
An extension of the generic lazy view could maybe be made to work with a different output size? The copying reshape on sparse matrices could be useful in a few places but for consistency it makes sense to give that a different name. |
Yes, but we have julia> A = rand(10,10);
julia> B = sub(A, 1:8, 2:7);
julia> @which reshape(B, (48,))
reshape(a::AbstractArray{T<:Any,N<:Any}, dims::Tuple{Vararg{Int64}}) at abstractarray.jl:202 which points to the copy version. Ref #10507. |
Yes, we would need to remove that copy implementation, so reshaping SubArrays becomes an error because it doesn't obey the contract. Then we'd need #10507 to get the function back. |
I wonder if we shouldn't rethink the whole reshape API more radically. After all, this is really the problem child here. Without it, everything else composes fairly nicely. |
@JeffBezanson, I see, I hadn't thought about forcing the user do an explicit For writing generic code, it seems possible people will define a |
One use case I see a lot is |
Not that I'm aware of. Over at #15449, However, as soon as you try writing, you're hosed. Unless you first say |
Worth pointing out that this construct is more common in test code than anywhere else. |
That sounds great, and a good example of why this issue is needed. We can
fully separate a no-copy reshape of a range from making a dense mutable
range.
|
Here's an idea: we could give |
👍 Do you throw an error if Since you're thinking about this, another thought: should we consider operators and symmetry? In operations like However, the "right" type to allocate may depend on both A potential API (which could be parallel to the existing API) is |
Way too late here but after having had some fights with |
Currently the documentation for
reshape
states:Copying or sharing should be consistent across array types. Perhaps use
reshape!
for a version that shares storage and savereshape
for a version that copies the array's contents.The text was updated successfully, but these errors were encountered: