使用 TensorFlow 模型进行预测

我遵循给定的 mnist 教程,能够训练模型并评估其准确性。然而,本教程并没有展示如何在给定模型的情况下进行预测。我对精确性不感兴趣,我只想使用这个模型来预测一个新的例子,在输出中可以看到所有的结果(标签) ,每个结果都有它的分数(排序或不排序)。

106197 次浏览

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.

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]

The question is specifically about the Google MNIST tutorial, which defines a predictor but doesn't apply it. Using guidance from Jonathan Hui's TensorFlow Estimator blog post, here is code which exactly fits the Google tutorial and does predictions:

from matplotlib import pyplot as plt


images = mnist.test.images[0:10]


predict_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x":images},
num_epochs=1,
shuffle=False)


mnist_classifier.predict(input_fn=predict_input_fn)


for image,p in zip(images,mnist_classifier.predict(input_fn=predict_input_fn)):
print(np.argmax(p['probabilities']))
plt.imshow(image.reshape(28, 28), cmap=plt.cm.binary)
plt.show()

2.0 Compatible Answer: Suppose you have built a Keras Model as shown below:

model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])


model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

Then Train and Evaluate the Model using the below code:

model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

After that, if you want to predict the class of a particular image, you can do it using the below code:

predictions_single = model.predict(img)

If you want to predict the classes of a set of Images, you can use the below code:

predictions = model.predict(new_images)

where new_images is an Array of Images.

For more information, refer this Tensorflow Tutorial.