Sling Academy
Home/Tensorflow/TensorFlow Keras: Building and Training Neural Networks

TensorFlow Keras: Building and Training Neural Networks

Last updated: December 17, 2024

Tutorial: Building and Training Neural Networks with TensorFlow Keras

Tf.keras, a high-level API of TensorFlow, dramatically simplifies the process of building and training neural networks. In this tutorial, we will create a neural network using TensorFlow Keras and train it to classify handwritten digits from the MNIST dataset.

Setting Up the Environment

First, make sure you have tensorflow installed in your Python environment. If not, install it using:

pip install tensorflow

Loading the Dataset

The MNIST dataset is readily available in TensorFlow for experimentation:

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

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

The dataset consists of 28 x 28 grayscale images, with each pixel ranging from 0 to 255. We need to normalize these values to the range of 0 to 1:

x_train, x_test = x_train / 255.0, x_test / 255.0

Building the Model

We will construct a simple neural network with an input layer, a hidden layer (Dense) with 128 neurons, and an output layer (Dense) with 10 units, representing the ten digits:

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10)
])

Here, Flatten transforms the images into 1D arrays, Dense adds layers of neurons, and Dropout prevents overfitting. We compile the model with a sparse categorical crossentropy loss and specify accuracy as a metric:

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

Training the Model

Fit the model to the training data:

model.fit(x_train, y_train, epochs=5)

The fit function trains the model for a specified number of epochs.

Evaluating the Model

Finally, evaluate the model on the test data to monitor its performance:

test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('\nTest accuracy:', test_acc)

Understanding the above basics can immensely bolster anyone looking to insightfully develop AI solutions leveraging TensorFlow Keras. Experiment with multiple layers and configurations to see how performance changes!

Summary

In this guide, we've introduced how to build and train a neural network using TensorFlow Keras to classify the MNIST dataset. We loaded the data, built the model, compiled it, and finally trained and evaluated its performance. The power of TensorFlow Keras lies in its ability to abstract complex processes, enabling developers of all levels to experiment with machine learning effortlessly.

Now that you're equipped with the foundational knowledge, try implementing permutation techniques such as varying layers, deciding activation functions based on task needs, and tweaking dropout rates to find the best-performing model configuration. Happy coding!

Next Article: TensorFlow Keras: Customizing Callbacks for Training

Previous Article: TensorFlow IO: Efficient Data Serialization

Series: Tensorflow Tutorials

Tensorflow

You May Also Like

  • TensorFlow `scalar_mul`: Multiplying a Tensor by a Scalar
  • TensorFlow `realdiv`: Performing Real Division Element-Wise
  • Tensorflow - How to Handle "InvalidArgumentError: Input is Not a Matrix"
  • TensorFlow `TensorShape`: Managing Tensor Dimensions and Shapes
  • TensorFlow Train: Fine-Tuning Models with Pretrained Weights
  • TensorFlow Test: How to Test TensorFlow Layers
  • TensorFlow Test: Best Practices for Testing Neural Networks
  • TensorFlow Summary: Debugging Models with TensorBoard
  • Debugging with TensorFlow Profiler’s Trace Viewer
  • TensorFlow dtypes: Choosing the Best Data Type for Your Model
  • TensorFlow: Fixing "ValueError: Tensor Initialization Failed"
  • Debugging TensorFlow’s "AttributeError: 'Tensor' Object Has No Attribute 'tolist'"
  • TensorFlow: Fixing "RuntimeError: TensorFlow Context Already Closed"
  • Handling TensorFlow’s "TypeError: Cannot Convert Tensor to Scalar"
  • TensorFlow: Resolving "ValueError: Cannot Broadcast Tensor Shapes"
  • Fixing TensorFlow’s "RuntimeError: Graph Not Found"
  • TensorFlow: Handling "AttributeError: 'Tensor' Object Has No Attribute 'to_numpy'"
  • Debugging TensorFlow’s "KeyError: TensorFlow Variable Not Found"
  • TensorFlow: Fixing "TypeError: TensorFlow Function is Not Iterable"