top of page

Deep Learning with Keras and the Functional API


Introduction


Deep learning is the frontier of AI, pushing the boundaries of what machines can learn from data. The Keras library, a high-level neural network API, has made deep learning more accessible and efficient. Particularly, the Keras functional API allows us to build models that are more flexible, with multiple inputs, outputs, and shared layers. In this tutorial, we will dive into the powerful world of the Keras functional API and explore how to build functional models.


Building Simple Models with Keras Functional API


Introduction to Keras Functional Models


In the world of deep learning, imagine the neural network as a sophisticated machine, where information flows in one end and predictions come out the other. The Keras functional API enables us to construct this machine with ease.

  1. Basic components: Input layer and Output layer

    • Input Layer: Like the entrance to a complex maze, it defines where the data enters.

    • Output Layer: The exit of the maze, where the final prediction is made.


from tensorflow.keras.layers import Input, Dense

input_layer = Input(shape=(10,))
output_layer = Dense(1)(input_layer)

Here we have created an input layer expecting data with 10 features and an output layer that will produce one value.

  1. Building simple models with one input and one output

    • A simple model can be a straight path from the entrance to the exit of the maze.

    • Example: A model predicting house prices based on features like size, location, etc.



Inputs and Their Definition


Defining inputs in Keras is akin to detailing the specifications of a car engine; it sets the stage for the rest of the construction.

  1. Using the Input() function from the Tensorflow Keras Layers module

    • It allows you to specify the shape of the input data, like how you'd tailor a suit to fit perfectly.


from tensorflow.keras.layers import Input

input_layer = Input(shape=(10,))

Here, shape=(10,) indicates that the model expects input with 10 features.


Tensor in Keras


Understanding tensors in Keras is essential, as they are the fundamental building blocks, like bricks in a building.

  1. Understanding the KerasTensor object and its role in the model

    • Tensors carry the data through the network, like conveyor belts in a factory.



Defining Outputs


The outputs in a Keras model are the final predictions, like the destination at the end of a journey.

  1. The structure of outputs in Keras and their common usage

    • Specifying the expected output shape is essential, as it gives the model direction.


  1. Specifying expected output shape using a dense layer

    • The dense layer is like a filter that refines the raw information into something meaningful.


from tensorflow.keras.layers import Dense

output_layer = Dense(1, activation='linear')(input_layer)

This code snippet creates an output layer with a linear activation function, expecting one value.


Connecting Inputs to Outputs


Like connecting the dots in a puzzle, we need to connect the layers to form a complete picture.

  1. The relationship between layers and tensors

    • Layers and tensors interact in a seamless flow, directing the data through the network.


  1. How to connect input and output layers in a functional model

    • It's like piecing together a jigsaw puzzle; each piece fits precisely with the others.


from tensorflow.keras import Model

model = Model(inputs=input_layer, outputs=output_layer)

This code connects the input and output layers to create a complete functional model.


Building Complex Models with Multiple Inputs and Outputs


Extending to Multiple Inputs


In a deep learning model, having multiple inputs is akin to having several doors to a building. Each door leads to a different section, but they all work together.

  1. Creating Keras models with two or more inputs

    • This allows for handling multiple aspects of data, like reading a novel from different perspectives.


from tensorflow.keras.layers import Input, Dense, concatenate

input1 = Input(shape=(10,))
input2 = Input(shape=(5,))
merged = concatenate([input1, input2])
output = Dense(1)(merged)

model = Model(inputs=[input1, input2], outputs=output)

Here, we have two inputs, one with 10 features and the other with 5. They are concatenated and passed to a dense output layer.

  1. Generalizing models to handle three or more inputs

    • It’s like cooking a dish with various ingredients; each one adds a unique flavor.


input3 = Input(shape=(8,))
merged = concatenate([input1, input2, input3])
output = Dense(1)(merged)

model = Model(inputs=[input1, input2, input3], outputs=output)

We added a third input with 8 features, enhancing the complexity of the model.


Designing Multiple Outputs


Imagine a factory assembly line with several endpoints; products may exit at different stages based on specific criteria.

  1. Building models capable of solving multiple problems with multiple outputs

    • This is like multitasking; performing different jobs at the same time.


from tensorflow.keras.layers import Input, Dense, concatenate

input_layer = Input(shape=(10,))
output1 = Dense(1, activation='linear')(input_layer)
output2 = Dense(5, activation='softmax')(input_layer)

