DEV Community

Cover image for Lesson 12 - What is TensorFlow?
Daniel Azevedo
Daniel Azevedo

Posted on

Lesson 12 - What is TensorFlow?

TensorFlow is an open-source machine learning framework developed by Google. It’s used by researchers and developers to build and train machine learning models for tasks like classification, regression, clustering, natural language processing, and more.

At its core, TensorFlow enables you to define complex computations on data using a data flow graph. The "Tensor" in TensorFlow refers to the data, and "Flow" refers to the way that data moves through the operations defined in the model.

Here’s a quick breakdown:

  • Tensor: Multidimensional arrays or data that flow through the system.
  • Flow: The process of passing tensors (data) through computational operations.

Why Use TensorFlow?

TensorFlow is popular for a few key reasons:

  1. Versatility: It can be used to build a wide range of machine learning models—from basic linear regression models to cutting-edge neural networks.
  2. Cross-Platform: TensorFlow can run on different platforms including desktops, servers, mobile devices, and even web browsers.
  3. Scalability: It supports both small-scale research experiments and large-scale, production-ready models.
  4. Community Support: Being open-source and backed by Google, TensorFlow has a massive community of developers and resources, making it easier to find solutions to problems or tutorials for learning.

How TensorFlow Works: Tensors and Data Flow

The fundamental building blocks of TensorFlow are tensors, which are essentially multidimensional arrays. These tensors are passed through a data flow graph that represents the various operations the model will perform.

A simple TensorFlow computation looks like this:

  1. Define the model: Create the structure of the model (for example, a neural network with different layers).
  2. Forward pass: Pass input data (tensors) through the model and perform calculations.
  3. Loss calculation: Measure how well or poorly the model’s predictions match the actual results (labelled data).
  4. Backpropagation: Use an optimization algorithm (like gradient descent) to adjust the model’s weights to improve its predictions.
  5. Iterate: Repeat the process until the model's predictions are accurate enough (i.e., the loss value is minimized).

Example: A Simple Neural Network in TensorFlow

Let’s take a look at an example to understand how to set up and train a model in TensorFlow. In this case, we'll build a simple neural network for image classification using the famous MNIST dataset (handwritten digits).

Step 1: Install TensorFlow

First, make sure TensorFlow is installed:

pip install tensorflow
Enter fullscreen mode Exit fullscreen mode

Step 2: Load and Prepare the Dataset

We'll use the MNIST dataset, which is a collection of 28x28 pixel images of handwritten digits (0-9). Each image is labeled with the corresponding digit.

import tensorflow as tf
from tensorflow.keras.datasets import mnist

# Load the dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the data
x_train, x_test = x_train / 255.0, x_test / 255.0
Enter fullscreen mode Exit fullscreen mode

Here, we’re loading the data and normalizing it to values between 0 and 1 for better training performance.

Step 3: Build the Model

Now, we’ll define a simple feedforward neural network model with an input layer, one hidden layer, and an output layer for classification.

# Create a simple model
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),  # Flatten the 28x28 images into a single layer
    tf.keras.layers.Dense(128, activation='relu'),  # Hidden layer with 128 neurons
    tf.keras.layers.Dense(10, activation='softmax') # Output layer with 10 neurons (one for each digit)
])

# Compile the model
model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy', 
              metrics=['accuracy'])
Enter fullscreen mode Exit fullscreen mode
  • The Flatten layer converts each 28x28 image into a 1D array (784 elements).
  • The Dense layers are fully connected layers. The hidden layer has 128 neurons and uses the ReLU activation function.
  • The output layer has 10 neurons (one for each class) and uses the softmax function to output probabilities for each class.

Step 4: Train the Model

We can now train the model using the training data.

# Train the model
model.fit(x_train, y_train, epochs=5)
Enter fullscreen mode Exit fullscreen mode

This line of code trains the model for 5 epochs, meaning the model will see the entire training dataset 5 times. During this process, TensorFlow adjusts the model’s weights to minimize the loss function.

Step 5: Evaluate the Model

After training, we evaluate the model’s performance on the test data:

# Evaluate the model on test data
model.evaluate(x_test, y_test, verbose=2)
Enter fullscreen mode Exit fullscreen mode

This will output the model’s accuracy on the test data, which gives you an idea of how well the model generalizes to unseen data.

Full Code Example

import tensorflow as tf
from tensorflow.keras.datasets import mnist

# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the data
x_train, x_test = x_train / 255.0, x_test / 255.0

# Define the model
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy', 
              metrics=['accuracy'])

# Train the model
model.fit(x_train, y_train, epochs=5)

# Evaluate the model
model.evaluate(x_test, y_test, verbose=2)
Enter fullscreen mode Exit fullscreen mode

Conclusion: Why TensorFlow is Powerful

TensorFlow is a robust and versatile tool for building machine learning models. It allows you to create, train, and deploy models that can tackle real-world problems across various industries. From image classification to natural language processing and beyond, TensorFlow’s flexibility makes it an excellent choice for developers and data scientists alike.

TensorFlow’s support for both high-level and low-level APIs makes it ideal for beginners and experts. Whether you want to experiment with simple models or dive deep into research, TensorFlow provides the tools to make your ideas a reality.

Keep Coding :)

Top comments (0)