张量流错误:从分类列组成交叉列



我想明确查看分类交叉特征列的输入张量,但得到错误:

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

什么是正确的实现?

上级:

我将最后 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())
...
~Anaconda3envstensorflowlibsite-packagestensorflowpythonopsgen_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

您需要先通过分类列传递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())

这就是你希望看到的吗?

最新更新