簡體   English   中英

Keras 神經網絡的 K 折交叉驗證

[英]K-fold cross validation for Keras Neural Network

您好已經調整了我的超參數,並希望對我的模型進行 kfold 交叉驗證。 我一直在尋找不同的方法,它似乎對我不起作用。 代碼如下:

tf.get_logger().setLevel(logging.ERROR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# Set random seeds for repeatable results
RANDOM_SEED = 3
random.seed(RANDOM_SEED)
np.random.seed(RANDOM_SEED)
tf.random.set_seed(RANDOM_SEED)

classes_values = [ "nearmiss", "normal" ]
classes = len(classes_values)

Y = tf.keras.utils.to_categorical(Y - 1, classes)

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=1)

input_length = X_train[0].shape[0]

train_dataset = tf.data.Dataset.from_tensor_slices((X_train, Y_train))
validation_dataset = tf.data.Dataset.from_tensor_slices((X_test, Y_test))

def get_reshape_function(reshape_to):
    def reshape(image, label):
        return tf.reshape(image, reshape_to), label
    return reshape

callbacks = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3,mode="auto")

model = Sequential()
model.add(Dense(200, activation='tanh',
    activity_regularizer=tf.keras.regularizers.l1(0.0001)))
model.add(Dropout(0.3))
model.add(Dense(44, activation='tanh',
    activity_regularizer=tf.keras.regularizers.l1(0.0001)))
model.add(Dropout(0.3))
model.add(Dense(68, activation='tanh',
    activity_regularizer=tf.keras.regularizers.l1(0.0001)))
model.add(Dropout(0.3))
model.add(Dense(44, activation='tanh',
    activity_regularizer=tf.keras.regularizers.l1(0.0001)))
model.add(Dropout(0.3))
model.add(Dense(classes, activation='softmax', name='y_pred'))

# this controls the learning rate
opt = Adam(learning_rate=0.0002, beta_1=0.9, beta_2=0.999)
# this controls the batch size, or you can manipulate the tf.data.Dataset objects yourself
BATCH_SIZE = 32
train_dataset = train_dataset.batch(BATCH_SIZE, drop_remainder=False)
validation_dataset = validation_dataset.batch(BATCH_SIZE, drop_remainder=False)

# train the neural network
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
history=model.fit(train_dataset, epochs=50, validation_data=validation_dataset, verbose=2, callbacks=callbacks)
model.test_on_batch(X_test, Y_test)
model.metrics_names
# Use this flag to disable per-channel quantization for a model.
# This can reduce RAM usage for convolutional models, but may have
# an impact on accuracy.
disable_per_channel_quantization = False 

如果有人可以指導我,因為我對 TensorFlow 和神經網絡非常陌生

我還沒有測試過,但這應該大致是你想要的。 您使用 sklearn KFold 方法將數據集拆分為不同的折疊,然后您只需將模型擬合到當前折疊。

tf.get_logger().setLevel(logging.ERROR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# Set random seeds for repeatable results
RANDOM_SEED = 3
random.seed(RANDOM_SEED)
np.random.seed(RANDOM_SEED)
tf.random.set_seed(RANDOM_SEED)

classes_values = [ "nearmiss", "normal" ]
classes = len(classes_values)

Y = tf.keras.utils.to_categorical(Y - 1, classes)


def get_reshape_function(reshape_to):
    def reshape(image, label):
        return tf.reshape(image, reshape_to), label
    return reshape

callbacks = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3,mode="auto")

def create_model():
    model = Sequential()
    model.add(Dense(200, activation='tanh',
        activity_regularizer=tf.keras.regularizers.l1(0.0001)))
    model.add(Dropout(0.3))
    model.add(Dense(44, activation='tanh',
        activity_regularizer=tf.keras.regularizers.l1(0.0001)))
    model.add(Dropout(0.3))
    model.add(Dense(68, activation='tanh',
        activity_regularizer=tf.keras.regularizers.l1(0.0001)))
    model.add(Dropout(0.3))
    model.add(Dense(44, activation='tanh',
        activity_regularizer=tf.keras.regularizers.l1(0.0001)))
    model.add(Dropout(0.3))
    model.add(Dense(classes, activation='softmax', name='y_pred'))
    return model

# this controls the learning rate
opt = Adam(learning_rate=0.0002, beta_1=0.9, beta_2=0.999)
# this controls the batch size, or you can manipulate the tf.data.Dataset objects yourself
BATCH_SIZE = 32


kf = KFold(n_splits=5)
kf.get_n_splits(X)
# Loop over the dataset to create seprate folds
for train_index, test_index in kf.split(X):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = Y[train_index], Y[test_index]

    input_length = X_train[0].shape[0]

    train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
    validation_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test))
    train_dataset = train_dataset.batch(BATCH_SIZE, drop_remainder=False)
    validation_dataset = validation_dataset.batch(BATCH_SIZE, drop_remainder=False)

    # Create a new model instance 
    model = create_model()
    model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])

    # train the model on the current fold
    history=model.fit(train_dataset, epochs=50, validation_data=validation_dataset, verbose=2, callbacks=callbacks)
    model.test_on_batch(X_test, y_test)

暫無
暫無

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

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