有关使用SoftmaxCentered Bijector的问题

时间:2018-11-30 21:50:30

标签: python-3.x tensorflow tensorflow-probability

我正在使用tensorflow_probability中的SofmaxCenter Bijector并出现一些错误。由于该文档处于起步阶段,因此我无法弄清楚出了什么问题。我希望你能帮助我。

基本上,鉴于X是三个分量的对数正态随机向量,我想创建另一个随机向量Y,将其定义为X的softmax-center变换。

以下代码片段未给出任何错误

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import functools

import matplotlib.pyplot as plt; plt.style.use('ggplot')
%matplotlib inline

import numpy as np
import seaborn as sns; sns.set_context('notebook')

import tensorflow as tf
import tensorflow_probability as tfp

tfd = tfp.distributions
tfb = tfp.bijectors

tfe = tf.contrib.eager
tfe.enable_eager_execution()

X = tfd.LogNormal(loc=[[-5.0, 0.0, 4.0]], 
                  scale=[[2.0, 1.0, 1.5]])
Y = tfd.TransformedDistribution(
    distribution=X, 
    bijector=tfb.SoftmaxCentered()
)

但是,当我尝试时,

Y.sample(10)

我遇到以下错误,

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-20-9ed8f482f3c1> in <module>
----> 1 Y.sample(10)

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py in sample(self, sample_shape, seed, name)
    684       samples: a `Tensor` with prepended dimensions `sample_shape`.
    685     """
--> 686     return self._call_sample_n(sample_shape, seed, name)
    687 
    688   def _log_prob(self, value):

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/transformed_distribution.py in _call_sample_n(self, sample_shape, seed, name, **kwargs)
    405       # returned result.
    406       y = self.bijector.forward(x, **kwargs)
--> 407       y = self._set_sample_static_shape(y, sample_shape)
    408 
    409       return y

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py in _set_sample_static_shape(self, x, sample_shape)
   1201     sample_ndims = sample_shape.ndims
   1202     batch_ndims = self.batch_shape.ndims
-> 1203     event_ndims = self.event_shape.ndims
   1204 
   1205     # Infer rank(x).

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py in event_shape(self)
    622       event_shape: `TensorShape`, possibly unknown.
    623     """
--> 624     return tf.TensorShape(self._event_shape())
    625 
    626   def is_scalar_event(self, name="is_scalar_event"):

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/transformed_distribution.py in _event_shape(self)
    345         static_override
    346         if self._is_maybe_event_override
--> 347         else self.distribution.event_shape)
    348 
    349   def _batch_shape_tensor(self):

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/bijectors/bijector.py in forward_event_shape(self, input_shape)
    680         after applying `forward`. Possibly unknown.
    681     """
--> 682     return self._forward_event_shape(tf.TensorShape(input_shape))
    683 
    684   def _inverse_event_shape_tensor(self, output_shape):

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/bijectors/softmax_centered.py in _forward_event_shape(self, input_shape)
     68 
     69   def _forward_event_shape(self, input_shape):
---> 70     if input_shape.ndims is None or input_shape[-1] is None:
     71       return input_shape
     72     return tf.TensorShape([input_shape[-1] + 1])

anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in __getitem__(self, key)
    614         return TensorShape(self._dims[key])
    615       else:
--> 616         return self._dims[key]
    617     else:
    618       if isinstance(key, slice):

IndexError: list index out of range

谢谢!

1 个答案:

答案 0 :(得分:1)

SoftmaxCentered希望对矢量事件形状进行操作,但是LogNormal分布具有标量事件形状。 如果要对独立LogNormal的向量进行softmax运算,则可以执行以下操作:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import functools

import matplotlib.pyplot as plt; plt.style.use('ggplot')
%matplotlib inline

import numpy as np
import seaborn as sns; sns.set_context('notebook')

import tensorflow as tf
import tensorflow_probability as tfp

tfd = tfp.distributions
tfb = tfp.bijectors

tfe = tf.contrib.eager
tfe.enable_eager_execution()

X = tfd.LogNormal(loc=[[-5.0, 0.0, 4.0]], 
                  scale=[[2.0, 1.0, 1.5]])
Z = tfd.Independent(X, reinterpreted_batch_ndims=1)
Y = tfd.TransformedDistribution(
    distribution=Z, 
    bijector=tfb.SoftmaxCentered()
)
Y.sample(2)

当然,请注意,SoftmaxCentered占据了3-D空间并将其投影到4-D空间中的3-D歧管上。这是为了提供可逆性。