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.
Tensor basics (creation, shapes, dtypes) in TensorFlow
Start learning this pattern below
Jump into concepts and practice - no test required
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.
tensor1 = tf.constant(5) # A scalar (single number)
tensor2 = tf.constant([1, 2, 3]) # A 1D tensor (vector)
tensor3 = tf.constant([[1, 2], [3, 4]]) # A 2D tensor (matrix)
tensor4 = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32) # Tensor with float data type
This program creates four tensors of different shapes and types, then prints their shapes and data types to show how to check these properties.
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)
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.
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
Solution
Step 1: Understand the definition of a tensor
A tensor is a generalization of arrays to multiple dimensions, used to hold data in TensorFlow.Step 2: Identify the correct description
Among the options, only a multi-dimensional array matches the tensor concept.Final Answer:
A multi-dimensional array that holds data -> Option DQuick Check:
Tensor = multi-dimensional array [OK]
- Confusing tensors with functions or layers
- Thinking tensors are only 2D matrices
- Mixing tensors with visualization tools
Solution
Step 1: Recall TensorFlow tensor creation functions
The correct function to create a constant tensor is tf.constant().Step 2: Check each option
Only tf.constant([1, 2, 3]) is a valid TensorFlow syntax for creating a constant tensor.Final Answer:
tf.constant([1, 2, 3]) -> Option CQuick Check:
Use tf.constant() to create tensors [OK]
- Using non-existent functions like tf.tensor or tf.array
- Confusing tensor creation with numpy arrays
- Misspelling function names
import tensorflow as tf x = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
Solution
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.Step 2: Match shape and dtype with options
Shape: (2, 2), dtype: float32 matches shape (2, 2) and dtype float32 exactly.Final Answer:
Shape: (2, 2), dtype: float32 -> Option BQuick Check:
Shape and dtype match code [OK]
- Assuming default dtype int32 instead of float32
- Confusing shape with flattened size
- Mixing up float32 and float64
import tensorflow as tf x = tf.constant([1, 2, 3], dtype=tf.string)
Solution
Step 1: Understand dtype compatibility with values
The list contains integers but dtype is set to tf.string, which causes a type mismatch error.Step 2: Identify the error cause
tf.constant cannot convert integers to strings automatically when dtype=tf.string is specified.Final Answer:
tf.constant does not support dtype=tf.string for numeric values -> Option AQuick Check:
Value types must match dtype [OK]
- Assuming automatic conversion between int and string
- Thinking nested lists fix dtype mismatch
- Ignoring error messages about dtype
Solution
Step 1: Check the required shape and dtype
The tensor must have shape (3, 1) and dtype int64, filled with 7.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.Final Answer:
tf.constant(7, shape=(3, 1), dtype=tf.int64) -> Option AQuick Check:
Shape and dtype match with scalar fill [OK]
- Using wrong dtype like int32 instead of int64
- Creating list instead of shaped tensor
- Using tf.fill without dtype specified
