【发布时间】:2018-05-02 17:42:12
【问题描述】:
得到错误:
ValueError: Mismatched label shape. Classifier configured with n_classes=1. Received 4. Suggested Fix: check your n_classes argument to the estimator and/or the shape of your label.
import pandas as pd
import tensorflow as tf
import numpy as np
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
csv_path = dir_path + "/good.csv"
CSV_COLUMN_NAMES = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', 'Quartile']
def load_data(y_name='Quartile'):
all = pd.read_csv(csv_path, names=CSV_COLUMN_NAMES, header=0)
one_hot = pd.get_dummies(all['Quartile'])
all = all.drop('Quartile', axis=1)
all = all.join(one_hot)
x = all.drop([0, 1, 2, 3], axis=1)
y = all[[0, 1, 2, 3]].copy()
size = x.shape[0]
cutoff = int(0.75*size)
train_x = x.head(cutoff)
train_y = y.head(cutoff)
test_x = x.tail(size-cutoff)
test_y = y.tail(size-cutoff)
return (train_x, train_y), (test_x, test_y)
def train_input_fn(features, labels, batch_size):
"""An input function for training"""
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
# Shuffle, repeat, and batch the examples.
dataset = dataset.shuffle(1000).repeat().batch(batch_size)
# Return the dataset.
return dataset
def eval_input_fn(features, labels, batch_size):
"""An input function for evaluation or prediction"""
features=dict(features)
if labels is None:
# No labels, use only features.
inputs = features
else:
inputs = (features, labels)
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices(inputs)
# Batch the examples
assert batch_size is not None, "batch_size must not be None"
dataset = dataset.batch(batch_size)
# Return the dataset.
return dataset
def main(argv):
batch_size = 50;
# Fetch the data
(train_x, train_y), (test_x, test_y) = load_data()
# Feature columns describe how to use the input.
my_feature_columns = []
for key in train_x.keys():
my_feature_columns.append(tf.feature_column.numeric_column(key=key))
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns,
hidden_units=[10, 10],
n_classes=4)
# Train the Model.
classifier.train(
input_fn=lambda:train_input_fn(train_x, train_y, batch_size), steps=10)
# Evaluate the model.
eval_result = classifier.evaluate(
input_fn=lambda:eval_input_fn(test_x, test_y, batch_size))
print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main)
我对输出使用单热编码(四分位数:通常为 1-4),因此它被转换为 4 列,命名为:0 1 2 3。但是当我运行它时,它会起作用好像我使用了n_classes=1,尽管我没有使用。我已经对这个问题做了一些研究,所以不要这么快建议this article 重复,因为那里提到的解决方案不能解决我的问题。我没有使用 mnist 数据集,我使用的是自定义数据集。任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: python tensorflow machine-learning