2

I have a working neural network loop so I can run neural networks using a predetermined number of nodes in my hidden layer ('nodes_list'). I then calculate the area under the ROC curve for each number of nodes and put this in a list ('roc_outcomes') for plotting purposes. However, I would like to loop over this loop 5 times to get an average area under the ROC curve for each of the three models (model 1: 20 nodes in hidden layer, model 2: 28 nodes in hidden layer, model 3: 38 nodes in hidden layer). This works fine when I am only trying it on one model, but when I iterate over more than one model instead of iterating over model 1 5 times, then model 2 5 times, then model 3 5 times....it iterates over model 1, then model 2, then model 3, and it does this 5 times. The purpose of this nested loop is for me to iterate over each neural network model 5 times, put the area under the ROC curve for each iteration into a list, calculate a mean of that list, and put the mean into a new list. Ultimately, I would like to have a list of three numbers (1 for each model) that is the mean area under the ROC curve for the 5 iterations of that model. Hopefully, I explained this well. Please ask for any clarification.

Here is my code:

nodes_list = [20, 28, 38]  # list with number of nodes in hidden layer per model
roc_outcomes = [] # list of ROC AUC

for i in np.arange(1,6):

    for nodes in nodes_list:
        # Add first layer
        model.add(Dense(units=n_cols, activation='relu', input_shape=(n_cols,)))
        # Add hidden layer
        model.add(Dense(units=nodes, activation='relu'))
        # Add output layer
        model.add(Dense(units=2, activation='softmax'))

        # Compile model
        model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        # Fit model
        model.fit(X, y, validation_split=0.33, epochs=epochs, callbacks=early_stopping_monitor, verbose=True)

        # Get predicted probabilities
        pred_prob = model.predict_proba(X)[:,1]

        # Calculate area under the curve (logit_roc_auc)
        logit_roc_auc = roc_auc_score(y[:,1], pred_prob) 
        # Append roc scores to the roc_outcomes list
        roc_outcomes.append(logit_roc_auc)

    # Get the mean of that list
    mean_roc = np.mean(roc_outcomes)
    # Append to another list
    mean_roc_outcomes = []
    mean_roc_outcomes.append(mean_roc)
1
  • I'm actually quite interested in this. I know this post is from two years ago, but I am curious if you can still recall the actual use of this. I understand that you want the means of different scores, but did this end up helping your actual prediction power of the model that you actually decided using? Commented Oct 7, 2020 at 20:37

1 Answer 1

2

Construct your loops like this:

for nodes in node_list:
    for i in range(0,5):
        #do your stuff

example:

myList = ['a', 'b', 'c']
for item in myList:
    for i in range(0,5):
        print(item, end=", ")

output:

a, a, a, a, a, b, b, b, b, b, c, c, c, c, c, 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.