Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making predictions with a TensorFlow model

Tags:

tensorflow

I followed the given mnist tutorials and was able to train a model and evaluate its accuracy. However, the tutorials don't show how to make predictions given a model. I'm not interested in accuracy, I just want to use the model to predict a new example and in the output see all the results (labels), each with its assigned score (sorted or not).

like image 847
user247866 Avatar asked Nov 14 '15 18:11

user247866


People also ask

How do you predict using TensorFlow model?

Just pull on node y and you'll have what you want. This applies to just about any model you create - you'll have computed the prediction probabilities as one of the last steps before computing the loss.

How do you connect model input data with predictions for machine learning?

To give inputs to a machine learning model, you have to create a NumPy array, where you have to input the values of the features you used to train your machine learning model. Then we can use that array in the model. predict() method, and at the end, it will give the predicted value as an output based on the inputs.


2 Answers

In the "Deep MNIST for Experts" example, see this line:

We can now implement our regression model. It only takes one line! We multiply the vectorized input images x by the weight matrix W, add the bias b, and compute the softmax probabilities that are assigned to each class.

y = tf.nn.softmax(tf.matmul(x,W) + b) 

Just pull on node y and you'll have what you want.

feed_dict = {x: [your_image]} classification = tf.run(y, feed_dict) print classification 

This applies to just about any model you create - you'll have computed the prediction probabilities as one of the last steps before computing the loss.

like image 128
dga Avatar answered Nov 09 '22 23:11

dga


As @dga suggested, you need to run your new instance of the data though your already predicted model.

Here is an example:

Assume you went though the first tutorial and calculated the accuracy of your model (the model is this: y = tf.nn.softmax(tf.matmul(x, W) + b)). Now you grab your model and apply the new data point to it. In the following code I calculate the vector, getting the position of the maximum value. Show the image and print that maximum position.

from matplotlib import pyplot as plt from random import randint num = randint(0, mnist.test.images.shape[0]) img = mnist.test.images[num]  classification = sess.run(tf.argmax(y, 1), feed_dict={x: [img]}) plt.imshow(img.reshape(28, 28), cmap=plt.cm.binary) plt.show() print 'NN predicted', classification[0] 
like image 32
Salvador Dali Avatar answered Nov 10 '22 00:11

Salvador Dali