简体   繁体   English

类型错误:签名不匹配。 键必须是 dtype<dtype: 'string'> ,得到<dtype:'int64'>

[英]TypeError: Signature mismatch. Keys must be dtype <dtype: 'string'>, got <dtype:'int64'>

While running the wide_n_deep_tutorial program from TensorFlow on my dataset, the following error is displayed.在我的数据集上从 TensorFlow 运行wide_n_deep_tutorial程序时,显示以下错误。

"TypeError: Signature mismatch. Keys must be dtype <dtype: 'string'>, got <dtype:'int64'>"

在此处输入图片说明

Following is the code snippet:以下是代码片段:

 def input_fn(df):
  """Input builder function."""
  # Creates a dictionary mapping from each continuous feature column name (k) to
  # the values of that column stored in a constant Tensor.
  continuous_cols = {k: tf.constant(df[k].values) for k in CONTINUOUS_COLUMNS}
  # Creates a dictionary mapping from each categorical feature column name (k)
  # to the values of that column stored in a tf.SparseTensor.
  categorical_cols = {k: tf.SparseTensor(
      indices=[[i, 0] for i in range(df[k].size)],
      values=df[k].values,
      shape=[df[k].size, 1])
                      for k in CATEGORICAL_COLUMNS}

  # Merges the two dictionaries into one.
  feature_cols = dict(continuous_cols)
  feature_cols.update(categorical_cols)
  # Converts the label column into a constant Tensor.
  label = tf.constant(df[LABEL_COLUMN].values)
  # Returns the feature columns and the label.

  return feature_cols, label



def train_and_eval():
  """Train and evaluate the model."""
  train_file_name, test_file_name = maybe_download()

  df_train=train_file_name
  df_test=test_file_name

  df_train[LABEL_COLUMN] = (
      df_train["impression_flag"].apply(lambda x: "generated" in x)).astype(str)

  df_test[LABEL_COLUMN] = (
      df_test["impression_flag"].apply(lambda x: "generated" in x)).astype(str)

  model_dir = tempfile.mkdtemp() if not FLAGS.model_dir else FLAGS.model_dir
  print("model directory = %s" % model_dir)

  m = build_estimator(model_dir)
  print('model succesfully build!')
  m.fit(input_fn=lambda: input_fn(df_train), steps=FLAGS.train_steps)
  print('model fitted!!')
  results = m.evaluate(input_fn=lambda: input_fn(df_test), steps=1)
  for key in sorted(results):
    print("%s: %s" % (key, results[key]))

Any help is appreciated.任何帮助表示赞赏。

would help to see the output prior to the error message to determine which part of the process this error tripped out at, but, the message says quite clearly that the key is expected to be a string whereas an integer was given instead.将有助于在错误消息之前查看输出,以确定此错误在流程的哪个部分跳闸,但是,该消息非常清楚地表明该键应该是一个字符串,而给出的是一个整数。 I am only guessing, but are the column names set out correctly in the earlier part of your script as they could potentially be the keys that are being referred to in this instance?我只是猜测,但是在脚本的前面部分中列名是否正确设置,因为它们可能是在这种情况下被引用的键?

Judging by your traceback , the problem you're having is caused by your inputs to feature columns, or the output of your input_fn .根据您的回溯判断,遇到的问题是由您对特征列的输入或input_fn的输出引起的。 Your sparse tensors are most likely being fed non-string dtypes for the values parameter;您的稀疏张量最有可能为values参数提供非字符串数据类型; sparse feature columns expect string values.稀疏特征列需要字符串值。 Ensure that you're feeding the correct data, and if you're sure you are, you can try the following:确保您提供了正确的数据,如果您确定是这样,您可以尝试以下操作:

categorical_cols = {k: tf.SparseTensor(
  indices=[[i, 0] for i in range(df[k].size)],
  values=df[k].astype(str).values,  # Convert sparse values to string type
  shape=[df[k].size, 1])
                  for k in CATEGORICAL_COLUMNS}

This is how I solved this challenge:我是这样解决这个挑战的:

from sklearn.model_selection import train_test_split

# split the data set 
X_train, X_test, y_train, y_test = train_test_split(M, N, test_size=0.3)

# covert string to int64 for training set
X_train = X_train[X_train.columns] = X_train[X_train.columns].apply(np.int64)
y_train = y_train.apply(np.int64)

# covert string to int64 for testing set
X_test = X_test[X_test.columns] = X_test[X_test.columns].apply(np.int64)
y_test = y_test.apply(np.int64)

暂无
暂无

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

相关问题 参数 `keys` 的 Dtype 必须是<dtype: 'string'> , 已收到:<dtype: 'int32'></dtype:></dtype:> - Dtype of argument `keys` must be <dtype: 'string'>, received: <dtype: 'int32'> Tensor(..., shape=(), dtype=int64) 必须与 Tensor(..., shape=(), dtype=resource) Keras 来自同一个图 - Tensor(…, shape=(), dtype=int64) must be from the same graph as Tensor(…, shape=(), dtype=resource) Keras Sklearn FeatureUnion 返回 TypeError: no supported conversion for types: (dtype('int64'), dtype('O')) - Sklearn FeatureUnion returns TypeError: no supported conversion for types: (dtype('int64'), dtype('O')) PANDAS:将 int64 转换为字符串导致 object dtype - PANDAS : converting int64 to string results in object dtype 将dtype(&#39;int64&#39;)转换为pandas数据框 - Converting dtype('int64') to pandas dataframe 将列表转换为dtype = int64的数组 - Convering a list into an array of dtype = int64 如何将对象 Dtype 转换为 int64? - how to convert object Dtype to int64? 如何将dtype从&#39;0&#39;转换为&#39;int64&#39;? - How to convert dtype from '0' to 'int64'? ufunc 'add' 不包含带有签名匹配类型的循环 (dtype('<u19'), dtype('int64')) -> 没有任何</u19'),> - ufunc 'add' did not contain a loop with signature matching types (dtype('<U19'), dtype('int64')) -> None 类型错误:无法根据 seaborn 中的“安全”规则将数组数据从 dtype(&#39;int64&#39;) 转换为 dtype(&#39;int32&#39;) - TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe' in seaborn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM