Tensorflow feature column for variable list of values

前端 未结 2 567
野性不改
野性不改 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: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)]
    

提交回复
热议问题