How to build an RNN with input as character but output a sequence of tokens?

Let’s assume I have “abbcab”-> {“Apple”,”Banana”}, “Bibb”-> {“Carrot”} so the output is a sequence of various length, but I can pad it to be the same length. My problem, how do I create a simple LSTM model that takes a string of characters of varying length and map it to a vector? What should be the last layer to return the vector?

• Look up "Association" in the Help – Daniel Huber Jan 25 at 10:24

This is a multilabel classification.

classes = Length@{"Apple", "Banana", "Carrot"};

net = NetChain[
{
EmbeddingLayer[8],
LongShortTermMemoryLayer[16],
SequenceLastLayer[],
LinearLayer[classes],
ElementwiseLayer[LogisticSigmoid]
},
"Input" -> NetEncoder[{"Characters"}]
];

netT = NetTrain[net, {"abbcab" -> {1, 1, 0}, "Bibb" -> {0, 0, 1}}, MaxTrainingRounds -> 100]


netT@"abbcab"


{0.822325, 0.864362, 0.163531}

netT@"Bibb"


{0.219002, 0.200206, 0.798121}

You should do your own post-processing to select top-n classes.

• Thank you!!!!! Thank you very much for the speedy and clear answer – user34018 Jan 25 at 13:30
• Can we have the output layer as tokens? The problem in this case is where to set the cut off point – user34018 Feb 11 at 17:10