我想确认一下我是否通过使用tflearn在python中通过DNN网络进行10倍交叉验证来实施监督学习是正确的。代码运行并得到一些非常好的结果,training_accuracy达到95.6%,验证准确率达到98.4%,但我不确定这是否是在tflearn中做到这一点的正确方法。 (见下面的代码)
最大的谜团是如何在DNN网络上使用model.fit,通过精心挑选的列车和验证数据多次训练模型。
我在下面的代码中的推理是,我已经将我的数据分成输入(X)和输出(Y)的11个部分,其中1个部分用作测试集(不用于训练或验证之间)通过选择1个部分进行验证而剩余的9个部分用于训练,迭代使用剩余的10个部分。我认为在tflearn中,这可以通过每次使用改变的训练和验证集在同一模型上调用model.fit方法10次来完成。
# create the network
network = create_original_Dexpression_network(dropout_keep_prob)
# create a custom tensorflow session to manage the used resources
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config = config)
# Final definition of model checkpoints and other configurations
model = tflearn.DNN(network, checkpoint_path=tf_checkpoints,
max_checkpoints=1, tensorboard_verbose=2, tensorboard_dir="./tflearn_logs/")
# Dividing the data in to test, training and validation sets
X_parts = data[0] #contains 11 np.arrays with input data
Y_parts = data[1] #contains 11 np.arrays with output data
test_part_index = 5 #the index of the part used as test data (input and output)
train_val_parts_indices= list(range(0,len(X_parts))) # create a list of the indices of all parts
train_val_parts_indices.remove(test_part_index) # remove the index of the part used for testing
print("train_val_parts_indices",train_val_parts_indices)
# fit k times (k = 10) each time with a different validation part and different training parts of the data
for i in range(len(train_val_parts_indices)):
print( "run " , i)
current_val_part_index = train_val_parts_indices[i] #select a part to serve as validation
current_train_part_indices = deepcopy(train_val_parts_indices) #copy all the possible parts from train_val_parts_indices
current_train_part_indices.remove(current_val_part_index) #remove the part used for validation in this run
print("current_val_part_index ",current_val_part_index)
print("current_train_part_indices",current_train_part_indices)
# create the trainings input and output from the selected parts
X_train = create_array_out_list_of_parts(X_parts,current_train_part_indices).reshape((-1,224,224,1))
Y_train = create_array_out_list_of_parts(Y_parts,current_train_part_indices).reshape((-1,7))
# create the validation parts from the selected part
X_val = X_parts[current_val_part_index]
Y_val = Y_parts[current_val_part_index]
# check the shapes
print("X_train.shape ", X_train.shape)
print("Y_train.shape ", Y_train.shape)
print("X_val.shape ", X_val.shape)
print("Y_val.shape ", Y_val.shape)
# use this data configuration to Fit the model, train for 1 epoch.
model.fit(X_train, Y_train, n_epoch=1, validation_set=(X_val,Y_val), shuffle=True, show_metric=True, batch_size=50, snapshot_step=2000,snapshot_epoch=True, run_id=RUNID)
# Save the model
model.save(tf_checkpoints + '/' + RUNID + '.model')
print("finished training and saving")