2017-04-25 23 views
3

我一直在研究Tensorflow一段時間,但是我似乎無法弄清楚的一件事是如何在tf.contrib中對模型的分類目標列進行編碼.learn模型。在Tensorflow中對目標列進行分類編碼

我知道,我們定義它類似於像下面的代碼輸入功能:

def input_fn(joined): 
    continuous_cols = {k: tf.constant(joined[k].values) 
        for k in CONTINUOUS_COLUMNS} 

    categorical_cols = {k: tf.SparseTensor(
     indices=[[i, 0] for i in range(joined[k].size)], 
     values=joined[k].values, 
     dense_shape=[joined[k].size, 1]) 
         for k in CATEGORICAL_COLUMNS} 

    # Merges the two dictionaries into one. 
    feature_cols = dict(continuous_cols.items() | categorical_cols.items()) 
    target = tf.constant(joined[target_col].values) 
    return feature_cols, target 

def train_input_fn(): 
    return input_fn(train_frame) 
def test_input_fn(): 
    return input_fn(test_frame) 

這工作完全正常的二元分類或情況下,我們預編碼的目標變量要麼sklearn的LabelEncoder或任何其他方法。但是如何使用tensflow編碼該變量,以便tf.contrib.learn可以接受它。

我試圖爲目標列的代碼更改爲以下:

target = tf.SparseTensor(
     indices=[[i, 0] for i in range(joined[target_col].size)], 
     values=joined[target_col].values, 
     dense_shape=[joined[target_col].size, 1]) 

由於它是一個字符串變量,所以我想稀疏張量應該這樣做 但這給出了錯誤:

ValueError: SparseTensor is not supported. 

任何人都可以幫助我指定我應該在目標分類變量的DNNClassifier模型的輸入函數中使用的佔位符。

回答

0

DNNClassifier支持標籤作爲張量沒有稀疏。期望的標籤類似於[[4],[5],[1]],其中每個值對應於整數id。如果您有字符串標籤,則可以使用label_keys參數。