简体   繁体   English

Tensorflow 抛出“TypeError: unhashable type: 'list'”错误

[英]Tensorflow throws "TypeError: unhashable type: 'list'" error

I'm learning Tensorflow.我正在学习 Tensorflow。 The following is my code.以下是我的代码。 The code is building a linear regression model using some features and trying to predict the MPG (fuel expense).该代码正在使用某些功能构建线性回归模型并尝试预测 MPG(燃料费用)。

The first part (Dataset preparation) of the code prepares the dataset for training.代码的第一部分(数据集准备)为训练准备数据集。 The second part (Begin Tensorflow) tries to build and train a linear regressor.第二部分(开始 Tensorflow)尝试构建和训练线性回归器。

The problem I encountered is when I call the train function on the linear regressor, an error was thrown...我遇到的问题是当我在线性回归器上调用train函数时,抛出了一个错误...

I don't know how to fix this error.我不知道如何解决这个错误。 And I don't know why "unhashable list" affects the training.而且我不知道为什么“unhashable list”会影响训练。

Please provide some insight on this.请对此提供一些见解。 Thanks.谢谢。

from __future__ import absolute_import, division, print_function
import pathlib
import pandas as pd
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
# tf.enable_eager_execution() # turn eager model on; this should only be called ONCE!
print(tf.__version__)
#-----------------------------------------------------
## Dataset preparation
# read dataset and preview
dataset_path = keras.utils.get_file("auto-mpg.data", "https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data")

column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight',
                'Acceleration', 'Model Year', 'Origin'] 
raw_dataset = pd.read_csv(dataset_path, names=column_names,
                      na_values = "?", comment='\t',
                      sep=" ", skipinitialspace=True)
dataset = raw_dataset.copy()

# erase NaN rows
dataset = dataset.dropna()

# Origin column is not magnitude meaningful, don't use this as feature!
origin = dataset.pop('Origin')

# Separate train & test dataset
dataset_train = dataset.sample(frac=0.8, random_state = 0)
dataset_test  = dataset.drop(dataset_train.index)

#-----------------------------------------------------
## Begin Tensorflow 
# build input fn
def train_input_fn(df, label_name):
  """
  Argus:
    df: pandas dataframe
    label_name: name of label column
  return:
    A function: <function tensorflow.python.estimator.inputs.pandas_io.input_fn>
  """
  return tf.estimator.inputs.pandas_input_fn(
    x = df,
    y = df[label_name],
    batch_size = 32,
    num_epochs = 5,
    shuffle    = True,
    queue_capacity = 1000,
    num_threads = 1
  )

# define model
feature_names = ['Cylinders', 'Displacement', 'Horsepower', 'Weight', 'Acceleration', 'Model Year'] 
feature_cols_tensor = [tf.feature_column.numeric_column(feature_names)]  # turn the string list into tensor object
linear_regressor = tf.estimator.LinearRegressor(feature_columns = feature_cols_tensor)

linear_regressor.train(
  train_input_fn(dataset_train, 'MPG'), 
  steps = 100
)

The following is the error messages以下是错误信息

INFO:tensorflow:Calling model_fn.

TypeErrorTraceback (most recent call last)
<ipython-input-14-c1814cca00b6> in <module>()
----> 1 linear_regressor.train(train_input_fn(dataset_train_norm, 'MPG'), steps = 100)

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.pyc in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
    361 
    362     saving_listeners = _check_listeners_type(saving_listeners)
--> 363     loss = self._train_model(input_fn, hooks, saving_listeners)
    364     logging.info('Loss for final step: %s.', loss)
    365     return self

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.pyc in _train_model(self, input_fn, hooks, saving_listeners)
    841       return self._train_model_distributed(input_fn, hooks, saving_listeners)
    842     else:
--> 843       return self._train_model_default(input_fn, hooks, saving_listeners)
    844 
    845   def _train_model_default(self, input_fn, hooks, saving_listeners):

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.pyc in _train_model_default(self, input_fn, hooks, saving_listeners)
    854       worker_hooks.extend(input_hooks)
    855       estimator_spec = self._call_model_fn(
--> 856           features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
    857       return self._train_with_estimator_spec(estimator_spec, worker_hooks,
    858                                              hooks, global_step_tensor,

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/estimator/estimator.pyc in _call_model_fn(self, features, labels, mode, config)
    829 
    830     logging.info('Calling model_fn.')
--> 831     model_fn_results = self._model_fn(features=features, **kwargs)
    832     logging.info('Done calling model_fn.')
    833 

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/estimator/canned/linear.pyc in _model_fn(features, labels, mode, config)
    430           optimizer=optimizer,
    431           partitioner=partitioner,
--> 432           config=config)
    433 
    434     super(LinearRegressor, self).__init__(

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/estimator/canned/linear.pyc in _linear_model_fn(features, labels, mode, head, feature_columns, optimizer, partitioner, config)
    155     logit_fn = _linear_logit_fn_builder(
    156         units=head.logits_dimension, feature_columns=feature_columns)
--> 157     logits = logit_fn(features=features)
    158 
    159     return head.create_estimator_spec(

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/estimator/canned/linear.pyc in linear_logit_fn(features)
     96         feature_columns=feature_columns,
     97         units=units,
---> 98         cols_to_vars=cols_to_vars)
     99     bias = cols_to_vars.pop('bias')
    100     if units > 1:

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/feature_column/feature_column.pyc in linear_model(features, feature_columns, units, sparse_combiner, weight_collections, trainable, cols_to_vars)
    409       nor `_CategoricalColumn`.
    410   """
--> 411   feature_columns = _clean_feature_columns(feature_columns)
    412   for column in feature_columns:
    413     if not isinstance(column, (_DenseColumn, _CategoricalColumn)):

/usr/local/envs/py2env/lib/python2.7/site-packages/tensorflow/python/feature_column/feature_column.pyc in _clean_feature_columns(feature_columns)
   2231   name_to_column = dict()
   2232   for column in feature_columns:
-> 2233     if column.name in name_to_column:
   2234       raise ValueError('Duplicate feature column name found for columns: {} '
   2235                        'and {}. This usually means that these columns refer to '

TypeError: unhashable type: 'list'

The problem is that you provide the tensor flow feature column function with a list, but it takes a single string denoting the name of the column.问题是您为张量流特征列函数提供了一个列表,但它需要一个表示列名称的字符串。

Replace the line:替换行:

feature_cols_tensor = [tf.feature_column.numeric_column(feature_names)]

With:和:

feature_cols_tensor = list(map(tf.feature_column.numeric_column, feature_names))

Your column Model Year seems to be a typo: I only find ModelYear without the space in your raw data.您的Model Year列似乎是一个错字:我只找到ModelYear而您的原始数据中没有空格。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM