Bird
Raised Fist0
TensorFlowml~5 mins

Tensor basics (creation, shapes, dtypes) in TensorFlow

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Tensors are like containers that hold numbers in machine learning. Knowing how to create them, check their shape, and type helps us organize and use data correctly.

When you want to store data like images or numbers for a machine learning model.
When you need to check the size or dimensions of your data before training.
When you want to make sure your data is the right type (like integers or decimals) for calculations.
When you want to create simple data examples to test your model.
When you want to convert lists or arrays into a format TensorFlow understands.
Syntax
TensorFlow
import tensorflow as tf

# Create a tensor
tensor = tf.constant([1, 2, 3], dtype=tf.int32)

# Check shape
shape = tensor.shape

# Check data type
dtype = tensor.dtype

tf.constant creates a tensor from data you give it.

shape tells you the size and dimensions of the tensor.

dtype tells you the data type of the tensor.

Examples
This creates a single number tensor, called a scalar.
TensorFlow
tensor1 = tf.constant(5)
# A scalar (single number)
This creates a list of numbers, which is a 1-dimensional tensor.
TensorFlow
tensor2 = tf.constant([1, 2, 3])
# A 1D tensor (vector)
This creates a 2-dimensional tensor like a table with rows and columns.
TensorFlow
tensor3 = tf.constant([[1, 2], [3, 4]])
# A 2D tensor (matrix)
This creates a tensor with decimal numbers (floats).
TensorFlow
tensor4 = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
# Tensor with float data type
Sample Model

This program creates four tensors of different shapes and types, then prints their shapes and data types to show how to check these properties.

TensorFlow
import tensorflow as tf

# Create different tensors
scalar = tf.constant(10)
vector = tf.constant([1, 2, 3])
matrix = tf.constant([[1, 2], [3, 4]])
float_tensor = tf.constant([1.5, 2.5, 3.5], dtype=tf.float32)

# Print shapes
print('Scalar shape:', scalar.shape)
print('Vector shape:', vector.shape)
print('Matrix shape:', matrix.shape)
print('Float tensor shape:', float_tensor.shape)

# Print data types
print('Scalar dtype:', scalar.dtype)
print('Vector dtype:', vector.dtype)
print('Matrix dtype:', matrix.dtype)
print('Float tensor dtype:', float_tensor.dtype)
OutputSuccess
Important Notes

A scalar tensor has no dimensions, shown as an empty shape ().

Use tf.constant to create tensors from Python lists or numbers.

Data types like tf.int32 or tf.float32 tell TensorFlow how to store numbers.

Summary

Tensors hold data in machine learning and can have different shapes like scalars, vectors, or matrices.

You can create tensors using tf.constant and specify their data type.

Checking a tensor's shape and data type helps you understand and use your data correctly.

Practice

(1/5)
1. What is a tensor in TensorFlow?
easy
A. A type of neural network layer
B. A function to train models
C. A tool to visualize data
D. A multi-dimensional array that holds data

Solution

  1. Step 1: Understand the definition of a tensor

    A tensor is a generalization of arrays to multiple dimensions, used to hold data in TensorFlow.
  2. Step 2: Identify the correct description

    Among the options, only a multi-dimensional array matches the tensor concept.
  3. Final Answer:

    A multi-dimensional array that holds data -> Option D
  4. Quick Check:

    Tensor = multi-dimensional array [OK]
Hint: Remember: tensors store data in arrays of any dimension [OK]
Common Mistakes:
  • Confusing tensors with functions or layers
  • Thinking tensors are only 2D matrices
  • Mixing tensors with visualization tools
2. Which of the following is the correct way to create a constant tensor with values [1, 2, 3] in TensorFlow?
easy
A. tf.array([1, 2, 3])
B. tf.tensor([1, 2, 3])
C. tf.constant([1, 2, 3])
D. tf.create_tensor([1, 2, 3])

Solution

  1. Step 1: Recall TensorFlow tensor creation functions

    The correct function to create a constant tensor is tf.constant().
  2. Step 2: Check each option

    Only tf.constant([1, 2, 3]) is a valid TensorFlow syntax for creating a constant tensor.
  3. Final Answer:

    tf.constant([1, 2, 3]) -> Option C
  4. Quick Check:

    Use tf.constant() to create tensors [OK]
Hint: Use tf.constant() to create fixed tensors [OK]
Common Mistakes:
  • Using non-existent functions like tf.tensor or tf.array
  • Confusing tensor creation with numpy arrays
  • Misspelling function names
3. What will be the shape and dtype of the tensor created by the code below?
import tensorflow as tf
x = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
medium
A. Shape: (2,), dtype: int32
B. Shape: (2, 2), dtype: float32
C. Shape: (4,), dtype: float64
D. Shape: (2, 2), dtype: int64

Solution

  1. Step 1: Analyze the tensor creation code

    The tensor is created from a list of lists [[1, 2], [3, 4]], so it has 2 rows and 2 columns, shape (2, 2). The dtype is explicitly set to tf.float32.
  2. Step 2: Match shape and dtype with options

    Shape: (2, 2), dtype: float32 matches shape (2, 2) and dtype float32 exactly.
  3. Final Answer:

    Shape: (2, 2), dtype: float32 -> Option B
  4. Quick Check:

    Shape and dtype match code [OK]
Hint: Shape matches nested list dimensions; dtype is as specified [OK]
Common Mistakes:
  • Assuming default dtype int32 instead of float32
  • Confusing shape with flattened size
  • Mixing up float32 and float64
4. The following code throws an error. What is the cause?
import tensorflow as tf
x = tf.constant([1, 2, 3], dtype=tf.string)
medium
A. tf.constant does not support dtype=tf.string for numeric values
B. The list must be nested for tf.string dtype
C. dtype argument is misspelled
D. No error; code runs fine

Solution

  1. Step 1: Understand dtype compatibility with values

    The list contains integers but dtype is set to tf.string, which causes a type mismatch error.
  2. Step 2: Identify the error cause

    tf.constant cannot convert integers to strings automatically when dtype=tf.string is specified.
  3. Final Answer:

    tf.constant does not support dtype=tf.string for numeric values -> Option A
  4. Quick Check:

    Value types must match dtype [OK]
Hint: Ensure values match dtype to avoid errors [OK]
Common Mistakes:
  • Assuming automatic conversion between int and string
  • Thinking nested lists fix dtype mismatch
  • Ignoring error messages about dtype
5. You want to create a tensor with shape (3, 1) filled with the value 7 and dtype int64. Which code snippet correctly does this?
hard
A. tf.constant(7, shape=(3, 1), dtype=tf.int64)
B. tf.constant([7]*3, dtype=tf.int64)
C. tf.fill((3, 1), 7)
D. tf.constant([[7], [7], [7]], dtype=tf.int32)

Solution

  1. Step 1: Check the required shape and dtype

    The tensor must have shape (3, 1) and dtype int64, filled with 7.
  2. Step 2: Evaluate each option

    tf.constant(7, shape=(3, 1), dtype=tf.int64) uses tf.constant with scalar 7, shape (3, 1), and dtype tf.int64, which matches all requirements exactly.
  3. Final Answer:

    tf.constant(7, shape=(3, 1), dtype=tf.int64) -> Option A
  4. Quick Check:

    Shape and dtype match with scalar fill [OK]
Hint: Use tf.constant with shape and dtype for scalar fill [OK]
Common Mistakes:
  • Using wrong dtype like int32 instead of int64
  • Creating list instead of shaped tensor
  • Using tf.fill without dtype specified