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来自定义自己的层(类)等