首页 文章

Tensorflow无法为Tensor 'x:0'提供形状(1,)的值,其形状为'(?, 128)'

提问于
浏览
0

我只浏览Stack Overflow和其他论坛,但找不到任何有用的问题 . 但它似乎与this question有关 .

我目前有一个经过训练的Tensorflow模型(128个输入和11个输出),我按照Tensorflow的MNIST教程保存了这个模型 .

它似乎是成功的,我现在有一个模型在这个文件夹中有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:无法为Tensor'x:0'提供shape(1,)的值,其形状为'(?,128)'Altough I打印'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:不可用类型:'list'

我只想对这个美丽的Tensorflow训练模型做一些预测 .

3 回答

  • 0

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

    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 将具有概率分数 .

  • 0

    我解决了这个问题!首先,我需要恢复所有的值(权重和偏差,并将它们分开) . 其次,我需要创建与训练模型中相同的输入,在我的情况下:

    X = tf.placeholder(tf.float32, [None, 128])
    

    然后只需调用预测:

    sess.run(prediction, feed_dict={X: unknownData})
    

    但我没有得到任何百分比分布,但我希望由于softmax功能 . 有谁知道如何访问这些?

  • 1

    首先,当你保存时,你必须在集合中添加你需要的占位符tf.add_to_collection('i',i),然后检索它们并将它们传递给feed_dict .

    在你的例子中是“我”:

    i = tf.get_collection('i')[0]
    #sess.run(prediction, feed_dict={i: d for i,d in zip(X, unknowndata.T)})
    

相关问题