ValueError: функции должны быть словарем `Tensor`s. Данный тип: - PullRequest
0 голосов
/ 12 марта 2020

Я новичок в машинном обучении и в настоящее время следую учебному пособию в блокноте jupyter. https://www.tensorflow.org/tutorials/estimator/linear

Однако я продолжал получать эту ошибку и не мог предсказать точность. Я попытался найти решение для Google, и они сказали, что это может быть устаревшая версия 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'>

1 Ответ

1 голос
/ 12 марта 2020

Вы не указали параметр feature_columns в оценщике.train ()

См. Пример здесь: 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')

и передайте их.

...