iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐘

[Illustrated] Creating a Model with Keras

に公開

Introduction

Let's create a deep learning model using Keras.

Target Audience

This article is intended for those who have some knowledge of machine learning terminology.
Specifically, it is fine if you have a vague understanding of basic terms such as:

  • Neural networks
  • Input layer, hidden layer, output layer
  • Weights, bias
  • Forward propagation

Note: If you want to learn the basics of Deep Learning, please check here ↓
https://zenn.dev/nekoallergy/books/904df952389317

What You'll Learn from This Article

  • How to create a model in Keras
  • How to add layers to a model
  • How to check the details of models and weights

Once you have mastered these, you should be able to create basic models for the time being.

Two Ways to Create a Model

There are two ways to create a model in Keras.

  1. Using the Sequential model (tf.keras.Sequential)
    ⇒ For creating relatively simple models
  2. Using the Functional API (tf.keras.Model)
    ⇒ For creating complex models

If you want to create a simple model, you can create the same model using either method. If you are creating a model with a complex structure, you will need to use the Functional API.

For now, beginners will likely find the Sequential model easier to understand. In this article, I will explain the steps for creating a model using Sequential.

Workflow for Model Creation

  1. Create the model framework
  2. Add layers to the model
  3. View model details

We will follow this workflow.


Created by NekoAllergy

0. Loading Required Libraries

Let's load the libraries.

# Load necessary libraries
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

1. Creating the Model Framework

Create Model
# Create a model
model = keras.Sequential(name="my_model")

The code above allows you to create a framework for the model. By giving the model a name in the name argument, it will be easier to manage later.
Ultimately, an instance named model is generated. This is the main body of the model. You will build the AI by giving it instructions such as "add a layer," "train," or "show the results."

# Check the model
print(model)
Output
<keras.engine.sequential.Sequential object at 0x00000220E23221F0>

When you display it with print(model), you can see that an instance has been created.

However, this only creates the framework of the model. The content is still empty. We will now add layers to this model.

2. Adding Layers to the Model

Adding Layers
# Add layers using the add method
model.add(layers.Dense(units=10, input_shape=(5,)))
model.add(layers.Dense(units=10))

The code above adds layers to the model. To add layers, you use the add method. By writing model.add(), you can add layers to the empty model you just created.

At this point, what you pass as an argument to the add method is the layer information. In neural networks, models are built by stacking various types of layers. Keras provides many types of layers that you can combine. Layers are contained in keras.layers. The following layers are commonly used:

  • Dense: Fully connected layer
  • Conv2D: Convolutional layer
  • MaxPooling2D: Pooling layer
  • Dropout: Dropout layer
  • Flatten: Data reshaping (transforming to 1D)
  • Reshape: Data reshaping (transforming to a specified dimension)

In this program, we used the fully connected Dense layer.

The first line represents the first layer. The units=10 specifies the number of neurons (number of units). Also, since this is the input layer, you need to specify the shape of the incoming data. This process is only required for the input layer. With input_shape=(5, ), we have specified that the input data consists of five numerical values. You can imagine five numbers being fully connected to 10 neurons.

The second line represents the second layer. Similar to the first layer, 10 neurons are specified. In other words, the 10 neurons of the first layer and the 10 neurons of the second layer are fully connected. Since this is the final layer in this case, we can see that the size of the output data is 10.

With just these two lines, we have successfully added layers to the model.
This model is a converter that "calculates based on 5 input numbers and outputs 10 numbers."

3. Checking Model Details

Let's check the structure of the model we created.

There are four commonly used methods for verification:

  • model.summary() method
  • model.layers attribute
  • model.weights attribute
  • model.output_shape attribute


.
.
.


The rest of the content is compiled in a book. I would be very happy if you would check it out ↓
https://zenn.dev/nekoallergy/books/deeplearning-advanced

Learn more about Artificial Intelligence/AI/Machine Learning

NekoAllergy AI

NekoAllergy AI
I share information about machine learning on YouTube. I would be happy if you could take a look if you have time.



Created by NekoAllergy

Discussion