[英]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:我所做的是:
_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
...
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.