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

时间:2020-07-10 02:37:48

标签: keras nlp lstm tensorflow2.0 tensorflow-lite

我的模型在“ imdb评论数据集”上受过训练,并且在预测电影评论的情绪时可以正常工作。但是,当我将模型转换为Tensorflow Lite时,它输出: 仅第一维不支持。张量'嵌入 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)

1 个答案:

答案 0 :(得分:1)

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

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