Skip to content
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

Update tensor_tutorial.py #2911

Closed
wants to merge 4 commits into from
Closed

Conversation

akhil-maker
Copy link

@akhil-maker akhil-maker commented Jun 7, 2024

Fixes #2899

Description

In my suggestion, we should add some more functionalities related to tensors to the tensors' tutorial. Thus, I have added some more functions and operations that one can perform on tensors.

Checklist

  • The issue that is being fixed is referred in the description (see above "Fixes #ISSUE_NUMBER")
  • Only one issue is addressed in this pull request
  • Labels from the issue that this PR is fixing are added to this pull request
  • No unnecessary issues are included into this pull request.

cc @albanD @sekyondaMeta @svekars @kit1980 @brycebortree

Copy link

pytorch-bot bot commented Jun 7, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/2911

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 61ff89f with merge base 6537199 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link
Contributor

Hi @akhil-maker!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@svekars svekars added the core Tutorials of any level of difficulty related to the core pytorch functionality label Jun 7, 2024
@brycebortree brycebortree requested a review from subramen June 7, 2024 16:22
# 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
Member

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.

# **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 stored as tensor object.
Copy link
Member

Choose a reason for hiding this comment

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

is_storage function checkes whether an object is stored as tensor object

This doesn't sound correct to me.

Copy link
Author

Choose a reason for hiding this comment

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

I have changed it, referring to the doc in
https://pytorch.org/docs/stable/generated/torch.is_storage.html

x = [1, 2, 3, 4, 5]

print(f"x is tensor: {torch.is_tensor(x)}\n")
print(f"x is tensor: {torch.is_storage(x)}\n")
Copy link
Member

Choose a reason for hiding this comment

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

copy-pasted not relevant x is tensor

Copy link
Author

Choose a reason for hiding this comment

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

Performed relevant changes

print(f"x is tensor: {torch.is_storage(x)}\n")

y = torch.randn(1, 2, 3, 4, 5)
print(f"x is tensor: {torch.is_tensor(y)}\n")
Copy link
Member

Choose a reason for hiding this comment

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

copy-pasted not relevant x is tensor

Copy link
Author

Choose a reason for hiding this comment

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

Relevant changes are completed


y = torch.randn(1, 2, 3, 4, 5)
print(f"x is tensor: {torch.is_tensor(y)}\n")
print(f"x is tensor: {torch.is_storage(y)}\n")
Copy link
Member

Choose a reason for hiding this comment

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

copy-pasted not relevant x is 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")
print(f"Total number of elements in 2D tensor: {torch.numel(x_data)}\n")
Copy link
Member

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

Copy link
Member

@kit1980 kit1980 left a comment

Choose a reason for hiding this comment

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

The changes don't make sense to me. Not clear what value it adds to the tutorial. The storage explanations look misleading and out of place.

@akhil-maker
Copy link
Author

Changes are made to improve the tensors tutorial by adding some more operations and functions. Certain functions are added to show an ease in creating tensors of different types (diagonal matrix). The storage explanation is changed as per the doc referred from pytorch docs official site.
I hope that it will add to the tensors tutorial more.
If more functions are needed to add, we can add there which will make the tensors tutorial more helpful for viewers to learn.

@akhil-maker
Copy link
Author

@subramen Please review and give your feedbacks

@akhil-maker
Copy link
Author

@kit1980 Please review the updated changes
Changes are also made as per your comments and reviews
Thanks!

@svekars
Copy link
Contributor

svekars commented Jun 10, 2024

@akhil-maker - can you please see my comment on the issue: #2899

@akhil-maker
Copy link
Author

@svekars - I have checked your comment on the issue #2899 and made the comment.
Found everything correct and everything is running fine without any break. Also attached output in the comment of the issue there.
Thanks!

@svekars svekars closed this Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed core Tutorials of any level of difficulty related to the core pytorch functionality docathon-h1-2024 easy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test the Tensors tutorial
4 participants