神经网络总是预测相同的值

问题描述 投票:0回答:1

我的神经网络总是预测相同的值,当我尝试标准化目标值时就会出现问题,如果我不这样做,它就可以正常工作。有人知道为什么吗?

def create_and_train_model_opti2(dff, target_column):
    df = dff.copy()
    
    
    y = df[target_column].values.reshape(-1, 1)
    X = df.drop([target_column], axis=1)
    #X = X.drop(['cmf_pos'], axis=1).values  
    
    # Normalize features
    scaler_X = MinMaxScaler(feature_range=(0, 1))
    X_scaled = scaler_X.fit_transform(X)
    
    # Normalize the target
    scaler_y = MinMaxScaler(feature_range=(0, 1))
    y_scaled = scaler_y.fit_transform(y)
    
    
    X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.2, random_state=42)
    
    
    model = Sequential([
        Dense(128, activation='relu', input_shape=(X_train.shape[1],), kernel_regularizer=l2(0.001)),
        Dropout(0.2),
        Dense(128, activation='relu', kernel_regularizer=l2(0.001)),
        Dropout(0.2),
        Dense(1)
    ])
    model.compile(optimizer=Adam(learning_rate=0.001), loss='mean_squared_error')
    
    
    history = model.fit(X_train, y_train, epochs=60, batch_size=64, validation_split=0.2)
    
    return model, history, y_train, y_test, X_train, X_test, scaler_y

def make_and_view_predictions(model, X_test, y_test, scaler_y):
    
    y_pred_scaled = model.predict(X_test)
    
    # Inverse  predictions
    y_pred = scaler_y.inverse_transform(y_pred_scaled)
    
    # Inverse transform the actual test 
    y_test_original = scaler_y.inverse_transform(y_test)
    
    
    for i in range(len(y_test)):
        print(f"Predicted: {y_pred[i][0]:.2f}, Actual: {y_test_original[i][0]:.2f}")
    
    return y_pred, y_test_original

这是我的输出:

Predicted: 353.50, Actual: 156.80
Predicted: 353.50, Actual: 193.57
Predicted: 353.50, Actual: 34.67
Predicted: 353.50, Actual: 281.60
Predicted: 353.50, Actual: 0.00
Predicted: 353.50, Actual: 558.53
Predicted: 353.50, Actual: 21.41
Predicted: 353.50, Actual: 0.00
Predicted: 353.50, Actual: 560.24
Predicted: 353.50, Actual: 311.48
Predicted: 353.50, Actual: 135.14
Predicted: 353.50, Actual: 0.00
Predicted: 353.50, Actual: 403.05
Predicted: 353.50, Actual: 0.00
Predicted: 353.50, Actual: 46.62
Predicted: 353.50, Actual: 2579.44

我尝试搜索图书馆,但没有任何效果

python neural-network
1个回答
0
投票

如果没有看到模型的权重,就无法准确说出发生了什么。它很可能没有学到任何东西,例如没有改变它的权重。尝试增加(或减少)学习率,添加更多时期,删除正则化。您的数据也可能严重倾斜或存在异常值,导致学习变得困难,尤其是在标准化之后。

© www.soinside.com 2019 - 2024. All rights reserved.