Tensorflow-Feature Column遇到的坑

在使用特征交叉函数

tf.contrib.layers.crossed_column([user_age, item_age], hash_bucket_size=100),

训练时出错:

InvalidArgumentError (see above for traceback): Dense inputs should be a matrix but received shape [1000] at position 0
[[Node: wide_and_deep_model/wide_model/weighted_sum_from_feature_columns/cross = SparseFeatureCross[N=0, dense_types=[DT_STRING, DT_STRING], hashed_output=true, internal_type=DT_STRING, num_buckets=100, out_type=DT_INT64, sparse_types=[], _device="/job:localhost/replica:0/task:0/cpu:0"](strided_slice_9, strided_slice)]]

分析:

数据维度不匹配,输入特征需要一个2D的tensor,提供的是1D的tensor,在不交叉时可能隐藏了这个问题,交叉时报错。

解决:扩展一个维度

examples_dict = {}
for n, header in enumerate(COLUMNS):
    examples_dict[header] = example_batch[:, n]

改为

examples_dict = {}
for n, header in enumerate(COLUMNS):
    examples_dict[header] = tf.expand_dims(example_batch[:, n], 1)



你可能感兴趣的:(Tensorflow-Feature Column遇到的坑)