Neural Network 04 (Construction of Neural Network)

1. Construction of neural network

There are two ways to build models in tf.Keras, one is through  Sequential construction and the other is through  Model class construction. The former stacks layers in a certain order , while the latter can be used to build more complex network models . First, we introduce the fully connected layer used to build the network:

tf.keras.layers.Dense(
    units, activation=None, use_bias=True, kernel_initializer='glorot_uniform',
    bias_initializer='zeros')

units: the number of neurons included in the current layer
Activation: activation function, relu, sigmoid, etc.
use_bias: whether to use bias, the default is to use bias
Kernel_initializer: the initialization method of weights, the default is Xavier initialization
bias_initializer: the initialization method of bias, Default is 0

1.1 Build via Sequential

Sequential() provides a list of layers to quickly build a neural network model. The implementation method is as follows:

# 导入相关的工具包
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# 定义一个Sequential模型,包含3层
model = keras.Sequential(
    [
        # 第一层(隐藏层):激活函数为relu,权重初始化为he_normal
        layers.Dense(3, activation="relu",
                     kernel_initializer="he_normal", name="layer1",input_shape=(3,)),
        # 第二层(隐藏层):激活函数为relu,权重初始化为he_normal
        layers.Dense(2, activation="relu",
                     kernel_initializer="he_normal", name="layer2"),
        # 第三层(输出层):激活函数为sigmoid,权重初始化为he_normal
        layers.Dense(2, activation="sigmoid",
                     kernel_initializer="he_normal", name="layer3"),
    ],
    name="my_Sequential" # 定义该模型的名字
)

# 展示模型结果
model.summary()

Only simple sequence models can be constructed through this sequential method , and more complex models cannot be implemented.

 

1.2 Build using function API

tf.keras provides a Functional API to build more complex models. The method is to use the layer as a callable object and return the tensor, and provide the input vector and output vector to the and parameters. The implementation method is  tf.keras.Model as  inputs follows  outputs :

# 导入工具包
import tensorflow as tf
# 定义模型的输入
inputs = tf.keras.Input(shape=(3,),name = "input")
# 第一层:激活函数为relu,其他默认
x = tf.keras.layers.Dense(3, activation="relu",name = "layer1")(inputs)
# 第二层:激活函数为relu,其他默认
x = tf.keras.layers.Dense(2, activation="relu",name = "layer2")(x)
# 第三层(输出层):激活函数为sigmoid
outputs = tf.keras.layers.Dense(2, activation="sigmoid",name = "layer3")(x)
# 使用Model来创建模型,指明输入和输出
model = tf.keras.Model(inputs=inputs, outputs=outputs,name="my_model") 

1.3 Constructed through subclasses of model

Build a model through a subclass of model. At this time, you need to define the layers of the neural network in and the  forward propagation process of the network__init__  in the call method . The implementation method is as follows:

# 导入工具包
import tensorflow as tf
# 定义model的子类
class MyModel(tf.keras.Model):
    # 在init方法中定义网络的层结构
    def __init__(self):
        super(MyModel, self).__init__()
        # 第一层:激活函数为relu,权重初始化为he_normal
        self.layer1 = tf.keras.layers.Dense(3, activation="relu",
                     kernel_initializer="he_normal", name="layer1",input_shape=(3,))
        # 第二层:激活函数为relu,权重初始化为he_normal
        self.layer2 =tf.keras.layers.Dense(2, activation="relu",
                     kernel_initializer="he_normal", name="layer2")
        # 第三层(输出层):激活函数为sigmoid,权重初始化为he_normal
        self.layer3 =tf.keras.layers.Dense(2, activation="sigmoid",
                     kernel_initializer="he_normal", name="layer3")
    # 在call方法中完成前向传播
    def call(self, inputs):
        x = self.layer1(inputs)
        x = self.layer2(x)
        return self.layer3(x)
# 实例化模型
model = MyModel()
# 设置一个输入,调用模型(否则无法使用summay())
x = tf.ones((1, 3))
y = model(x)

Guess you like

Origin blog.csdn.net/peng_258/article/details/132834288