tensorflow2.x学习笔记五:构建模型的三种方式

前言

可以使用以下3种方式构建模型:

  • 使用Sequential按层顺序构建模型,
  • 使用函数式API构建任意结构模型,
  • 继承Model基类构建自定义模型。

对于顺序结构的模型,优先使用Sequential方法构建。

如果模型有多输入或者多输出,或者模型需要共享权重,或者模型具有残差连接等非顺序结构,推荐使用函数式API进行创建。

如果无特定必要,尽可能避免使用Model子类化的方式构建模型,这种方式提供了极大的灵活性,但也有更大的概率出错。

一,Sequential按层顺序创建模型

tf.keras.backend.clear_session()

model = models.Sequential()

model.add(layers.Embedding(MAX_WORDS,7,input_length=MAX_LEN))
model.add(layers.Conv1D(filters = 64,kernel_size = 5,activation = "relu"))
model.add(layers.MaxPool1D(2))
model.add(layers.Conv1D(filters = 32,kernel_size = 3,activation = "relu"))
model.add(layers.MaxPool1D(2))
model.add(layers.Flatten())
model.add(layers.Dense(1,activation = "sigmoid"))

model.compile(optimizer='Nadam',
            loss='binary_crossentropy',
            metrics=['accuracy',"AUC"])

model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        (None, 200, 7)            70000     
_________________________________________________________________
conv1d (Conv1D)              (None, 196, 64)           2304      
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 98, 64)            0         
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 96, 32)            6176      
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 48, 32)            0         
_________________________________________________________________
flatten (Flatten)            (None, 1536)              0         
_________________________________________________________________
dense (Dense)                (None, 1)                 1537      
=================================================================
Total params: 80,017
Trainable params: 80,017
Non-trainable params: 0

二,函数式API创建任意结构模型

tf.keras.backend.clear_session()

inputs = layers.Input(shape=(200,))
x  = layers.Embedding(MAX_WORDS,7)(inputs)

branch1 = layers.SeparableConv1D(64,3,activation="relu")(x)
branch1 = layers.MaxPool1D(3)(branch1)
branch1 = layers.SeparableConv1D(32,3,activation="relu")(branch1)
branch1 = layers.GlobalMaxPool1D()(branch1)

branch2 = layers.SeparableConv1D(64,5,activation="relu")(x)
branch2 = layers.MaxPool1D(5)(branch2)
branch2 = layers.SeparableConv1D(32,5,activation="relu")(branch2)
branch2 = layers.GlobalMaxPool1D()(branch2)

branch3 = layers.SeparableConv1D(64,7,activation="relu")(x)
branch3 = layers.MaxPool1D(7)(branch3)
branch3 = layers.SeparableConv1D(32,7,activation="relu")(branch3)
branch3 = layers.GlobalMaxPool1D()(branch3)

concat = layers.Concatenate()([branch1,branch2,branch3])
outputs = layers.Dense(1,activation = "sigmoid")(concat)

model = models.Model(inputs = inputs,outputs = outputs)

model.compile(optimizer='Nadam',
            loss='binary_crossentropy',
            metrics=['accuracy',"AUC"])

model.summary()



