TensorFlow
Tensor
Variable
Machine Learning
Deep Learning

What's the difference between Tensor and Variable in Tensorflow

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

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 .

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.