Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

How to Create a Boolean Tensor in PyTorch

Home How to Create a Boolean Tensor in PyTorch
How to Create a Boolean Tensor in PyTorch
  • Written by krunallathiya21
  • August 21, 2025
  • 0 Com
PyTorch

To create a Boolean tensor efficiently in PyTorch, use the torch.tensor() method and pass the dtype=torch.bool explicitly (inferred if data is bools), where each element holds a True or False value.

Creating a Boolean Tensor in PyTorch
import torch

bool_tensor = torch.tensor([True, False, True], dtype=torch.bool)

print(bool_tensor)

# Output: tensor([ True, False,  True], dtype=torch.bool)

You can use this boolean tensor for masking, condition checks, logical operations, or indexing.

Tensor initialization methods

Generating All False using torch.zeros()

Generating All False using torch.zeros()

If you want to create a Boolean tensor where each value is False, you can use torch.zeros() method.

import torch

bool_false = torch.zeros(3, dtype=torch.bool)

print(bool_false)

# Output: tensor([False, False, False])

In this code, we created a Boolean tensor with three elements, all of which are False.

Generating All True using torch.ones()

Generating All True using torch.ones() If you want to create a Boolean tensor where each value is True, you can use torch.ones() method.
import torch

bool_true = torch.ones(3, dtype=torch.bool)

print(bool_true)

# Output: tensor([ True,  True,  True])
In this code, we created a Boolean tensor with three elements, all of which are True.

Fill with a value using torch.full()

Fill with a value using torch.full() Using torch.full() method, you can create a Boolean tensor of either True values or False.
import torch

bool_full_true = torch.full((4,), True, dtype=torch.bool)

print(bool_full_true)

# Output: tensor([True, True, True, True])

bool_full_false = torch.full((4,), False, dtype=torch.bool)

print(bool_full_false)

# Output: tensor([False, False, False, False])

In this code, we created both True and False-valued tensors.

That’s all!
Post Views: 31
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
image
image
image
image
image
logo

Address: 121 King Street, Melbourne Victoria 3000 Australia.

hamela@example.com

+36 (0) 1779 228 338..

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact
SUPPORT
  • Content Strategy
  • Copywriting
  • Content Marketing
  • Web Design
QUICK LINKS
  • Marketplace
  • Documentation
  • Customers
  • Carrers
INSTAGRAM

Copyright by @Themesflat  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS