【问题标题】:Convert Keras .h5 model to TFLite .tflite with TensorFlow v2.2使用 TensorFlow v2.2 将 Keras .h5 模型转换为 TFLite .tflite
【发布时间】:2020-09-10 11:24:31
【问题描述】:

我正在尝试将使用 Keras 定义的网络转换为 tflite。网络如下:

model = tf.keras.Sequential([
        # Embedding
        tf.keras.layers.Embedding(vocab_size, embedding_dim, batch_input_shape=[BATCH_SIZE, None]),
        # GRU unit
        tf.keras.layers.GRU(rnn_units, return_sequences=True, stateful=True, recurrent_initializer='glorot_uniform'),
        # Fully connected layer
        tf.keras.layers.Dense(vocab_size)
        ])

但是,当我尝试导出到 .tflite 时,似乎由于 GRU 层的存在而出现问题。

# Save trained model in .h5 format
keras_file = 'inference.h5'
tf.keras.models.save_model(model, keras_file)

# Load .h5 model with custom loss function
model = load_model('inference.h5', custom_objects={'loss': loss})

# Converting a tf.Keras model to a TensorFlow Lite model.
converter    = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

有错误:

ValueError: Input 0 of node sequential_8/gru_2/AssignVariableOp was passed float from sequential_8/gru_2/68029:0 incompatible with expected resource.

有解决这个问题的办法吗?

【问题讨论】:

    标签: tensorflow keras neural-network tensorflow-lite


    【解决方案1】:

    目前不支持有状态,可以试试设置stateful=False吗?

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 2019-06-10
      • 2021-06-11
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 2022-12-22
      • 2020-12-03
      相关资源
      最近更新 更多