簡體   English   中英

類型錯誤:簽名不匹配。 鍵必須是 dtype<dtype: 'string'> ,得到<dtype:'int64'>

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

在我的數據集上從 TensorFlow 運行wide_n_deep_tutorial程序時,顯示以下錯誤。

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

在此處輸入圖片說明

以下是代碼片段:

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

任何幫助表示贊賞。

將有助於在錯誤消息之前查看輸出,以確定此錯誤在流程的哪個部分跳閘,但是,該消息非常清楚地表明該鍵應該是一個字符串,而給出的是一個整數。 我只是猜測,但是在腳本的前面部分中列名是否正確設置,因為它們可能是在這種情況下被引用的鍵?

根據您的回溯判斷,遇到的問題是由您對特征列的輸入或input_fn的輸出引起的。 您的稀疏張量最有可能為values參數提供非字符串數據類型; 稀疏特征列需要字符串值。 確保您提供了正確的數據,如果您確定是這樣,您可以嘗試以下操作:

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}

我是這樣解決這個挑戰的:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM