【问题标题】:ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.data.ops.dataset_ops.RepeatDataset'>ValueError: features 应该是 `Tensor`s ​​的字典。给定类型:<class 'tensorflow.python.data.ops.dataset_ops.RepeatDataset'>
【发布时间】:2020-06-24 01:43:42
【问题描述】:

我是机器学习的新手,目前正在学习 jupyter notebook 中的教程。 https://www.tensorflow.org/tutorials/estimator/linear

但是,我不断收到此错误,无法预测准确性。我试图用谷歌搜索解决方案,他们说它可能是过时的 TF 版本。但是,我的 TF 目前是 '2.0.0-alpha0' 版本,我使用的是 python 3.7.4。

感谢您的宝贵时间!

CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck',
                       'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']

feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
  vocabulary = dftrain[feature_name].unique()  # gets a list of all unique values from given feature column
  feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))

# The cryptic lines of code inside the append() create an object that our model can use to map string values like "male" and "female" to integers. 
# This allows us to avoid manually having to encode our dataframes.
# https://www.tensorflow.org/api_docs/python/tf/feature_column/categorical_column_with_vocabulary_list?version=stable

    def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32):
      def input_function():  # inner function, this will be returned
        ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))  # create tf.data.Dataset object with data and its label
        if shuffle:
          ds = ds.shuffle(1000)  # randomize order of data
        ds = ds.batch(batch_size).repeat(num_epochs)  # split dataset into batches of 32 and repeat process for number of epochs
        return ds  # return a batch of the dataset
      return input_function  # return a function object for use

    train_input_fn = make_input_fn(dftrain, y_train)  # here we will call the input_function that was returned to us to get a dataset object we can feed to the model
    eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)

    linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
    # We create a linear estimtor by passing the feature columns we created earlier

    linear_est.train(train_input_fn)  # train 
    result = linear_est.evaluate(eval_input_fn)  # get model metrics/stats by testing on tetsing data

    clear_output()  # clears consoke output
    print(result['accuracy'])  # the result variable is simply a dict of stats about our model
    INFO:tensorflow:Calling model_fn.
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-27-1a944b4b2878> in <module>
    ----> 1 linear_est.train(train_input_fn)  # train
          2 result = linear_est.evaluate(eval_input_fn)  # get model metrics/stats by testing on tetsing data
          3 
          4 clear_output()  # clears consoke output
          5 print(result['accuracy'])  # the result variable is simply a dict of stats about our model

    C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
        352 
        353       saving_listeners = _check_listeners_type(saving_listeners)
    --> 354       loss = self._train_model(input_fn, hooks, saving_listeners)
        355       logging.info('Loss for final step: %s.', loss)
        356       return self

    C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
       1181       return self._train_model_distributed(input_fn, hooks, saving_listeners)
       1182     else:
    -> 1183       return self._train_model_default(input_fn, hooks, saving_listeners)
       1184 
       1185   def _train_model_default(self, input_fn, hooks, saving_listeners):

    C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py in _train_model_default(self, input_fn, hooks, saving_listeners)
       1211       worker_hooks.extend(input_hooks)
       1212       estimator_spec = self._call_model_fn(
    -> 1213           features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
       1214       global_step_tensor = training_util.get_global_step(g)
       1215       return self._train_with_estimator_spec(estimator_spec, worker_hooks,

    C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py in _call_model_fn(self, features, labels, mode, config)
       1169 
       1170     logging.info('Calling model_fn.')
    -> 1171     model_fn_results = self._model_fn(features=features, **kwargs)
       1172     logging.info('Done calling model_fn.')
       1173 

    C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_estimator\python\estimator\canned\linear.py in _model_fn(features, labels, mode, config)
        337           partitioner=partitioner,
        338           config=config,
    --> 339           sparse_combiner=sparse_combiner)
        340 
        341     super(LinearClassifier, self).__init__(

    C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_estimator\python\estimator\canned\linear.py in _linear_model_fn(features, labels, mode, head, feature_columns, optimizer, partitioner, config, sparse_combiner)
        141   if not isinstance(features, dict):
        142     raise ValueError('features should be a dictionary of `Tensor`s. '
    --> 143                      'Given type: {}'.format(type(features)))
        144 
        145   optimizer = optimizers.get_optimizer_instance(

    ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.data.ops.dataset_ops.RepeatDataset'>

【问题讨论】:

    标签: python tensorflow machine-learning tensor valueerror


    【解决方案1】:

    你没有在estimater.train()中指定feature_columns参数

    请参见此处的示例: https://www.tensorflow.org/guide/data#tfestimator

    您需要声明功能名称和数据类型,例如:

    embark = tf.feature_column.categorical_column_with_hash_bucket('embark_town', 32)
    cls = tf.feature_column.categorical_column_with_vocabulary_list('class', ['First', 'Second', 'Third']) 
    age = tf.feature_column.numeric_column('age')
    

    并将它们传递进去。

    【讨论】:

      猜你喜欢
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 2021-07-06
      • 2017-06-20
      • 1970-01-01
      • 2020-06-09
      • 2019-09-17
      相关资源
      最近更新 更多