在Tensorflow中恢复和预测

我正在参考这个链接进行癌症预测。现在,在我的训练和测试阶段完成后,我想输入新的数据并进行预测。为此,我保存了模型并恢复它来获取预测结果,但我在尝试时遇到了以下错误:

ValueError: Cannot feed value of shape (31,) for Tensor 'Placeholder_1:0', which has shape '(?, 31)'

以下是我的代码:

saver = tf.train.Saver()sampletest = [-0.24222039 -0.75688274 -0.26264569 -0.75637054 -0.7154845  -0.55675554 -0.51883267 -0.69442359 -0.87362527 -1.46135011 -0.05206671 -0.2790065 -0.28614862 -0.1934161  -0.38264881 -0.1295509   0.05817795 -0.32080093-0.64650773 -0.19383338 -0.14508449 -0.74260509 -0.66173979 -0.73123076-0.68635871 -0.78697688 -0.4790055  -0.71702336 -0.90543288 -1.1197415-0.41889736]with tf.Session() as sess:    sess.run(tf.global_variables_initializer())    for epoch in range(training_epochs):        for batch in range(int(n_samples / batch_size)):            batch_x = input_X[batch * batch_size: (1 + batch) * batch_size]            batch_y = input_Y[batch * batch_size: (1 + batch) * batch_size]            print(batch_x[0])            sess.run([optimizer], feed_dict={x: batch_x,                                             y_: batch_y,                                             pkeep: training_dropout})            saver.save(sess,'.\cancer_model')        # Display logs after every 10 epochs        if (epoch) % display_step == 0:            train_accuracy, newCost = sess.run([accuracy, cost],                                               feed_dict={x: input_X, y_: input_Y, pkeep: training_dropout})            valid_accuracy, valid_newCost = sess.run([accuracy, cost],                                                     feed_dict={x: input_X_valid, y_: input_Y_valid, pkeep: 1})            print("Epoch:", epoch, "Acc =", "{:.5f}".format(train_accuracy), "Cost =", "{:.5f}".format(newCost),                  "Valid_Acc =", "{:.5f}".format(valid_accuracy), "Valid_Cost = ", "{:.5f}".format(valid_newCost))            # Record the results of the model            accuracy_history.append(train_accuracy)            cost_history.append(newCost)            valid_accuracy_history.append(valid_accuracy)            valid_cost_history.append(valid_newCost)            # If the model does not improve after 15 logs, stop the training.            if valid_accuracy < max(valid_accuracy_history) and epoch > 100:                stop_early += 1                if stop_early == 15:                    break            else:                stop_early = 0    print("Optimization Finished!")with tf.Session() as sess:    saver = tf.train.import_meta_graph('.\Cancer_Model\cancer_model.meta')    saver.restore(sess, tf.train.latest_checkpoint('.\Cancer_Model'))    prediction = sess.run(y4,feed_dict={x:sampletest})    print(prediction)

请帮助我解决这个问题。


回答:

问题在于您的模型期望接收一批样本,而您只提供了一个样本。尝试将以下代码替换为:

prediction = sess.run(y4, feed_dict={x: sampletest})

替换为:

prediction = sess.run(y4, feed_dict={x: [sampletest]})

这样,您将在prediction中得到一个包含单个元素的“批次”结果。

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注