我正在尝试使用TensorFlow编写一个模型,该模型使用预制的DNNClassifier来对虹膜花进行分类。
BATCH_SIZE=100
TRAIN_STEPS=1000
def main(unused_argv):
# Load training and eval data
iris = tf.contrib.learn.datasets.load_dataset("iris")
data = iris.data
labels = iris.target
train_data = data[:100]
train_labels = labels[:100]
eval_data = data[100:]
eval_labels = labels[100:]
# Feature columns
feature_columns = [
tf.feature_column.numeric_column(key="SepalLength"),
tf.feature_column.numeric_column(key="SepalWidth"),
tf.feature_column.numeric_column(key="PetalLength"),
tf.feature_column.numeric_column(key="PetalWidth")
]
# Build DNN with 2 hidden layers, 10 neurons each
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns, hidden_units=[10, 10], n_classes=3)
# Train model
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x=train_data,
y=train_labels,
batch_size=BATCH_SIZE,
num_epochs=None,
shuffle=True
)
classifier.train(input_fn=train_input_fn, steps=TRAIN_STEPS)
# Evaluate model
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x=eval_data,
y=eval_labels,
num_epochs=1,
shuffle=False
)
eval_results = classifier.evaluate(input_fn=eval_input_fn)
print(eval_results)
我收到以下错误:
ValueError:功能应该是
的字典Tensor
s。
据我所知,功能列应该是列表。我的实施有什么问题?