Python 张量流:n_类问题的估计量
获取错误:Python 张量流:n_类问题的估计量,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,获取错误: ValueError:标签形状不匹配。使用n_类=1配置的分类器。收到4份。建议的解决方法:检查n_类参数与估计器和/或标签形状。 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_NAM
ValueError:标签形状不匹配。使用n_类=1配置的分类器。收到4份。建议的解决方法:检查n_类参数与估计器和/或标签形状。
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
,尽管我没有这样做。我已经做了一些关于这个问题的研究,所以不要这么快就提出重复的建议,因为这里提到的解决方案并不能解决我的问题。我没有使用mnist数据集,我使用的是自定义数据集。任何帮助都将不胜感激,谢谢 如果我没记错的话,tf.estimator.DNNClassifier
需要一个密集的标签(比如,[2]),而不是一个热标签(比如,[0,0,1])。因此,不要使用pd.get_dummies,并确保标签是一维数据
误导性信息已在PR中更正: