2017-10-18 108 views
0

我tensorflow應用程序崩潰與此錯誤:ADDN必須具有相同的大小和形狀與tf.estimator.LinearClassifier

Inputs to operation linear/linear_model/weighted_sum_no_bias of type AddN must have the same size and shape: Input 0: [3,1] != input 1: [9,1]

感激,如果任何人都可以點我的根本原因。

我有一個tfrecord文件有如下記載:

features { 
     feature { 
     key: "_label" 
     value { 
      float_list { 
      value: 1.0 
      } 
     } 
     } 
     feature { 
     key: "category" 
     value { 
      bytes_list { 
      value: "14" 
      value: "25" 
      value: "29" 
      } 
     } 
     } 
     feature { 
     key: "demo" 
     value { 
      bytes_list { 
      value: "gender:male" 
      value: "first_name:baerwulf52" 
      value: "country:us" 
      value: "city:manlius" 
      value: "region:us_ny" 
      value: "language:en" 
      value: "signup_hour_of_day:1" 
      value: "signup_day_of_week:3" 
      value: "signup_month_of_year:1" 
      } 
     } 
     } 
} 

我的規格是和跟隨

{ 
    'category': VarLenFeature(dtype=tf.string), 
    '_label': FixedLenFeature(shape=(1,), dtype=tf.float32, default_value=None), 
    'demo': VarLenFeature(dtype=tf.string) 
} 

而且我tensorflow代碼:

category = tf.feature_column.categorical_column_with_vocabulary_list(key="category", vocabulary_list=["null", "14", "25", "29"], 

demo = tf.feature_column.categorical_column_with_vocabulary_list(key="demo", vocabulary_list=["gender:male", "first_name:baerwulf52", 
                      "country:us", "city:manlius", "region:us_ny", 
                      "language:en", "signup_hour_of_day:1", 
                      "signup_day_of_week:3", 
                      "signup_month_of_year:1"]) 

feature_columns = [category, demo] 

def get_input_fn(dataset): 
    def _fn(): 
     iterator = dataset.make_one_shot_iterator() 
     next_elem = iterator.get_next() 
     ex = tf.parse_single_example(next_elem, features=spec) 
     label = ex.pop('_label') 
     return ex, label 

    return _fn 


model = tf.estimator.LinearClassifier(
    feature_columns=feature_columns, 
    model_dir=fp("model") 
) 

model.train(input_fn=get_input_fn(train_dataset), steps=100) 

回答

0

這是因爲你特徵category具有尺寸= 3(3個值),但是特徵demon在您的tfrecord中有9個維度(9個值)。使用LinearClassifier時,您需要確保所有功能具有相同的尺寸。

+0

我這工作看到。我認爲tensorflow會自動將它們分成27個記錄。謝謝。 – user8797249

+0

@明星有什麼特別的理由限制?這是否也意味着LinearClassifiers不支持[indicator_columns](https://www.tensorflow.org/api_docs/python/tf/feature_column/indicator_column)或[weighted_categorical_columns](https://www.tensorflow.org/api_docs/python/tf/feature_column/weighted_categorical_column)? – aarkay

1

這個問題似乎是因爲LinearClassifier.train方法需要投入的批次和代碼調用:

ex = tf.parse_single_example(next_elem, features=spec) 

如果你改變了input_fn到

iterator = dataset.batch(2).make_one_shot_iterator() 
next_batch = iterator.get_next() 
ex = tf.parse_example(next_batch, features=spec) 
+0

這是超級有用!謝謝你的提示! – user8797249

相關問題