python - 如何在 tensorflow 中将字典转换为张量

标签 python tensorflow sparse-matrix

我正在学习有关 tensorflow 的教程:https://www.tensorflow.org/tutorials/wide

有很多分类特征必须使用 tf.feature_column.categorical_column_with_vocabulary_list() 转换为稀疏矩阵。

但是,我不想使用预定义的 Estimator,

m = tf.estimator.LinearClassifier(
    model_dir=model_dir, feature_columns=base_columns + crossed_columns)

我更喜欢使用装扮的 NN 模型,其中:

estimator = tf.contrib.learn.Estimator(model_fn=model)
estimator.fit(input_fn=input_fn(df, num_epochs=100, shuffle=True), \ 
              steps=100)

所以在model()中,会有

 def model(features, labels, mode): 
    ...
    node = tf.add(tf.matmul(features, w), b)
    ...

然后,我得到如下错误:

 TypeError: Failed to convert object of type <class 'dict'> to Tensor.
 Contents: {'education': <tf.Tensor
 'random_shuffle_queue_DequeueUpTo:1' shape=(?,) dtype=string>, 'age':
 <tf.Tensor 'random_shuffle_queue_DequeueUpTo:2' shape=(?,) dtype=float64> ... 

我的问题是如何将特征转换为可用作输入的张量。

希望我已经清楚地描述了问题。提前谢谢你。

最佳答案

features 是 Tensor 的字典,你可以通过 features['education'] 得到一个 Tensor,但是这个 Tensor 仍然是 string 类型,它仍然不能使用 tf.add(tf.matmul(features, w), b),你应该处理你的字符串使用 tf.feature_column.categorical_column_with_vocabulary_list() 将特征输入数字特征。

更新:

可以查看官方dnn implementation ,在 def dnn_logit_fn 部分,它使用 feature_column_lib.input_layerfeaturescolumns生成输入层,columnstf.feature_columns.*的列表。

当定义 tf.feature_columns.* 时,例如 tf.feature_column.categorical_column_with_vocabulary_list(),它接受必须存在于 features.keys() 中的字符串 作为第一个参数,它将张量从 features 连接到 feature_column 以告诉 tf 如何将原始输入(字符串)张量处理为特征张量(数字)。

关于python - 如何在 tensorflow 中将字典转换为张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46052659/

相关文章:

tensorflow - Tensorboard:为什么梯度图中有锯齿形图案?

python - 稀疏数组的 numpy 向量列表

python - 点积稀疏矩阵

python - 与MongoDB结合使用Dataclass中的 '_id'应该如何处理?

python - GLade Python 中带有 CSS 的复选框

python - python打印函数的问题

python - 如果 if 语句在函数外部,是否可以将 else 放在函数内部?

python - 在预训练的 BERT 上进行微调后如何导出/保存文本分类器

tensorflow - 如何识别放大的图像?

cuda - CUSPARSE_STATUS_INTERNAL_ERROR 与 cuSparse cusparseSnnz 函数