我想加載以前保存的TENSOFLOW模型(圖形和變量)。Tenorflow加載模型錯誤
以下是我的訓練
tf.global_variables_initializer().run()
y = tf.matmul(x, W) + b
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
for batch_index in range(batch_size):
batch_xs, batch_ys = sample_dataframe(train_df, N=batch_size)
#print(batch_xs.shape)
#print(batch_ys.shape)
sess.run(train_step, feed_dict = {x: batch_xs, y_:batch_ys})
if batch_index % 100 == 0:
print("Batch "+str(batch_index))
correct_predictions = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
print("Accuracy: "+str(sess.run(accuracy,
feed_dict = {x: batch_xs, y_: batch_ys})))
#print("Predictions "+str(y))
#print("Training accuracy: %.1f%%" %accuracy())
if batch_index + 1 == batch_size:
#Save the trained model
print("Exporting trained model")
builder = saved_model_builder.SavedModelBuilder(EXPORT_DIR)
builder.add_meta_graph_and_variables(sess, ['simple-MNIST'])
builder.save(as_text=True)
請忽略如何模型定義(這只是一個玩具爲例),並檢查只在被調用保存方法的最後幾行中導出的模型。一切都很順利,模型正確保存在FS中。
當我嘗試LO加載導出模型,我總是得到以下錯誤:
TypeError: Can not convert a MetaGraphDef into a Tensor or Operation.
下面是我加載模型:
with tf.Session() as sess:
print(tf.saved_model.loader.maybe_saved_model_directory(export_dir))
saved_model = tf.saved_model.loader.load(sess, ['simple-MNIST'], export_dir)
sess.run(saved_model)
任何想法如何解決呢?看來該模型已經以錯誤的格式導出,但我無法弄清楚如何改變它。
下面是一個用於加載模型並對其進行評分的簡單腳本。
with tf.device("/cpu:0"):
x = tf.placeholder(tf.float32, shape =(batch_size, 784))
W = tf.Variable(tf.truncated_normal(shape=(784, 10), stddev=0.1))
b = tf.Variable(tf.zeros([10]))
y_ = tf.placeholder(tf.float32, shape=(batch_size, 10))
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(tf.saved_model.loader.maybe_saved_model_directory(export_dir))
saved_model = tf.saved_model.loader.load(sess, ['simple-MNIST'], export_dir)
batch_xs, batch_ys = sample_dataframe(train_df, N=batch_size)
y = tf.matmul(x, W) + b
correct_predictions = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
print("Test Accuracy: "+ str(sess.run(accuracy, feed_dict = {x: batch_xs, y_: batch_ys})))
運行在一個全新的PYTHON背景下,這個腳本,將比分以非常低精度模型(似乎負荷模型方法還沒有正確設置圖形變量)
謝謝!
謝謝您的回答。我認爲你是對的,但我仍然在努力尋找解決方案。 我試圖模型加載到一個單獨的筆記本電腦,但這些變量似乎未初始化(我也HADO到redifined他們爲了使代碼的功能)。 如果我在相同的上下文(本例中爲筆記本)中加載模型,則模型會正確記錄新條目,但不會在我嘗試將其加載到全新筆記本中時得到新條目。 – luke035
一旦加載模型,您想如何處理(繼續訓練,用它進行推理,檢索一些特定的變量/張量...)? – kaufmanu
我想用它來推理新條目。當我保存模型時,我認爲它是完整的。再次感謝你! – luke035