Bird
Raised Fist0
PyTorchml~5 mins

Tensor data types in PyTorch

Choose your learning style10 modes available
Introduction
Tensors hold numbers in PyTorch. Choosing the right data type helps your model work well and fast.
When you want to save memory by using smaller number types.
When you need precise calculations using floating-point numbers.
When working with whole numbers like labels or counts.
When you want to convert data between types for compatibility.
When you want to speed up training by using faster data types.
Syntax
PyTorch
torch.tensor(data, dtype=torch.<data_type>)
Use like float32, int64, etc., to specify the tensor's number type.
If you don't set dtype, PyTorch picks a default based on your data.
Examples
Creates a tensor of floating-point numbers with 32-bit precision.
PyTorch
import torch
x = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32)
Creates a tensor of 64-bit integers, good for indexing or labels.
PyTorch
y = torch.tensor([1, 2, 3], dtype=torch.int64)
Creates a tensor of boolean values.
PyTorch
z = torch.tensor([True, False, True], dtype=torch.bool)
Sample Model
This program shows how to create tensors with float, int, and bool data types and prints their values and types.
PyTorch
import torch

# Create tensors with different data types
float_tensor = torch.tensor([1.5, 2.5, 3.5], dtype=torch.float32)
int_tensor = torch.tensor([1, 2, 3], dtype=torch.int64)
bool_tensor = torch.tensor([True, False, True], dtype=torch.bool)

# Print tensor details
print(f"Float tensor: {float_tensor}, dtype: {float_tensor.dtype}")
print(f"Int tensor: {int_tensor}, dtype: {int_tensor.dtype}")
print(f"Bool tensor: {bool_tensor}, dtype: {bool_tensor.dtype}")
OutputSuccess
Important Notes
Using float32 is common for neural networks because it balances speed and precision.
Integer tensors are often used for labels or counting, not for calculations.
Boolean tensors are useful for masks or conditions in your data.
Summary
Tensors store numbers with specific data types in PyTorch.
Choosing the right data type helps your model run efficiently.
You can create tensors with float, int, or bool types using the dtype argument.

Practice

(1/5)
1. What is the default data type of a tensor created by torch.tensor([1.0, 2.0, 3.0]) in PyTorch?
easy
A. torch.bool
B. torch.int32
C. torch.float32
D. torch.float64

Solution

  1. Step 1: Understand default tensor creation

    When you create a tensor with floating point numbers without specifying dtype, PyTorch uses torch.float32 by default.
  2. Step 2: Check the given tensor values

    The values are floats (1.0, 2.0, 3.0), so the default dtype is torch.float32.
  3. Final Answer:

    torch.float32 -> Option C
  4. Quick Check:

    Default float tensor dtype = torch.float32 [OK]
Hint: Floats default to torch.float32 unless specified otherwise [OK]
Common Mistakes:
  • Assuming default is torch.float64
  • Thinking integers default to float
  • Confusing bool with float
2. Which of the following is the correct way to create an integer tensor in PyTorch?
easy
A. torch.tensor([1, 2, 3], dtype=torch.float32)
B. torch.tensor([1, 2, 3], dtype=torch.string)
C. torch.tensor([1, 2, 3], dtype=torch.bool)
D. torch.tensor([1, 2, 3], dtype=torch.int64)

Solution

  1. Step 1: Identify integer data types in PyTorch

    PyTorch uses torch.int64 (also called LongTensor) for integer tensors by default.
  2. Step 2: Check the dtype argument in options

    torch.tensor([1, 2, 3], dtype=torch.int64) uses dtype=torch.int64 which is correct for integer tensors.
  3. Final Answer:

    torch.tensor([1, 2, 3], dtype=torch.int64) -> Option D
  4. Quick Check:

    Integer tensor dtype = torch.int64 [OK]
Hint: Use dtype=torch.int64 for integer tensors [OK]
Common Mistakes:
  • Using float32 for integers
  • Using invalid dtype like torch.string
  • Confusing bool with int
3. What will be the output dtype of the following code?
import torch
t = torch.tensor([0, 1, 2], dtype=torch.bool)
t = t.to(torch.float32)
print(t.dtype)
medium
A. torch.float32
B. torch.int64
C. torch.bool
D. torch.float64

Solution

  1. Step 1: Understand initial tensor dtype

    The tensor is created with dtype=torch.bool, so initially it stores boolean values.
  2. Step 2: Conversion using .to(torch.float32)

    The tensor is converted to float32 type using the .to() method.
  3. Final Answer:

    torch.float32 -> Option A
  4. Quick Check:

    Conversion changes dtype to torch.float32 [OK]
Hint: Use .to() to change tensor dtype easily [OK]
Common Mistakes:
  • Assuming dtype stays bool after conversion
  • Confusing .to() with .type()
  • Expecting int64 after conversion
4. Identify the error in this PyTorch code snippet:
import torch
t = torch.tensor([1, 0, 1], dtype=torch.bool)
print(t + 1)
medium
A. No error, output will be tensor([2, 1, 2], dtype=torch.int64)
B. Syntax error in tensor creation
C. Cannot add integer to a bool tensor directly
D. Runtime error due to unsupported operation

Solution

  1. Step 1: Check tensor dtype and operation

    The tensor is boolean, but PyTorch allows arithmetic with bool tensors by promoting them to int.
  2. Step 2: Calculate the addition result

    Adding 1 to each bool value (True=1, False=0) results in tensor([2, 1, 2]) with dtype promoted to int64.
  3. Final Answer:

    No error, output will be tensor([2, 1, 2], dtype=torch.int64) -> Option A
  4. Quick Check:

    Bool + int promotes to int tensor [OK]
Hint: Bool tensors convert to int when used in math ops [OK]
Common Mistakes:
  • Thinking addition with bool causes error
  • Expecting output dtype to remain bool
  • Confusing syntax error with runtime behavior
5. You want to create a tensor of shape (3, 3) filled with zeros but with boolean type. Which code snippet correctly does this?
hard
A. torch.zeros((3, 3), dtype=torch.int32)
B. torch.zeros((3, 3), dtype=torch.bool)
C. torch.zeros((3, 3))
D. torch.zeros((3, 3), dtype=torch.float32)

Solution

  1. Step 1: Understand tensor creation with zeros

    torch.zeros creates a tensor filled with zeros; specifying dtype sets the data type.
  2. Step 2: Choose correct dtype for boolean tensor

    To get a boolean tensor, use dtype=torch.bool as in torch.zeros((3, 3), dtype=torch.bool).
  3. Final Answer:

    torch.zeros((3, 3), dtype=torch.bool) -> Option B
  4. Quick Check:

    Zeros tensor with dtype bool = torch.zeros((3, 3), dtype=torch.bool) [OK]
Hint: Use dtype=torch.bool with torch.zeros for boolean tensors [OK]
Common Mistakes:
  • Omitting dtype and getting float tensor
  • Using int32 instead of bool
  • Confusing default dtype with bool
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard