python - Tensorflow:估计器 n_classes 问题

标签 python tensorflow machine-learning

收到错误:

ValueError:标签形状不匹配。配置为 n_classes=1 的分类器。已收到 4. 建议的修复:检查估算器的 n_classes 参数和/或标签的形状。

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)

我对输出使用 one-hot 编码(这是一个四分位数:通常为 1-4),因此它被转换为 4 列,名为:0 1 2 3。但是当我去运行它时,它会起作用就好像我使用了 n_classes=1 即使我没有。我对这个问题做了一些研究,所以不要这么快提出建议this article作为重复,因为那里提到的解决方案不能解决我的问题。我没有使用 mnist 数据集,而是使用自定义数据集。任何帮助将不胜感激,谢谢!

最佳答案

如果我没记错的话,tf.estimator.DNNClassifier 需要一个密集标签(例如,[2]),而不是单热标签(例如,[0, 0, 1]) 。因此,不要使用 pd.get_dummies,并确保您的标签是一维数据。

PR中的误导性信息已更正:https://github.com/tensorflow/tensorflow/pull/18305 .

关于python - Tensorflow:估计器 n_classes 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50125058/

相关文章:

python - mlxtend 中 StackingClassifier 函数中的元分类器是什么?

machine-learning - Tensorflow、谷歌云 ML : how to use previous checkpoint to train new images?

python - 如何旋转行的唯一值并标记其出现?

python - Pandas - 在两列中查找具有匹配值的行并在另一列中乘以值

python - 类型错误 : geturl() takes exactly 1 argument (2 given)

python - 使用索引作为键从数据框创建字典

tensorflow - Keras模型的Google云平台VM实例内存分配错误

python - Tensorflow:提取所有其他元素

python - 喀拉斯 : TypeError: 'AddNL' object has no attribute '__getitem__'

machine-learning - Tensorflow 多元线性回归结果为 NaN