首页 > 解决方案 > 双向 LSTM 文本分类模型转换为 TFLite 模型时出错

问题描述

我的模型在“imdb 评论数据集”上进行了训练,在预测电影评论的情绪时效果很好。但是,当我将模型转换为 Tensorflow Lite 时,它​​会输出: None is only supported in the 1st dimension。张量“嵌入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)

标签: kerasnlplstmtensorflow2.0tensorflow-lite

解决方案


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

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


推荐阅读