TensorFlow
Tensor
Variable
Machine Learning
Deep Learning
What's the difference between Tensor and Variable in Tensorflow
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In TensorFlow, understanding the distinction between Tensor
and Variable
is crucial for efficiently designing and implementing machine learning models. Both of these components are essential in building computational graphs, but they serve different purposes. This article delves into the technical differences between Tensors and Variables and provides examples to clarify their roles and behaviors within TensorFlow.
Tensors in TensorFlow
Definition and Characteristics
- Static Type and Shape: Tensors are immutable data structures, meaning once created, the content cannot be modified. They possess a fixed shape and data type, such as integer, float, or string.
- Dimensionality: Tensors are characterized by a rank that denotes their number of dimensions (e.g., scalar, vector, matrix).
- Usage: They are primarily used to represent inputs and outputs for functions within the graph and intermediate data processing.
Example of Tensor Creation
- Rank: The number of indices required to uniquely identify an element in
T. - Shape: Defined by an n-dimensional integer array denoting the size of the tensor in each dimension.
- Mutable State: Unlike tensors, variables are mutable, meaning their values can be changed. Variables are meant to hold and persist state across the executions of the graph.
- Trainable Parameters: They are often used to store parameters, like weights and biases, which are optimized during training.
- Initialization and Scope: Variables need explicit initialization. They can be scoped and reused with
tf.variable_scope.

