use*_*399 5 python sparse-matrix tensorflow
我正在关注有关 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)
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用装扮的 NN 模型,它具有:
estimator = tf.contrib.learn.Estimator(model_fn=model)
estimator.fit(input_fn=input_fn(df, num_epochs=100, shuffle=True), \
steps=100)
Run Code Online (Sandbox Code Playgroud)
所以在model(),会有
def model(features, labels, mode):
...
node = tf.add(tf.matmul(features, w), b)
...
Run Code Online (Sandbox Code Playgroud)
然后,我收到了如下错误:
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> ...
Run Code Online (Sandbox Code Playgroud)
我的问题是如何将 转换features为可用作输入的张量。
希望我已经清楚地描述了这个问题。先感谢您。
features是一个字典,你可以通过likeTensor得到一个,但这仍然是类型,它仍然不能使用,你应该用like将你的字符串类型特征处理成数字特征。Tensorfeatures['education']Tensorstringtf.add(tf.matmul(features, w), b)tf.feature_column.categorical_column_with_vocabulary_list()
您可以查看官方的dnn 实现,def dnn_logit_fn部分它用于从和feature_column_lib.input_layer生成输入层,并且是 的列表。featurescolumnscolumnstf.feature_columns.*
当定义tf.feature_columns.*诸如 时tf.feature_column.categorical_column_with_vocabulary_list(),它接受必须存在于中的字符串features.keys()作为第一个参数,它将张量连接features到 feature_column 来告诉 tf 如何将原始输入(字符串)张量处理为特征张量(数字)。
| 归档时间: |
|
| 查看次数: |
9970 次 |
| 最近记录: |