gpt4 book ai didi

python - Tensorflow:估计器 n_classes 问题

转载 作者:行者123 更新时间:2023-11-30 09:47:45 24 4
gpt4 key购买 nike

收到错误:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com