簡體   English   中英

使用 k 折交叉驗證構建神經網絡

[英]Building neural network using k-fold cross validation

我是深度學習的新手,嘗試使用 4 折交叉驗證來實現 neural.network 進行訓練、測試和驗證。 主題是使用現有數據集對車輛進行分類。

准確度結果為 0.7。
訓練精度

紀元的一個例子 output

我也不知道代碼是否正確以及如何提高准確性。

這是代碼:

!pip install category_encoders

import tensorflow as tf
from sklearn.model_selection import KFold
import pandas as pd
import numpy as np
from tensorflow import keras
import category_encoders as ce
from category_encoders import OrdinalEncoder

car_data = pd.read_csv('car_data.csv')

car_data.columns = ['Purchasing', 'Maintenance', 'No_Doors','Capacity','BootSize','Safety','Evaluation']

# Extract the features and labels from the dataset
X = car_data.drop(['Evaluation'], axis=1)

Y = car_data['Evaluation']

encoder = ce.OrdinalEncoder(cols=['Purchasing', 'Maintenance', 'No_Doors','Capacity','BootSize','Safety'])
X = encoder.fit_transform(X)
X = X.to_numpy()

Y_df = pd.DataFrame(Y, columns=['Evaluation'])
encoder = OrdinalEncoder(cols=['Evaluation'])
Y_encoded = encoder.fit_transform(Y_df)

Y = Y_encoded.to_numpy()

input_layer = tf.keras.layers.Input(shape=(X.shape[1]))

# Define the hidden layers
hidden_layer_1 = tf.keras.layers.Dense(units=64, activation='relu', kernel_initializer='glorot_uniform')(input_layer)
hidden_layer_2 = tf.keras.layers.Dense(units=32, activation='relu', kernel_initializer='glorot_uniform')(hidden_layer_1)
# Define the output layer
output_layer = tf.keras.layers.Dense(units=1, activation='sigmoid', kernel_initializer='glorot_uniform')(hidden_layer_2)
# Create the model
model = tf.keras.Model(inputs=input_layer, outputs=output_layer)

# Initialize the 4-fold cross-validation
kfold = KFold(n_splits=4, shuffle=True, random_state=42)

# Initialize a list to store the scores
scores = []
quality_weights= []
# Compile the model
model.compile(optimizer='adam',
              loss=''sparse_categorical_crossentropy'',
              metrics=['accuracy'],
              sample_weight_mode='temporal')

for train_index, test_index in kfold.split(X,Y):
    # Split the data into train and test sets
    X_train, X_test = X[train_index], X[test_index]
    Y_train, Y_test = Y[train_index], Y[test_index]

    # Fit the model on the training data
    model.fit(X_train, Y_train, epochs=300, batch_size=64, sample_weight=quality_weights)

    # Evaluate the model on the test data
    score = model.evaluate(X_test, Y_test)

    # Append the score to the scores list
    scores.append(score[1])
    plt.plot(history.history['accuracy'])
    plt.title('Model Training Accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train'], loc='upper left')
    plt.show()
  
# Print the mean and standard deviation of the scores
print(f'Mean accuracy: {np.mean(scores):.3f} +/- {np.std(scores):.3f}')

首先引起我注意的是這里:

model.fit(X_train, Y_train, epochs=300, batch_size=64, sample_weight=quality_weights)

您的quality_weights應該是輸入大小的 numpy 數組。 參考這里: https://keras.io/api/models/model_training_apis/#fit-method

如果改變似乎沒有幫助,那么可能是你的網絡似乎沒有從數據中學習。 一些可能的原因可能是:

  1. The.network 有點太淺了。 嘗試再添加一個隱藏層,看看是否有任何改進
  2. 從代碼中我看不到輸入數據的大小。 它是否有足夠的數據點來進行 4 折交叉驗證? 你能以某種方式增加數據嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM