Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between view and view_as in PyTorch?

Tags:

pytorch

I am building neural networks in Pytorch, I see view and view_as used interchangeably in various implementation what is the difference between them?

like image 349
Jibin Mathew Avatar asked Mar 28 '19 17:03

Jibin Mathew


People also ask

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 is the difference between View and reshape in PyTorch?

Difference between view() and reshape(): view() cannot apply on 'non-contiguous' tensor /view. It returns a view. reshape() can apply on both 'contiguous' and 'non-contiguous' tensor/view.

Is view and reshape same?

The semantics of reshape() are that it may or may not share the storage and you don't know beforehand. Another difference is that reshape() can operate on both contiguous and non-contiguous tensor while view() can only operate on contiguous tensor. Also see here about the meaning of contiguous .

What does view () do in Python?

A view function, or view for short, 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 . . . or anything, really.


1 Answers

view and view_as are very similar with a slight difference. In view() the shape of the desired output tensor is to be passed in as the parameter, whereas in view_as() a tensor whose shape is to be mimicked is passed.

tensor.view_as(other) is equivalent to tensor.view(other.size())

like image 149
Jibin Mathew Avatar answered Sep 27 '22 19:09

Jibin Mathew