- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试对 MNIST 数据集使用 tensorflow 估计器。出于某种原因,它一直说我的 n_classes
设置为 1,即使它是 10!
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
feature_columns = [tf.feature_column.numeric_column("x", shape=[784])]
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
hidden_units=[500, 500, 500],
n_classes=10,
model_dir="/tmp/MT")
for i in range(100000):
xdata, ydata = mnist.train.next_batch(500)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x":xdata},
y=ydata,
num_epochs=None,
shuffle=True)
classifier.train(input_fn=train_input_fn, steps=2000)
# Define the test inputs
test_input_fn = tf.estimator.inputs.numpy_input_fn(
x= {"x":mnist.test.images},
y= mnist.test.labels,
num_epochs=1,
shuffle=False)
# Evaluate accuracy.
accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]
print("\nTest Accuracy: {0:f}\n".format(accuracy_score))
错误:
ValueError: Mismatched label shape. Classifier configured with n_classes=1. Received 10. Suggested Fix: check your n_classes argument to the estimator and/or the shape of your label.
Process finished with exit code 1
最佳答案
这是个好问题。 tf.estimator.DNNClassifier
正在使用 tf.losses.sparse_softmax_cross_entropy
损失,换句话说,它需要 ordinal 编码,而不是 one-hot (在文档中找不到,只有 the source code ):
labels
must be a denseTensor
with shape matchinglogits
, namely[D0, D1, ... DN, 1]
. Iflabel_vocabulary
given,labels
must be a stringTensor
with values from the vocabulary. Iflabel_vocabulary
is not given,labels
must be an integerTensor
with values specifying the class index.
您应该使用 one_hot=False
读取数据,并将标签转换为 int32 以使其工作:
y=ydata.astype(np.int32)
关于python - TensorFlow 估计器类数不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48114258/
我刚开始使用 Dagger 2,想知道与我目前用来实现依赖注入(inject)的技术相比,它有什么优势。 目前,为了实现 DI,我创建了一个具有两种风格的项目:mock 和 prod。在这些风格中,我
我是一名优秀的程序员,十分优秀!