Skip to main content
Crowdfunding
Python + AI for Geeks
Practice

Easy and Intuitive Neural Network Library, Keras

Keras is a library that helps in easily creating and training deep learning models and is a popular library when used with TensorFlow.

In simple terms, Keras is a tool created to make TensorFlow easier to use.


What Can You Do with Keras?

Using Keras, you can create a variety of AI models as follows:

  • Handwritten Digit Recognition: A model that automatically classifies numbers

  • Image Classification: A model that classifies people, cars, cats, etc., in an input image

  • Natural Language Processing: A model that assesses whether a news article is positive or negative

  • GAN (Generative Adversarial Networks): A model that generates new images


Basic Keras Code Example

Here is an example of creating a simple AI model.

Example of Creating a Basic Neural Network Model with Keras
from tensorflow import keras
from tensorflow.keras import layers

# Create model
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=(10,)),
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid')
])

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

# Output model summary
model.summary()

Code Explanation

The main points of the above code are as follows:

You don’t need to understand specialized terms like relu, sigmoid, adam in the code just yet. We'll explain these terms in detail in the Deep Learning Basics chapter.


  • from tensorflow import keras: Imports the Keras module from the TensorFlow library.

  • from tensorflow.keras import layers: Imports the layers module from the Keras module.

  • keras.Sequential(): Builds a deep learning model by stacking neural network layers sequentially.

  • Dense(64, activation='relu', input_shape=(10,)): Creates a layer with 64 neurons and uses ReLU as the activation function. The input data is expected to be a one-dimensional vector with 10 features.

  • Dense(32, activation='relu'): Creates a layer with 32 neurons and uses ReLU as the activation function.

  • Dense(1, activation='sigmoid'): Creates an output layer with 1 neuron and uses Sigmoid as the activation function. Sigmoid converts the output value to a probability between 0 and 1 for binary classification problems.

  • model.compile(): Compiles the model. Here, it uses the adam optimizer, binary_crossentropy loss function, and accuracy metric.

  • model.summary(): Prints out a summary of the model’s structure.


As demonstrated, Keras is a handy library that allows for easy implementation of complex neural networks with concise code.

In the next lesson, we will learn how to use Keras to train models using real datasets.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.