Tensorflow feature column for variable list of values

前端 未结 2 566
野性不改
野性不改 2021-02-13 12:49

From the TensorFlow docs it\'s clear how to use tf.feature_column.categorical_column_with_vocabulary_list to create a feature column which takes as input some strin

相关标签:
2条回答
  • 2021-02-13 13:08

    you should use tf.feature_column.indicator_column see https://www.tensorflow.org/versions/master/api_docs/python/tf/feature_column/indicator_column

    0 讨论(0)
  • 2021-02-13 13:17

    Here is an example how to feed data to the indicator column:

    features = {'letter': [['A','A'], ['C','D'], ['E','F'], ['G','A'], ['X','R']]}
    
    letter_feature = tf.feature_column.categorical_column_with_vocabulary_list(
                    "letter", ["A", "B", "C"], dtype=tf.string)
    
    indicator = tf.feature_column.indicator_column(letter_feature)
    tensor = tf.feature_column.input_layer(features, [indicator])
    
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print(session.run([tensor]))
    

    Which outputs:

    [array([[2., 0., 0.],
           [0., 0., 1.],
           [0., 0., 0.],
           [1., 0., 0.],
           [0., 0., 0.]], dtype=float32)]
    
    0 讨论(0)
提交回复
热议问题