TfLite LSTM模型 [英] TfLite LSTM models

查看:0
本文介绍了TfLite 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) 

推荐答案

要扩展田林的答案,这里是显示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 Model Hubhttps://tfhub.dev/s?deployment-format=lite

这篇关于TfLite LSTM模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