Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中神经网络循环的多次迭代_Python_Loops_For Loop_Neural Network - Fatal编程技术网

python中神经网络循环的多次迭代

python中神经网络循环的多次迭代,python,loops,for-loop,neural-network,Python,Loops,For Loop,Neural Network,我有一个工作的神经网络循环,所以我可以在我的隐藏层(“节点列表”)中使用预定数量的节点运行神经网络。然后,我计算每个节点数的ROC曲线下的面积,并将其放入列表(“ROC_结果”)中进行绘图。但是,我想在这个循环上循环5次,以获得三个模型(模型1:隐藏层中的20个节点,模型2:隐藏层中的28个节点,模型3:隐藏层中的38个节点)的ROC曲线下的平均面积。当我只在一个模型上尝试它时,它工作得很好,但是当我迭代多个模型而不是迭代模型1 5次,然后迭代模型2 5次,然后迭代模型3 5次…它迭代模型1,然

我有一个工作的神经网络循环,所以我可以在我的隐藏层(“节点列表”)中使用预定数量的节点运行神经网络。然后,我计算每个节点数的ROC曲线下的面积,并将其放入列表(“ROC_结果”)中进行绘图。但是,我想在这个循环上循环5次,以获得三个模型(模型1:隐藏层中的20个节点,模型2:隐藏层中的28个节点,模型3:隐藏层中的38个节点)的ROC曲线下的平均面积。当我只在一个模型上尝试它时,它工作得很好,但是当我迭代多个模型而不是迭代模型1 5次,然后迭代模型2 5次,然后迭代模型3 5次…它迭代模型1,然后迭代模型2,然后迭代模型3,它会这样做5次。 这个嵌套循环的目的是让我对每个神经网络模型迭代5次,将每次迭代的ROC曲线下的区域放入一个列表,计算该列表的平均值,然后将平均值放入一个新列表。最后,我想要一个三个数字的列表(每个模型1个),这是该模型5次迭代的ROC曲线下的平均面积。希望我能解释清楚。请要求任何澄清

这是我的密码:

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)

像这样构造循环:

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

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

a, a, a, a, a, b, b, b, b, b, c, c, c, c, c, 

实际上我对这个很感兴趣。我知道这篇文章是两年前写的,但我很好奇你是否还记得这篇文章的实际用途。我知道你想要不同分数的平均值,但这是否有助于你实际决定使用的模型的实际预测能力?