0

How to use cross-validation approach instead of train_ test split in this deep neural network code Actually, I'm merging the 3 deep neural network models... Firstly I combined the 3 deep neural network models and then perform classification. .due to the accuracy issue, I want to use a cross-validation approach instead of a train test split please guide me on how to use a cross-validation approach instead of a train_ test split in this deep neural network code...

here is my code , if anyone can change this code train test into cross-validation then I'll be very thankful

from keras.layers import Dense
from sklearn.model_selection import train_test_split
from keras.models import Model
from keras.layers import Input 
import tensorflow as tf
import os, numpy


# random seed for reproducibility

numpy.random.seed(101)
tf.random.set_seed(101)
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'

# loading load pima indians diabetes dataset, past 5 years of medical history
dataset = numpy.loadtxt('https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv', delimiter=",")
# split into input (X) and output (Y) variables, splitting csv data
X = dataset[:, 0:8]
Y = dataset[:, 8]
x_train, x_validation, y_train, y_validation = train_test_split(
    X, Y, test_size=0.20, random_state=5)

# Define Model A 
input_layer = Input(shape=(8,))
A2 = Dense(10, activation='relu')(input_layer)
A3 = Dense(50, activation='relu')(A2)
A4 = Dense(50, activation='relu')(A3)
A5 = Dense(50, activation='relu')(A4)
A6 = Dense(50, activation='relu')(A5)
A7 = Dense(50, activation='relu')(A6)
A8 = Dense(10, activation='relu')(A7)
A9 = Dense(5, activation='relu')(A8)
model_a = Model(inputs=input_layer, outputs=A9, name="ModelA")

# Define Model B 
input_layer = Input(shape=(8,))
B2 = Dense(13, activation='relu')(input_layer)
B3 = Dense(12, activation='relu')(B2)
B4 = Dense(16, activation='relu')(B3)
B5 = Dense(16, activation='relu')(B4)
B6 = Dense(14, activation='relu')(B5)
B7 = Dense(5, activation='relu')(B6)
model_b = Model(inputs=input_layer, outputs=B7, name="ModelB")

# Define Model C
input_layer = Input(shape=(8,))
C2 = Dense(33, activation='relu')(input_layer)
C3 = Dense(23, activation='relu')(C2)
C4 = Dense(17, activation='relu')(C3)
C5 = Dense(9, activation='relu')(C4)
C6 = Dense(17, activation='relu')(C5)
C7 = Dense(13, activation='relu')(C6)
C8 = Dense(9, activation='relu')(C7)
C9 = Dense(9, activation='relu')(C8)
C10 = Dense(5, activation='relu')(C9)
model_c = Model(inputs=input_layer, outputs=C10, name="ModelC")
all_three_models = [model_a, model_b, model_c]
all_three_models_input = Input(shape=all_three_models[0].input_shape[1:])


models_output = [model(all_three_models_input) for model in all_three_models]
Concat           = tf.keras.layers.concatenate(models_output, name="Concatenate")
final_out     = Dense(1, activation='sigmoid')(Concat)

final_model   = Model(inputs=all_three_models_input, outputs=final_out, name='Ensemble')

# call the function to fit to the data (training the network)
final_model.compile(loss="binary_crossentropy",
              optimizer="adam", metrics=['accuracy'])

model_save = tf.keras.callbacks.ModelCheckpoint(
                'merge_best.h5',
                monitor="val_accuracy",
                verbose=0,
                save_best_only=True,
                save_weights_only=True,
                mode="max",
                save_freq="epoch"
            )

# call the function to fit to the data (training the network)
final_model.fit(x_train, y_train, epochs=1000, batch_size=256, callbacks=[model_save],
          validation_data=(x_validation, y_validation))


# evaluate the model
final_model.load_weights('merge_best.h5')
scores = final_model.evaluate(x_validation,y_validation)
print("\n%s: %.2f%%" % (final_model.metrics_names[1], scores[1] * 100))
final_model.save('diabetes_risk_nn.h5')
Innat
  • 16,113
  • 6
  • 53
  • 101
Ayat noor
  • 3
  • 4
  • Does this answer your question? [Cross Validation in Keras](https://stackoverflow.com/questions/48085182/cross-validation-in-keras) – Ichigo Apr 18 '21 at 19:13
  • You should first understand what CV means, start from here https://stats.stackexchange.com/questions/1826/cross-validation-in-plain-english – Innat Apr 21 '21 at 02:03
  • I already have the concept related cross validation & used it in the code but got the error, can you help me to fix that error? @ M.Innat.. – Ayat noor Apr 25 '21 at 19:14

0 Answers0