Bidirectional LSTM Text Classification Model to TFLite Model转换错误

Error in the conversion of Bidirectional LSTM Text Classification Model to TFLite Model

我的模型是在“imdb 评论数据集”上训练的,在预测电影评论的情绪时效果很好。但是,当我为 Tensorflow Lite 转换我的模型时,它会输出: None 仅在第一个维度中受支持。张量 'embedding 1 input' 的形状“[None, None]”无效。 在训练我的模型时,我没有指定特定的形状,因此我不确定我的模型要传递什么形状才能与我的 android 应用程序一起使用。 (只要我将 embedding_input 形状转换为其他形状,就会创建 TFLite 模型,但不适用于我的 android 应用程序)

模型代码:

from tensorflow import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense , Input , LSTM , Embedding, Dropout , Activation, GRU, Flatten
from keras.layers import Bidirectional, GlobalMaxPool1D
from keras.models import Model, Sequential
from keras.layers import Convolution1D
from keras import initializers, regularizers, constraints, optimizers, layers

max_features = 6000
tokenizer = Tokenizer(num_words=max_features)
tokenizer.fit_on_texts(df['Processed_Reviews'])
list_tokenized_train = tokenizer.texts_to_sequences(df['Processed_Reviews'])

maxlen = 130
X_t = pad_sequences(list_tokenized_train, maxlen=maxlen)
y = df['sentiment']

embed_size = 128
model = Sequential()
model.add(Embedding(max_features, embed_size))
model.add(Bidirectional(LSTM(32, return_sequences = True)))
model.add(GlobalMaxPool1D())
model.add(Dense(20, activation="relu"))
model.add(Dropout(0.05))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

batch_size = 100
epochs = 3
model.fit(X_t,y, batch_size=batch_size, epochs=epochs, validation_split=0.2)

#Conversion Code
import tensorflow as tf

inference_model = tf.keras.models.load_model('imdb-reviews-final.h5')
#inference_model.input.set_shape((6000, 128)) --> Reshaping allows model conversion to happen, but does not actually work with the app
converter = tf.lite.TFLiteConverter.from_keras_model(inference_model)
tflite_model = converter.convert()
open("model.tflite", "wb").write(tflite_model)

当前稳定版本的 Tensorflow 不支持动态输入形状。

但是,使用每晚构建可以解决您的问题。我在讨论此方法的 Tensorflow github 中找到了 this issue。但是,我不确定这是否适用于 Android。