2
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ Look up "Association" in the Help $\endgroup$ – Daniel Huber Jan 25 at 10:24
5
$\begingroup$

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]

enter image description here

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.