Skip to content

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

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions beginner_source/blitz/tensor_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)


######################################################################
# **From a NumPy array**
#
Expand All @@ -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)
Copy link
Contributor

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?

Copy link
Author

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.

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.
Expand Down Expand Up @@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is x_data suddenly here?

Copy link
Author

Choose a reason for hiding this comment

The 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.
Relevant code is changed

#
#
# Over 100 tensor operations, including transposing, indexing, slicing,
# mathematical operations, linear algebra, random sampling, and more are
# comprehensively described
Expand Down
Loading