Featured Article

Neural Network Keras Example: Master Deep Learning Fast

Kenneth Jul 13, 2026

In the contemporary landscape of machine learning and deep learning, oneabrates the rise of a powerful tool called Keras. Built on top of TensorFlow, Keras has emerged as a user-friendly, modular, and extensible neural network library written in Python. It empowers developers and researchers alike to build and deploy scalable machine learning models with ease. Let's delve into the realm of neural networks and explore a practical example using Keras.

Convolutional Neural Network in Keras- Learn the concept in detail.
Convolutional Neural Network in Keras- Learn the concept in detail.

To begin, let's understand why Keras has gained significant traction. Keras' core design principles revolve around enabling fast experimentation, a modular approach to build models, and easy and fast prototyping. It leverages TensorFlow's capabilities to deliver high-performance deep learning models, making it an ideal choice for both research and production environments. Now, let's dive into a simple yet insightful Keras example.

Neural Networks with Keras in R: A QuickStart Guide
Neural Networks with Keras in R: A QuickStart Guide

Building a Simple Neural Network with Keras

To illustrate the power and simplicity of Keras, let's create a basic feedforward neural network (FFNN) to classify handwritten digits using the MNIST dataset. This dataset consists of 60,000 training images and 10,000 test images, with each image being 28x28 pixels in size and grayscale.

Redes Neurais
Redes Neurais

The following example will guide you through creating a simple neural network, compiling it, training it, and evaluating its performance.

Importing Necessary Libraries

Neural Networks Explained in Simple Terms
Neural Networks Explained in Simple Terms

Let's start by importing the required libraries and loading the MNIST dataset.

```python import keras from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers.convolutional import Conv2D, MaxPooling2D ```

Preprocessing the Data

Understanding Artificial Neural Networks, with Example using Keras.
Understanding Artificial Neural Networks, with Example using Keras.

Now, let's load the MNIST dataset and preprocess the data to fit our model's input shape.

```python (x_train, y_train), (x_test, y_test) = mnist.load_data() # Reshape dataset to have a single channel x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32') x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32') # Normalize inputs from 0-255 to 0-1 x_train = x_train / 255 x_test = x_test / 255 # One hot encode outputs y_train = keras.utils.np_utils.to_categorical(y_train) y_test = keras.utils.np_utils.to_categorical(y_test) ```

Defining the Neural Network Architecture

the diagram shows how many different connections are connected to each other
the diagram shows how many different connections are connected to each other

Next, we'll define our Convolutional Neural Network (CNN) architecture using Keras.

Model Creation

neural network
neural network
an image of a diagram showing the functions of nematic nematics and how they work
an image of a diagram showing the functions of nematic nematics and how they work
Neural Networks—A New Model for “The Kind of Problem a City Is”
Neural Networks—A New Model for “The Kind of Problem a City Is”
Mastering Convolutional Neural Networks: A Beginner Guide
Mastering Convolutional Neural Networks: A Beginner Guide
the cover of deep learning with keras
the cover of deep learning with keras
Debunking Artificial Neural Networks (ANN) with practical examples
Debunking Artificial Neural Networks (ANN) with practical examples
Modern Keras: The Comprehensive Guide to Deep Learning with the Keras API and Python
Modern Keras: The Comprehensive Guide to Deep Learning with the Keras API and Python
a red strawberry sitting on top of a white table next to a line graph with numbers
a red strawberry sitting on top of a white table next to a line graph with numbers
Build & Train a Neural Network in Python Using TensorFlow, Keras & Scikit-Learn
Build & Train a Neural Network in Python Using TensorFlow, Keras & Scikit-Learn

Let's go ahead and define our CNN model using the Sequential API provided by Keras.

```python model = Sequential() model.add(Conv2D(30, (5, 5), input_shape=(28, 28, 1), activation='relu')) model.add(MaxPooling2D()) model.add(Conv2D(15, (3, 3), activation='relu')) model.add(MaxPooling2D()) model.add(Dropout(0.2)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dense(10, activation='softmax')) ```

Compiling and Training the Model

It's now time to compile our model by specifying an optimizer, loss function, and metrics for evaluation. After that, we'll train the model using our training data.

```python model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, batch_size=200) ```

Evaluating the Model

The final step is to evaluate the model's performance using our test data.

```python scores = model.evaluate(x_test, y_test, verbose=0) print("CNN Error: %.2f%%" % (100-scores[1]*100)) ```

Now, after training for 10 epochs, you should observe that the model achieves a high accuracy on the test dataset. This example demonstrates the simplicity and efficiency of building and training neural networks with Keras. Its modular and user-friendly nature enables researchers and developers to iterate quickly and build complex models with ease.

The Keras ecosystem offers numerous opportunities for further exploration, such as transfer learning with pre-trained models, customizing layers, and utilizing advanced techniques like call-backs or using GPUs for faster training. Dive deeper into Keras' extensive functionalities and discover the endless possibilities of neural networks.