AddN必须与tf.estimator.LinearClassifier具有相同的大小和形状

时间:2017-10-18 18:03:48

标签: python tensorflow

我的tensorflow应用程序因此错误而崩溃:

  

对于AddN类型的操作linear / linear_model / weighted_sum_no_bias的输入必须具有相同的大小和形状:输入0:[3,1]!=输入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)
}

我的张量流代码:

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) 

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

这是因为您的要素category维度= 3(3个值),但要素demon在您的tfrecord中有维度9(9个值)。使用LinearClassifier时,您需要确保所有功能都具有相同的尺寸。