Model: "model"
________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
========================================================================================
input_1 (InputLayer)            [(None, 200)]        0                                            
________________________________________________________________________________________
embedding (Embedding)           (None, 200, 7)       70000    input_1[0][0]                    
________________________________________________________________________________________
separable_conv1d (SeparableConv (None, 198, 64)      533      embedding[0][0]                  
________________________________________________________________________________________
separable_conv1d_2 (SeparableCo (None, 196, 64)      547      embedding[0][0]                  
________________________________________________________________________________________
separable_conv1d_4 (SeparableCo (None, 194, 64)      561      embedding[0][0]                  
________________________________________________________________________________________
max_pooling1d (MaxPooling1D)    (None, 66, 64)       0        separable_conv1d[0][0]           
________________________________________________________________________________________
max_pooling1d_1 (MaxPooling1D)  (None, 39, 64)       0        separable_conv1d_2[0][0]         
________________________________________________________________________________________
max_pooling1d_2 (MaxPooling1D)  (None, 27, 64)       0        separable_conv1d_4[0][0]         
________________________________________________________________________________________
separable_conv1d_1 (SeparableCo (None, 64, 32)       2272     max_pooling1d[0][0]              
________________________________________________________________________________________
separable_conv1d_3 (SeparableCo (None, 35, 32)       2400     max_pooling1d_1[0][0]            
________________________________________________________________________________________
separable_conv1d_5 (SeparableCo (None, 21, 32)       2528     max_pooling1d_2[0][0]            
________________________________________________________________________________________
global_max_pooling1d (GlobalMax (None, 32)           0        separable_conv1d_1[0][0]         
________________________________________________________________________________________
global_max_pooling1d_1 (GlobalM (None, 32)           0        separable_conv1d_3[0][0]         
________________________________________________________________________________________
global_max_pooling1d_2 (GlobalM (None, 32)           0        separable_conv1d_5[0][0]         
________________________________________________________________________________________
concatenate (Concatenate)       (None, 96)           0        global_max_pooling1d[0][0]       
                                                              global_max_pooling1d_1[0][0]     
                                                              global_max_pooling1d_2[0][0]     
________________________________________________________________________________________
dense (Dense)                   (None, 1)            97       concatenate[0][0]                
========================================================================================
Total params: 78,938
Trainable params: 78,938
Non-trainable params: 0

三,继承Model类创建自定义模型

# 先自定义一个残差模块,为自定义Layer

class ResBlock(layers.Layer):
    def __init__(self, kernel_size, **kwargs):
        super(ResBlock, self).__init__(**kwargs)
        self.kernel_size = kernel_size
    
    def build(self,input_shape):
        self.conv1 = layers.Conv1D(filters=64,kernel_size=self.kernel_size,
                                   activation = "relu",padding="same")
        self.conv2 = layers.Conv1D(filters=32,kernel_size=self.kernel_size,
                                   activation = "relu",padding="same")
        self.conv3 = layers.Conv1D(filters=input_shape[-1],
                                   kernel_size=self.kernel_size,
                                   activation = "relu",padding="same")
        self.maxpool = layers.MaxPool1D(2)
        super(ResBlock,self).build(input_shape) # 相当于设置self.built = True
    
    def call(self, inputs):
        x = self.conv1(inputs)
        x = self.conv2(x)
        x = self.conv3(x)
        x = layers.Add()([inputs,x])
        x = self.maxpool(x)
        return x
class ImdbModel(models.Model):
    def __init__(self):
        super(ImdbModel, self).__init__()
        
    def build(self,input_shape):
        self.embedding = layers.Embedding(MAX_WORDS,7)
        self.block1 = ResBlock(7)
        self.block2 = ResBlock(5)
        self.dense = layers.Dense(1,activation = "sigmoid")
        super(ImdbModel,self).build(input_shape)
    
    def call(self, x):
        x = self.embedding(x)
        x = self.block1(x)
        x = self.block2(x)
        x = layers.Flatten()(x)
        x = self.dense(x)
        return(x)

tf.keras.backend.clear_session()

model = ImdbModel()
model.build(input_shape =(None,200))


model.compile(optimizer='Nadam',
            loss='binary_crossentropy',
            metrics=['accuracy',"AUC"])
model.summary()

Model: "imdb_model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        multiple                  70000     
_________________________________________________________________
res_block (ResBlock)         multiple                  19143     
_________________________________________________________________
res_block_1 (ResBlock)       multiple                  13703     
_________________________________________________________________
dense (Dense)                multiple                  351       
=================================================================
Total params: 103,197
Trainable params: 103,197
Non-trainable params: 0

参考链接:eat_tensorflow2_in_30_days

你可能感兴趣的:(tensorflow,深度学习,神经网络)