我试图弄清楚为什么每次加载已经保存的模型时都会收到不同的预测和概率。基础数据集没有改变,这意味着我保存和加载模型的方法一定存在问题。
这是我的火车。py:
weights_1 = tf.Variable(tf.truncated_normal([input_size, hidden_layer_size_1], stddev=stddev), name="weights_1")
biases_1 = tf.Variable(tf.constant(bias_weight_init, shape=[hidden_layer_size_1]), name='biases_1')
outputs_1 = tf.nn.relu(tf.matmul(inputs, weights_1) + biases_1)
weights_2 = tf.Variable(tf.truncated_normal([hidden_layer_size_1, hidden_layer_size_2], stddev=stddev), name="weights_2")
biases_2 = tf.Variable(tf.constant(bias_weight_init, shape=[hidden_layer_size_2]), name='biases_2')
outputs_2 = tf.nn.relu(tf.matmul(outputs_1, weights_2) + biases_2)
weights_3 = tf.Variable(tf.truncated_normal([hidden_layer_size_2, output_size], stddev=stddev), name="weights_3")
biases_3 = tf.Variable(tf.constant(bias_weight_init, shape=[output_size]), name='biases_3')
outputs = tf.nn.relu(tf.matmul(outputs_2, weights_3) + biases_3)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = targets, logits = outputs))
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
init = tf.global_variables_initializer()
sess = tf.InteractiveSession()
sess.run(init)
time_epochs = time.time()
for epoch in range(training_epochs):
avg_cost = 0.0
start_time = time.time()
total_batch_train = int(train_samples / batch_size)
total_batch_validation = int(validation_samples / batch_size)
### Begin Training
for i_train in range(total_batch_train):
batch_x, batch_y = next_batch(batch_size, x_train, y_train)
_, c = sess.run([optimizer, cost], feed_dict = {inputs: batch_x, targets: batch_y})
avg_cost += c / total_batch_train
timer = time.time() - start_time
### Begin Validation
validation_loss = 0.
for i_valid in range(total_batch_validation):
batch_x_valid, batch_y_valid = next_batch(batch_size, x_validation, y_validation)
_, validation_loss = sess.run([optimizer, cost], feed_dict = {inputs: batch_x_valid, targets: batch_y_valid})
export_path = os.getcwd() + './savedmodel'
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
tensor_info_x = tf.saved_model.utils.build_tensor_info(inputs)
tensor_info_y = tf.saved_model.utils.build_tensor_info(outputs)
prediction_signature = (
tf.saved_model.signature_def_utils.build_signature_def(
inputs={'inputs': tensor_info_x},
outputs={'outputs': tensor_info_y},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))
builder.add_meta_graph_and_variables(
sess, [tf.saved_model.tag_constants.SERVING],
signature_def_map={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
prediction_signature
},
)
builder.save()
这是我的load.py:
sess=tf.Session()
signature_key = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
input_key = 'inputs'
output_key = 'outputs'
export_path = os.getcwd()+'./savedmodel'
meta_graph_def = tf.saved_model.loader.load(
sess,
[tf.saved_model.tag_constants.SERVING],
export_path)
signature = meta_graph_def.signature_def
x_tensor_name = signature[signature_key].inputs[input_key].name
y_tensor_name = signature[signature_key].outputs[output_key].name
inputs = sess.graph.get_tensor_by_name(x_tensor_name)
prediction = sess.graph.get_tensor_by_name(y_tensor_name)
classes = tf.argmax(prediction,1)
predicted_classes = classes.eval(session=sess,feed_dict={inputs: X_scaled})
unique, counts = np.unique(predicted_classes, return_counts=True)
print(np.asarray((unique, counts)).T)
我不知道我的问题在哪里,希望你们能帮助我,甚至给我一个可能包含更好解决方案的住所?