Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function for Reshape View?

Tags:

view

julia

In Julia v0.5, how do you make a function which is like reshape but instead returns a view? ArrayViews.jl has a reshape_view function but it doesn't seem directly compatible with the new view function. I just want to reshape u to some tuple sizeu where I don't know the dimensions.

like image 921
Chris Rackauckas Avatar asked Aug 04 '16 21:08

Chris Rackauckas


People also ask

What is reshape function?

Reshaping means changing the shape of an array. The shape of an array is the number of elements in each dimension. By reshaping we can add or remove dimensions or change number of elements in each dimension.

Why do we do reshape (- 1 1?

If you have an array of shape (2,4) then reshaping it with (-1, 1), then the array will get reshaped in such a way that the resulting array has only 1 column and this is only possible by having 8 rows, hence, (8,1).

What does view () do in PyTorch?

Returns a new tensor with the same data as the self tensor but of a different shape . Otherwise, it will not be possible to view self tensor as shape without copying it (e.g., via contiguous() ).

What does view () do in Python?

As per Django Documentation, A view function is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image, anything that a web browser can display.


1 Answers

If you reshape a 'view', the output is a reshaped 'view'.

If your initial variable is a normal array, you can convert it to a view 'on the fly' during your function call.

There are no reallocations during this operation, as per your later comment: you can confirm this with the pointer function. The objects aren't the same, in the sense that they are interpreted as pointers to a different 'type', but the memory address is the same.

julia> A = ones(5,5,5); B = view(A, 2:4, 2:4, 2:4); C = reshape(B, 1, 27);

julia> is(B,C)
false

julia> pointer(B)
Ptr{Float64} @0x00007ff51e8b1ac8

julia> pointer(C)
Ptr{Float64} @0x00007ff51e8b1ac8

julia> C[1:5] = zeros(1,5);

julia> A[:,:,2]
5×5 Array{Float64,2}:
 1.0  1.0  1.0  1.0  1.0
 1.0  0.0  0.0  1.0  1.0
 1.0  0.0  0.0  1.0  1.0
 1.0  0.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0
like image 108
Tasos Papastylianou Avatar answered Jan 01 '23 19:01

Tasos Papastylianou