Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of tf.feature_column.indicator_column

Tags:

tensorflow

I’m reading tensorflow’s document about tf.feature_column.indicator_column.

In this document, there is an example.

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”]

My problem is the omitted(...) part of this code. I just want a complete, running, simple example. And I can’t find a kind example including tf.Example and so on.

Can anyone make this complete?

Thank you for advance.

like image 228
plhn Avatar asked Nov 02 '25 02:11

plhn


2 Answers

I hope this is an easy and clean way of visualizing it. It does NOT complete the (...) from your question though, but I think they illustrate how to use tf.feature_column.indicator_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))

Understanding it a bit more:

I strongly suggest the reading of this. Specifically, I really like this picture:

enter image description here

It shows that, while the categorical columns are mapping to integeres, the indicator column is in its turn doing the conversion to one-hot / multi-hot encoding.

like image 69
Elisio Quintino Avatar answered Nov 04 '25 11:11

Elisio Quintino


I'm struggeling with the TF documentation myself. I believe the first ellipsis is just to indicate that one could have more than this single column. But for the 'parse_example' method a second input parameter ('serialized') is required (, as you probably already have found out yourself by now.)

Following code works for me and returns values as described in the docu on evaluation:

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())

There are probably more elegant ways, but since there are no other answers (for both of us) I hope this helps.

like image 40
hvsp Avatar answered Nov 04 '25 12:11

hvsp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!