tensorflow错误:从分类列组成交叉列

时间:2018-09-20 13:50:52

标签: python tensorflow

我想显式地查看分类交叉特征列的输入张量,但会收到错误:

ValueError: Items of feature_columns must be a _DenseColumn. You can wrap a categorical column with an embedding_column or indicator_column. Given: _VocabularyListCategoricalColumn(key='education', vocabulary_list=('7th', '8th', '10th', '6th', '1th'), dtype=tf.string, default_value=-1, num_oov_buckets=0)

代码:

import tensorflow as tf
import tensorflow.feature_column as fc

import numpy as np

tf.enable_eager_execution()  

x = {'education': ['7th', '8th', '10th', '10th', '6th', '1th'],
     'occupation': ['pro', 'sport', 'sci', 'model', 'pro', 'tech'],}
y = {'output': [1, 2, 3, 4, 5, 6]}

education = fc.categorical_column_with_vocabulary_list(
    'education',
    ['7th', '8th', '10th', '6th', '1th'])
occupation = fc.categorical_column_with_vocabulary_list(
    'occupation',
    ['pro', 'sport', 'sci', 'model', 'tech'])

education_x_occupation = tf.feature_column.crossed_column(
    ['education', 'occupation'], 30)

feat_cols = [
    education,
    occupation,
    education_x_occupation]

fc.input_layer(x, feat_cols) # output

什么是正确的实现?

UPD:

我将最后5个字符串更改为

feat_cols = [
    fc.indicator_column(education),
    fc.indicator_column(occupation),
    fc.indicator_column(education_x_occupation)]

example_data = fc.input_layer(x, feat_cols) # output
print(example_data.numpy())

,我收到以下错误,我怀疑它们是否对应于tf或python。我应该首先处理溢出吗?

---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
OverflowError: Python int too large to convert to C long

During handling of the above exception, another exception occurred:

SystemError                               Traceback (most recent call last)
<ipython-input-125-db0c0525e02b> in <module>()
      4     fc.indicator_column(education_x_occupation)]
      5 
----> 6 example_data = fc.input_layer(x, feat_cols) # output
      7 print(example_data.numpy())

...

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\gen_sparse_ops.py in sparse_cross(indices, values, shapes, dense_inputs, hashed_output, num_buckets, hash_key, out_type, internal_type, name)
   1327         dense_inputs, "hashed_output", hashed_output, "num_buckets",
   1328         num_buckets, "hash_key", hash_key, "out_type", out_type,
-> 1329         "internal_type", internal_type)
   1330       _result = _SparseCrossOutput._make(_result)
   1331       return _result

SystemError: <built-in function TFE_Py_FastPathExecute> returned a result with an error set

1 个答案:

答案 0 :(得分:0)

您需要先通过fc.indicator_column()传递类别列,然后才能查看它们。 尝试将最后几行修改为此:

feat_cols = [
    fc.indicator_column(education),
    fc.indicator_column(occupation),
    fc.indicator_column(education_x_occupation)]

example_data = fc.input_layer(x, feat_cols) # output
print(example_data.numpy())

是您希望看到的吗?