python深度学习⽅法代码整理
使⽤keras框架构建模型。
中的主要数据结构是model(模型),它提供定义完整计算图的⽅法。通过将图层添加到现有模型/计算图,我们可以构建出复杂的神经⽹络。
代码中使⽤的为
model = Sequential()
Sequential模型可以构建⾮常复杂的神经⽹络,包括全连接神经⽹络、卷积神经⽹络(CNN)、循环神经⽹络(RNN)等等。
Sequential更准确的应该理解为堆叠,通过堆叠许多层,构建出深度神经⽹络。
Sequential模型的核⼼操作是添加layers(图层)
参考1:
参考2:
参考3:
1、CNN
"""配置参数"""
N = 25000 # 最⼤词数
input_dim = N + 1 # 词库⼤⼩
output_dim = 100 # 词嵌⼊维度
kernel_size = 7 # 卷积核⼤⼩
units = filters = 64 # RNN神经元数量、卷积滤波器数量
maxlen = 100 # 序列长度
batch_size = 64 # 每批数据量⼤⼩
epochs = 20 # 训练最⼤轮数
verbose = 2 # 训练过程展⽰
patience = 10 # 没有进步的训练轮数
callbacks = [EarlyStopping('val_acc', patience=patience)]
C = [1.0,2.0]
num_classes = len(C)
DISCARD_FLAG = {'c', 'd', 'e', 'o', 'p', 'r', 'u', 'uv', 'y', 'NUM'}
def model_cnn():
layer_name='CNN'
model = Sequential()
model.add(Embedding(input_dim, output_dim, input_length=maxlen, input_shape=(maxlen,)))
model.add(Conv1D(units, kernel_size * 2, padding='same', activation='relu'))
model.add(MaxPool1D(pool_size=2)) # strides默认等于pool_size
model.add(Conv1D(units * 2, kernel_size, padding='same', activation='relu'))
model.add(GlobalMaxPool1D()) # 对于时序数据的全局最⼤池化
model.add(Dense(num_classes, activation='softmax'))
modelpile('adam', 'categorical_crossentropy', ['acc'])
# 训练、预测
history = model.fit(x11, y11, batch_size, epochs, verbose, callbacks, validation_data=(x12, y12)) e = len(history.history['acc'])
print(model.evaluate(x2, y2, batch_size, verbose), e)
# 验证集加⼊训练、预测
model.fit(x12, y12, batch_size, int(e * validation_size) + 1, verbose, callbacks)
# tk.yellow(model.evaluate(x2, y2, batch_size, verbose))
y_pred = model.predict(x2)
b = np.ones(len(y_pred))
# y_pred = np.insert(y_pred,0,values=b,axis=1)
# print(y_pred)
y_predict_class = np.argmax(y_pred,axis=1)
y_true=np.argmax(y2,axis=1)
# print(y_predict_class)
# print(y_true)
2、LSTM/BiLSTM
def model_lstm():
for layer, layer_name in (
(LSTM(units), 'LSTM') ,
(Bidirectional(LSTM(units)), 'BiLSTM'),
# (Bidirectional(GRU(units)), 'BiGRU'),
# (GRU(units), 'GRU')):
# tk.cyan(layer_name)
# 建模
model = Sequential()
model.add(Embedding(input_dim, output_dim, input_length=maxlen, input_shape=(maxlen,))) model.add(layer)
model.add(Dense(units=num_classes, activation='softmax'))
modelpile('adam', 'categorical_crossentropy', ['acc'])
# 训练、预测
history = model.fit(x11, y11, batch_size, epochs, verbose, callbacks, validation_data=(x12, y12)) e = len(history.history['acc'])
# print(model.evaluate(x2, y2, batch_size, verbose), e)
# 验证集加⼊训练、预测
model.fit(x12, y12, batch_size, int(e * validation_size) + 1,verbose, callbacks)
y_pred=model.predict(x2)
#b = np.ones(len(y_pred))
# y_pred = np.insert(y_pred,0,values=b,axis=1)
# print(y_pred)
y_predict_class = np.argmax(y_pred,axis=1)
y_true=np.argmax(y2,axis=1)
# print(y_predict_class)
# print(y_true)
3、BERT
1)Bert_only
#构建模型
def build_model_BERT_Only():
bert_model = load_trained_model_from_checkpoint(config_path, checkpoint_path, seq_len=None)
for l in bert_model.layers:
x1_in = Input(shape=(None,))
x2_in = Input(shape=(None,))
x = bert_model([x1_in, x2_in])
cls_layer = Lambda(lambda x: x[:, 0])(x) ## 取出[CLS]对应的向量⽤来做分类,[cls]能代表整句话在经过token后 output = Dense(2, activation='softmax')(cls_layer)
model = Model([x1_in, x2_in], output)
modelpile(
loss='categorical_crossentropy',
optimizer=Adam(1e-5),
metrics=['accuracy']
)
model.summary()
return model
#训练模型
def train_model(allTrainData, allValData, tokenizer,modelName):
python代码转换model = build_model_BERT_Only()
filepath='checkpoint/BertNoTrain_'+ modelName+'_{epoch:02d}-{accuracy:.4f}-{val_accuracy:.4f}.h5'
early_stopping = EarlyStopping(monitor='loss', patience=3,verbose=1) # 早停法,防⽌过拟合
plateau = ReduceLROnPlateau(monitor="loss", verbose=1, mode='max', factor=0.5,
patience=2) # 当评价指标不在提升时,减少学习率
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, period=1,
save_best_only=True, mode='min', save_weights_only=False) # 保存最好的模型
train_D = data_generator( allTrainData,tokenizer)
valid_D = data_generator(allValData,tokenizer)
history = model.fit_generator(
train_D.__iter__(),
steps_per_epoch=len(train_D),
epochs=10,
validation_data=valid_D.__iter__(),
validation_steps=len(valid_D),
callbacks=[early_stopping, plateau, checkpoint]
)
model.save_weights('keras_bert_'+ modelName+'.h5')
#model.save('keras_lstm')
return history
2)bert+cnn
x1_in = Input(shape=(None,))
x2_in = Input(shape=(None,))
x = bert_model([x1_in, x2_in])
c = Conv1D(128, 3, activation='relu')(x)
c = GlobalMaxPooling1D()(c)
c = Dropout(0.3)(c)
output = Dense(2, activation='softmax')(c)
model = Model([x1_in, x2_in], output)
modelpile(
loss='categorical_crossentropy',
optimizer=Adam(1e-5),
metrics=['accuracy']
)
model.summary()
return model
3)bert+lstm
def build_model_LSTM():
bert_model = load_trained_model_from_checkpoint(config_path, checkpoint_path, seq_len=None)
for l in bert_model.layers:
x1_in = Input(shape=(None,))
x2_in = Input(shape=(None,))
print("加载bert模型")
x = bert_model([x1_in, x2_in]) # cls_layer = Lambda(lambda x: x[:, 0])(x) ## 取出[CLS]对应的向
量⽤来做分类 T = LSTM(128, return_sequences=False)(x)
T = Dropout(0.3)(T)
output = Dense(2, activation='softmax')(T)
model = Model([x1_in, x2_in], output)
modelpile(
loss='categorical_crossentropy',
optimizer=Adam(1e-5),
metrics=['accuracy']
)
model.summary()
return model
4)bert+bilstm
x1_in = Input(shape=(None,))
x2_in = Input(shape=(None,))
x = bert_model([x1_in, x2_in])
T = Bidirectional(LSTM(128, return_sequences=False))(x) T = Dropout(0.3)(T)
output = Dense(2, activation='softmax')(T)
model = Model([x1_in, x2_in], output)
modelpile(
loss='categorical_crossentropy',
optimizer=Adam(1e-5),
metrics=['accuracy']
)
model.summary()
return model
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论