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 :: linear search algorithm python 
Python :: using format str with variable 
Python :: python 3.9 32 bit 
Python :: code academy magic 8 bal code python 
Python :: looking up object address in python 
Python :: registration of the path django urls in the core app or main app 
Python :: lambda2 criterion python 
Python :: get channel name by channel id discord py 
Python :: OSError Traceback (most recent call last) <ipython-input-74-8920269c5588 in <module() 9 10 run_with_ngrok(app) --- 11 app.run() 
Python :: dataframe ggplot rownames order 
Python :: attach short list to pandas dataframe with filler 
Python :: Python NumPy broadcast_to() Function Syntax 
Python :: pathlib home 
Python :: get minimum value function with anealing in python 
Python :: data framing with Pandas 
Python :: Python NumPy require Function Example without requirements attribute 
Python :: how to change text in heatmap matplotlib 
Python :: tf idf vectorizer regression -logistic 
Python :: model compile keras 
Python :: NumPy rot90 Example Rotating Three times 
Python :: scipy kullbach leibler divergence 
Python :: django view - mixins and GenericAPIView (list or create - GET, POST) 
Python :: How to run a method before/after all class function calls with arguments passed? 
Python :: xampp python 
Python :: how to process numerical data machine learning 
Python :: python how do I count the time that it takes for the sorting to execute in seconds? [closed] 
Python :: groupby and add aggregated column 
Python :: how to plot graph between f1 score and random forest parameters 
Python :: cuenta atras segundero python 
Python :: Invenco Order Dict 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =