【问题标题】:Error in the conversion of Bidirectional LSTM Text Classification Model to TFLite Model双向 LSTM 文本分类模型转换为 TFLite 模型时出错
【发布时间】:2020-07-10 02:37:48
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 您好,谢谢!我在想这可能是问题所在,因为我刚刚查看了 tensorflow 解释器网站并在页面底部找到了这个。无论如何,我会尝试一下,如果它不起作用,我将等待解决此问题的 tensorflow 版本。同时,我可以创建一个新模型来完成相同的任务。再次感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 2018-08-17
    • 2021-01-24
    • 1970-01-01
    • 2019-05-04
    相关资源
    最近更新 更多