A tensor is a multidimensional array that is used to store data. So to use a tensor, we have to import the torch module.

To create a tensor the method used is tensor().

**Syntax:**

torch.tensor(data)

Where data is a multi-dimensional array.

## torch.count_nonzero()

torch.count_nonzero() is used to return the total number of non-zero elements present in the tensor. It takes two parameters.

**Syntax:**

torch.count_nonzero(tensor_object,dim)

**Parameters:**

- The tensor is the input tensor.
- dim is to reduce the dimension. dim=0 specifies column comparison, which will get the total sum of non-zeros along a column, and dim=1 specifies row comparison, which will get the total sum of non-zeros along the row.

## Example 1:

In this example, we will create a tensor with two dimensions that has two rows and two columns and apply count_nonzero() on the rows.

import torch

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.tensor([[0,0],[1,0]])

#display

print(data)

print()

#get count of non zeros along rows

print(“Total number of Non zeros across rows:”)

print(torch.count_nonzero(data, dim=1))

**Output:**

[1, 0]])

Total number of Non zeros across rows:

tensor([0, 1])

We can see that the total number of nonzeros in the first row is 0 and in the second row is 1.

## Example 2:

In this example, we will create a tensor with two dimensions that has two rows and two columns and apply count_nonzero() on the columns.

import torch

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.tensor([[0,0],[1,0]])

#display

print(data)

print()

#get count of non zeros along columns

print(“Total number of Non zeros across columns:”)

print(torch.count_nonzero(data, dim=0))

**Output:**

[1, 0]])

Total number of Non zeros across columns:

tensor([1, 0])

We can see that the total number of nonzeros in the first column is 1 and in the second column is 0.

## Work with CPU

If you want to run the count_nonzero() function on the CPU, then we have to create a tensor with a cpu() function. This will run on a CPU machine.

At this time, when we are creating a tensor, we can use the cpu() function.

**Syntax:**

torch.tensor(data).cpu()

## Example 1:

In this example, we will create a tensor with two dimensions on the CPU that has two rows and two columns and apply count_nonzero() on rows.

import torch

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.tensor([[0,0],[1,0]]).cpu()

#display

print(data)

print()

#get count of non zeros along rows

print("Total number of Non zeros across rows:")

print(torch.count_nonzero(data, dim=1))

**Output:**

[1, 0]])

Total number of Non zeros across rows:

tensor([0, 1])

We can see that the total number of nonzeros in the first row is 0 and in the second row is 1.

## Example 2:

In this example, we will create a tensor with 2 dimensions on the CPU that has two rows and two columns and apply count_nonzero() on the columns.

import torch

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.tensor([[0,0],[1,0]]).cpu()

#display

print(data)

print()

#get count of non zeros along columns

print("Total number of Non zeros across columns:")

print(torch.count_nonzero(data, dim=0))

**Output:**

[1, 0]])

Total number of Non zeros across columns:

tensor([1, 0])

We can see that the total number of nonzeros in the first column is 1 and in the second column is 0.

## Conclusion

In this PyTorch lesson, we discussed the count_nonzero() function. It returns the total number of non-zero elements present in the tensor. We saw different examples and worked these examples on a CPU machine.