我试图用DNNClassifier
解决与独热编码输出的MNIST。
但是,出现错误
“ValueError异常:不匹配的标签形状分类器被配置为与n_classes = 1 10接收建议的修复:检查
n_classes
参数估计器和/或您的标签的形状。”
我知道类似的问题可能之前已经问,但有没有,如果我真的想与DNNClassifier
一热打码输出,我可以解决这个问题的任何新的方式?谢谢
import numpy as np
import keras
import tensorflow as tf
from keras.datasets import mnist
# the data is split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#convert the single output label to 10 output label
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
classifier = tf.estimator.DNNClassifier(
feature_columns=[tf.feature_column.numeric_column("x", shape=[28, 28])],
hidden_units=[10],
optimizer=tf.train.AdamOptimizer(learning_rate=0.001),
n_classes=10,
)
# Define the training inputs
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": x_train},
y=y_train,
num_epochs=None,
batch_size=50,
shuffle=True,
)
classifier.train(input_fn=train_input_fn, steps=100000)
# Define the test inputs
test_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": x_test},
y=y_test,
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*100))
试试下面的代码(在numpy_input_fn
函数调用)
y=y_train.astype(np.int32),
代替
y=y_train,
此外注释掉to_categorical
电话。