我只是浏览了Stack Overflow和其他论坛,但找不到对我的问题有用的任何东西。但这似乎与this question有关。

根据Tensorflow的MNIST教程,我目前保存了一个经过训练的Tensorflow模型(128个输入和11个输出)。

似乎成功了,现在我在此文件夹中有了一个模型,其中包含3个文件(.meta,.ckpt.data和.index)。但是,我想还原它并将其用于预测:

#encoding[0] => numpy ndarray (128, ) # anyway a list with only one entry
#unknowndata = np.array(encoding[0])[None]
unknowndata = np.expand_dims(encoding[0], axis=0)
print(unknowndata.shape) # Output (1, 128)

# Restore pre-trained tf model
with tf.Session() as sess:
    #saver.restore(sess, "models/model_1.ckpt")
    saver = tf.train.import_meta_graph('models/model_1.ckpt.meta')
    saver.restore(sess,tf.train.latest_checkpoint('models/./'))
    y = tf.get_collection('final tensor') # tf.nn.softmax(tf.matmul(y2, W3) + b3)
    X = tf.get_collection('input') # tf.placeholder(tf.float32, [None, 128])

    # W1 = tf.get_collection('vars')[0]
    # b1 = tf.get_collection('vars')[1]
    # W2 = tf.get_collection('vars')[2]
    # b2 = tf.get_collection('vars')[3]
    # W3 = tf.get_collection('vars')[4]
    # b3 = tf.get_collection('vars')[5]

    # y1 = tf.nn.relu(tf.matmul(X, W1) + b1)
    # y2 = tf.nn.relu(tf.matmul(y1, W2) + b2)
    # yLog = tf.matmul(y2, W3) + b3
    # y = tf.nn.softmax(yLog)

    prediction = tf.argmax(y, 1)

    print(sess.run(prediction, feed_dict={i: d for i,d in zip(X, unknowndata.T)}))
    # also had sess.run(prediction, feed_dict={X: unknowndata.T}) and also not transposed, still errors

# Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # one should be 1 obviously with a specific percentage


在那里我只遇到问题...


  ValueError:无法为形状为((?,128)'的张量'x:0'提供形状(1,)的值
  完全我打印出“ unknowndata”的形状,它与(1,128)匹配。
  我也尝试过


sess.run(prediction, feed_dict={X: unknownData})) # with transposed etc. but nothing worked for me there I got the other error



  TypeError:无法散列的类型:“列表”


我只想对这个美丽的Tensorflow训练模型进行一些预测。

最佳答案

prediction张量是通过y上的argmax获得的。您可以在执行prediction时将y添加到输出Feed中,而不仅仅是返回sess.run

output_feed = [prediction, y]
preds, probs = sess.run(output_feed, print(sess.run(prediction, feed_dict={i: d for i,d in zip(X, unknowndata.T)}))


preds将具有模型的预测,而probs将具有概率分数。

关于python - Tensorflow无法提供形状为((?,128)''的Tensor'x:0'的shape(1,)值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46496213/

10-12 21:14