How to map keras input sequence to multi-label vector

Viewed 94

How can I create a layer in Keras (I am using the functional API) that transforms an input sequence to a multi-label vector. The layer must execute the mapping, I don't want to learn the mapping by learning parameter weights.

As input I have vectors as shown here in keras:

[  nan   nan   nan   nan   nan   nan  342.  367.  453. 1971.   nan   nan
   nan   nan   2]

and I need to map the first 10 numbers of the input vector to a vector that looks like this:

[x1 =0,x2=0,..., x342=1, ..., x367=1,... x453=1,... x1971=1,... xN]

How can I create a layer that can do this? Is there an existing layer for this?

1 Answers

The first part about choosing 10 numbers can be accompliced by this code using a Lambda layer:

take_n_layer=Lambda(lambda z: z[:, :len(classes)], name='take_n', output_shape=(None, 10))(input_layer)

The second question about mapping the input can be done by using a CategoryEncoding layer with the output_mode binary:

input_categoric_encoding = tf.keras.layers.experimental.preprocessing.CategoryEncoding(max_tokens=num_skills, output_mode="binary")
Related