双向LSTM文本分类模型转换为TFLite模型时出错



我的模型在;imdb reviews dataset";并且在预测影评的情绪时效果良好。然而,当我将我的模型转换为Tensorflow Lite时,它会输出:只有在第一个维度中才支持None。张量"嵌入1输入"具有无效形状"[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中发现了这个问题,在这里讨论了这个方法。然而,我不确定这是否适用于Android。

最新更新