-2

I finished building the DNN model for the Titanic Dataset. Given that, how do I make predictions on the X_test? My code can be accessed through my github:

https://github.com/isaac-altair/Titanic-Dataset

Thanks

1 Answers1

0

When you trained your model you asked tensorflow to evaluate your train_op. Your train_op is your optimizer, e.g.:

train_op = tf.train.AdamOptimizer(...).minimize(cost)

You ran something like this to train the model:

sess.run([train_op], feed_dict={x:data, y:labels})

The train_op depends on things like the gradients and the operations that update the weights, so all of these things happened when you ran the train_op.

At inference time you simply ask it to perform different calculations. You can have the optimizer defined, but if you don't ask it to run the optimizer it won't perform any of the actions that the optimizer is dependent on. You probably have an output of the network called logits (you could call it anything, but logits is the most common and seen in most tutorials). You might also have defined an op called accuracy which computes the accuracy of the batch. You can get the value of those with a similar request to tensorflow:

sess.run([logits, accuracy], feed_dict={x:data, y:labels})

Almost any tutorial will demonstrate this. My favorite tutorials are here: https://github.com/aymericdamien/TensorFlow-Examples

David Parks
  • 30,789
  • 47
  • 185
  • 328
  • Hello David, thank your answer. I was tweaking around with the code and the following seems to work, "with tf.Session() as sess: saver.restore(sess, final_model_path) output= sess.run(tf.argmax(logits,1),feed_dict={X: X_test})". Anyways, since you are the only person to answer here, I will count your answer as correct. Cheers. – Alexander Brown May 04 '18 at 18:06
  • You should not define `tf.argmax(logits,1)` in your call to `sess.run`. What you are doing there is adding a new `argmax` op to the graph every time you call `sess.run`. Surely not what you intended, and although minor, it's a memory leak by adding extraneous ops to the graph. You should define that once before you create the session. In fact if you ever create operations after creating your session you've almost certainly got a bug on your hand. – David Parks May 04 '18 at 18:12
  • Hello David, thank you for your guidance. Per my understanding, should I define the `tf.argmax(logits,1)` in the block of code where I am calculating the accuracy and loss for the validation set? – Alexander Brown May 04 '18 at 19:45
  • You should define it where you define your cost function. That is normally the same definition regardless of which dataset is being used. If you're defining a different graph per dataset because of differences in preprocessing you should look into `tf.data.Dataset` which will separate preprocessing into a separate workflow. – David Parks May 04 '18 at 19:48
  • I see. I will work on it later on as I am currently eating my dinner. I will follow up with you once the update is made. Thanks. – Alexander Brown May 04 '18 at 19:50