Your First TensorFlow Program
Your First TensorFlow Program
Theory solidifies the moment you run real code. This section introduces the two most fundamental objects in TensorFlow: tf.constant and tf.Variable and shows how they behave in practice, including how tf.Variable participates in automatic differentiation via GradientTape.
Hello World with TensorFlow
In machine learning, the 'Hello, World!' equivalent is not printing text; it is performing a tensor operation. The official TensorFlow customisation basics guide demonstrates this pattern as the canonical starting point:
import tensorflow as tf
# Basic operations — TensorFlow converts Python types automatically
print(tf.math.add(1, 2)) # tf.Tensor(3, shape=(), dtype=int32)
print(tf.math.add([1, 2], [3, 4])) # tf.Tensor([4 6], shape=(2,), ...)
print(tf.math.square(5)) # tf.Tensor(25, shape=(), ...)
print(tf.math.reduce_sum([1, 2, 3])) # tf.Tensor(6, shape=(), ...)
# Operator overloading works naturally
print(tf.math.square(2) + tf.math.square(3)) # tf.Tensor(13, ...)
# Matrix multiplication — the backbone of neural networks
a = tf.constant([[1.0, 2.0],
[3.0, 4.0]])
b = tf.constant([[5.0, 6.0],
[7.0, 8.0]])
print(tf.matmul(a, b))
# tf.Tensor([[19. 22.]
[43. 50.]], shape=(2, 2), dtype=float32)
Understanding tf.constant and tf.Variable
According to the official TensorFlow basics guide, tf.Tensor objects are immutable, and to store model weights or other mutable state, you use tf.Variable. This distinction is fundamental — it determines what can be trained and what cannot:
- tf.constant: immutable; value is fixed at creation. Use for inputs, labels, and any data that should not change during computation.
- tf.Variable: mutable; value can be updated with assign(), assign_add(), and assign_sub(). Use for model weights and biases that TensorFlow updates during backpropagation.
import tensorflow as tf
# tf.constant: immutable
lr = tf.constant(0.001, dtype=tf.float32)
print(lr) # tf.Tensor(0.001, shape=(), dtype=float32)
# Attempting to assign to a constant raises an AttributeError
# lr.assign(0.01) ← this would fail
# tf.Variable: mutable
weight = tf.Variable(0.5, dtype=tf.float32)
print(weight) # <tf.Variable 'Variable:0' shape=() dtype=float32>
weight.assign(0.7) # set to a new value
weight.assign_add(0.1) # add in-place → 0.8
weight.assign_sub(0.05) # subtract → 0.75
print(weight.numpy()) # 0.75
Automatic differentiation with GradientTape
# GradientTape records operations on Variables for backpropagation
x = tf.Variable(1.0)
def f(x):
return x**2 + 2*x - 5
with tf.GradientTape() as tape:
y = f(x)
grad = tape.gradient(y, x) # dy/dx = 2x + 2
print(grad.numpy()) # 4.0 (at x=1: 2*1 + 2 = 4)
Why GradientTape Matters
GradientTape is the engine behind neural network training. During a forward pass, it records every operation on Variables. During the backward pass, it replays those operations in reverse to compute gradients, exactly what model.fit() does automatically for you behind the scenes.










