You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

TensorFlow报错:unhashable type: 'list' 模型预测问题求助

Fixing "TypeError: unhashable type: 'list'" in TensorFlow Next-Word Prediction

Hey there! Let's work through that error you're hitting when trying to use your saved TensorFlow model for next-word prediction.

First, let's break down the issue: this error pops up because you're using a Python list as a key in your feed_dict—but TensorFlow requires hashable objects (like tf.placeholder tensors) as keys here. The most likely culprit is that your X variable is a list instead of the actual placeholder tensor from your trained model.

Here's how to fix this step by step:

  • Check what X really is
    Before running sess.run, add a quick print to confirm its type:

    print(type(X))
    

    If this outputs <class 'list'>, that's your problem. When you saved the model, X was a tf.placeholder object—you need to grab that exact tensor from the saved graph, not redefine it as a list.

  • Load your model correctly to fetch the right tensors
    A common beginner mistake is redefining the graph from scratch when loading the model, which leads to mismatched tensors. Instead, load the saved meta graph and pull the original X and Prediction tensors using their names:

    # Load the meta graph and restore saved variables
    saver = tf.train.import_meta_graph('C:\\Naresh\\SpecialProject\\MachineLearning-II\\Model\\RNN_WordPrediction\\Model.meta')
    saver.restore(sess, tf.train.latest_checkpoint('C:\\Naresh\\SpecialProject\\MachineLearning-II\\Model\\RNN_WordPrediction\\'))
    
    # Fetch the original placeholder and prediction node from the graph
    graph = tf.get_default_graph()
    # Replace 'X:0' with the actual name of your training placeholder (check your training code!)
    X = graph.get_tensor_by_name('X:0')
    # Replace 'Prediction:0' with your prediction node's name from training
    Prediction = graph.get_tensor_by_name('Prediction:0')
    

    To get the exact names, go back to your training code and print X.name and Prediction.name right before saving the model—those are the names you need to use here.

  • Validate your input x shape
    Even if X is fixed, make sure x matches the shape your model expects. For example, if your training input was a 2D array (batch size × sequence length), x should be a 2D list/array like [[word_id_1, word_id_2, ..., word_id_n]], not a 1D list.

  • Fix file path escaping
    Quick heads-up: in Python, backslashes in file paths need to be escaped (use \\ instead of \). Your original save path 'C:\Naresh\...' could cause issues—update it to 'C:\\Naresh\\SpecialProject\\MachineLearning-II\\Model\\RNN_WordPrediction\\Model' for both save and load steps.

Once you adjust these, your sess.run(Prediction, feed_dict={X:x}) call should run without that unhashable type error.

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

火山引擎 最新活动