我正在学习有关 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_layer
从features
和columns
生成输入层,columns
是tf.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/