【问题标题】:Enable Dropout with TFlite使用 TFlite 启用 Dropout
【发布时间】:2021-11-25 20:16:38
【问题描述】:

我在 Keras 中用 Dropout 训练了一个密集的全连接神经网络,现在想用 TFlite 部署它。为了从模型中采样我们在预测期间保持 dropout 的动作(我们使用该模型来优化 Contextual Multi Armed Bandit。):

prediction = model(X, training=True)

我的问题是:有没有办法在 tflite 模型中也保持 dropout?也许硬编码退出?

这样我们也可以从部署的模型中采样操作。

【问题讨论】:

    标签: python tensorflow machine-learning keras tensorflow-lite


    【解决方案1】:

    我设法保持辍学。 有两件事要做。

    1. 在构建 keras 模型时在 Dropout 层设置训练标志(这会一直启用 Dropout)
    x = Dropout(0.1)(x, training=True)
    
    1. 设置 TFlite 转换器标志
    converter = tf.lite.TFLiteConverter.from_keras_model(actor.model)
    converter.target_spec.supported_ops = [
                    tf.lite.OpsSet.TFLITE_BUILTINS,  # enable TensorFlow Lite ops.
                    tf.lite.OpsSet.SELECT_TF_OPS  # enable TensorFlow ops.
                ]
    tflite_model = converter.convert()
    
    open("converted_model.tflite", "wb").write(tflite_model)
    

    遗憾的是,这会稍微增加模型加载时间。

    参考: https://www.tensorflow.org/lite/guide/ops_select

    【讨论】:

      猜你喜欢
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2018-01-12
      • 2021-01-09
      • 1970-01-01
      • 2019-04-15
      • 2020-02-04
      相关资源
      最近更新 更多