【问题标题】: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】:
我设法保持辍学。
有两件事要做。
- 在构建 keras 模型时在 Dropout 层设置训练标志(这会一直启用 Dropout)
x = Dropout(0.1)(x, training=True)
- 设置 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