Tensorflow tf.feature\u column.indicator\u column示例
我正在读tensorflow的文件关于 在本文档中,有一个示例Tensorflow tf.feature\u column.indicator\u column示例,tensorflow,Tensorflow,我正在读tensorflow的文件关于 在本文档中,有一个示例 name = indicator_column(categorical_column_with_vocabulary_list( 'name', ['bob', 'george', 'wanda']) columns = [name, ...] features = tf.parse_example(..., features=make_parse_example_spec(columns)) dense_tensor
name = indicator_column(categorical_column_with_vocabulary_list(
'name', ['bob', 'george', 'wanda'])
columns = [name, ...]
features = tf.parse_example(..., features=make_parse_example_spec(columns))
dense_tensor = input_layer(features, columns)
dense_tensor == [[1, 0, 0]] # If "name" bytes_list is ["bob"]
dense_tensor == [[1, 0, 1]] # If "name" bytes_list is ["bob", "wanda"]
dense_tensor == [[2, 0, 0]] # If "name" bytes_list is ["bob", "bob”]
我的问题是代码中被省略的(…
)部分。我只想要一个完整的,运行的,简单的例子。我找不到一个好的例子,包括tf.example等等
有人能完成吗
谢谢你的提前通知。我自己也在努力学习TF文档。 我相信第一个省略号只是为了表示一个人可以拥有比这一列更多的内容。但是对于'parse_example'方法,需要第二个输入参数('serialized')(,因为您现在可能已经知道了。) 以下代码适用于我,并返回评估文档中描述的值:
import tensorflow as tf
name = tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_list(
'name', ['bob', 'george', 'wanda']))
columns = [name]
feature_dict = {'name': tf.train.Feature(bytes_list=tf.train.BytesList(value=['bob', 'wanda']))}
example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
tf_example = tf.parse_example(serialized=[example.SerializeToString()],
features=tf.feature_column.make_parse_example_spec(columns))
dense_tensor = tf.feature_column.input_layer(tf_example, columns)
sess = tf.InteractiveSession()
tf.tables_initializer().run()
print(dense_tensor.eval())
可能还有更优雅的方式,但由于没有其他答案(对我们两人来说),我希望这会有所帮助。我希望这是一种简单明了的可视化方式。虽然它没有完成你问题中的(…),但我认为它们说明了如何使用
tf.feature\u column.indicator\u column
:
import tensorflow as tf
colors_column = tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_list(
key='color',
vocabulary_list=["Green", "Red", "Blue", "Yellow"]
))
input_layer = tf.feature_column.input_layer(
features={'color': tf.constant(value="Red", dtype=tf.string, shape=(1,))},
feature_columns=[colors_column])
with tf.Session() as sess:
sess.run(tf.initialize_all_tables())
print(sess.run(input_layer))
再多了解一点:
我强烈建议你阅读这本书。具体来说,我非常喜欢这张照片:
它表明,当分类列映射到整数时,指示符列依次执行到一个热/多个热编码的转换