Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在tensorflow中将dict转换为张量_Python_Tensorflow_Sparse Matrix - Fatal编程技术网

Python 如何在tensorflow中将dict转换为张量

Python 如何在tensorflow中将dict转换为张量,python,tensorflow,sparse-matrix,Python,Tensorflow,Sparse Matrix,以下是关于tensorflow的教程: 有许多分类特征必须用tf.feature\u column.categorical\u column\u和\u词汇表\u list()转换为稀疏矩阵。 但是,我不想使用预定义的估计器 m = tf.estimator.LinearClassifier( model_dir=model_dir, feature_columns=base_columns + crossed_columns) 我更喜欢使用服装NN模型,包括: estimator =

以下是关于tensorflow的教程:

有许多分类特征必须用
tf.feature\u column.categorical\u column\u和\u词汇表\u list()转换为稀疏矩阵。

但是,我不想使用预定义的估计器

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> ... 
TypeError:无法将类型的对象转换为Tensor。
内容:{‘教育’:,‘年龄’:
... 
我的问题是如何将
特征
转换为可以用作输入的张量


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

功能是一个
张量的dict
你可以通过类似
功能['education']
来获得一个
张量,但是这个
张量仍然是
字符串的类型,它仍然不能使用
tf.add(tf.matmul(features,w),b)
,您应该使用like
tf.feature\u column.categorical\u column\u with\u词汇表()将字符串类型特征处理为数字特征。

更新: 您可以在
def dnn\u logit\u fn
部分中检查office,它用于从
功能
生成输入层,
tf.feature\u列的列表。*


定义
tf.feature\u columns.*
tf.feature\u column.categorical\u column\u和
,它接受必须存在于
features.keys()
中的字符串作为第一个参数,它将
features
的张量连接到feature\u列,以告诉tf如何处理原始输入(字符串)将张量转换为特征张量(数字)。

非常感谢您的回复。但是如何将转换后的
特征
tf.contrib.learn.Estimator
链接?