title: Predicting next word through Multi class classification neural network in R
body: <p>I am trying to predict a status with n.ahead=1. I have a list of 19 specific Status's. Using a dummy data of it, I have created train_x as training data which has beed converted from text to sequences, and train_y as labels which have been one hot encoded.</p>
  <p>For eg,
  I have a sequence: Sales, ILQA, Screen, Approval, Closure
  I take first 4 as training and last (Closure) as label and try to predict using LSTM the last label.</p>
  <p>However, I get a error as specified below. Upon specifying just 1 output in last dense layer, the model works, but when I specify 19(reqd no. of classes), it throws an error.</p>
  <pre><code>library(dplyr)
  library(tokenizers)
  library(devtools)
  library(reticulate)
  library(tensorflow)
  library(keras)
  ##### Main Code ######
  
  # Team Lead data
  capacity &lt;- data.frame(Status=c(&quot;Pre-sales&quot;,&quot;With Sales&quot;,&quot;Doc&quot;,&quot;RTS&quot;,&quot;RTS QC&quot;,&quot;EIP&quot;,&quot;Translation&quot;,&quot;Issue&quot;
                                  ,&quot;Screen&quot;,&quot;ILQAP&quot;,&quot;ILQAR&quot;,&quot;M&amp;I&quot;,&quot;ILQAA&quot;,&quot;M&amp;IR&quot;,&quot;ECDD&quot;,&quot;Rejects&quot;,&quot;Tool&quot;,&quot;Approval&quot;,&quot;Closure&quot;),
                         AHT=c(0.2,0.2,0.1,0.4,0.3,0.2,0.3,0.2,0.3,0.3,0.2,0.5,0.4,0.2,0.2,0.2,0.3,0.1,0.1),
                         Rank=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19))
  
  
  ###### Dummy data creation ######
  dat&lt;-data.frame(replicate(8,sample(c(&quot;Pre-sales&quot;,&quot;With Sales&quot;,&quot;Doc&quot;,&quot;RTS&quot;,&quot;RTS QC&quot;,&quot;EIP&quot;,&quot;Translation&quot;,&quot;Issue&quot;
                                  ,&quot;Screen&quot;,&quot;ILQAP&quot;,&quot;ILQAR&quot;,&quot;M&amp;I&quot;,&quot;ILQAA&quot;,&quot;M&amp;IR&quot;,&quot;ECDD&quot;,&quot;Rejects&quot;,&quot;Tool&quot;,&quot;Approval&quot;,&quot;Closure&quot;),
                                     size = 50, replace=TRUE)))
  
  combined&lt;-data.frame()
  for(i in 1:nrow(dat)){
    combined[i,1] = (paste(dat[i,1:(ncol(dat)-1)],collapse=&quot; &quot;))
  }
  colnames(combined)&lt;-c(&quot;Text&quot;)
  
  #### LSTM  #######
  #devtools::install_github(&quot;rstudio/tensorflow&quot;)
  #devtools::install_github(&quot;rstudio/keras&quot;)
  #tensorflow::install_tensorflow()
  tensorflow::tf_config()
  #conda_create(&quot;r-reticulate&quot;)
  
  ## Model creation
  maxlen&lt;-8 # X.shape[1]
  
  model &lt;- keras_model_sequential() %&gt;%
    layer_embedding(20, 8, input_length=maxlen) %&gt;%
    layer_lstm(units = 32, return_sequences=TRUE,activation=&quot;relu&quot;) %&gt;%
    layer_lstm(units = 32, return_sequences=TRUE,activation=&quot;relu&quot;) %&gt;%
    layer_lstm(units = 32) %&gt;%
    layer_dense(units=19, activation=&quot;softmax&quot;)
  
  model %&gt;% compile(optimizer=&quot;adam&quot;, loss=&quot;categorical_crossentropy&quot;,
                    metrics=c(&quot;acc&quot;))
  
  ## train_x data
  tok &lt;- keras::text_tokenizer(10,lower=TRUE,split=&quot; &quot;)
  keras::fit_text_tokenizer(tok, combined$Text) 
  data_idx &lt;- keras::texts_to_sequences(tok,combined$Text)
  train_x&lt;- data_idx %&gt;% 
    pad_sequences(maxlen=maxlen)
  dim(train_x)
  
  ## train_y data
  library(mltools)
  library(data.table)
  library(superml)
  train_y &lt;- data.frame(dat[,ncol(dat)])
  colnames(train_y) &lt;- c(&quot;Status&quot;)
  train_y$Status &lt;- as.factor(train_y$Status)
  encoded &lt;- one_hot(as.data.table(train_y$Status))
  dim(encoded)
  
  ## Model evaluation
  model %&gt;% evaluate(train_x, encoded)
  pred &lt;- model %&gt;% 
    predict(train_x)
  length(pred)
  length(encoded)
  table(Predicted=pred, Actual=train_y)
  </code></pre>
  <p>Error while running model evaluation. I get Value error: found unexpected keys that do not correspond to any model output: dict keys (19 of them as stated above ). Expected [‘dense_2’]</p>
  <pre><code> Error in py_call_impl(callable, dots$args, dots$keywords) : 
    ValueError: in user code:
  
      C:\Users\kalra\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\training.py:1224 test_function  *
          return step_function(self, iterator)
      C:\Users\kalra\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\training.py:1215 step_function  **
          outputs = model.distribute_strategy.run(run_step, args=(data,))
      C:\Users\kalra\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
          return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
      C:\Users\kalra\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
          return self._call_for_each_replica(fn, args, kwargs)
      C:\Users\kalra\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_ 
  </code></pre>