While PyTorch is another popular framework, this guide focuses on TensorFlow due to its widespread adoption and comprehensive ecosystem.
Inference in TensorFlow
Let’s start by understanding how to implement inference (making predictions) using TensorFlow.Coffee Roasting Example
Consider a coffee roasting optimization problem where you control:- Temperature: The heating temperature for raw coffee beans
- Duration: How long to roast the beans
Understanding the dataset
Understanding the dataset
In this dataset:
- Positive examples (good coffee) form a triangle region
- Too low temperature → undercooked beans
- Too short duration → not properly roasted
- Too high temperature or too long → overcooked/burnt beans
Basic TensorFlow Inference Code
Here’s how to perform inference for a sample input (200°C for 17 minutes):import numpy as np
import tensorflow as tf
# Input: temperature (200°C) and duration (17 minutes)
x = np.array([[200.0, 17.0]])
# Create first hidden layer with 3 units and sigmoid activation
layer_1 = tf.keras.layers.Dense(units=3, activation='sigmoid')
Dense is the term for the standard neural network layers you’ve learned about. As you advance, you’ll encounter other layer types, but dense layers are fundamental.
# Create second layer with 1 unit and sigmoid activation
layer_2 = tf.keras.layers.Dense(units=1, activation='sigmoid')
# Apply layer 2 to layer 1 output
a2 = layer_2(a1)
# a2 might be: 0.8
Handwritten Digit Classification
Let’s apply the same concepts to a more complex example: recognizing handwritten digits.Network Architecture
Building Neural Networks with Sequential
TensorFlow provides a more elegant way to build neural networks using the Sequential API.The Sequential Model
Instead of manually passing data through each layer, you can tell TensorFlow to string layers together:The Sequential API tells TensorFlow to create a neural network by sequentially connecting layers. TensorFlow handles the data flow automatically.
Training Data Format
For the coffee roasting example:Complete Training Workflow
Idiomatic TensorFlow Code
By convention, TensorFlow code doesn’t explicitly assign layers to variables. Here’s the more common style:Digit Classification Complete Example
Key TensorFlow Concepts
Dense Layers
Standard fully-connected neural network layers
Sequential API
Easily stack layers to create neural networks
Compile Step
Specify loss function and optimizer
Fit Method
Train the model on your dataset
Predict Method
Make predictions on new data
Activation Functions
Transform outputs (sigmoid, relu, etc.)
Understanding What TensorFlow Does
While TensorFlow makes it easy to build neural networks in just a few lines of code, it’s important to understand what’s happening under the hood. This knowledge helps you debug issues and optimize performance.
Why Understanding Matters
- Debugging
- Optimization
- Performance
When something doesn’t work (which is common), understanding the internals helps you identify and fix issues quickly.
Data Handling in TensorFlow
TensorFlow handles data in a specific format using NumPy arrays and tensors. Understanding this format is crucial for building effective models.
- Input data should be NumPy arrays or TensorFlow tensors
- Shape matters: ensure your data dimensions match layer expectations
- TensorFlow processes data in batches for efficiency
Next Steps
Now that you understand TensorFlow implementation:- Explore Vectorization to understand efficient implementations
- Learn about Neural Network Training in depth
- Practice with the accompanying notebooks to solidify your understanding
