Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pytorch view -1 meaning

If there is any situation that you don't know how many rows you want but are sure of the number of columns, then you can specify this with a -1. (Note that you can extend this to tensors with more dimensions. Only one of the axis value can be -1). This is a way of telling the library: "give me a tensor that has these many columns and you compute the appropriate number of rows that is necessary to make this happen".
Comment

view(-1) in pytorch

import torch

x = torch.arange(6)

print(x.view(3, -1))      # inferred size will be 2 as 6 / 3 = 2
# tensor([[ 0.,  1.],
#         [ 2.,  3.],
#         [ 4.,  5.]])

print(x.view(-1, 6))      # inferred size will be 1 as 6 / 6 = 1
# tensor([[ 0.,  1.,  2.,  3.,  4.,  5.]])

print(x.view(1, -1, 2))   # inferred size will be 3 as 6 / (1 * 2) = 3
# tensor([[[ 0.,  1.],
#          [ 2.,  3.],
#          [ 4.,  5.]]])

# print(x.view(-1, 5))    # throw error as there's no int N so that 5 * N = 6
# RuntimeError: invalid argument 2: size '[-1 x 5]' is invalid for input with 6 elements

print(x.view(-1, -1, 3))  # throw error as only one dimension can be inferred
# RuntimeError: invalid argument 1: only one dimension can be inferred
Comment

view(-1 1) pytorch

view is similar to numpy's reshape
"view" shares the underlying data with the original tensor, so it is really
a view into the old tensor instead of creating a brand new one
Comment

PREVIOUS NEXT
Code Example
Python :: model o weight 
Python :: python insert 
Python :: python get memory address of variable 
Python :: subprocess print logs 
Python :: how to check python version on terminal 
Python :: python get response from url 
Python :: set cookie in chrome 
Python :: python regex get string before character 
Python :: erase % sign in row pandas 
Python :: How to get the value of an Entry widget in Tkinter? 
Python :: remove all instances from list python 
Python :: pandas xlsx to dataframe 
Python :: pandas read first column as index 
Python :: smtpauthenticationerror 
Python :: how to change a header in pandas 
Python :: mongodb aggregate count 
Python :: isnumeric python 
Python :: boto3 paginate 
Python :: left join outer apply 
Python :: with open python 
Python :: print only numbers from string python 
Python :: import sklearn.metrics from plot_confusion_matrix 
Python :: python Non-UTF-8 code starting with 
Python :: python datetime day of year 
Python :: python ordered dictionary 
Python :: how to play audio in python 
Python :: replace character in string python 
Python :: python input function 
Python :: generate new secret key django 
Python :: python copy variable 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =