简体   繁体   English

获取 AttributeError: 'Tensor' object 在保存 tensorflow model 时没有属性 'log_prob'

[英]Getting AttributeError: 'Tensor' object has no attribute 'log_prob' while saving a tensorflow model

I am trying to save a variational autoencoder built using TensorFlow and Tensorflow probability.我正在尝试保存使用 TensorFlow 和 Tensorflow 概率构建的变分自动编码器。 My goal is to serve the model using TensorFlow serving.我的目标是使用 TensorFlow 服务来服务 model。 I am open to serving the model in other fashion.我愿意以其他方式为 model 提供服务。

Here is my model:这是我的 model:

def dense_layers(sizes):
return tfk.Sequential([tfkl.Dense(size, activation=tf.nn.leaky_relu) for size in sizes])

original_dim = 30
input_shape = (30,)
intermediary_dims = [20, 10, 8]
latent_dim = 2


prior = tfd.MultivariateNormalDiag(
        loc=tf.zeros([latent_dim]),
        scale_identity_multiplier=1.0)

encoder = tfk.Sequential([
    tfkl.InputLayer(input_shape=input_shape, name='encoder_input'),
    dense_layers(intermediary_dims),
    tfkl.Dense(tfpl.MultivariateNormalTriL.params_size(latent_dim), activation=None),
    tfpl.MultivariateNormalTriL(latent_dim, 
                           activity_regularizer=tfpl.KLDivergenceRegularizer(prior)),
], name='encoder')

encoder.summary()

decoder = tfk.Sequential([
    tfkl.InputLayer(input_shape=[latent_dim]),
    dense_layers(reversed(intermediary_dims)),
    tfkl.Dense(tfpl.IndependentNormal.params_size(original_dim), activation=None),
    tfpl.IndependentNormal(original_dim),
], name='decoder')

decoder.summary()

vae = tfk.Model(inputs=encoder.inputs,
                outputs=decoder(encoder.outputs[0]),
                name='vae_mlp')

negloglik = lambda x, rv_x: -rv_x.log_prob(x)

vae.compile(optimizer=tf.keras.optimizers.RMSprop(), 
            loss=negloglik)

vae.summary()

Here is how I am trying to save the model:这是我尝试保存 model 的方法:

tf.keras.models.save_model(
                vae,
                "/opt/notebooks/saved/vae/1",
                overwrite=True,
                include_optimizer=True,
                save_format=None,
                signatures=None,
                options=None
            )

The long term solution involves pushing for tensorflow bug #742 to be fixed.长期解决方案包括推动修复 tensorflow 错误 #742 In the meantime, you can do what I did, modifying tensorflow code at file tensorflow_probability/python/layers/distribution_layer.py .与此同时,你可以像我一样,在tensorflow_probability/python/layers/distribution_layer.py文件中修改 tensorflow 代码。 What I did was:我所做的是:

  1. Add a check in _make_kl_divergence_fn function, in order to avoid computing the divergence when the input is a Tensor object, like this:_make_kl_divergence_fn function 中添加检查,以避免在输入为Tensor object 时计算散度,如下所示:
    with tf.name_scope('kldivergence_loss'):
      if isinstance(distribution_a, tf.Tensor):
          return 0.0
      ...
  1. Either change KLDivergenceRegularizer for a KLDivergenceAddLoss layer, or add a get_config method to the KLDivergenceRegularizer object:KLDivergenceAddLoss层更改KLDivergenceRegularizer ,或将get_config方法添加到KLDivergenceRegularizer object:
  def get_config(self):
    config = {'use_exact_kl': self._use_exact_kl,
              'test_points_reduce_axis': self._test_points_reduce_axis,
              'weight': self._weight}
    return dict(list(config.items()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到错误此错误:'Tensor'对象没有属性'log_prob' - I am getting error this error : 'Tensor' object has no attribute 'log_prob' TensorFlow:AttributeError:'Tensor'对象没有属性'log10' - TensorFlow: AttributeError: 'Tensor' object has no attribute 'log10' TensorFlow:AttributeError:'Tensor'对象没有属性'shape' - TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape' tensorflow教程代码失败并出现AttributeError:“ Tensor”对象没有属性“ numpy” - tensorflow tutorial code fails with AttributeError: 'Tensor' object has no attribute 'numpy' Tensorflow 2.3: AttributeError: 'Tensor' 对象没有属性 'numpy' - Tensorflow 2.3: AttributeError: 'Tensor' object has no attribute 'numpy' AttributeError:“Tensor”对象在 Tensorflow 2.1 中没有属性“numpy” - AttributeError: 'Tensor' object has no attribute 'numpy' in Tensorflow 2.1 构建 NER model 并获得以下信息: AttributeError: 'Tensor' object has no attribute '_keras_history' - Building an NER model and getting the following: AttributeError: 'Tensor' object has no attribute '_keras_history' AttributeError: 'CustomAugment' 对象在保存 TensorFlow 模型时没有属性 '__name__' - AttributeError: 'CustomAugment' object has no attribute '__name__' when saving TensorFlow model AttributeError: 'Tensor' 对象在尝试使用 'Model' 时没有属性 'input' - AttributeError: 'Tensor' object has no attribute 'input' when trying to use 'Model' AttributeError:“张量”object 在自定义 model 中没有属性“numpy” - AttributeError: 'Tensor' object has no attribute 'numpy' in a custom model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM