2

I am following this link for cancer prediction. Now after my training & test phase is done i want to give new data as input and want the prediction. For that i am saving the model and restoring it to get the prediction, but i am getting error as

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

Below is my code:

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)

Please help me with this.

Ironman
  • 1,330
  • 2
  • 19
  • 40

2 Answers2

2

The problem is that you model expects a batch of examples, and you are just giving one. Try replacing:

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

With:

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

Then you will get a "batch" of results in prediction with a single element.

jdehesa
  • 58,456
  • 7
  • 77
  • 121
  • I tried now i am getting new error. `FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_1 [[Node: Variable_1/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Variable_1)]]` – Ironman Apr 17 '18 at 10:45
  • i fixed that FailedPreconditionError by using `sess.run(tf.global_variables_initializer())`. – Ironman Apr 17 '18 at 10:51
  • 1
    @Ironman That's a different issue... The model you have restored have uninitialized variables, either the value was not properly saved or it did not need to be saved... As you said you can use `sess.run(tf.global_variables_initializer())` but that will initialize all the trained weights as well... Maybe try `sess.run(tf.global_variables_initializer())` first and `saver.restore(sess, tf.train.latest_checkpoint('.\Cancer_Model'))` later. – jdehesa Apr 17 '18 at 10:52
2

i guess since the model is restored,the placeholder of input of y4 was renamed to Variable_1 to avoid naming confusing of graph variable, try this and see

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

though i think prediction = sess.run(y4,feed_dict={"Variable_1:0":sampletest}) would work too and also you should restore y4 as

y_4 = graph.get_operation_by_name('y4:0')

then run

prediction = sess.run(y_4,feed_dict={"Variable_1:0":[sampletest]})
Eliethesaiyan
  • 2,327
  • 1
  • 22
  • 35
  • can you please explain me what does that 0 do in `Variable_1:0`. – Ironman Apr 17 '18 at 11:28
  • an op has nodes, d= tf.add(a,b) , node:0 will be the reference of a, and node:1 will be the reference of b in op called d, here is a good details about tensorflow naming https://stackoverflow.com/questions/37849322/how-to-understand-the-term-tensor-in-tensorflow/37870634#37870634 – Eliethesaiyan Apr 18 '18 at 01:22