Introduction
TensorFlow is a comprehensive platform for building and deploying machine learning models with efficient computation graphs.
Tensors
import tensorflow as tf
# Constant tensor
scalar = tf.constant(5)
vector = tf.constant([1, 2, 3])
matrix = tf.constant([[1, 2], [3, 4]])
# Variable (trainable)
var = tf.Variable(tf.random.normal([10, 10]))
# Data types
float32_tensor = tf.constant([1.0], dtype=tf.float32)
int32_tensor = tf.constant([1], dtype=tf.int32)
Eager Execution
# Eager execution is default in TF2
a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 6], [7, 8]])
result = tf.matmul(a, b)
print(result) # Immediate evaluation
tf.function
@tf.function
def matmul(a, b):
return tf.matmul(a, b)
# Compiled to graph for faster execution
result = matmul(a, b)
Autograd
x = tf.Variable(3.0)
with tf.GradientTape() as tape:
y = x**2
gradient = tape.gradient(y, x)
print(gradient) # tf.Tensor(6.0, shape=(), dtype=float32)
# For trainable variables
w = tf.Variable(tf.random.normal([10, 5]))
b = tf.Variable(tf.zeros([5]))
with tf.GradientTape() as tape:
y = tf.matmul(x, w) + b
loss = tf.reduce_mean(y**2)
grads = tape.gradient(loss, [w, b])
GPU Acceleration
# Check GPU availability
print(tf.config.list_physical_devices('GPU'))
# Move tensor to GPU
if tf.config.list_physical_devices('GPU'):
with tf.device('/GPU:0'):
a = tf.constant([[1, 2], [3, 4]])
result = a * 2
Practice Problems
- Create tensors of different shapes
- Use tf.function for optimization
- Compute gradients
- Move tensors to GPU
- Use TensorFlow Hub