简体   繁体   English

ValueError:特征应该是一个“张量”的字典。 给定类型:<class 'tensorflow.python.data.ops.dataset_ops.RepeatDataset'>

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

I am new to machine learning and currently following a tutorial in jupyter notebook.我是机器学习的新手,目前正在学习 jupyter notebook 中的教程。 https://www.tensorflow.org/tutorials/estimator/linear https://www.tensorflow.org/tutorials/estimator/linear

However, I kept getting this error and was unable to predict the accuracy.但是,我不断收到此错误,无法预测准确性。 I have tried to google for the solution and they said it could be outdated TF version.我试图用谷歌搜索解决方案,他们说它可能是过时的 TF 版本。 However, my TF is currently at version '2.0.0-alpha0' and I am using python 3.7.4.但是,我的 TF 目前版本为“2.0.0-alpha0”,我使用的是 python 3.7.4。

Thank you for your time!感谢您的时间!

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'>

You haven't specified the feature_columns parameter in estimater.train()您尚未在feature_columns () 中指定feature_columns参数

See example here: https://www.tensorflow.org/guide/data#tfestimator请参阅此处的示例: https : //www.tensorflow.org/guide/data#tfestimator

You need to declare the feature names and data types like:您需要声明功能名称和数据类型,例如:

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')

and pass them in.并将它们传递进来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:特征应该是`Tensor`的字典。 给定类型: <class 'tensorflow.python.framework.ops.Tensor'> - ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.framework.ops.Tensor'> ValueError:特征应该是一个“张量”的字典。 给定类型:<class 'NoneType'> - ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'NoneType'> Tensorflow Batchnormalization - 类型错误:轴必须是整数或列表,类型给定:<class 'tensorflow.python.framework.ops.Tensor'> - Tensorflow Batchnormalization - TypeError: axis must be int or list, type given: <class 'tensorflow.python.framework.ops.Tensor'> TensorFlow-ValueError:功能应该是`Tensor`s的字典 - TensorFlow - ValueError: features should be a dictionary of `Tensor`s 什么是 tensorflow.python.data.ops.dataset_ops._OptionsDataset? - What is tensorflow.python.data.ops.dataset_ops._OptionsDataset? How do I add a dimension to class 'tensorflow.python.data.ops.dataset_ops.DatasetV1Adapter' object in Python? - How do I add a dimension to class 'tensorflow.python.data.ops.dataset_ops.DatasetV1Adapter' object in Python? 在 tensorflow.python.data.ops.dataset_ops.BatchDataset 的最后注明数据类型的 object 究竟是什么? - What exactly is the object whose datatype is noted in the very end of a tensorflow.python.data.ops.dataset_ops.BatchDataset? 类型错误:<tf.tensor: shape ...> 有类型<class 'tensorflow.python.framework.ops.eagertensor'> ,但预期其中之一:(<class 'int'> ,)</class></class></tf.tensor:> - TypeError: <tf.Tensor: shape ... > has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,) 使用本机tensorflow ops扩展张量 - Expanding tensor using native tensorflow ops 什么是 tensorflow.python.ops - What is tensorflow.python.ops
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM