Get started with TensorFlow and learn the fundamentals
TensorFlow is Google's open-source machine learning framework that makes it easy to build, train, and deploy neural networks. Originally developed for internal use at Google, it's now the most popular deep learning framework in the world.
TensorFlow provides a comprehensive ecosystem of tools, libraries, and community resources that lets you easily build and deploy ML-powered applications. Whether you're a researcher, developer, or student, TensorFlow makes machine learning accessible and scalable.
Let's get TensorFlow installed on your system. We'll use Python and pip for the installation.
First, make sure you have Python 3.7-3.11 installed:
python --version
# or
python3 --version
Install TensorFlow using pip:
# Install TensorFlow
pip install tensorflow
# Or if you're using Python 3 specifically
pip3 install tensorflow
We'll also need some supporting libraries:
# Install additional libraries we'll use
pip install numpy pandas matplotlib jupyter
Let's start with a simple "Hello, TensorFlow!" program to verify everything is working correctly.
import tensorflow as tf
import numpy as np
# Print TensorFlow version
print("TensorFlow version:", tf.__version__)
# Create a simple tensor
hello = tf.constant("Hello, TensorFlow!")
print(hello)
# Create some numeric tensors
a = tf.constant(3.0)
b = tf.constant(4.0)
c = tf.add(a, b)
print(f"a = {a}")
print(f"b = {b}")
print(f"a + b = {c}")
The fundamental data structure in TensorFlow is the tensor. A tensor is a multi-dimensional array similar to NumPy arrays, but with additional capabilities for GPU acceleration and automatic differentiation.
# Different types of tensors
import tensorflow as tf
# Scalar (0-dimensional tensor)
scalar = tf.constant(42)
print("Scalar:", scalar)
print("Shape:", scalar.shape)
print("Rank:", tf.rank(scalar))
# Vector (1-dimensional tensor)
vector = tf.constant([1, 2, 3, 4])
print("\nVector:", vector)
print("Shape:", vector.shape)
print("Rank:", tf.rank(vector))
# Matrix (2-dimensional tensor)
matrix = tf.constant([[1, 2], [3, 4], [5, 6]])
print("\nMatrix:", matrix)
print("Shape:", matrix.shape)
print("Rank:", tf.rank(matrix))
# 3D Tensor
tensor_3d = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("\n3D Tensor:", tensor_3d)
print("Shape:", tensor_3d.shape)
print("Rank:", tf.rank(tensor_3d))
# Basic tensor operations
x = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
y = tf.constant([[5, 6], [7, 8]], dtype=tf.float32)
# Addition
addition = tf.add(x, y) # or x + y
print("Addition:\n", addition)
# Multiplication (element-wise)
multiplication = tf.multiply(x, y) # or x * y
print("\nElement-wise multiplication:\n", multiplication)
# Matrix multiplication
matmul = tf.matmul(x, y) # or x @ y
print("\nMatrix multiplication:\n", matmul)
# Transpose
transpose = tf.transpose(x)
print("\nTranspose of x:\n", transpose)
# Reshape
reshaped = tf.reshape(x, [4, 1])
print("\nReshaped x:\n", reshaped)
TensorFlow tensors are similar to NumPy arrays but with key differences:
import numpy as np
import tensorflow as tf
# NumPy array
np_array = np.array([[1, 2], [3, 4]])
print("NumPy array:")
print(np_array)
print("Type:", type(np_array))
# TensorFlow tensor
tf_tensor = tf.constant([[1, 2], [3, 4]])
print("\nTensorFlow tensor:")
print(tf_tensor)
print("Type:", type(tf_tensor))
# Convert between NumPy and TensorFlow
tf_from_numpy = tf.constant(np_array)
numpy_from_tf = tf_tensor.numpy()
print("\nTF from NumPy:", tf_from_numpy)
print("NumPy from TF:", numpy_from_tf)
TensorFlow has two main types of tensors: constants and variables.
# Constants - values that don't change
constant_tensor = tf.constant([1, 2, 3])
print("Constant:", constant_tensor)
# Variables - values that can be modified during training
variable_tensor = tf.Variable([1, 2, 3])
print("Variable:", variable_tensor)
# Modify a variable
variable_tensor.assign([4, 5, 6])
print("Variable after assignment:", variable_tensor)
# Variables are often used for model parameters
weights = tf.Variable(tf.random.normal([3, 2]))
bias = tf.Variable(tf.zeros([2]))
print("\nWeights:\n", weights)
print("Bias:", bias)
Keras is TensorFlow's high-level API that makes building neural networks much easier. It's now integrated directly into TensorFlow as tf.keras
.
import tensorflow as tf
from tensorflow import keras
# Simple example: creating a neural network layer
layer = keras.layers.Dense(units=2, input_shape=[3])
# Create some input data
input_data = tf.constant([[1.0, 2.0, 3.0]])
# Pass data through the layer
output = layer(input_data)
print("Layer output:", output)
print("Layer weights:", layer.weights)
Try this exercise to practice what you've learned:
tf.constant()
# Your solution here
import tensorflow as tf
# 1. Create two 3x3 matrices
matrix1 = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)
matrix2 = tf.constant([[9, 8, 7], [6, 5, 4], [3, 2, 1]], dtype=tf.float32)
# 2. Element-wise operations
addition = matrix1 + matrix2
subtraction = matrix1 - matrix2
multiplication = matrix1 * matrix2
# 3. Matrix multiplication
matmul = tf.matmul(matrix1, matrix2)
# 4. Random variable
random_var = tf.Variable(tf.random.normal([3, 3]))
# 5. Print shapes and ranks
print("Matrix1 shape:", matrix1.shape, "rank:", tf.rank(matrix1))
print("Addition result:\n", addition)
print("Matrix multiplication result:\n", matmul)
Congratulations! You've taken your first steps with TensorFlow. You've learned:
In the next lesson, we'll dive into working with real data by loading and preprocessing the MNIST dataset, setting the stage for building our first neural network.