在训练和验证过程中精度较高,在使用相同数据集进行预测时精度较低

2024-05-19 03:44:46 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试着训练Keras的模型。在训练和验证时有很高的准确性(我使用的是分数,但准确性也很高)。但是,当我试图预测某个数据集时,我的准确度会降低。即使我预测训练集。所以我想这不是关于过度装修的问题。那么问题出在哪里呢

import matplotlib.pyplot as plt

skf = StratifiedKFold(n_splits=5)
for train_index, test_index in skf.split(X, y):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    X_train,x_val,y_train,y_val = train_test_split(X_train, y_train, test_size=0.5,stratify = y_train)
    y_train = encode(y_train)
    y_val = encode(y_val)
    
    model = Sequential()
    model.add(Dense(50,input_dim=X_train.shape[1],activation='tanh'))
    model.add(Dropout(0.5))
    model.add(Dense(25,activation='tanh'))
    model.add(Dropout(0.5))
    model.add(Dense(10,activation='tanh'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))   
    
    opt = Adam(learning_rate=0.001)
    model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['acc', ta.utils.metrics.f1score])  
    history = model.fit(X_train, y_train, 
                        validation_data=(x_val, y_val),
                        epochs=5000,
                        verbose=0)
    
    plt.plot(history.history['f1score'])
    plt.plot(history.history['val_f1score'])
    plt.title('model accuracy')
    plt.ylabel('f1score')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
    break 

结果是here。正如您在培训和验证集中看到的结果一样

和预测代码:

from sklearn.metrics import f1_score

y_pred = model.predict(x_train)
y_pred = decode(y_pred)
y_train_t = decode(y_train)
print(f1_score(y_train_t, y_pred))

结果是0.64,比预期的0.9小

我的解码和编码:

def encode(y):
    Y=np.zeros((y.shape[0],2))
    for i in range(len(y)):
        if y[i]==1:
            Y[i][1]=1
        else :
            Y[i][0]=1
    return Y

def decode(y):
    Y=np.zeros((y.shape[0]))
    for i in range(len(y)):
        if np.argmax(y[i])==1:
            Y[i]=1
        else :
            Y[i]=0
    return Y

Tags: intestaddforindexmodeltrainplt
2条回答

我认为您应该将binary_crossentropy更改为categorical_crossentropy,因为您使用了一种热编码

因为你使用的是最后一层

model.add(Dense(2, activation='softmax')

你不应该在model.compile()中使用loss='binary_crossentropy',而应该在loss='categorical_crossentropy'中使用loss='binary_crossentropy'

由于这个错误,模型拟合过程中显示的结果可能是错误的——sklearn的f1_score返回的结果是真实的结果

与您的问题无关(因为我猜后续问题将是如何改进?),我们实际上从不将activation='tanh'用于隐藏层(改为尝试relu)。此外,默认情况下应使用辍学而不是(尤其是使用如此高的值0.5);注释掉所有脱落层,仅在模型过度拟合时才将其添加回(不需要时使用脱落层会影响性能)

相关问题 更多 >

    热门问题