简体   繁体   English

Keras 神经网络的 K 折交叉验证

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

Hi have already tuned my hyperparameters and would like to perfrom kfold cross validation for my model.您好已经调整了我的超参数,并希望对我的模型进行 kfold 交叉验证。 I have being looking around for different methods it won't seem to work for me.我一直在寻找不同的方法,它似乎对我不起作用。 The code is here below:代码如下:

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 

Appericate if someone could guide me on this as I am very new to TensorFlow and neural network如果有人可以指导我,因为我对 TensorFlow 和神经网络非常陌生

I haven't tested it, but this should roughly be what you want.我还没有测试过,但这应该大致是你想要的。 You use the sklearn KFold method to split the dataset into different folds, and then you simply fit the model on the current fold.您使用 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