model = Model(inputs=input_layer, outputs=[output1, output2])

Here, the model has one input and two outputs, one for regression and one for classification.


Working with Datasets and Building Keras Models


Part III: Working with Datasets

  1. College Basketball Data (1989-2017)

    • This dataset provides a rich ground for analysis, akin to analyzing the performance of a sports team over several seasons.


import pandas as pd

# Load the dataset
data_regular = pd.read_csv('regular_season.csv')
data_tournament = pd.read_csv('tournament.csv')

# Preview the regular season data
data_regular.head()

Here, you can visualize the first few rows of the dataset as a DataFrame:

team_idhome_awaywin_losspoints_difftournament_seed123homewin103456awayloss-56

  1. Understanding the Seed Difference

    • The seed difference is a crucial feature, much like understanding the ranking difference between two competing players.


# Calculate the seed difference
data_regular['seed_difference'] = data_regular['home_seed'] - data_regular['away_seed']


Part IV: Building and Compiling Keras Models

  1. Building Keras Models

    • Constructing a Keras model is akin to building a structure with Lego blocks.


from tensorflow.keras import Model
from tensorflow.keras.layers import Input, Dense

input_layer = Input(shape=(5,))
hidden_layer = Dense(10, activation='relu')(input_layer)
output_layer = Dense(1, activation='linear')(hidden_layer)

model = Model(inputs=input_layer, outputs=output_layer)

Here, we have defined a simple model with one hidden layer.

  1. Compiling the Model

    • Compiling a model is like finalizing the architectural blueprint.


model.compile(optimizer='adam', loss='mean_squared_error')

  1. Summarizing and Plotting the Model

    • A glimpse into the structure of your deep learning model.


model.summary()
# Optionally, plot the model
# from tensorflow.keras.utils import plot_model
# plot_model(model)

This will generate a summary table showcasing the model's structure, layers, and trainable parameters.


Fitting and Evaluating Models with Basketball Data


Part V: Fitting and Evaluating Models with Basketball Data

  1. Understanding Basketball Data

    • To begin, think of the basketball data as a chess game, where understanding the rules and strategies provides insight into predicting the outcomes.


# Analyzing seed differences and score differences
seed_diff = data_regular['seed_difference']
score_diff = data_regular['score_difference']

# Correlation between seed difference and score difference
correlation = seed_diff.corr(score_diff)
print(f'Correlation between seed difference and score difference: {correlation}')

  1. Building and Fitting the Model

    • Building and training a deep learning model is similar to teaching a player new techniques.


from sklearn.model_selection import train_test_split

# Splitting the data
X_train, X_test, y_train, y_test = train_test_split(seed_diff, score_diff, test_size=0.2)

# Reshaping data
X_train = X_train.values.reshape(-1, 1)
X_test = X_test.values.reshape(-1, 1)

# Fitting the model
history = model.fit(X_train, y_train, epochs=50, batch_size=32)

The output will show the training progress for each epoch, detailing the loss and any other metrics defined during compilation.

  1. Evaluating the Model

    • Evaluating the model is like assessing a player's performance in a game.


# Evaluating the model
evaluation = model.evaluate(X_test, y_test)
print(f'Evaluation result: {evaluation}')

The evaluation will provide a quantitative measure of the model's performance on unseen data.

  1. Making Predictions

    • Finally, making predictions is like guessing the outcome of a game based on the players' past performances.


# Making predictions
predictions = model.predict(X_test)

# Previewing the first 5 predictions
print(predictions[:5])

This will display the model's predicted score differences for the test data.


Conclusion


Deep learning with Keras and the Functional API is a versatile and robust approach to predictive modeling. In this tutorial, we explored various techniques from simple to complex modeling with real-world data, akin to understanding and playing a strategic game. From defining inputs and outputs to working with multiple layers, we've learned how to build, compile, train, and evaluate a Keras model.


The hands-on approach with basketball data allowed us to uncover correlations and predictions, creating a vivid experience for practitioners at various levels. Whether you are a beginner exploring the foundational concepts or a seasoned data scientist interested in advanced functionalities, Keras offers an intuitive pathway to deep learning exploration.


This tutorial is your compass in the ever-evolving world of data science and artificial intelligence, guiding you toward more complex and rewarding journeys in the field. By understanding the fundamentals and applying them practically, you can pave the way for innovative and impactful solutions.

bottom of page