tensorflow 如何保存带有DenseVariational层的模型?

ff29svar  于 2022-12-13  发布在  其他
关注(0)|答案(3)|浏览(143)

我正在尝试构建一个具有DenseVariational层的模型,以便它可以报告认知不确定性。https://www.tensorflow.org/probability/examples/Probabilistic_Layers_Regression#figure_3_epistemic_uncertainty
模型训练工作得很好,现在我想保存模型并将其加载到生产环境中。

Layer DenseVariational has arguments in `__init__` and therefore must override `get_config`.

然后我又补充道

class CustomVariational(tfp.layers.DenseVariational):
  def get_config(self):
        config = super().get_config().copy()
        config.update({
            'units': self.units,
            'make_posterior_fn': self._make_posterior_fn,
            'make_prior_fn': self._make_prior_fn
        })
        return config

但由于新错误而失败

Unable to create link (name already exists)

DenseVariational图层是否仅用于研究?

3yhwsihp

3yhwsihp1#

我认为我们可以通过使用save_weights方法来规避这个问题。

sr4lhrrt

sr4lhrrt2#

当你把with tf.name_scope(...)加到先验和后验函数中时,它应该被解析,否则它们最终对两个Tensor都有相同的名称。
我们也在修复示例教程colab,应该很快就会上线,谢谢。

更新:

我们不是在应用程序中修复它,而是在库中修复它:https://github.com/tensorflow/probability/commit/0ca065fb526b50ce38b68f7d5b803f02c78c8f16。一旦更新,重复的Tensor名称应该被解决。谢谢。

v64noz0r

v64noz0r3#

已经快2年了,问题还在继续。
解决方法是仅存储权重:

tf.keras.Model.save_weights(filepath, overwrite=True)

然后,您可以使用相同的模型结构并加载权重。
例如:
第一次
初始化具有相同结构的新模型:
第一个
我希望这对你有帮助!

相关问题