2016-09-27 82 views
1

雖然從TensorFlow在我的數據集運行wide_n_deep_tutorial程序,會顯示以下錯誤。TypeError:簽名不匹配。關鍵字必須是D型細胞<D型:「串」>,有<D類:「Int64的」>

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

enter image description here

以下是代碼片段:

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

任何幫助表示讚賞。

回答

0

將有助於看到之前的錯誤信息輸出,以確定這個錯誤在起出過程的哪一部分,但是,有消息稱很清楚,關鍵是預期是一個字符串,而整數給予,而不是。我只是猜測,但是腳本的前面部分是否正確設置了列名,因爲它們可能是在此實例中引用的鍵?

0

根據your traceback來判斷,您遇到的問題是由您輸入的特徵列或input_fn的輸出引起的。對於values參數,您的稀疏張量很可能會被輸入非字符串dtypes;稀疏特徵列期望字符串值。確保您提供的數據正確無誤,如果您確定自己的數據正確,則可以嘗試以下操作:

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} 
相關問題