You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow模型恢复后出现未初始化变量错误的原因咨询

Why You're Seeing the Uninitialized Variable Error

The error occurs because the model weights from your checkpoint weren't actually loaded into the TensorFlow session, even though the checkpoint file contains the variable values. Here's how to fix it:

1. Fix the Checkpoint Restore Path

Your tf.train.latest_checkpoint call has a syntax mistake that's preventing it from finding the checkpoint correctly. The comma separating arguments is incorrectly placed inside the string, making the path invalid.

Original problematic line:

imported_meta.restore(sess, tf.train.latest_checkpoint(checkpoint_dir="/tmp/,latest_filename="checkpoint"))

Corrected versions:
Either separate the arguments properly:

imported_meta.restore(sess, tf.train.latest_checkpoint(checkpoint_dir="/tmp/", latest_filename="checkpoint"))

Or simplify (since latest_filename defaults to "checkpoint"):

imported_meta.restore(sess, tf.train.latest_checkpoint("/tmp/"))

For maximum reliability, skip latest_checkpoint entirely and use the exact checkpoint path you saved:

imported_meta.restore(sess, "/tmp/new_trained_model.ckpt")

2. Ensure You're Using the Same Session for Restore and Evaluation

Your evaluate function relies on tf.get_default_session(), so you need to guarantee the session where you restored the model is the default when running evaluation. Use a session context manager to enforce this:

with tf.Session() as sess:
    # Load the meta graph and restore weights
    imported_meta = tf.train.import_meta_graph("/tmp/new_trained_model.ckpt.meta")
    imported_meta.restore(sess, "/tmp/new_trained_model.ckpt")
    
    # Retrieve tensors from the imported graph (critical if you didn't define them in the current scope)
    graph = tf.get_default_graph()
    x = graph.get_tensor_by_name("x:0")  # Replace with your actual tensor name from the saved model
    y = graph.get_tensor_by_name("y:0")
    keep_prob = graph.get_tensor_by_name("keep_prob:0")
    logits = graph.get_tensor_by_name("logits:0")
    
    # Recreate the accuracy operation using the imported tensors
    correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
    accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    
    # Run evaluation
    test_accuracy = evaluate(X_test, y_test)
    print(f"Test Accuracy: {test_accuracy}")

3. Verify Tensors Are From the Imported Graph

If you redefine placeholders like x, y, or keep_prob after importing the meta graph, you're creating new, uninitialized tensors instead of using the ones from your saved model. Always retrieve existing tensors from the imported graph using get_tensor_by_name (you can find tensor names in your original model code or via inspect_checkpoint).

Why This Works

Fixing the restore path ensures TensorFlow properly loads variable values from the checkpoint into the session. Using the same session for restore and evaluation guarantees those values are available when running the accuracy operation. Retrieving tensors from the imported graph ensures you're using the exact model structure you saved.

内容的提问来源于stack exchange,提问作者Saurabh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:45:23