Summarize and record the three mainstream ways to develop and build neural network models in Keras: sequence model, function model, and subclass model.

Keras is an easy-to-use and powerful neural network modeling library developed based on the Python language. Keras provides a high-level API that enables users to easily define and train neural network models, whether for classification, regression, or other tasks.

The main features of Keras are as follows:

  1. Simple and easy to use: Keras's design concept is user-friendly. It provides a simple and consistent API, making model construction, training, evaluation and deployment very simple. There is no need to write a lot of low-level code, and the model can be quickly implemented.

  2. Multiple backend support: Keras supports multiple deep learning backends, including TensorFlow, Theano, and CNTK. Users can choose the appropriate backend according to their own needs for model development. Keras has been included in TensorFlow version 2.0 and has become its official high-level API.

  3. Multiple modeling methods: Keras provides different modeling methods, including sequence model (Sequential Model), functional model (Functional Model) and subclassing model (Subclassing Model). Users can choose a suitable modeling method according to their needs, from simple linear models to complex nonlinear models.

  4. A large number of predefined layers and models: Keras provides a wealth of predefined layers (for example, fully connected layers, convolutional layers, pooling layers, etc.) and models (for example, VGG, ResNet, etc.), which users can use directly. , to speed up model development.

  5. Support custom layers and loss functions: Keras supports user-defined layers and loss functions. Users can customize specific layers or loss functions according to their own needs and combine them with other predefined layers and loss functions.

If you have referred to my blog posts before, you should know that basically 90% of my projects are built based on keras+Tensorflow, and the development practice of related projects based on PyTorch will be less, because I initially got into deep learning. I came into contact with Tensorflow when I was young, and now I want to systematically sort out and compare it.

This article will start with the most familiar keras, and systematically summarize and review the mainstream methods of building models with keras. Keras provides three main modeling methods: sequence model, function model and subclass model. Each method is described in detail below.

1. Sequential Model:
   The sequence model is the simplest modeling method in Keras. It builds a neural network model by stacking layers in order. There is only one input and one output between each layer. This method is suitable for simple linear stacking models or models with only a single input/output.
   The modeling steps of the sequence model are as follows:
   - Import the `Sequential` class and the layers to be used (for example, `Dense`, `Conv2D`, `MaxPooling2D`, etc.).
   - Create a model instance using the `Sequential` class.
   - Add layers to the model incrementally by calling the `add` method of the model instance.
   - Use the `compile` method to configure the model's optimizer, loss function and evaluation metrics.
   - Use the `fit` method to train the model.
   - Use the `evaluate` method to evaluate the model.

If you are building a relatively basic model without any special network links such as residual connections and multi-branch structures, this method will definitely be the first choice. It is very simple and easy to understand. Until now, I still think keras is very readable. , which is very friendly for learning and understanding. Here, taking the Mnist data set as an example, an example implementation is given:

import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 数据预处理
x_train = x_train.reshape(-1, 784).astype('float32') / 255.0
x_test = x_test.reshape(-1, 784).astype('float32') / 255.0
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)

# -----------------------------
# 使用序列模型建立模型
model = Sequential([
    Dense(64, activation='relu', input_shape=(784,)),
    Dense(10, activation='softmax')
])

# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_test, y_test))

# 模型评估
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test Accuracy:', test_acc)


2. Functional Model:
   The functional model is a more flexible modeling method that allows the construction of models with multiple inputs and multiple outputs, as well as complex models containing layer sharing and skip connections. Nonlinear model structures can be built by creating explicit data flow graphs between layers.
   The modeling steps of the function model are as follows:
   - Import the `Model` class and the layers to be used (for example, `Input`, `Conv2D`, `MaxPooling2D`, etc.).
   - Create an input tensor (`Input`) for the model and pass it to the layer that needs to be connected to that input.
   - Build a model by connecting the output of each layer to the input of the next layer.
   - Use the `Model` class to specify the input and output of the model and create a model instance.
   - Use the `compile` method to configure the model's optimizer, loss function and evaluation metrics.
   - Use the `fit` method to train the model.
   - Use the `evaluate` method to evaluate the model.

I usually use this method very frequently. In a simple sentence, it can be summarized as follows: Function models that can be completed by sequence models can be completed. Sequence models that can be completed by functional models may not be completed. If you just want to learn to master a mainstream If so, you can directly choose the function model. Classic residual networks, multi-branch network structures, etc. are all built based on the function model. Here we also take the Mnist data set as an example to give a code example:

import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 数据预处理
x_train = x_train.reshape(-1, 784).astype('float32') / 255.0
x_test = x_test.reshape(-1, 784).astype('float32') / 255.0
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)


# 使用函数式模型建立模型
inputs = tf.keras.Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)
outputs = Dense(10, activation='softmax')(x)

model = tf.keras.Model(inputs=inputs, outputs=outputs)

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

test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test Accuracy:', test_acc)


3. Subclassing Model:
   The subclassing model is a way to build a model using Python's inheritance mechanism. It provides the greatest flexibility and can customize forward propagation logic and back propagation logic as needed. The model building process can be fully customized by writing a subclass that inherits from the `Model` class.
   The modeling steps of the subclass model are as follows:
   - Import the `Model` class and the layers to be used (for example, `Dense`, `Conv2D`, `MaxPooling2D`, etc.).
   - Create a subclass that inherits from the `Model` class, define the `__init__` method of the class, and instantiate the model layer and variables.
   - Define the `call` method in the subclass to implement the forward propagation logic of the model.
   - Create an instance of the model.
   - Use the `compile` method to configure the model's optimizer, loss function and evaluation metrics.
   - Use the `fit` method to train the model.
   - Use the `evaluate` method to evaluate the model.

The main reason for the frequency of use of this method is that it is relatively more complicated to implement, and there are still problems with the preservation of the model. Therefore, for me, the frequency of use of this subclass model construction method itself is not high. High, here we also take the Mnist data set as an example to give a code example:

import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 数据预处理
x_train = x_train.reshape(-1, 784).astype('float32') / 255.0
x_test = x_test.reshape(-1, 784).astype('float32') / 255.0
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)


# 使用子类模型建立模型
class MyModel(tf.keras.Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.dense1 = Dense(64, activation='relu')
        self.dense2 = Dense(10, activation='softmax')

    def call(self, inputs):
        x = self.dense1(inputs)
        outputs = self.dense2(x)
        return outputs

model = MyModel()

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

test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test Accuracy:', test_acc)

Guess you like

Origin blog.csdn.net/Together_CZ/article/details/132426338