TensorFlow DNNClassifier断言失败:[标签必须<= n_classes-1]

时间:2019-03-20 06:20:04

标签: python pandas tensorflow machine-learning deep-learning

我正在使用TensorFlow(1.13.1)来训练DNN分类器,我是TensorFlow的新手,并且在许多类上都遇到了错误。

  

注意:我在Google上搜索了很多,找到了两个解决方案,例如提到课程数量,并且我已经应用了这些解决方案,但无法解决我的问题,因此请不要将其标记为重复,请!

这是我到目前为止尝试过的:

CSV_COLUMNS = 'ALL_COLUMNS'.split(',')
LABEL_COLUMN = 'class'
DEFAULTS = [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]]

input功能:

def reade_dataset(file_name, mode, batch_size = 512):
  def _input_fn():
#     attributes = None
#     label = None
    def decode_csv(value_column):
      # TODO #1: Use tf.decode_csv to parse the provided line
      columns = tf.decode_csv(value_column, record_defaults=DEFAULTS)
      # TODO #2: Make a Python dict.  The keys are the column names, the values are from the parsed data
      features = dict(zip(CSV_COLUMNS, columns))
      # TODO #3: Return a tuple of features, label where features is a Python dict and label a float
      label = features.pop(LABEL_COLUMN)
      print(label)
      return features, label
    file_list = None
    file_list = tf.gfile.Glob(file_name)
    dataset = (tf.data.TextLineDataset(file_list).map(decode_csv))

    num_epochs = None
    if mode == tf.estimator.ModeKeys.TRAIN:
      num_epochs = None
      dataset = dataset.shuffle(buffer_size=10*batch_size)
    else:
      num_epochs = 1

    dataset = dataset.repeat(num_epochs).batch(batch_size)
    return dataset.make_one_shot_iterator().get_next()
  return _input_fn
  

我有两个班[2, 4]

培训和评估:

def train_and_evaluate(output_dir):
  EVAL_INTERVAL = 300
  run_config = tf.estimator.RunConfig(save_checkpoints_secs=EVAL_INTERVAL, keep_checkpoint_max=3)
  estimator = tf.estimator.DNNClassifier(model_dir=output_dir,
                                         feature_columns=get_cols(),
                                         hidden_units=[1024, 512, 256],
                                         n_classes=2,
                                         config=run_config)
  train_spec = tf.estimator.TrainSpec(input_fn=reade_dataset('train.csv', tf.estimator.ModeKeys.TRAIN), max_steps=1000)
  exporter = tf.estimator.LatestExporter('exporter', _serving_input_fn)
  eval_spec = tf.estimator.EvalSpec(input_fn=reade_dataset('eval.csv', tf.estimator.ModeKeys.EVAL),
                                                          steps=None,
                                                          start_delay_secs=60,
                                                          throttle_secs=EVAL_INTERVAL,
                                                          exporters=exporter)
  tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

这是我遇到的错误:

  

InvalidArgumentError:断言失败:[标签必须<= n_classes-1]   [条件x <= y不按元素进行:x(dnn / head / labels:0)=]   [[2] [4] [2] ...] [y(dnn / head / assert_range / Const:0)=] [1] [[node   dnn / head / assert_range / assert_less_equal / Assert / AssertGuard / Assert   (定义为   /usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/canned/head.py:1561)   ]]

0 个答案:

没有答案