-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Update tensor_tutorial.py #2911
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
data = [[1, 2], [3, 4]] | ||
x_data = torch.tensor(data) | ||
|
||
|
||
###################################################################### | ||
# **From a NumPy array** | ||
# | ||
|
@@ -51,6 +52,16 @@ | |
|
||
|
||
###################################################################### | ||
# **Tensor as Diagonal Matrix | ||
# | ||
# Like NumPy operations, the eye function creates a diagonal matrix of which the diagonal elements have ones and | ||
# off diagonal elements have zeros | ||
|
||
x_eye = torch.eye(3, 4) | ||
print(f"Diagonal Tensor: \n {x_eye} \n") | ||
|
||
###################################################################### | ||
# | ||
# **With random or constant values:** | ||
# | ||
# ``shape`` is a tuple of tensor dimensions. In the functions below, it determines the dimensionality of the output tensor. | ||
|
@@ -94,6 +105,33 @@ | |
# Tensor Operations | ||
# ~~~~~~~~~~~~~~~~~ | ||
# | ||
# **Check for a tensor** | ||
# | ||
# We can check whether an object in python is a tensor or not. Typically, is_tensor function checks | ||
# and is_storage function checkes whether an object is a pytorch storage object. | ||
x = [1, 2, 3, 4, 5] | ||
|
||
print(f"x is tensor: {torch.is_tensor(x)}\n") | ||
print(f"x is pytorch storage object: {torch.is_storage(x)}\n") | ||
|
||
y = torch.randn(1, 2, 3, 4, 5) | ||
print(f"y is tensor: {torch.is_tensor(y)}\n") | ||
print(f"y is pytorch storage object: {torch.is_storage(y)}\n") | ||
|
||
###################################################################### | ||
# Here x is not a tensor | ||
# while y is a tensor but it is not stored. | ||
# | ||
# **Count Elements in a Tensor** | ||
# | ||
# To check the total number of elements in an input tensor object, the numerical element function can be used. | ||
print(f"Total number of elements in tensor y: {torch.numel(y)}\n") | ||
# | ||
data = [[1, 2], [3, 4]] | ||
x_data = torch.tensor(data) | ||
print(f"Total number of elements in 2D tensor: {torch.numel(x_data)}\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I thought it would be good if we can reuse the previously defined data again but I think we should define x_data again so as to make it easy for the viewer. |
||
# | ||
# | ||
# Over 100 tensor operations, including transposing, indexing, slicing, | ||
# mathematical operations, linear algebra, random sampling, and more are | ||
# comprehensively described | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the
eye
operator is interesting here? What does it adds to the tutorial?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The eye operator is an interesting operator for tensors that shows there are operations defined in tensors also just like numpy with which one can create a diagonal matrix easily with a one line code.