python - TfLite LSTM 模型

标签 python tensorflow keras deep-learning lstm

我还没有找到任何可以使用的预训练 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/

相关文章:

python - Tensorflow中的numpy随机选择

Tensorflow 2.0 Keras 模型子类化

lambda - 无法理解 lambda 层如何在 keras 中工作?

python - 俄罗斯方 block 计时问题

python - 按值字符串;引用字典?

python - tensorflow 错误 : TypeError: __init__() got an unexpected keyword argument 'dct_method'

windows - 相同的代码,在 windows/ubuntu (Keras/Tensorflow) 上的准确性非常不同

machine-learning - keras 和 scikit-learn 计算精度的差异

python - 如何使用极坐标 LazyFrame 对不同列上的列表进行平均

python - Python 可以实现快速蜡烛图吗