我还没有找到任何可以使用的预训练 lstm 模型。 tfLite 是否提供了任何预训练的 lstm 模型? 我尝试创建 tflite 模型,但在转换时遇到问题?您能提供创建 tfLite 模型的确切脚本吗? tfLite 是否有任何用于创建最新版本 tfLite LSTM 模型的脚本? 这是我创建 tfLite 模型的脚本。但它不起作用。
import numpy as np
import tensorflow as tf
model = tf.keras.Sequential()
# Add an Embedding layer expecting input vocab of size 1000, and
# output embedding dimension of size 64.
model.add(tf.keras.layers.Embedding(input_dim=1000, output_dim=64))
# Add a LSTM layer with 128 internal units.
model.add(tf.keras.layers.LSTM(128))
# Add a Dense layer with 10 units.
model.add(tf.keras.layers.Dense(10))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])
model.summary()
#model.fit_generator(train_data_generator.generate(), len(train_data)//(batch_size*num_steps), num_epochs,
# validation_data=valid_data_generator.generate(),
# validation_steps=len(valid_data)//(batch_size*num_steps), callbacks=[checkpointer])
tf.saved_model.save(model, "saved_model_keras_dir")
model.save('my_lstm_model')
# x_train =
#(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
#x_train, x_test = x_train / 255.0, x_test / 255.0
# Cast x_train & x_test to float32.
#x_train = x_train.astype(np.float32)
#x_test = x_test.astype(np.float32)
#model.fit(x_train, y_train, epochs=5)
#model.evaluate(x_test, y_test)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# Step 3: Convert the Keras model to TensorFlow Lite model.
tflite_model = converter.convert()
#sess = tf.compat.v1.keras.backend.get_session()
#input_tensor = sess.graph.get_tensor_by_name('embedding_1:0')
#output_tensor = sess.graph.get_tensor_by_name('dense_1:0')
#converter = tf.lite.TFLiteConverter.from_session(
sess, [input_tensor], [output_tensor])
#tflite = converter.convert()
print('Model converted successfully!')
# Save the model.
with open('lstmmodel.tflite', 'wb') as f:
f.write(tflite_model)
最佳答案
为了扩展 Tian Lin 的答案,这里是显示 LSTM 转换的 Colab 笔记本的链接:https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/experimental_new_converter/Keras_LSTM_fusion_Codelab.ipynb
据我所知,TF Lite 没有预先训练的 LSTM 模型...但您可以尝试查看TF Lite 模型中心:https://tfhub.dev/s?deployment-format=lite
关于python - TfLite LSTM 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64609796/