性能指标不会随模型或超参数调整而改变

时间:2019-07-08 14:40:37

标签: deep-learning lstm metrics

我的模型遇到问题,无论哪种模型(LSTM双向,单向或CNN)或什么参数(学习率,辍学,层次),它们都提供相同的度量。例如,对于CNN模型是[['f1score',0.8137931034482759],['precision',0.686046511627907],['recall',1.0],['accuracy',0.686046511627907]。 LSTM也是[['f1score',0.8137931034482759],['precision',0.686046511627907],['recall',1.0],['accuracy',0.686046511627907]。

我尝试更改学习率,增加辍学率,增加层次。获取上面列出的相同指标。奇怪的是,几天前,当我测试CNN模型(相同的代码)时,它为我提供了更好,更不同的指标,准确度约为80%。仅供参考,我正在使用5 k倍进行交叉验证,而我目前专注于二进制分类。我总共有大约800个数据样本。我尝试更改时间和批处理大小,但是并没有太大变化。我正在使用集群计算,因此不确定是否会影响它。例如,我下面有CNN模型和LSTM单向模型。

def CNN_model(word_index, embedding_matrix, max_len, categorical):
    model = Sequential()
    optm = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
    model.add(Embedding(len(word_index)+1, 300, weights=[embedding_matrix], input_length=max_len, trainable=False))
    #model.add(Dropout(0.2))
    model.add(Conv1D(128, 5, activation='relu'))
    model.add(GlobalMaxPooling1D())
    model.add(Dense(10))
    #model.add(BatchNormalization())     #BATCH NORMALIZATION   
    model.add(Activation('relu'))
    if (categorical):
        model.add(Dense(3, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer=optm, metrics=['accuracy'])
    else:
        model.add(Dense(1, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=optm, metrics=['accuracy'])

    return model

def LSTM_Uni_model(word_index, embedding_matrix, max_len, categorical):
    optm = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
    model = Sequential()
    model.add(Embedding(len(word_index)+1, 300, weights=[embedding_matrix], input_length=max_len, trainable=False))
    model.add(Dropout(0.2))    #DROPOUT
    model.add(Conv1D(filters=32, kernel_size=3, padding='same'))
    #model.add(BatchNormalization())     #BATCH NORMALIZATION   
    model.add(Activation('relu'))
    model.add(MaxPooling1D(pool_size=2))
    model.add(LSTM(100))
    #model.add(BatchNormalization())     #BATCH NORMALIZATION
    if (categorical):
        model.add(Dense(3, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer=optm, metrics=['accuracy'])
    else:
        model.add(Dense(1, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=optm, metrics=['accuracy'])

    return model


def LSTM_Uni(X_train, y_train, X_test, y_test, word_index, embedding_matrix, max_len, seed, categorical):
    earlystop = EarlyStopping(monitor='val_loss', min_delta=0, patience=2, verbose=1, mode='auto', restore_best_weights=True) # pateince is number of epochs
    callbacks_list = [earlystop]
    if (categorical):
        kfold = list(KFold(n_splits=5, shuffle=True, random_state=seed).split(X_train, y_train))
    else:
        kfold = list(StratifiedKFold(n_splits=5, shuffle=True, random_state=seed).split(X_train, y_train))
    model_infos = []
    metrics = []
    model = None
    for i,(train, test) in enumerate(kfold):
        model = None
        model = LSTM_Uni_model(word_index, embedding_matrix, max_len, categorical)
        model_info=model.fit(X_train[train], y_train[train], epochs=10, batch_size=12, validation_data=(X_train[test], y_train[test]),
                               callbacks=callbacks_list, verbose=1)
        print("Performance plot of fold {}:".format(i+1))
        # summarize history in plot
        plot_model_history(model_info)
        model_infos.append(model_info)

        #Final evaluation of the model
        metrics, y_pred = evaluate_model(metrics, categorical, model, y_test, X_test)

    print(model.summary())

    return y_pred, metrics, model_infos

我只为LSTM单向提供了交叉验证代码,但是我有另一组代码,其中除了CNN之外,它们具有几乎相同的代码。我没有错误,每个模型都提供指标。如前所述,唯一的问题是每种型号的零件都一样。我对深度学习有点陌生,所以我不知道自己是否在某个地方犯了小错误。

0 个答案:

没有答案