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

[Term Entry] PyTorch Tensor Operations: .is_nonzero() #6090

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

raghav-97
Copy link
Contributor

Description

Added new documentation for PyTorch's is_nonzero() method under docs/content/pytorch/concepts/tensor-operations/terms/is-nonzero/is-nonzero.md

Issue Solved

[Term Entry] PyTorch Tensor Operations: .is_nonzero() #6069

Type of Change

  • Adding a new entry

Checklist

  • All writings are my own.
  • My entry follows the Codecademy Docs style guide.
  • My changes generate no new warnings.
  • I have performed a self-review of my own writing and code.
  • I have checked my entry and corrected any misspellings.
  • I have made corresponding changes to the documentation if needed.
  • I have confirmed my changes are not being pushed from my forked main branch.
  • I have confirmed that I'm pushing from a new branch named after the changes I'm making.
  • I have linked any issues that are relevant to this PR in the Issues Solved section.

@mamtawardhani mamtawardhani self-assigned this Feb 4, 2025
@mamtawardhani mamtawardhani added new entry New entry or entries status: under review Issue or PR is currently being reviewed pytorch PyTorch labels Feb 4, 2025
@mamtawardhani mamtawardhani linked an issue Feb 5, 2025 that may be closed by this pull request
3 tasks
Copy link
Collaborator

@mamtawardhani mamtawardhani left a comment

Choose a reason for hiding this comment

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

Hey @raghav-97 , thank you for contributing to Codecademy Docs, the entry is nicely written! 😄

I've suggested a few changes, could you please review and modify those at your earliest convenience? Thank you! 😃

Comment on lines 2 to 14
Title: '.is_nonzero()'
Description: 'Checks if all elements of a tensor are non-zero. Returns a boolean value indicating whether the tensor contains any non-zero elements.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Data Structures'
- 'Functions'
- 'Boolean'
- 'Tensors'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/computer-science'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Title: '.is_nonzero()'
Description: 'Checks if all elements of a tensor are non-zero. Returns a boolean value indicating whether the tensor contains any non-zero elements.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Data Structures'
- 'Functions'
- 'Boolean'
- 'Tensors'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/computer-science'
Title: '.is_nonzero()'
Description: 'Checks if a tensor's single element is non-zero, returning a boolean indicating whether the element is non-zero.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Boolean'
- 'Data Structures'
- 'Functions'
- 'Tensor'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/computer-science'

- 'paths/computer-science'
---

In PyTorch, the **`.is_nonzero()`** method is used to check if a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) contains any non-zero elements. It returns a boolean value (True or False) indicating whether the tensor has at least one element that is not zero. This method is particularly useful for conditional checks or validation in tensor operations.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
In PyTorch, the **`.is_nonzero()`** method is used to check if a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) contains any non-zero elements. It returns a boolean value (True or False) indicating whether the tensor has at least one element that is not zero. This method is particularly useful for conditional checks or validation in tensor operations.
In PyTorch, the **`.is_nonzero()`** method is used to check if a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) contains a non-zero element. It returns `True` if the tensor's element is non-zero and `False` if it is zero. This method is particularly useful for conditional checks or validation in tensor operations.

## Syntax

```pseudo
Tensor.is_nonzero()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Tensor.is_nonzero()
torch.is_nonzero(input)

Copy link
Collaborator

Choose a reason for hiding this comment

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

For the correct syntax, please refer to - https://pytorch.org/docs/stable/generated/torch.is_nonzero.html

Comment on lines 25 to 28
The method takes no parameters and returns:

- `True` if the tensor has exactly one element and that element is non-zero
- `False` in all other cases (multiple elements, single zero element, or empty tensor)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
The method takes no parameters and returns:
- `True` if the tensor has exactly one element and that element is non-zero
- `False` in all other cases (multiple elements, single zero element, or empty tensor)
- `input`: tensor with a single element. The method checks if this single element is non-zero.

Comment on lines 33 to 47
import torch

# Define a tensor with non-zero elements
tensor1 = torch.tensor([0.0, 1.0, 2.0])

# Check if the tensor contains any non-zero elements
result1 = tensor1.is_nonzero()
print(result1) # Output: True

# Define a tensor with all zero elements
tensor2 = torch.tensor([0.0, 0.0, 0.0])

# Check if the tensor contains any non-zero elements
result2 = tensor2.is_nonzero()
print(result2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
import torch
# Define a tensor with non-zero elements
tensor1 = torch.tensor([0.0, 1.0, 2.0])
# Check if the tensor contains any non-zero elements
result1 = tensor1.is_nonzero()
print(result1) # Output: True
# Define a tensor with all zero elements
tensor2 = torch.tensor([0.0, 0.0, 0.0])
# Check if the tensor contains any non-zero elements
result2 = tensor2.is_nonzero()
print(result2)
import torch
# Define a tensor with a non-zero element
tensor1 = torch.tensor(5.0)
# Check if the tensor contains a non-zero element
result1 = torch.is_nonzero(tensor1)
print(result1)
# Define a tensor with a zero element
tensor2 = torch.tensor(0.0)
# Check if the tensor contains a non-zero element
result2 = torch.is_nonzero(tensor2)
print(result2)

@raghav-97
Copy link
Contributor Author

Hey @mamtawardhani thank you for the review. I have made the suggested changes, could you please look into it and also let me know if any more changes are required.

@mamtawardhani
Copy link
Collaborator

Hey @mamtawardhani thank you for the review. I have made the suggested changes, could you please look into it and also let me know if any more changes are required.

Hey @raghav-97 the changes are not committed, could you please check again?

@raghav-97
Copy link
Contributor Author

@mamtawardhani I made the changes and tried testing them locally using yarn test, but getting error and I am unable to figure it out. Can you guide me where can I refer to in order to solve this?
Thank you

Copy link
Collaborator

@mamtawardhani mamtawardhani left a comment

Choose a reason for hiding this comment

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

Thank you for contributing to Codecademy Docs @raghav-97 😄
The entry looks good for a second round of review! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new entry New entry or entries pytorch PyTorch status: review 1️⃣ completed status: under review Issue or PR is currently being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Term Entry] PyTorch Tensor Operations: .is_nonzero()
3 participants