Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 用张量流模型进行预测_Tensorflow - Fatal编程技术网

Tensorflow 用张量流模型进行预测

Tensorflow 用张量流模型进行预测,tensorflow,Tensorflow,我遵循给定的mnist教程,能够训练模型并评估其准确性。然而,这些教程并没有展示如何在给定模型的情况下进行预测。我对准确性不感兴趣,我只想使用该模型预测一个新示例,并在输出中查看所有结果(标签),每个结果都有其指定的分数(排序或不排序)。在“”示例中,请参见此行: 我们现在可以实现回归模型了。只需要一行!我们 将矢量化的输入图像x乘以权重矩阵W,将 偏差b,并计算分配给 每节课 y = tf.nn.softmax(tf.matmul(x,W) + b) 只要拉动节点y,你就会得到你想要的 fe

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

在“”示例中,请参见此行:

我们现在可以实现回归模型了。只需要一行!我们 将矢量化的输入图像x乘以权重矩阵W,将 偏差b,并计算分配给 每节课

y = tf.nn.softmax(tf.matmul(x,W) + b)
只要拉动节点y,你就会得到你想要的

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

这适用于您创建的任何模型-您将计算预测概率,作为计算损失之前的最后一步。

正如@dga所建议的,您需要通过已预测的模型运行新的数据实例

以下是一个例子:

假设您阅读了第一个教程并计算了模型的精度(模型如下:
y=tf.nn.softmax(tf.matmul(x,W)+b)
)。现在,获取模型并将新的数据点应用于它。在下面的代码中,我计算向量,得到最大值的位置。显示图像并打印最大位置

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]

这个问题是关于预测的,它定义了一个预测,但没有应用它。下面的代码完全符合Google教程的要求,并且可以进行预测:

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兼容答案:假设您构建了Keras模型,如下所示:

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'])
然后使用以下代码对模型进行培训和评估:

model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
predictions_single = model.predict(img)
predictions = model.predict(new_images)
之后,如果要预测特定图像的类别,可以使用以下代码:

model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
predictions_single = model.predict(img)
predictions = model.predict(new_images)
如果要预测一组图像的类别,可以使用以下代码:

model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
predictions_single = model.predict(img)
predictions = model.predict(new_images)
其中,
new_images
是一个图像数组


有关更多信息,请在convnet示例上测试此建议时参考此。

(使用
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)
我得到的
参数无效:您必须用dtype float为占位符张量'placeholder_2'输入一个值,对于简单的softmax示例,它可以正常工作。知道为什么会这样吗?我可以回答我自己的评论:convnet示例在提要中有一个附加变量,我没有添加该变量。在这种情况下,提要应该是look如下:
feed_dict={x:[your_image],keep_prob:1.0}
代码的输出类似于[False-True-False…,True-False-True],但我想将其转换为[3 1 3…,1 5 1],哪些类标签不正确而不是False。我们如何才能获得分类错误而不是False的标签?
tf.run()
似乎已被删除,但
y.eval(提要)
对我很有用。关于这个问题的更完整的想法,可以在这里找到。希望它能帮助我建立一个存储库,您可以在其中绘制数字,并用自己的数据测试模型。它没有附带说明。不过,我制作了一个视频,其中包含高级概述。