keras搭建模型架構的方式及自定義層(繼承Layer)

1. Sequential()+add

詳見博客:https://blog.csdn.net/weixin_43178406/article/details/97000893

# 網絡
model = Sequential()
model.add(Dense(units = 10, input_dim = 1))
model.add(Activation('tanh'))
model.add(Dense(units =1, activation='tanh'))

或者

model = Sequential([
                    Dense(units = 500, input_shape = (X_train.shape[1],), kernel_regularizer=l2(0.003)),# kernel_regularizer:權值正則化項,bias_regularizer:偏置項的正則化, activity_regularizer:激活項的正則化
                    Activation('tanh'),
                    Dropout(0.1),
                    Dense(units = 200, activation='tanh', kernel_regularizer=l2(0.003)),
                    Dropout(0.2),
                    Dense(units=100, activation='tanh', kernel_regularizer=l2(0.003)),
                    Dropout(0.3),
                    Dense(units=10, activation='softmax', kernel_regularizer=l2(0.003))
                      
])

2. Input&Model

def bi_lstm(max_len, max_cnt, embed_size, embedding_matrix):
    _input = Input(shape=(max_len,), dtype='int32')
    _embed = Embedding(max_cnt, embed_size, input_length=max_len, weights=[embedding_matrix], trainable=False)(_input)
    _embed = SpatialDropout1D(0.2)(_embed) # 詞向量的dropout
    lstm_result = Bidirectional(LSTM(100, return_sequences=False), merge_mode='sum')(_embed)
    fc = Dropout(0.5)(lstm_result)
    fc = Dense(10)(fc)
    fc = Dropout(0.1)(fc)
    preds = Activation('softmax')(fc)

    model = Model(inputs=_input, outputs=preds)
    return model

可以看到這種方式下,Input指定了輸入的shape,下面調用keras定義好的層時,可將上一步的結果通過括號+結果名稱的方式輸入,如fc = Dropout(0.5)(lstm_result)lstm_result就是lstm的輸出結果

3. 自定義層(繼承Layer)

在keras中也可以自定義層,類似於pytorch繼承Module,keras通過繼承Layer來自定義自己的層(類)等

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章