2017-09-05 959 views
0

我對tensorflow教程如下:https://www.tensorflow.org/tutorials/wide如何將字典轉換爲張量在tensorflow

有許多具有轉換爲稀疏矩陣tf.feature_column.categorical_column_with_vocabulary_list()類別特徵。

,但我不想使用預定義的估計,

m = tf.estimator.LinearClassifier(
    model_dir=model_dir, feature_columns=base_columns + crossed_columns) 

我更喜歡使用一個打扮的神經網絡模型,具有:

estimator = tf.contrib.learn.Estimator(model_fn=model) 
estimator.fit(input_fn=input_fn(df, num_epochs=100, shuffle=True), \ 
       steps=100) 

所以在model(),將有

def model(features, labels, mode): 
    ... 
    node = tf.add(tf.matmul(features, w), b) 
    ... 

然後,我得到了如下錯誤:

TypeError: Failed to convert object of type <class 'dict'> to Tensor. 
Contents: {'education': <tf.Tensor 
'random_shuffle_queue_DequeueUpTo:1' shape=(?,) dtype=string>, 'age': 
<tf.Tensor 'random_shuffle_queue_DequeueUpTo:2' shape=(?,) dtype=float64> ... 

我的問題是如何將features轉換爲可用作輸入的張量。

希望我已經說清楚了這個問題。先謝謝你。

回答

0

特點是Tensor一個字典,你可以通過像features['education']得到Tensor,但這種Tensor仍然類型的string,它仍然無法使用tf.add(tf.matmul(features, w), b),你應該處理您的字符串類型的特徵爲數字的功能,就如同tf.feature_column.categorical_column_with_vocabulary_list()

更新:

您可以查看官方dnn implementation,在def dnn_logit_fn一部分,它使用feature_column_lib.input_layerfeaturescolumns產生輸入層,和columnstf.feature_columns.*列表。

當定義一個tf.feature_columns.*tf.feature_column.categorical_column_with_vocabulary_list(),它接受其必須在feautres.keys()作爲第一參數存在的字符串時,它從features張量連接到feature_column告訴TF如何將原始輸入(字符串)張量加工成特徵張量(數字的)。

+0

非常感謝您的回覆。但是,如何將轉換後的'features'與'tf.contrib.learn.Estimator'鏈接? – user2413399

+0

@ user2413399答案已更新。 –

+0

謝謝!這有助於很多,並解決了我的問題。 – user2413399