还原模型后,由于命名问题,我无法使用占位符
所以我有以下两个功能可以保存和恢复我的模型:
def save_model(sess,x,y_labels,accuracy,keep_prob,step,saved_model_file):
inputs_model={"x_placeholder":x,"y_labels_placeholder":y_labels,"keep_prob_placeholder":keep_prob}
output_model={"accuracy_placeholder":accuracy,"Adam_opt":step}
builder = tf.saved_model.builder.SavedModelBuilder(saved_model_file)
builder.add_meta_graph_and_variables(sess, ["myTag"], signature_def_map= {"myModel": tf.saved_model.signature_def_utils.predict_signature_def(inputs_model,output_model)})
builder.save()
def restore_model(save_model_path,graph,sess):
print("Restoring saved model...")
tf.saved_model.loader.load(sess,["myTag"],save_model_path)
y_labels=graph.get_tensor_by_name("y_labels_placeholder:0")
x=graph.get_tensor_by_name("x_placeholder:0")
keep_prob=graph.get_tensor_by_name("keep_prob_placeholder:0")
accuracy=graph.get_tensor_by_name("accuracy_placeholder:0")
step=graph.get_operation_by_name("Adam_opt")
return (x,y_labels,keep_prob,step,accuracy)
函数运行正常,我可以通过调用restore函数来恢复张量和操作:
(x,y_labels,keep_prob,step,accuracy)=restore_model(save_model_path,graph1,sess)
但是,在第一次培训期间以及保存任何模型或重新加载任何模型之前,我正在运行此培训步骤的代码行:
sess.run(step,feed_dict={x: mbatch_x,y_labels: mbatch_y,keep_prob: 0.5})
并且运行良好(初始化的占位符正在供稿)
但是在我恢复了模型之后,后一行代码无法正常工作, 占位符未送入存在一些问题
错误消息是
InvalidArgumentError(请参阅上面的回溯):您必须使用dtype float输入占位符张量'keep_prob_placeholder_1'的值 [[节点keep_prob_placeholder_1(定义为E:/nn/NN_1.py:60)]]
我知道这与占位符的命名有关,但我不知道确切的语法