Back to Course

TensorFlow Basics & Setup

Get started with TensorFlow and learn the fundamentals

45-60 minutes Beginner Level Hands-on Coding

What is TensorFlow?

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.

Why TensorFlow?

  • Easy to Use: High-level Keras API makes building models intuitive
  • Flexible: Works on mobile, web, desktop, and cloud
  • Scalable: From single device to large distributed systems
  • Production Ready: Used by Google, Uber, Airbnb, and thousands of companies
  • Rich Ecosystem: TensorBoard, TensorFlow Lite, TensorFlow.js, and more

Installation & Setup

Let's get TensorFlow installed on your system. We'll use Python and pip for the installation.

Step 1: Verify Python Installation

First, make sure you have Python 3.7-3.11 installed:

python --version
# or
python3 --version

Step 2: Install TensorFlow

Install TensorFlow using pip:

# Install TensorFlow
pip install tensorflow

# Or if you're using Python 3 specifically
pip3 install tensorflow

Step 3: Install Additional Libraries

We'll also need some supporting libraries:

# Install additional libraries we'll use
pip install numpy pandas matplotlib jupyter

Your First TensorFlow Program

Let's start with a simple "Hello, TensorFlow!" program to verify everything is working correctly.

🚀 Hello TensorFlow

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}")
Expected Output:
TensorFlow version: 2.13.0
tf.Tensor(b'Hello, TensorFlow!', shape=(), dtype=string)
a = tf.Tensor(3.0, shape=(), dtype=float32)
b = tf.Tensor(4.0, shape=(), dtype=float32)
a + b = tf.Tensor(7.0, shape=(), dtype=float32)

Understanding Tensors

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.

Tensor Basics

# 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))

Tensor Operations

# 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 vs NumPy

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)

Key Differences

  • GPU Support: TensorFlow tensors can run on GPUs automatically
  • Automatic Differentiation: TensorFlow can compute gradients automatically
  • Graph Execution: Operations can be optimized and distributed
  • Immutable: TensorFlow tensors are immutable (can't be changed in-place)

Variables vs Constants

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)

Introduction to Keras

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)

Hands-On Exercise

Try this exercise to practice what you've learned:

Exercise 1: Tensor Playground

  1. Create two 3x3 matrices using tf.constant()
  2. Perform element-wise addition, subtraction, and multiplication
  3. Compute the matrix multiplication of the two matrices
  4. Create a variable initialized with random values
  5. Print the shapes and ranks of all tensors
# 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)

Common Issues

  • GPU not detected: TensorFlow will automatically use CPU if no GPU is available
  • Version conflicts: Make sure you're using compatible versions of Python and TensorFlow
  • Memory issues: Large tensors can consume significant memory
  • Dtype mismatches: Be careful about data types when performing operations

What's Next?

Congratulations! You've taken your first steps with TensorFlow. You've learned:

  • How to install and set up TensorFlow
  • The basics of tensors and operations
  • The difference between constants and variables
  • An introduction to the Keras API

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.