title: Variation in keras and mlmodel results
body: <p>I made a keras model for next word prediction. When i exported it to mlmodel and used it for text prediction, the results of the keras and the mlmodel varied. I used to Netron to visualize the model. I realized that the weights of some layers have changed during the conversion. But I had referred the official coreml documentation for this. Can you help me with this issue?. Thanks in advance.</p>
  
  <p>When I saw difference in weights and predictions, I tried using onnx to convert the keras to onnx and onnx to mlmodel. But there was this error: "Unsupported ONNX ops of type: Cast,Gather,Shape,Identity" while converting the onnx model to mlmodel.</p>
  
  <p>This is my LSTM model:</p>
  
  <pre><code>model = Sequential()
  model.add(Embedding(vocab_size, 10, input_length=max_length-1))
  model.add(LSTM(100, return_sequences=True))
  model.add(LSTM(50))
  model.add(Dense(100, activation='relu'))
  model.add(Dense(vocab_size, activation='softmax'))
  print(model.summary())
  model.compile(loss='categorical_crossentropy', optimizer='adam', 
   metrics=['accuracy'])
  model.fit(X, y, epochs=100, verbose=2)
  </code></pre>
  
  <p>The prediction function:</p>
  
  <pre><code>def generate_seq(model, tokenizer, seq_length, seed_text, n_words):
   result = list()
   #n_words is the number of predictions I want
   in_text = seed_text
   # generate a fixed number of words
   for _ in range(n_words):
      # encode the text as integer
      encoded = tokenizer.texts_to_sequences([in_text])[0]
      # truncate sequences to a fixed length
      encoded = pad_sequences([encoded], maxlen=seq_length, truncating='pre')
      y_prob=model.predict(encoded)
  
              #sort out the top n_words in order of the highest prob
      top_indices = y_prob.argsort()[0][-n_words:][::-1]
  
  
      # map predicted word index to word
      out_word = ''
      for top_index in top_indices:
          for word, index in tokenizer.word_index.items():
              if index == top_index:
                  out_word+= word+' '
                          break
  
   return out_word
  </code></pre>
  
  <p>The keras to coreml conversion code:</p>
  
  <pre><code>from keras.models import load_model
  import coremltools
  
  model = coremltools.converters.keras.convert("model_train.h5")
  model.save("model_train.mlmodel")
  </code></pre>
  
  <p>The results are totally variant. For e.g: For the input: "I read",the keras model predicts "it,book,this" whereas the mlmodel predicts "thought,had,was".</p>
  
  <p>These are the weights of the embedding layer of my keras and mlmodel.
  <a href="https://www.filehosting.org/file/details/820874/weightsKerasModel.npy" rel="nofollow noreferrer">Keras Model</a>
  <a href="https://www.filehosting.org/file/details/820875/weightsMLMODEL.npy" rel="nofollow noreferrer">ML Model</a></p>