Получение ошибки:
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
, хотя я не сделал. Я провел небольшое исследование по этой проблеме, поэтому не спешите предлагать эту статью как дубликат, потому что упомянутые там решения не решают мою проблему. Я не использую набор данных mnist, я использую пользовательский набор данных. Любая помощь будет принята с благодарностью, спасибо!