我有一个工作的神经网络循环,所以我可以在我的隐藏层('nodes_list')中使用预定数量的节点来运行神经网络。然后,我计算每个节点的ROC曲线下的面积,并将其放在一个列表('roc_outcomes')中用于绘图目的。但是,我想循环这个循环5次,以获得三个模型(模型1:隐藏层中的20个节点,模型2:隐藏层中的28个节点,模型3:隐藏层中的38个节点)中每个模型的ROC曲线下的平均面积。当我只在一个模型上尝试时,这很好用,但当我迭代多个模型而不是迭代模型1 5次,然后是模型2 5次,然后是模型3 5 times....it迭代模型1,然后模型2,然后模型3,它会这样做5次。这个嵌套循环的目的是让我迭代每个神经网络模型5次,将每次迭代的ROC曲线下的区域放入一个列表中,计算该列表的平均值,并将该平均值放入一个新的列表中。最终,我希望有一个包含三个数字(每个模型一个)的列表,这是该模型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)发布于 2018-08-14 23:47:27
像这样构建你的循环:
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, https://stackoverflow.com/questions/51845056
复制相似问题