Python 在神经网络中计算每次迭代的分数
我正在使用不同的节点大小和其他参数,结合参数调整等,构建一个神经网络。我试图迭代多个模型,以便能够将每次迭代的分数存储在一个列表中,并最终计算平均分数 然而,我只得到每个循环一次计算。我不确定迭代是否正确,因为对于每个节点大小,我只得到一个分数,而不是三个分数Python 在神经网络中计算每次迭代的分数,python,loops,neural-network,Python,Loops,Neural Network,我正在使用不同的节点大小和其他参数,结合参数调整等,构建一个神经网络。我试图迭代多个模型,以便能够将每次迭代的分数存储在一个列表中,并最终计算平均分数 然而,我只得到每个循环一次计算。我不确定迭代是否正确,因为对于每个节点大小,我只得到一个分数,而不是三个分数 node_list = [64, 128, 256] # list with number of nodes in hidden layer per model test_outcomes = [] # list of model sc
node_list = [64, 128, 256] # list with number of nodes in hidden layer per model
test_outcomes = [] # list of model scores
r2_outcomes = [] #list of r2 scores
for nodes in node_list:
for i in range(0,5):
# Initialising the ANN
model = Sequential()
# Adding the input layer and the first hidden layer
model.add(Dense(units = 128, activation = 'relu', input_shape = (x_train.shape[1],)))
model.add(BatchNormalization())
#model.add(Dropout(0.2))
# Adding the second hidden layer
model.add(Dense(units = nodes, activation = 'relu'))
model.add(BatchNormalization())
#model.add(Dropout(0.3))
# Adding the output layer
model.add(Dense(units = 1))
# Compile the ANN
model.compile(loss='mean_squared_error', optimizer = Adam(lr = 0.001), metrics=['mean_absolute_error'])
# ANN Summary
model.summary()
# Fit model
model.fit(x_train, y_train, batch_size = 25, epochs = 50, verbose = 0 ,validation_data = (x_valid, y_valid))
# Get predictions
y_pred = model.predict(x_valid)
# Model Evaluation Score
score = model.evaluate(x_valid, y_valid, verbose=1)
test_loss = round(score[0], 3)
print ('Test loss:', test_loss)
test_outcomes.append(test_loss)
#Calculate R_Squared
r_squared = r2_score(y_valid, y_pred)
r2_outcomes.append(r_squared)
'Evaluation Scores'
# Get the mean of that list
mean_test= np.mean(test_outcomes)
# Append to another list
mean_test_scores = []
mean_test_scores.append(mean_test)
# Get the mean of that list
mean_r2 = np.mean(test_outcomes)
# Append to another list
mean_r2_scores = []
mean_r2_scores.append(mean_r2)
在进行迭代之后,是否有可能存储其中最好的评分模型